|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/operators/scatter_op.h" |
| 16 | +#include "paddle/framework/ddim.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | + |
| 21 | +class ScatterOp : public framework::OperatorWithKernel { |
| 22 | + public: |
| 23 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 24 | + |
| 25 | + protected: |
| 26 | + void InferShape(const framework::InferShapeContext &ctx) const override { |
| 27 | + PADDLE_ENFORCE_EQ(ctx.Input<Tensor>("Index")->dims().size(), 1, |
| 28 | + "Update Index should be 1-D."); |
| 29 | + PADDLE_ENFORCE_EQ(ctx.Input<Tensor>("Ref")->dims().size(), |
| 30 | + ctx.Input<Tensor>("Updates")->dims().size(), |
| 31 | + "Reference and Updates should have the same shape size"); |
| 32 | + PADDLE_ENFORCE_EQ(ctx.Input<Tensor>("Updates")->dims()[0], |
| 33 | + ctx.Input<Tensor>("Index")->dims()[0], |
| 34 | + "Updates and Index should have same batch-size."); |
| 35 | + framework::DDim data_dim(ctx.Input<Tensor>("Updates")->dims()); |
| 36 | + for (int i = 1; i < data_dim.size(); ++i) |
| 37 | + PADDLE_ENFORCE_EQ(data_dim[i], ctx.Input<Tensor>("Updates")->dims()[i]); |
| 38 | + ctx.Output<Tensor>("Out")->Resize(ctx.Input<Tensor>("Ref")->dims()); |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +class ScatterGradOp : public framework::OperatorWithKernel { |
| 43 | + public: |
| 44 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 45 | + |
| 46 | + protected: |
| 47 | + void InferShape(const framework::InferShapeContext &ctx) const override { |
| 48 | + auto *dUpdates = ctx.Output<Tensor>(framework::GradVarName("Updates")); |
| 49 | + auto *Updates = ctx.Input<Tensor>("Updates"); |
| 50 | + auto *dRef = ctx.Output<Tensor>(framework::GradVarName("Ref")); |
| 51 | + auto *Ref = ctx.Input<Tensor>("Ref"); |
| 52 | + |
| 53 | + dRef->Resize(Ref->dims()); |
| 54 | + dUpdates->Resize(Updates->dims()); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +class ScatterOpMaker : public framework::OpProtoAndCheckerMaker { |
| 59 | + public: |
| 60 | + ScatterOpMaker(framework::OpProto *proto, |
| 61 | + framework::OpAttrChecker *op_checker) |
| 62 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 63 | + AddInput("Ref", "The source input of scatter op"); |
| 64 | + AddInput("Index", |
| 65 | + "The index input of scatter op where Ref will be updated"); |
| 66 | + AddInput("Updates", "The updated value of updates op"); |
| 67 | + AddOutput("Out", "The output of add op"); |
| 68 | + AddComment(R"DOC( |
| 69 | +Scatter Operator by selecting from the first axis, |
| 70 | +
|
| 71 | +Out = Ref |
| 72 | +Out[Index] = Ref[Index] + Updates |
| 73 | +)DOC"); |
| 74 | + } |
| 75 | +}; |
| 76 | +} // namespace operators |
| 77 | +} // namespace paddle |
| 78 | + |
| 79 | +namespace ops = paddle::operators; |
| 80 | +REGISTER_OP(scatter, ops::ScatterOp, ops::ScatterOpMaker, scatter_grad, |
| 81 | + ops::ScatterGradOp); |
| 82 | +REGISTER_OP_CPU_KERNEL(scatter, |
| 83 | + ops::ScatterOpKernel<paddle::platform::CPUPlace, float>); |
| 84 | +REGISTER_OP_CPU_KERNEL( |
| 85 | + scatter_grad, |
| 86 | + ops::ScatterGradientOpKernel<paddle::platform::CPUPlace, float>); |
0 commit comments