scatter_op.cc 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zchen0211 已提交
2

Z
zchen0211 已提交
3 4 5
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
Z
zchen0211 已提交
6

Z
zchen0211 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Z
zchen0211 已提交
8

Z
zchen0211 已提交
9 10 11 12 13
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. */
Z
zchen0211 已提交
14

S
sneaxiy 已提交
15
#include <memory>
16 17
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
18
#include "paddle/phi/core/ddim.h"
19 20
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/ternary.h"
Z
zchen0211 已提交
21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

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

29
 protected:
30
  framework::OpKernelType GetExpectedKernelType(
Y
Yu Yang 已提交
31
      const framework::ExecutionContext& ctx) const override {
32 33 34
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
Y
Yu Yang 已提交
35
  }
Z
zchen0211 已提交
36 37 38 39 40 41
};

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

42
 protected:
43
  framework::OpKernelType GetExpectedKernelType(
Y
Yu Yang 已提交
44
      const framework::ExecutionContext& ctx) const override {
45 46 47
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
Y
Yu Yang 已提交
48
  }
Z
zchen0211 已提交
49 50 51 52
};

class ScatterOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
53
  void Make() override {
D
dzhwinter 已提交
54 55
    AddInput("X", "The source input of scatter op");
    AddInput("Ids", "The index input of scatter op where X will be updated");
56 57
    AddInput("Updates", "The updated value of scatter op");
    AddOutput("Out", "The output of scatter op");
58
    AddAttr<bool>("overwrite",
T
tianshuo78520a 已提交
59
                  "(bool, default: True) "
60 61 62 63 64 65
                  "The mode that updating the output when has same index,"
                  "If True, use the overwrite mode to update the output"
                  "of the same index, if False, use the accumulate mode to"
                  "update the output of the same index,Default value is True."
                  "You can set overwrite=False to implement scatter_add.")
        .SetDefault(true);
Z
zchen0211 已提交
66
    AddComment(R"DOC(
K
kexinzhao 已提交
67
Scatter Operator.
Z
zchen0211 已提交
68

K
kexinzhao 已提交
69 70 71
This operator obtains output by updating the input on selected indices on the first axis:

$$
D
dzhwinter 已提交
72
Out = X \\
73
Out[Ids] = Updates
K
kexinzhao 已提交
74 75
$$

Z
zchen0211 已提交
76 77 78
)DOC");
  }
};
Q
Qiao Longfei 已提交
79

H
hong 已提交
80 81
template <typename T>
class ScatterGradMaker : public framework::SingleGradOpMaker<T> {
S
sneaxiy 已提交
82
 public:
H
hong 已提交
83
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
S
sneaxiy 已提交
84 85

 protected:
86
  void Apply(GradOpPtr<T> op) const override {
S
sneaxiy 已提交
87
    op->SetType("scatter_grad");
H
hong 已提交
88 89 90 91 92 93 94
    op->SetInput("Ids", this->Input("Ids"));
    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());
S
sneaxiy 已提交
95 96 97
  }
};

98
DECLARE_NO_NEED_BUFFER_VARS_INFERER(ScatterGradNoNeedBufferVarsInferer,
Z
Zeng Jinle 已提交
99
                                    "Updates");
S
sneaxiy 已提交
100

101 102
DECLARE_INPLACE_OP_INFERER(ScatterInplaceInferer, {"X", "Out"});

Z
zchen0211 已提交
103 104 105
}  // namespace operators
}  // namespace paddle

106 107 108 109 110 111
DELCARE_INFER_SHAPE_FUNCTOR(scatter, ScatterInferShapeFunctor,
                            PT_INFER_META(phi::ScatterInferMeta));

DELCARE_INFER_SHAPE_FUNCTOR(scatter_grad, ScatterGradInferShapeFunctor,
                            PT_INFER_META(phi::ScatterGradInferMeta));

Z
zchen0211 已提交
112
namespace ops = paddle::operators;
Y
Yang Yang 已提交
113
REGISTER_OPERATOR(scatter, ops::ScatterOp, ops::ScatterOpMaker,
H
hong 已提交
114
                  ops::ScatterGradMaker<paddle::framework::OpDesc>,
115
                  ops::ScatterGradMaker<paddle::imperative::OpBase>,
116
                  ops::ScatterInplaceInferer, ScatterInferShapeFunctor);
S
sneaxiy 已提交
117
REGISTER_OPERATOR(scatter_grad, ops::ScatterGradOp,
118 119
                  ops::ScatterGradNoNeedBufferVarsInferer,
                  ScatterGradInferShapeFunctor);