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

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
    PADDLE_ENFORCE(ctx->HasInputs("Ids"), "SplitIdsOp must has input Ids.");
Q
Qiao Longfei 已提交
56 57 58
    PADDLE_ENFORCE(ctx->HasOutputs("Out"), "SplitIdsOp must has output Out.");

    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(
C
chengduo 已提交
69
        framework::GetDataTypeOfVar(ctx.MultiInputVar("Ids").front()),
S
seiriosPlus 已提交
70 71
        ctx.GetPlace());
  }
Q
Qiao Longfei 已提交
72 73 74 75
};

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

S
seiriosPlus 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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 已提交
99 100 101 102 103
}  // namespace operators
}  // namespace paddle

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

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