-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add paddle.incubate.graph_send_recv API #37205
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
wawltor
merged 31 commits into
PaddlePaddle:develop
from
DesmonDay:add_fused_gather_scatter
Nov 19, 2021
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f3b6ee4
add cpu version, using set: sum, min, max
DesmonDay 470aa0f
add cpu version: mean
DesmonDay b31111c
improve cpu code and fix dynamic memory allcation problem
DesmonDay a54860c
fix arg error, add index judge, delete fp16
DesmonDay 2f79165
fix bug in CudaAtomicMax and CudaAtomicMin
DesmonDay 8316d6e
add CUDA version
DesmonDay 80accd8
fix grad_op bug for index
DesmonDay 868a50a
add op test, add correct cpu grad op
DesmonDay d2da1ee
Add correct CUDA Mean grad
DesmonDay 0235923
[Add] Successful MEAN and SUM
DesmonDay 484a84b
[Add] Successful MIN and MAX in CPU
DesmonDay a16e412
[Add] Successful MIN and MAX in CUDA
DesmonDay b27858d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
DesmonDay 071425f
fix windows dtype ci
DesmonDay f9666b9
fix ROCM ci by adding HIP flag
DesmonDay 66e9af0
rename fused_gather_scatter to send_recv
DesmonDay aa3042d
unify name as send and recv
DesmonDay 05c4acf
change zero index return time
DesmonDay c4408b5
add send_recv incubate api
DesmonDay 8e435f5
fix index data type, add unittest case for API
DesmonDay e7a3c0d
delete redundant input tensor
DesmonDay 42708f7
fix en example and docs, add default value in pool_type
DesmonDay a1adbf1
add shape judge and max grid judge
DesmonDay ca1a3c1
fix comment
DesmonDay f1c9c22
fix index type bug
DesmonDay 6d7d4fb
add const &
DesmonDay ed0c10e
fix en docs
DesmonDay 6c59c79
delete numpy in examples
DesmonDay 52a48a1
add unittest for int input
DesmonDay 4bb5669
fix send_recv comment
DesmonDay f0137ec
change send_recv to graph_send_recv
DesmonDay 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
|
|
||
| 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/fluid/operators/graph_send_recv_op.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| class GraphSendRecvOP : public framework::OperatorWithKernel { | ||
| public: | ||
| using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
|
||
| void InferShape(framework::InferShapeContext* ctx) const override { | ||
| OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "GraphSendRecv"); | ||
| OP_INOUT_CHECK(ctx->HasInput("Src_index"), "Input", "Src_index", | ||
| "GraphSendRecv"); | ||
| OP_INOUT_CHECK(ctx->HasInput("Dst_index"), "Input", "Dst_index", | ||
| "GraphSendRecv"); | ||
| OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "GraphSendRecv"); | ||
|
|
||
| auto src_index_dims = ctx->GetInputDim("Src_index"); | ||
| if (src_index_dims.size() == 2) { | ||
| PADDLE_ENFORCE_EQ(src_index_dims[1], 1, | ||
| platform::errors::InvalidArgument( | ||
| "The last dim of Src_index should be 1 when it " | ||
| "is 2D, but we get %d", | ||
| src_index_dims[1])); | ||
| } else { | ||
| PADDLE_ENFORCE_EQ( | ||
| src_index_dims.size(), 1, | ||
| platform::errors::InvalidArgument( | ||
| "The Src_index should be 1D, when it is not 2D, but we get %d", | ||
| src_index_dims.size())); | ||
| } | ||
|
|
||
| auto dst_index_dims = ctx->GetInputDim("Dst_index"); | ||
| if (dst_index_dims.size() == 2) { | ||
| PADDLE_ENFORCE_EQ(dst_index_dims[1], 1, | ||
| platform::errors::InvalidArgument( | ||
| "The last dim of Dst_index should be 1 when it " | ||
| "is 2D, but we get %d", | ||
| dst_index_dims[1])); | ||
| } else { | ||
| PADDLE_ENFORCE_EQ( | ||
| dst_index_dims.size(), 1, | ||
| platform::errors::InvalidArgument("The Dst_index should be 1D, " | ||
| "when it is not 2D, but we get %d", | ||
| dst_index_dims.size())); | ||
| } | ||
|
|
||
| PADDLE_ENFORCE_EQ( | ||
| src_index_dims[0], dst_index_dims[0], | ||
| platform::errors::InvalidArgument( | ||
| "Src_index and Dst_index should have the same shape.")); | ||
|
|
||
| auto dims = ctx->GetInputDim("X"); | ||
| ctx->SetOutputDim("Out", dims); | ||
|
|
||
| if (ctx->Attrs().Get<std::string>("pool_type") == "MEAN") { | ||
| OP_INOUT_CHECK(ctx->HasOutput("Dst_count"), "Output", "Dst_count", | ||
| "GraphSendRecv"); | ||
| ctx->SetOutputDim("Dst_count", {dims[0]}); | ||
| } | ||
| } | ||
|
|
||
| protected: | ||
| framework::OpKernelType GetExpectedKernelType( | ||
| const framework::ExecutionContext& ctx) const override { | ||
| return framework::OpKernelType( | ||
| OperatorWithKernel::IndicateVarDataType(ctx, "X"), | ||
| ctx.device_context()); | ||
| } | ||
| }; | ||
|
|
||
| class GraphSendRecvGradOp : public framework::OperatorWithKernel { | ||
| public: | ||
| using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
|
||
| void InferShape(framework::InferShapeContext* ctx) const override { | ||
| auto in_dims = ctx->GetInputDim(framework::GradVarName("Out")); | ||
| ctx->SetOutputDim(framework::GradVarName("X"), in_dims); | ||
| } | ||
|
|
||
| protected: | ||
| framework::OpKernelType GetExpectedKernelType( | ||
| const framework::ExecutionContext& ctx) const override { | ||
| return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType( | ||
| ctx, framework::GradVarName("Out")), | ||
| ctx.device_context()); | ||
| } | ||
| }; | ||
|
|
||
| class GraphSendRecvOpMaker : public framework::OpProtoAndCheckerMaker { | ||
| public: | ||
| void Make() override { | ||
| AddInput("X", | ||
| "The input tensor with data type float32, float64, int32, int64."); | ||
| AddInput("Src_index", "The source index tensor."); | ||
| AddInput("Dst_index", "The destination index tensor."); | ||
| AddOutput("Out", "Output tensor of graph_send_recv op."); | ||
| AddOutput("Dst_count", | ||
| "Count tensor of Dst_index, mainly for MEAN pool_type.") | ||
| .AsIntermediate(); | ||
| AddAttr<std::string>("pool_type", | ||
| "(string, default 'SUM')" | ||
| "Define different pool types to receive the result " | ||
| "tensors of Dst_index.") | ||
| .SetDefault("SUM") | ||
| .InEnum({"SUM", "MEAN", "MIN", "MAX"}); | ||
| AddComment(R"DOC( | ||
| Graph Learning Send_Recv combine operator. | ||
|
|
||
| $Out = Recv(Send(X, Src_index), Dst_index, pool_type)$ | ||
|
|
||
| This operator is mainly used in Graph Learning domain, and the main purpose is to reduce | ||
| intermediate memory consumption in the process of message passing. | ||
| Take `x` as the input tensor, we first use `src_index` to gather corresponding data, | ||
| and then use `dst_index` to update the corresponding position of output tensor in different | ||
| pooling types, like sum, mean, max, or min. | ||
|
|
||
| )DOC"); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class GraphSendRecvGradOpMaker : public framework::SingleGradOpMaker<T> { | ||
| public: | ||
| using framework::SingleGradOpMaker<T>::SingleGradOpMaker; | ||
|
|
||
| protected: | ||
| void Apply(GradOpPtr<T> op) const override { | ||
| op->SetType("graph_send_recv_grad"); | ||
| op->SetInput("Src_index", this->Input("Src_index")); | ||
| op->SetInput("Dst_index", this->Input("Dst_index")); | ||
|
|
||
| if (BOOST_GET_CONST(std::string, this->GetAttr("pool_type")) == "MEAN") { | ||
| op->SetInput("Dst_count", this->Output("Dst_count")); | ||
| } | ||
|
|
||
| if (BOOST_GET_CONST(std::string, this->GetAttr("pool_type")) == "MIN" || | ||
| BOOST_GET_CONST(std::string, this->GetAttr("pool_type")) == "MAX") { | ||
| op->SetInput("X", this->Input("X")); | ||
| op->SetInput("Out", this->Output("Out")); | ||
| } | ||
|
|
||
| op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); | ||
| op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); | ||
| op->SetAttrMap(this->Attrs()); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
| using CPU = paddle::platform::CPUDeviceContext; | ||
|
|
||
| REGISTER_OPERATOR(graph_send_recv, ops::GraphSendRecvOP, | ||
| ops::GraphSendRecvOpMaker, | ||
| ops::GraphSendRecvGradOpMaker<paddle::framework::OpDesc>, | ||
| ops::GraphSendRecvGradOpMaker<paddle::imperative::OpBase>); | ||
| REGISTER_OPERATOR(graph_send_recv_grad, ops::GraphSendRecvGradOp); | ||
| REGISTER_OP_CPU_KERNEL(graph_send_recv, ops::GraphSendRecvOpKernel<CPU, float>, | ||
| ops::GraphSendRecvOpKernel<CPU, double>, | ||
| ops::GraphSendRecvOpKernel<CPU, int>, | ||
| ops::GraphSendRecvOpKernel<CPU, int64_t>); | ||
|
|
||
| REGISTER_OP_CPU_KERNEL(graph_send_recv_grad, | ||
| ops::GraphSendRecvGradOpKernel<CPU, float>, | ||
| ops::GraphSendRecvGradOpKernel<CPU, double>, | ||
| ops::GraphSendRecvGradOpKernel<CPU, int>, | ||
| ops::GraphSendRecvGradOpKernel<CPU, int64_t>); | ||
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.
TODO: add fp16 support.