split_ids_op.cc 3.0 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 {
S
seiriosPlus 已提交
55 56
    OP_INOUT_CHECK(ctx->HasInput("Ids"), "Input", "Ids", "SplitIdsOp");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "SplitIdsOp");
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 62 63 64 65 66 67
      PADDLE_ENFORCE_EQ(
          ids_dims[0], 2,
          platform::errors::InvalidArgument(
              "ShapeError: The dimensions of the 'split_ids' must be 2. "
              "But received split_ids's dimensions = %d, "
              "split_ids's shape = [%s].",
              ids_dims[0].size(), ids_dims[0]));
68
    }
Q
Qiao Longfei 已提交
69
  }
S
seiriosPlus 已提交
70 71 72 73 74

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
75
        OperatorWithKernel::IndicateVarDataType(ctx, "Ids"), ctx.GetPlace());
S
seiriosPlus 已提交
76
  }
Q
Qiao Longfei 已提交
77 78 79 80
};

class SplitIdsOpInferVarType : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
81
  void operator()(framework::InferVarTypeContext *ctx) const override {
82 83
    auto input_type = ctx->GetInputType("Ids");
    ctx->SetOutputType("Out", input_type, framework::ALL_ELEMENTS);
Q
Qiao Longfei 已提交
84 85 86 87 88 89 90 91
  }
};

}  // namespace operators
}  // namespace paddle

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

Q
Qiao Longfei 已提交
94
REGISTER_OP_CPU_KERNEL(
95 96
    split_ids, ops::SplitIdsOpKernel<paddle::platform::CPUPlace, int64_t>,
    ops::SplitIdsOpKernel<paddle::platform::CPUPlace, float>);