-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor operator python test framework and add sum operator #3882
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,27 @@ class OperatorBase { | |
|
||
const VariableNameMap& Inputs() const { return inputs_; } | ||
const VariableNameMap& Outputs() const { return outputs_; } | ||
|
||
const std::vector<std::string> InputsNames() const { | ||
std::vector<std::string> result; | ||
for (auto& kv : inputs_) { | ||
for (auto& name : kv.second) { | ||
result.push_back(name); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
const std::vector<std::string> OutputsNames() const { | ||
std::vector<std::string> result; | ||
for (auto& kv : outputs_) { | ||
for (auto& name : kv.second) { | ||
result.push_back(name); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
//! Get a input with argument's name described in `op_proto` | ||
std::string Input(const std::string& name) const; | ||
//! Get a input which has multiple variables. | ||
|
@@ -311,9 +332,9 @@ class InferShapeContext { | |
} | ||
|
||
template <typename T> | ||
std::vector<const T*> MultiOutput(const std::string& name) const { | ||
std::vector<T*> MultiOutput(const std::string& name) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure, should we name this DuplicableOutput? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think DuplicableOutput is more accerate |
||
auto names = op_.Outputs(name); | ||
std::vector<const T*> res; | ||
std::vector<T*> res; | ||
res.reserve(names.size()); | ||
std::transform(names.begin(), names.end(), std::back_inserter(res), | ||
[&](const std::string& sub_name) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* 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(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here should check all dims of inputs are same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
PADDLE_ENFORCE_GT(N, 1, "Input tensors count should > 1."); | ||
|
||
auto dim_zero = ins[0]->dims(); | ||
out->Resize(dim_zero); | ||
} | ||
}; | ||
|
||
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>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let us don't use I think we can move this using directive into class SumKernel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, all the operators have the same problem. I will make another pr to move using in head file in all operators |
||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation is very slow because it starts many GPU kernels. Maybe we could have a better implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will make a more efficient implementation in next PR. This PR is mainly focus on supporting multi-inputs/outputs. |
||
} | ||
} | ||
}; | ||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that same as
OutputVars
method?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the same. I will remove this method