commit
477d92bcd2
@ -0,0 +1,73 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/operators/sum_op.h"
|
||||
#include <vector>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
using framework::Tensor;
|
||||
|
||||
class SumOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
auto ins = ctx.MultiInput<framework::Tensor>("X");
|
||||
auto *out = ctx.Output<framework::Tensor>("Out");
|
||||
int N = ins.size();
|
||||
|
||||
auto in_dim = ins[0]->dims();
|
||||
|
||||
PADDLE_ENFORCE_GT(N, 1, "Input tensors count should > 1.");
|
||||
for (int i = 1; i < N; i++) {
|
||||
auto dim = ins[i]->dims();
|
||||
PADDLE_ENFORCE(in_dim == dim, "Input tensors must have same shape");
|
||||
}
|
||||
out->Resize(in_dim);
|
||||
}
|
||||
};
|
||||
|
||||
class SumOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
SumOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "the input tensors of sum operator.").AsDuplicable();
|
||||
AddOutput("Out", "the output tensor of sum operator.");
|
||||
AddComment(R"DOC(
|
||||
Sum the input tensors.
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
class SumGradOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
auto outputs = ctx.MultiOutput<Tensor>(framework::GradVarName("X"));
|
||||
auto dims = ctx.Input<Tensor>(framework::GradVarName("Out"))->dims();
|
||||
for (auto output : outputs) {
|
||||
output->Resize(dims);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP(sum, ops::SumOp, ops::SumOpMaker, sum_grad, ops::SumGradOp);
|
||||
REGISTER_OP_CPU_KERNEL(sum, ops::SumKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(sum_grad,
|
||||
ops::SumGradKernel<paddle::platform::CPUPlace, float>);
|
@ -0,0 +1,18 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/operators/sum_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(sum, ops::SumKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(sum_grad,
|
||||
ops::SumGradKernel<paddle::platform::GPUPlace, float>);
|
@ -0,0 +1,65 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#pragma once
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
template <typename T, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
|
||||
|
||||
template <typename Place, typename T>
|
||||
class SumKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
auto ins = context.MultiInput<Tensor>("X");
|
||||
auto* out = context.Output<Tensor>("Out");
|
||||
out->mutable_data<T>(context.GetPlace());
|
||||
|
||||
auto place = context.GetEigenDevice<Place>();
|
||||
auto result = EigenVector<T>::Flatten(*out);
|
||||
|
||||
int N = ins.size();
|
||||
auto in = EigenVector<T>::Flatten(*(ins[0]));
|
||||
result.device(place) = in;
|
||||
for (int i = 1; i < N; i++) {
|
||||
auto in = EigenVector<T>::Flatten(*(ins[i]));
|
||||
result.device(place) = result + in;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Place, typename T>
|
||||
class SumGradKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
auto* input = context.Input<Tensor>(framework::GradVarName("Out"));
|
||||
auto outs = context.MultiOutput<Tensor>(framework::GradVarName("X"));
|
||||
for (auto out : outs) {
|
||||
out->mutable_data<T>(context.GetPlace());
|
||||
}
|
||||
|
||||
auto place = context.GetEigenDevice<Place>();
|
||||
auto in = EigenVector<T>::Flatten(*input);
|
||||
for (auto out : outs) {
|
||||
auto result = EigenVector<T>::Flatten(*out);
|
||||
result.device(place) = in;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,24 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestSumOp(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "sum"
|
||||
x0 = np.random.random((3, 4)).astype('float32')
|
||||
x1 = np.random.random((3, 4)).astype('float32')
|
||||
x2 = np.random.random((3, 4)).astype('float32')
|
||||
self.inputs = {"X": {"x0": x0, "x1": x1, "x2": x2}}
|
||||
y = x0 + x1 + x2
|
||||
self.outputs = {'Out': y}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(["x0"], "Out")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue