-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add reduce op #4086
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
Merged
Merged
Add reduce op #4086
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3994e91
Add reduce_op
guoshengCS c8d8771
Revise the reduce_op unit test accordingly
guoshengCS 630273d
Fix reduce_op according to CI log
guoshengCS 8b3bf28
Refine reduce_op and follow comments
guoshengCS 1295e5e
Refine reduce_op unit test and add newline at end of file
guoshengCS 477a6a0
Refine reduce_op, follow comments and remove ReduceGradEigenFreeKernel
guoshengCS 99b8dbb
Merge branch 'develop' of https://github.com/PaddlePaddle/paddle into…
guoshengCS be58c63
Merge branch 'develop' of https://github.com/PaddlePaddle/paddle into…
guoshengCS e33b411
Adapt reduce_op according to up-to-date dev
guoshengCS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* 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/reduce_op.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
using framework::Tensor; | ||
|
||
class ReduceOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
protected: | ||
void InferShape(framework::InferShapeContextBase *ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("X"), | ||
"Input(X) of ReduceOp should not be null."); | ||
PADDLE_ENFORCE(ctx->HasOutput("Out"), | ||
"Output(Out) of ReduceOp should not be null."); | ||
auto x_dims = ctx->GetInputDim("X"); | ||
auto x_rank = x_dims.size(); | ||
PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported."); | ||
int dim = ctx->Attrs().Get<int>("dim"); | ||
if (dim < 0) dim = x_rank + dim; | ||
PADDLE_ENFORCE_LT( | ||
dim, x_rank, | ||
"The dim should be in the range [-rank(input), rank(input))."); | ||
bool keep_dim = ctx->Attrs().Get<bool>("keep_dim"); | ||
auto dims_vector = vectorize(x_dims); | ||
if (keep_dim || x_rank == 1) { | ||
dims_vector[dim] = 1; | ||
} else { | ||
dims_vector.erase(dims_vector.begin() + dim); | ||
} | ||
auto out_dims = framework::make_ddim(dims_vector); | ||
ctx->SetOutputDim("Out", out_dims); | ||
if (dim != 0) { | ||
// Only pass LoD when not reducing on the first dim. | ||
ctx->ShareLoD("X", /*->*/ "Out"); | ||
} | ||
} | ||
}; | ||
|
||
class ReduceGradOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
protected: | ||
void InferShape(framework::InferShapeContextBase *ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null."); | ||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), | ||
"Input(Out@GRAD) should not be null."); | ||
auto x_dims = ctx->GetInputDim("X"); | ||
auto x_rank = x_dims.size(); | ||
PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported."); | ||
int dim = ctx->Attrs().Get<int>("dim"); | ||
if (dim < 0) dim = x_rank + dim; | ||
PADDLE_ENFORCE_LT( | ||
dim, x_rank, | ||
"The dim should be in the range [-rank(input), rank(input))."); | ||
auto x_grad_name = framework::GradVarName("X"); | ||
if (ctx->HasOutput(x_grad_name)) { | ||
ctx->SetOutputDim(x_grad_name, x_dims); | ||
} | ||
} | ||
}; | ||
|
||
class ReduceOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
ReduceOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput( | ||
"X", | ||
"(Tensor) The input tensor. Tensors with rank at most 6 are supported"); | ||
AddOutput("Out", "(Tensor) The result tensor."); | ||
AddAttr<int>( | ||
"dim", | ||
"(int, default 1) The dimension to reduce. " | ||
"Must be in the range [-rank(input), rank(input)). " | ||
"If `dim < 0`, the dim to reduce is `rank + dim`. " | ||
"Noting that reducing on the first dim will make the LoD info lost.") | ||
.SetDefault(0); | ||
AddAttr<bool>("keep_dim", | ||
"(bool, default false) " | ||
"If true, retain the reduced dimension with length 1.") | ||
.SetDefault(false); | ||
comment_ = R"DOC( | ||
{ReduceOP} operator computes the {reduce} of input tensor along the given dimension. | ||
The result tensor has 1 fewer dimension than the input unless `keep_dim` is true. | ||
)DOC"; | ||
AddComment(comment_); | ||
} | ||
|
||
protected: | ||
std::string comment_; | ||
|
||
void Replace(std::string &src, std::string from, std::string to) { | ||
std::size_t len_from = std::strlen(from.c_str()); | ||
std::size_t len_to = std::strlen(to.c_str()); | ||
for (std::size_t pos = src.find(from); pos != std::string::npos; | ||
pos = src.find(from, pos + len_to)) { | ||
src.replace(pos, len_from, to); | ||
} | ||
} | ||
|
||
void SetComment(std::string name, std::string op) { | ||
Replace(comment_, "{ReduceOP}", name); | ||
Replace(comment_, "{reduce}", op); | ||
} | ||
}; | ||
|
||
class ReduceSumOpMaker : public ReduceOpMaker { | ||
public: | ||
ReduceSumOpMaker(framework::OpProto *proto, | ||
framework::OpAttrChecker *op_checker) | ||
: ReduceOpMaker(proto, op_checker) { | ||
SetComment("ReduceSum", "sum"); | ||
AddComment(comment_); | ||
} | ||
}; | ||
|
||
class ReduceMeanOpMaker : public ReduceOpMaker { | ||
public: | ||
ReduceMeanOpMaker(framework::OpProto *proto, | ||
framework::OpAttrChecker *op_checker) | ||
: ReduceOpMaker(proto, op_checker) { | ||
SetComment("ReduceMean", "mean"); | ||
AddComment(comment_); | ||
} | ||
}; | ||
|
||
class ReduceMaxOpMaker : public ReduceOpMaker { | ||
public: | ||
ReduceMaxOpMaker(framework::OpProto *proto, | ||
framework::OpAttrChecker *op_checker) | ||
: ReduceOpMaker(proto, op_checker) { | ||
SetComment("ReduceMax", "max"); | ||
AddComment(comment_); | ||
} | ||
}; | ||
|
||
class ReduceMinOpMaker : public ReduceOpMaker { | ||
public: | ||
ReduceMinOpMaker(framework::OpProto *proto, | ||
framework::OpAttrChecker *op_checker) | ||
: ReduceOpMaker(proto, op_checker) { | ||
SetComment("ReduceMin", "min"); | ||
AddComment(comment_); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
|
||
REGISTER_OP(reduce_sum, ops::ReduceOp, ops::ReduceSumOpMaker, reduce_sum_grad, | ||
ops::ReduceGradOp); | ||
REGISTER_OP_CPU_KERNEL( | ||
reduce_sum, | ||
ops::ReduceKernel<paddle::platform::CPUPlace, float, ops::SumFunctor>); | ||
REGISTER_OP_CPU_KERNEL(reduce_sum_grad, | ||
ops::ReduceGradKernel<paddle::platform::CPUPlace, float, | ||
ops::SumGradFunctor>); | ||
|
||
REGISTER_OP(reduce_mean, ops::ReduceOp, ops::ReduceMeanOpMaker, | ||
reduce_mean_grad, ops::ReduceGradOp); | ||
REGISTER_OP_CPU_KERNEL( | ||
reduce_mean, | ||
ops::ReduceKernel<paddle::platform::CPUPlace, float, ops::MeanFunctor>); | ||
REGISTER_OP_CPU_KERNEL(reduce_mean_grad, | ||
ops::ReduceGradKernel<paddle::platform::CPUPlace, float, | ||
ops::MeanGradFunctor>); | ||
|
||
REGISTER_OP(reduce_max, ops::ReduceOp, ops::ReduceMaxOpMaker, reduce_max_grad, | ||
ops::ReduceGradOp); | ||
REGISTER_OP_CPU_KERNEL( | ||
reduce_max, | ||
ops::ReduceKernel<paddle::platform::CPUPlace, float, ops::MaxFunctor>); | ||
REGISTER_OP_CPU_KERNEL(reduce_max_grad, | ||
ops::ReduceGradKernel<paddle::platform::CPUPlace, float, | ||
ops::MaxOrMinGradFunctor>); | ||
|
||
REGISTER_OP(reduce_min, ops::ReduceOp, ops::ReduceMaxOpMaker, reduce_min_grad, | ||
ops::ReduceGradOp); | ||
REGISTER_OP_CPU_KERNEL( | ||
reduce_min, | ||
ops::ReduceKernel<paddle::platform::CPUPlace, float, ops::MinFunctor>); | ||
REGISTER_OP_CPU_KERNEL(reduce_min_grad, | ||
ops::ReduceGradKernel<paddle::platform::CPUPlace, float, | ||
ops::MaxOrMinGradFunctor>); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* 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/reduce_op.h" | ||
|
||
namespace ops = paddle::operators; | ||
|
||
REGISTER_OP_GPU_KERNEL( | ||
reduce_sum, | ||
ops::ReduceKernel<paddle::platform::GPUPlace, float, ops::SumFunctor>); | ||
REGISTER_OP_GPU_KERNEL(reduce_sum_grad, | ||
ops::ReduceGradKernel<paddle::platform::GPUPlace, float, | ||
ops::SumGradFunctor>); | ||
|
||
REGISTER_OP_GPU_KERNEL( | ||
reduce_mean, | ||
ops::ReduceKernel<paddle::platform::GPUPlace, float, ops::MeanFunctor>); | ||
REGISTER_OP_GPU_KERNEL(reduce_mean_grad, | ||
ops::ReduceGradKernel<paddle::platform::GPUPlace, float, | ||
ops::MeanGradFunctor>); | ||
|
||
REGISTER_OP_GPU_KERNEL( | ||
reduce_max, | ||
ops::ReduceKernel<paddle::platform::GPUPlace, float, ops::MaxFunctor>); | ||
REGISTER_OP_GPU_KERNEL(reduce_max_grad, | ||
ops::ReduceGradKernel<paddle::platform::GPUPlace, float, | ||
ops::MaxOrMinGradFunctor>); | ||
|
||
REGISTER_OP_GPU_KERNEL( | ||
reduce_min, | ||
ops::ReduceKernel<paddle::platform::GPUPlace, float, ops::MinFunctor>); | ||
REGISTER_OP_GPU_KERNEL(reduce_min_grad, | ||
ops::ReduceGradKernel<paddle::platform::GPUPlace, float, | ||
ops::MaxOrMinGradFunctor>); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
我觉得将min,max等作为attr来写,注册一个kernel,这样cc文件会更加简短精炼。
目前ReduceMinOpMaker和ReduceMaxOpMaker等基本都是重复的。现在分开写了四个kernel,写成一个后,会省将近3/4的代码。
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.
抱歉啊,这个回复的晚了,这里参考了下tensorflow的做法 https://github.com/tensorflow/tensorflow/blob/216dcbf1e08c02d87774120ebd5b251c5c30c56c/tensorflow/core/kernels/reduction_ops_sum.cc#L26 ,另外pytorch中也有类似的reduce操作 https://github.com/pytorch/pytorch/blob/master/torch/autograd/_functions/reduce.py ,感觉可能分为多个OP在意义上更清楚一些,另外将min、max作为attr感觉会kernel部分的代码会比较长,后续可能也会加下其他的reduce操作,functor的话还有一个潜在好处是的可以复用目前的kernel不太容易复用,我也不能确定哪种会更好。多谢评论与思考建议~