split_ids_op.cc 3.3 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 17 18 19 20 21

namespace paddle {
namespace operators {

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

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

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

  Out(3 output):
S
seiriosPlus 已提交
36 37 38 39 40 41 42 43
    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 已提交
44 45 46 47 48 49 50 51 52
)DOC");
  }
};

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

  void InferShape(framework::InferShapeContext *ctx) const override {
S
seiriosPlus 已提交
53
    PADDLE_ENFORCE(ctx->HasInputs("Ids"), "SplitIdsOp must has input Ids.");
Q
Qiao Longfei 已提交
54 55 56
    PADDLE_ENFORCE(ctx->HasOutputs("Out"), "SplitIdsOp must has output Out.");

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

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
C
chengduo 已提交
67
        framework::GetDataTypeOfVar(ctx.MultiInputVar("Ids").front()),
S
seiriosPlus 已提交
68 69
        ctx.GetPlace());
  }
Q
Qiao Longfei 已提交
70 71 72 73
};

class SplitIdsOpInferVarType : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
74 75 76 77
  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 已提交
78 79 80 81
    }
  }
};

S
seiriosPlus 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
class SplitIdsOpGradMaker : public framework::SingleGradOpDescMaker {
 public:
  using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

 protected:
  std::unique_ptr<framework::OpDesc> Apply() const override {
    auto grad = new framework::OpDesc();
    grad->SetType("concat");
    grad->SetInput("X", OutputGrad("Out"));
    grad->SetOutput("Out", InputGrad("Ids"));
    grad->SetAttr("axis", 0);
    return std::unique_ptr<framework::OpDesc>(grad);
  }
};

Q
Qiao Longfei 已提交
97 98 99 100 101
}  // namespace operators
}  // namespace paddle

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

Q
Qiao Longfei 已提交
104
REGISTER_OP_CPU_KERNEL(
105 106
    split_ids, ops::SplitIdsOpKernel<paddle::platform::CPUPlace, int64_t>,
    ops::SplitIdsOpKernel<paddle::platform::CPUPlace, float>);