Skip to content
Merged
Show file tree
Hide file tree
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 Oct 28, 2021
470aa0f
add cpu version: mean
DesmonDay Oct 29, 2021
b31111c
improve cpu code and fix dynamic memory allcation problem
DesmonDay Oct 29, 2021
a54860c
fix arg error, add index judge, delete fp16
DesmonDay Nov 3, 2021
2f79165
fix bug in CudaAtomicMax and CudaAtomicMin
DesmonDay Nov 4, 2021
8316d6e
add CUDA version
DesmonDay Nov 8, 2021
80accd8
fix grad_op bug for index
DesmonDay Nov 9, 2021
868a50a
add op test, add correct cpu grad op
DesmonDay Nov 11, 2021
d2da1ee
Add correct CUDA Mean grad
DesmonDay Nov 11, 2021
0235923
[Add] Successful MEAN and SUM
DesmonDay Nov 12, 2021
484a84b
[Add] Successful MIN and MAX in CPU
DesmonDay Nov 15, 2021
a16e412
[Add] Successful MIN and MAX in CUDA
DesmonDay Nov 15, 2021
b27858d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
DesmonDay Nov 15, 2021
071425f
fix windows dtype ci
DesmonDay Nov 15, 2021
f9666b9
fix ROCM ci by adding HIP flag
DesmonDay Nov 15, 2021
66e9af0
rename fused_gather_scatter to send_recv
DesmonDay Nov 16, 2021
aa3042d
unify name as send and recv
DesmonDay Nov 16, 2021
05c4acf
change zero index return time
DesmonDay Nov 16, 2021
c4408b5
add send_recv incubate api
DesmonDay Nov 16, 2021
8e435f5
fix index data type, add unittest case for API
DesmonDay Nov 17, 2021
e7a3c0d
delete redundant input tensor
DesmonDay Nov 17, 2021
42708f7
fix en example and docs, add default value in pool_type
DesmonDay Nov 17, 2021
a1adbf1
add shape judge and max grid judge
DesmonDay Nov 17, 2021
ca1a3c1
fix comment
DesmonDay Nov 17, 2021
f1c9c22
fix index type bug
DesmonDay Nov 17, 2021
6d7d4fb
add const &
DesmonDay Nov 17, 2021
ed0c10e
fix en docs
DesmonDay Nov 17, 2021
6c59c79
delete numpy in examples
DesmonDay Nov 18, 2021
52a48a1
add unittest for int input
DesmonDay Nov 18, 2021
4bb5669
fix send_recv comment
DesmonDay Nov 18, 2021
f0137ec
change send_recv to graph_send_recv
DesmonDay Nov 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions paddle/fluid/operators/graph_send_recv_op.cc
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.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add fp16 support.

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>);
Loading