merge_ids_op.cc 4.3 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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

    http://www.apache.org/licenses/LICENSE-2.0

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. */

W
Wu Yi 已提交
15
#include "paddle/fluid/operators/distributed_ops/merge_ids_op.h"
Q
qiaolongfei 已提交
16 17 18 19 20 21 22

namespace paddle {
namespace operators {

class MergeIdsOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
S
seiriosPlus 已提交
23 24 25 26 27 28 29 30 31
    AddInput("Ids", "(LoDTensor) the input ids with shape{batch_num, 1}")
        .AsDuplicable();
    AddInput("Rows", "(LoDTensor) the input ids with shape{row_size, 1}, ")
        .AsDuplicable();
    AddInput("X",
             "(LoDTensors) multi input tensor with shape{Rows, N}, N is the "
             "size of embedding table")
        .AsDuplicable();
    AddOutput("Out", "(LoDTensor) The merged outputs of the input tensors.")
Q
qiaolongfei 已提交
32 33 34 35
        .AsDuplicable();

    AddComment(R"DOC(
Merge multi LoDTensor's into one according to Ids's shard num.
Q
qiaolongfei 已提交
36 37 38 39 40 41 42 43 44 45 46 47


split_ids_op -> prefetch_op -> merge_ids_op


merge_ids_op should be used after split_ids_op and prefetch_op, split_ids_op
 will split input Ids into multiple tensors according to Id's shard number.
prefetch_op will send them to parameter server to prefetch embedding value
back. During split, the order of ids is disordered. In merge_ids_op we use
the original Ids to restore the order of the fetched embedding value and
 also pass the lod information to the merged output.

Q
qiaolongfei 已提交
48

Q
qiaolongfei 已提交
49
Example:
Q
qiaolongfei 已提交
50 51 52 53 54

    Ids = [1,2,3,4,5,6] # 3 shared

split_ids_op ->

Q
qiaolongfei 已提交
55 56 57
    Id0 = [3, 6] # id % 3 == 0
    Id1 = [1, 4] # id % 3 == 1
    Id2 = [2, 5] # id % 3 == 2
Q
qiaolongfei 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

prefetch_op ->

    X0 = [[0.3 0.3]   # 3
          [0.6 0.6]]  # 6
    X1 = [[0.1 0.1]   # 1
          [0.4 0.4]]  # 4
    X2 = [[0.2 0.2]   # 2
          [0.5 0.5]]  # 5

merge_ids_op ->

    Out = [[0.1 0.1]  # 1
           [0.2 0.2]  # 2
           [0.3 0.3]  # 3
           [0.4 0.4]  # 4
           [0.5 0.5]  # 5
           [0.6 0.6]] # 6
Q
qiaolongfei 已提交
76 77 78 79 80 81 82 83 84
)DOC");
  }
};

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

  void InferShape(framework::InferShapeContext *ctx) const override {
S
seiriosPlus 已提交
85 86 87 88 89 90 91
    PADDLE_ENFORCE(ctx->HasInputs("Ids"),
                   "MergeIdsOp must has multi input Ids.");
    PADDLE_ENFORCE(ctx->HasInputs("Rows"),
                   "MergeIdsOp must has multi input Rows.");
    PADDLE_ENFORCE(ctx->HasInputs("X"), "MergeIdsOp must has multi input X.");
    PADDLE_ENFORCE(ctx->HasOutputs("Out"),
                   "MergeIdsOp must has multi output Out.");
Q
qiaolongfei 已提交
92 93

    auto ids_var_type = ctx->GetInputsVarType("Ids").front();
S
seiriosPlus 已提交
94
    auto ids_dims = ctx->GetInputsDim("Ids");
Q
qiaolongfei 已提交
95
    if (ids_var_type == framework::proto::VarType::LOD_TENSOR) {
S
seiriosPlus 已提交
96 97
      PADDLE_ENFORCE_EQ(ids_dims[0].size(), 2);
      PADDLE_ENFORCE_EQ(ids_dims[0][1], 1);
Q
qiaolongfei 已提交
98 99 100 101 102 103 104 105
    }
    auto x_var_type = ctx->GetInputsVarType("X");
    for (auto &var_type : x_var_type) {
      PADDLE_ENFORCE_EQ(var_type, framework::proto::VarType::LOD_TENSOR,
                        "input X only support lod tensors");
    }
    ctx->ShareLoD("Ids", "Out");
  }
Q
qiaolongfei 已提交
106 107 108 109 110 111 112 113 114

 private:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(
            ctx.MultiInput<framework::Tensor>("X").front()->type()),
        ctx.GetPlace());
  }
Q
qiaolongfei 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
};

class MergeIdsOpInferVarType : public framework::VarTypeInference {
 public:
  void operator()(const framework::OpDesc &op_desc,
                  framework::BlockDesc *block) const override {
    auto *input_var = block->Var(op_desc.Input("Ids")[0]);
    for (auto &out_var : op_desc.Output("Out")) {
      block->Var(out_var)->SetType(input_var->GetType());
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(merge_ids, ops::MergeIdsOp, ops::MergeIdsOpMaker,
                  ops::MergeIdsOpInferVarType);
REGISTER_OP_CPU_KERNEL(
Q
qiaolongfei 已提交
135
    merge_ids, ops::MergeIdsOpKernel<paddle::platform::CPUPlace, float>);