|
| 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( |
| 25 | + "X", |
| 26 | + "(LoDTensors) multi input tensor with shape{batch_num, N}, N is the " |
| 27 | + "size of embedding table") |
| 28 | + .AsDuplicable(); |
| 29 | + AddOutput("Out", "(LoDTensor) The merged outputs of the input tensors."); |
| 30 | + |
| 31 | + AddComment(R"DOC( |
| 32 | +Merge multi LoDTensor's into one according to Ids's shard num. |
| 33 | +
|
| 34 | +
|
| 35 | +split_ids_op -> prefetch_op -> merge_ids_op |
| 36 | +
|
| 37 | +
|
| 38 | +merge_ids_op should be used after split_ids_op and prefetch_op, split_ids_op |
| 39 | + will split input Ids into multiple tensors according to Id's shard number. |
| 40 | +prefetch_op will send them to parameter server to prefetch embedding value |
| 41 | +back. During split, the order of ids is disordered. In merge_ids_op we use |
| 42 | +the original Ids to restore the order of the fetched embedding value and |
| 43 | + also pass the lod information to the merged output. |
| 44 | +
|
| 45 | +
|
| 46 | +Example: |
| 47 | +
|
| 48 | + Ids = [1,2,3,4,5,6] # 3 shared |
| 49 | +
|
| 50 | +split_ids_op -> |
| 51 | +
|
| 52 | + Id0 = [3, 6] # id % 3 == 0 |
| 53 | + Id1 = [1, 4] # id % 3 == 1 |
| 54 | + Id2 = [2, 5] # id % 3 == 2 |
| 55 | +
|
| 56 | +prefetch_op -> |
| 57 | +
|
| 58 | + X0 = [[0.3 0.3] # 3 |
| 59 | + [0.6 0.6]] # 6 |
| 60 | + X1 = [[0.1 0.1] # 1 |
| 61 | + [0.4 0.4]] # 4 |
| 62 | + X2 = [[0.2 0.2] # 2 |
| 63 | + [0.5 0.5]] # 5 |
| 64 | +
|
| 65 | +merge_ids_op -> |
| 66 | +
|
| 67 | + Out = [[0.1 0.1] # 1 |
| 68 | + [0.2 0.2] # 2 |
| 69 | + [0.3 0.3] # 3 |
| 70 | + [0.4 0.4] # 4 |
| 71 | + [0.5 0.5] # 5 |
| 72 | + [0.6 0.6]] # 6 |
| 73 | +)DOC"); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +class MergeIdsOp : public framework::OperatorWithKernel { |
| 78 | + public: |
| 79 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 80 | + |
| 81 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 82 | + PADDLE_ENFORCE(ctx->HasInput("Ids"), "MergeIdsOp must has input Ids."); |
| 83 | + PADDLE_ENFORCE(ctx->HasInputs("X"), "MergeIdsOp must has input X."); |
| 84 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), "MergeIdsOp must has output Out."); |
| 85 | + |
| 86 | + auto ids_var_type = ctx->GetInputsVarType("Ids").front(); |
| 87 | + auto ids_dims = ctx->GetInputDim("Ids"); |
| 88 | + if (ids_var_type == framework::proto::VarType::LOD_TENSOR) { |
| 89 | + PADDLE_ENFORCE_EQ(ids_dims.size(), 2); |
| 90 | + PADDLE_ENFORCE_EQ(ids_dims[1], 1); |
| 91 | + } |
| 92 | + auto x_var_type = ctx->GetInputsVarType("X"); |
| 93 | + for (auto &var_type : x_var_type) { |
| 94 | + PADDLE_ENFORCE_EQ(var_type, framework::proto::VarType::LOD_TENSOR, |
| 95 | + "input X only support lod tensors"); |
| 96 | + } |
| 97 | + ctx->ShareLoD("Ids", "Out"); |
| 98 | + } |
| 99 | + |
| 100 | + private: |
| 101 | + framework::OpKernelType GetExpectedKernelType( |
| 102 | + const framework::ExecutionContext &ctx) const override { |
| 103 | + return framework::OpKernelType( |
| 104 | + framework::ToDataType( |
| 105 | + ctx.MultiInput<framework::Tensor>("X").front()->type()), |
| 106 | + ctx.GetPlace()); |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +class MergeIdsOpInferVarType : public framework::VarTypeInference { |
| 111 | + public: |
| 112 | + void operator()(const framework::OpDesc &op_desc, |
| 113 | + framework::BlockDesc *block) const override { |
| 114 | + auto *input_var = block->Var(op_desc.Input("Ids")[0]); |
| 115 | + for (auto &out_var : op_desc.Output("Out")) { |
| 116 | + block->Var(out_var)->SetType(input_var->GetType()); |
| 117 | + } |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +} // namespace operators |
| 122 | +} // namespace paddle |
| 123 | + |
| 124 | +namespace ops = paddle::operators; |
| 125 | +REGISTER_OPERATOR(merge_ids, ops::MergeIdsOp, ops::MergeIdsOpMaker, |
| 126 | + ops::MergeIdsOpInferVarType); |
| 127 | +REGISTER_OP_CPU_KERNEL( |
| 128 | + merge_ids, ops::MergeIdsOpKernel<paddle::platform::CPUPlace, float>); |
0 commit comments