|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/split_ids_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class SplitIdsOpMaker : public framework::OpProtoAndCheckerMaker { |
| 21 | + public: |
| 22 | + SplitIdsOpMaker(OpProto *proto, OpAttrChecker *op_checker) |
| 23 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 24 | + AddInput("Ids", "(LoDTensor) the input ids with shape{batch_num, 1}"); |
| 25 | + AddOutput("Out", "(LoDTensor) The outputs of the input Ids.") |
| 26 | + .AsDuplicable(); |
| 27 | + |
| 28 | + AddComment(R"DOC( |
| 29 | +Split a LoDTensor of Ids into multi LoDTensors, the number is pserver's number |
| 30 | +Example: |
| 31 | + Input: |
| 32 | + X = [1,2,3,4,5,6] |
| 33 | +
|
| 34 | + Out(3 output): |
| 35 | + out0 = [3, 6] |
| 36 | + out1 = [1, 4] |
| 37 | + out2 = [2, 5] |
| 38 | +)DOC"); |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +class SplitIdsOp : public framework::OperatorWithKernel { |
| 43 | + public: |
| 44 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 45 | + |
| 46 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 47 | + PADDLE_ENFORCE(ctx->HasInput("Ids"), "SplitIdsOp must has input Ids."); |
| 48 | + PADDLE_ENFORCE(ctx->HasOutputs("Out"), "SplitIdsOp must has output Out."); |
| 49 | + |
| 50 | + auto ids_var_type = ctx->GetInputsVarType("Ids").front(); |
| 51 | + PADDLE_ENFORCE_EQ(ids_var_type, framework::proto::VarType::LOD_TENSOR); |
| 52 | + |
| 53 | + auto ids_dims = ctx->GetInputDim("Ids"); |
| 54 | + PADDLE_ENFORCE_EQ(ids_dims.size(), 2); |
| 55 | + PADDLE_ENFORCE_EQ(ids_dims[1], 1); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +class SplitIdsOpInferVarType : public framework::VarTypeInference { |
| 60 | + public: |
| 61 | + void operator()(const framework::OpDesc &op_desc, |
| 62 | + framework::BlockDesc *block) const override { |
| 63 | + for (auto &out_var : op_desc.Output("Out")) { |
| 64 | + block->Var(out_var)->SetType(framework::proto::VarType::LOD_TENSOR); |
| 65 | + } |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +} // namespace operators |
| 70 | +} // namespace paddle |
| 71 | + |
| 72 | +namespace ops = paddle::operators; |
| 73 | +REGISTER_OPERATOR(split_ids, ops::SplitIdsOp, ops::SplitIdsOpMaker, |
| 74 | + ops::SplitIdsOpInferVarType); |
| 75 | +REGISTER_OP_CPU_KERNEL( |
| 76 | + split_ids, ops::SplitIdsOpKernel<paddle::platform::CPUPlace, int64_t>); |
0 commit comments