scatter_nd_add_op.cc 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2019 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 <memory>
#include <vector>
17 18
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
19
#include "paddle/phi/core/ddim.h"
20 21
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/ternary.h"
22 23 24 25 26 27 28 29 30 31 32

namespace paddle {
namespace operators {

class ScatterNdAddOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
33 34
    PADDLE_ENFORCE_EQ(OperatorWithKernel::IndicateVarDataType(ctx, "X"),
                      OperatorWithKernel::IndicateVarDataType(ctx, "Updates"),
35 36
                      platform::errors::InvalidArgument(
                          "Ref and Updates must have same type"));
37
    return framework::OpKernelType(
38 39
        framework::TransToProtoVarType(
            ctx.Input<framework::Tensor>("X")->type()),
40
        ctx.device_context());
41 42 43 44 45 46 47 48 49 50
  }
};

class ScatterNdAddGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
51 52 53
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
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
  }
};

class ScatterNdAddOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "The source input of scatter_nd_add op");
    AddInput("Index",
             "The index input of scatter_nd_add op where X will be updated");
    AddInput("Updates", "The updated value of scatter_nd_add op");
    AddOutput("Out", "The output of scatter_nd_add op");
    AddComment(R"DOC(
Scatter_nd_add Operator.

Output is obtained by applying sparse addition to a single value or slice in a Variable.

      Given:
        * Case 1:
            ref = [0, 1, 2, 3, 4, 5]
            index = [[1], [2], [3], [1]]
            updates = [9, 10, 11, 12]

          we get:

            output = [0, 22, 12, 14, 4, 5]

        * Case 2:
            ref = [[65, 17], [-14, -25]]
            index = [[], []]
            updates = [[[-1, -2], [1, 2]],
                       [[3, 4], [-3, -4]]]
            ref.shape = (2, 2)
            index.shape = (2, 0)
            updates.shape = (2, 2, 2)

          we get:

            output = [[67, 19], [-16, -27]]
)DOC");
  }
};

H
hong 已提交
96 97
template <typename T>
class ScatterNdAddGradMaker : public framework::SingleGradOpMaker<T> {
98
 public:
H
hong 已提交
99
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
100 101

 protected:
102
  void Apply(GradOpPtr<T> op) const override {
103
    op->SetType("scatter_nd_add_grad");
H
hong 已提交
104 105 106 107 108 109 110
    op->SetInput("Index", this->Input("Index"));
    op->SetInput("Updates", this->Input("Updates"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetOutput(framework::GradVarName("Updates"),
                  this->InputGrad("Updates"));
    op->SetAttrMap(this->Attrs());
111 112 113
  }
};

114
DECLARE_NO_NEED_BUFFER_VARS_INFERER(ScatterNdAddGradNoNeedBufferVarsInferer,
Z
Zeng Jinle 已提交
115
                                    "Updates");
116 117 118 119 120 121

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

122 123
DECLARE_INFER_SHAPE_FUNCTOR(scatter_nd_add, ScatterNdAddInferShapeFunctor,
                            PD_INFER_META(phi::ScatterNdAddInferMeta));
124

125
DECLARE_INFER_SHAPE_FUNCTOR(scatter_nd_add_grad,
126
                            ScatterNdAddGradInferShapeFunctor,
127
                            PD_INFER_META(phi::ScatterNdAddGradInferMeta));
128

129
REGISTER_OPERATOR(scatter_nd_add, ops::ScatterNdAddOp, ops::ScatterNdAddOpMaker,
H
hong 已提交
130
                  ops::ScatterNdAddGradMaker<paddle::framework::OpDesc>,
131 132
                  ops::ScatterNdAddGradMaker<paddle::imperative::OpBase>,
                  ScatterNdAddInferShapeFunctor);
133 134

REGISTER_OPERATOR(scatter_nd_add_grad, ops::ScatterNdAddGradOp,
135 136
                  ops::ScatterNdAddGradNoNeedBufferVarsInferer,
                  ScatterNdAddGradInferShapeFunctor);