|
| 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/merge_ids_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class MergeIdsOpMaker : public framework::OpProtoAndCheckerMaker { |
| 21 | + public: |
| 22 | + void Make() override { |
| 23 | + AddInput("Ids", "(LoDTensor) the input ids with shape{batch_num, 1}"); |
| 24 | + AddInput("X", |
| 25 | + "(LoDTensor) the input tensor with shape{batch_num, N}, N is the " |
| 26 | + "size of embedding table") |
| 27 | + .AsDuplicable(); |
| 28 | + AddOutput("Out", "(LoDTensor) The merged outputs of the input tensors."); |
| 29 | + |
| 30 | + AddComment(R"DOC( |
| 31 | +Merge multi LoDTensor's into one according to Ids's shard num. |
| 32 | +The values in the input LoDTensor are lookuped from the output of splite_ids_op |
| 33 | +Example: |
| 34 | + Input: |
| 35 | + Ids = [1,2,3,4,5,6] |
| 36 | + X0 = [[0.1 0.2] # 3 |
| 37 | + [0.2 0.3]] # 6 |
| 38 | + X1 = [[0.3 0.4] # 1 |
| 39 | + [0.4 0.5]] # 4 |
| 40 | + X2 = [[0.5 0.6] # 2 |
| 41 | + [0.6 0.7]] # 5 |
| 42 | +
|
| 43 | + Output: |
| 44 | + Out = [[0.3 0.4] # 1 |
| 45 | + [0.5 0.6] # 2 |
| 46 | + [0.1 0.2] # 3 |
| 47 | + [0.4 0.5] # 4 |
| 48 | + [0.6 0.7] # 5 |
| 49 | + [0.2 0.3]] # 6 |
| 50 | +)DOC"); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +class MergeIdsOp : public framework::OperatorWithKernel { |
| 55 | + public: |
| 56 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 57 | + |
| 58 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 59 | + PADDLE_ENFORCE(ctx->HasInput("Ids"), "MergeIdsOp must has input Ids."); |
| 60 | + PADDLE_ENFORCE(ctx->HasInputs("X"), "MergeIdsOp must has input X."); |
| 61 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), "MergeIdsOp must has output Out."); |
| 62 | + |
| 63 | + auto ids_var_type = ctx->GetInputsVarType("Ids").front(); |
| 64 | + auto ids_dims = ctx->GetInputDim("Ids"); |
| 65 | + if (ids_var_type == framework::proto::VarType::LOD_TENSOR) { |
| 66 | + PADDLE_ENFORCE_EQ(ids_dims.size(), 2); |
| 67 | + PADDLE_ENFORCE_EQ(ids_dims[1], 1); |
| 68 | + } |
| 69 | + auto x_var_type = ctx->GetInputsVarType("X"); |
| 70 | + for (auto &var_type : x_var_type) { |
| 71 | + PADDLE_ENFORCE_EQ(var_type, framework::proto::VarType::LOD_TENSOR, |
| 72 | + "input X only support lod tensors"); |
| 73 | + } |
| 74 | + ctx->ShareLoD("Ids", "Out"); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +class MergeIdsOpInferVarType : public framework::VarTypeInference { |
| 79 | + public: |
| 80 | + void operator()(const framework::OpDesc &op_desc, |
| 81 | + framework::BlockDesc *block) const override { |
| 82 | + auto *input_var = block->Var(op_desc.Input("Ids")[0]); |
| 83 | + for (auto &out_var : op_desc.Output("Out")) { |
| 84 | + block->Var(out_var)->SetType(input_var->GetType()); |
| 85 | + } |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +} // namespace operators |
| 90 | +} // namespace paddle |
| 91 | + |
| 92 | +namespace ops = paddle::operators; |
| 93 | +REGISTER_OPERATOR(merge_ids, ops::MergeIdsOp, ops::MergeIdsOpMaker, |
| 94 | + ops::MergeIdsOpInferVarType); |
| 95 | +REGISTER_OP_CPU_KERNEL( |
| 96 | + merge_ids, ops::MergeIdsOpKernel<paddle::platform::CPUPlace, int64_t>, |
| 97 | + ops::MergeIdsOpKernel<paddle::platform::CPUPlace, float>); |
0 commit comments