split_ids_op.cc 2.8 KB
Newer Older
Q
Qiao Longfei 已提交
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/split_ids_op.h"
Q
Qiao Longfei 已提交
16

M
minqiyang 已提交
17 18
#include <memory>

Q
Qiao Longfei 已提交
19 20 21 22 23
namespace paddle {
namespace operators {

class SplitIdsOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
24
  void Make() override {
S
seiriosPlus 已提交
25 26 27 28
    AddInput("Ids", "(LoDTensor) the input ids with shape{batch_num, 1}")
        .AsDuplicable();

    AddOutput("Out", "(LoDTensors) The outputs of the input Ids.")
Q
Qiao Longfei 已提交
29 30 31 32 33 34
        .AsDuplicable();

    AddComment(R"DOC(
Split a LoDTensor of Ids into multi LoDTensors, the number is pserver's number
Example:
  Input:
S
seiriosPlus 已提交
35
    X = [[1,2,3,4,5,6],[2,3]]
Q
Qiao Longfei 已提交
36 37

  Out(3 output):
S
seiriosPlus 已提交
38 39 40 41 42 43 44 45
    if compress is True:
        out0 = [3, 3, 6]
        out1 = [1, 4]
        out2 = [2, 2, 5]
    else:
        out0 = [3, 6]
        out1 = [1, 4]
        out2 = [2, 5]
Q
Qiao Longfei 已提交
46 47 48 49 50 51 52 53 54
)DOC");
  }
};

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

  void InferShape(framework::InferShapeContext *ctx) const override {
Z
zhangchunle 已提交
55 56
    PADDLE_ENFORCE(ctx->HasInputs("Ids"), "SplitIdsOp must have input Ids.");
    PADDLE_ENFORCE(ctx->HasOutputs("Out"), "SplitIdsOp must have output Out.");
Q
Qiao Longfei 已提交
57 58

    auto ids_var_type = ctx->GetInputsVarType("Ids").front();
S
seiriosPlus 已提交
59
    auto ids_dims = ctx->GetInputsDim("Ids");
60
    if (ids_var_type == framework::proto::VarType::LOD_TENSOR) {
S
seiriosPlus 已提交
61
      PADDLE_ENFORCE_EQ(ids_dims[0].size(), 2);
62
    }
Q
Qiao Longfei 已提交
63
  }
S
seiriosPlus 已提交
64 65 66 67 68

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
69
        OperatorWithKernel::IndicateVarDataType(ctx, "Ids"), ctx.GetPlace());
S
seiriosPlus 已提交
70
  }
Q
Qiao Longfei 已提交
71 72 73 74
};

class SplitIdsOpInferVarType : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
75 76 77 78
  void operator()(framework::InferVarTypeContext *ctx) const override {
    auto input_type = ctx->GetType(ctx->Input("Ids")[0]);
    for (auto &out_var : ctx->Output("Out")) {
      ctx->SetType(out_var, input_type);
Q
Qiao Longfei 已提交
79 80 81 82 83 84 85 86 87
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(split_ids, ops::SplitIdsOp, ops::SplitIdsOpMaker,
88
                  ops::SplitIdsOpInferVarType);
S
seiriosPlus 已提交
89

Q
Qiao Longfei 已提交
90
REGISTER_OP_CPU_KERNEL(
91 92
    split_ids, ops::SplitIdsOpKernel<paddle::platform::CPUPlace, int64_t>,
    ops::SplitIdsOpKernel<paddle::platform::CPUPlace, float>);