graph_send_recv_op.cc 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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>);