split_ids_op.cc 3.4 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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. */

#include "paddle/fluid/operators/split_ids_op.h"

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 67 68 69 70

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(
            ctx.MultiInput<framework::Tensor>("Ids").front()->type()),
        ctx.GetPlace());
  }
Q
Qiao Longfei 已提交
71 72 73 74 75 76
};

class SplitIdsOpInferVarType : public framework::VarTypeInference {
 public:
  void operator()(const framework::OpDesc &op_desc,
                  framework::BlockDesc *block) const override {
77
    auto *input_var = block->Var(op_desc.Input("Ids")[0]);
Q
Qiao Longfei 已提交
78
    for (auto &out_var : op_desc.Output("Out")) {
79
      block->Var(out_var)->SetType(input_var->GetType());
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>);