|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 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/fluid/operators/sequence_scatter_op.h" |
| 16 | +#include "paddle/fluid/framework/eigen.h" |
| 17 | +#include "paddle/fluid/framework/op_registry.h" |
| 18 | +#include "paddle/fluid/operators/gather.h" |
| 19 | +#include "paddle/fluid/operators/scatter.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace operators { |
| 23 | + |
| 24 | +using Tensor = framework::Tensor; |
| 25 | +using LoDTensor = framework::LoDTensor; |
| 26 | + |
| 27 | +class SequenceScatterOpMaker : public framework::OpProtoAndCheckerMaker { |
| 28 | + public: |
| 29 | + void Make() override { |
| 30 | + AddInput("X", "(Tensor) The source input of sequence scatter op"); |
| 31 | + AddInput("Ids", |
| 32 | + "(LoDTensor) The index input of sequence scatter op where X" |
| 33 | + " will be updated, must be a LoDTensor"); |
| 34 | + AddInput("Updates", |
| 35 | + "(LoDTensor) The values to scatter to the input tensor " |
| 36 | + "X, must be a LoDTensor with the same LoD information as Ids"); |
| 37 | + AddOutput("Out", |
| 38 | + "(Tensor) The output tensor of sequence scatter op, which " |
| 39 | + "has the same dims as X"); |
| 40 | + AddComment(R"DOC( |
| 41 | +Sequence Scatter Operator. |
| 42 | +
|
| 43 | +This operator scatters the Updates tensor to the input X. It uses the LoD |
| 44 | +information of Ids to select the rows to update, and use the values in Ids as |
| 45 | +the columns to update in each row of X. |
| 46 | +
|
| 47 | +Following are cases to better explain how this works: |
| 48 | +
|
| 49 | +Example 1: |
| 50 | +Given an all-ones Tensor input(X) |
| 51 | + X.data = [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0], |
| 52 | + [1.0, 1.0, 1.0, 1.0, 1.0, 1.0], |
| 53 | + [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]] |
| 54 | + X.dims = [3, 6] |
| 55 | +a LoDTensor input(Ids) |
| 56 | + Ids.data = [[0], [1], [2], [5], [4], [3], [2], [1], [3], [2], [5], [4]] |
| 57 | + Ids.lod = [[0, 3, 8, 12]] |
| 58 | +and a Tensor input(Updates) |
| 59 | + Updates.data = [[0.3], [0.3], [0.4], [0.1], [0.2], [0.3], [0.4], [0.0], [0.2], [0.3], [0.1], [0.4]] |
| 60 | + Updates.lod = [[ 0, 3, 8, 12]] |
| 61 | +then we get an output Tensor |
| 62 | + Out.data = [[1.3, 1.3, 1.4, 1.0, 1.0, 1.0], |
| 63 | + [1.0, 1.0, 1.4, 1.3, 1.2, 1.1], |
| 64 | + [1.0, 1.0, 1.3, 1.2, 1.4, 1.1]] |
| 65 | + Out.dims = X.dims = [3, 6] |
| 66 | +)DOC"); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +class SequenceScatterOp : public framework::OperatorWithKernel { |
| 71 | + public: |
| 72 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 73 | + |
| 74 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 75 | + // Enforce has inputs and outputs |
| 76 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 77 | + "Input(X) of SequenceScatterOp should not be null."); |
| 78 | + PADDLE_ENFORCE(ctx->HasInput("Ids"), |
| 79 | + "Input(Ids) of SequenceScatterOp should not be null."); |
| 80 | + PADDLE_ENFORCE(ctx->HasInput("Updates"), |
| 81 | + "Input(Updates) of SequenceScatterOp should not be null."); |
| 82 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 83 | + "Output(Out) of SequenceScatterOp should not be null."); |
| 84 | + |
| 85 | + // Set output dim the same as input |
| 86 | + auto ref_dims = ctx->GetInputDim("X"); |
| 87 | + ctx->SetOutputDim("Out", ref_dims); |
| 88 | + |
| 89 | + // Enforce the Updates and Ids are the same shape |
| 90 | + PADDLE_ENFORCE_EQ(ctx->GetInputDim("Updates")[0], |
| 91 | + ctx->GetInputDim("Ids")[0], |
| 92 | + "Updates and Ids should have same shape."); |
| 93 | + |
| 94 | + // Enforce LoD of ids and updates be the same |
| 95 | + if (ctx->IsRuntime()) { |
| 96 | + framework::Variable* ids_var = |
| 97 | + boost::get<framework::Variable*>(ctx->GetInputVarPtrs("Ids")[0]); |
| 98 | + framework::Variable* updates_var = |
| 99 | + boost::get<framework::Variable*>(ctx->GetInputVarPtrs("Updates")[0]); |
| 100 | + |
| 101 | + auto& ids_lod = ids_var->Get<LoDTensor>().lod(); |
| 102 | + auto& updates_lod = updates_var->Get<LoDTensor>().lod(); |
| 103 | + PADDLE_ENFORCE_EQ(ids_lod.size(), 1, |
| 104 | + "Currently only level 1 LoD could be" |
| 105 | + " processed by sequence scatter op."); |
| 106 | + PADDLE_ENFORCE_EQ(updates_lod.size(), 1, |
| 107 | + "Currently only level 1 LoD " |
| 108 | + "could be processed by sequence scatter op."); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + protected: |
| 113 | + framework::OpKernelType GetExpectedKernelType( |
| 114 | + const framework::ExecutionContext& ctx) const override { |
| 115 | + return framework::OpKernelType( |
| 116 | + framework::ToDataType(ctx.Input<Tensor>("X")->type()), |
| 117 | + platform::CPUPlace()); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +class SequenceScatterGradOp : public framework::OperatorWithKernel { |
| 122 | + public: |
| 123 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 124 | + |
| 125 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 126 | + ctx->SetOutputDim(framework::GradVarName("Updates"), |
| 127 | + ctx->GetInputDim("Updates")); |
| 128 | + ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); |
| 129 | + } |
| 130 | + |
| 131 | + protected: |
| 132 | + framework::OpKernelType GetExpectedKernelType( |
| 133 | + const framework::ExecutionContext& ctx) const override { |
| 134 | + return framework::OpKernelType( |
| 135 | + framework::ToDataType(ctx.Input<Tensor>("X")->type()), |
| 136 | + platform::CPUPlace()); |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +} // namespace operators |
| 141 | +} // namespace paddle |
| 142 | + |
| 143 | +namespace ops = paddle::operators; |
| 144 | +REGISTER_OPERATOR(sequence_scatter, ops::SequenceScatterOp, |
| 145 | + ops::SequenceScatterOpMaker, |
| 146 | + paddle::framework::DefaultGradOpDescMaker<true>); |
| 147 | +REGISTER_OPERATOR(sequence_scatter_grad, ops::SequenceScatterGradOp); |
| 148 | +REGISTER_OP_CPU_KERNEL(sequence_scatter, ops::SequenceScatterOpKernel<float>, |
| 149 | + ops::SequenceScatterOpKernel<double>, |
| 150 | + ops::SequenceScatterOpKernel<int>, |
| 151 | + ops::SequenceScatterOpKernel<int64_t>); |
| 152 | +REGISTER_OP_CPU_KERNEL(sequence_scatter_grad, |
| 153 | + ops::SequenceScatterGradientOpKernel<float>, |
| 154 | + ops::SequenceScatterGradientOpKernel<double>, |
| 155 | + ops::SequenceScatterGradientOpKernel<int>, |
| 156 | + ops::SequenceScatterGradientOpKernel<int64_t>); |
0 commit comments