|
| 1 | +/* Copyright (c) 2016 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 | +#include "paddle/framework/lod_rank_table.h" |
| 15 | +#include "paddle/framework/op_registry.h" |
| 16 | +namespace paddle { |
| 17 | +namespace operators { |
| 18 | + |
| 19 | +class LoDRankTableOp : public framework::OperatorBase { |
| 20 | + public: |
| 21 | + LoDRankTableOp(const std::string &type, |
| 22 | + const framework::VariableNameMap &inputs, |
| 23 | + const framework::VariableNameMap &outputs, |
| 24 | + const framework::AttributeMap &attrs) |
| 25 | + : OperatorBase(type, inputs, outputs, attrs) {} |
| 26 | + void Run(const framework::Scope &scope, |
| 27 | + const platform::DeviceContext &dev_ctx) const override { |
| 28 | + auto x = scope.FindVar(Input("X"))->Get<framework::LoDTensor>(); |
| 29 | + auto *out = |
| 30 | + scope.FindVar(Output("Out"))->GetMutable<framework::LoDRankTable>(); |
| 31 | + out->Reset(x.lod(), static_cast<size_t>(Attr<int>("level"))); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +class LoDRankTableOpProtoMaker : public framework::OpProtoAndCheckerMaker { |
| 36 | + public: |
| 37 | + LoDRankTableOpProtoMaker(framework::OpProto *proto, |
| 38 | + framework::OpAttrChecker *op_checker) |
| 39 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 40 | + AddInput("X", |
| 41 | + "(LoDTensor) input lod tensor, must contain lod information."); |
| 42 | + AddOutput("Out", "(LoDRankTable) The rank table of specific level."); |
| 43 | + AddAttr<int>("level", "(int) the specific lod level to rank.") |
| 44 | + .SetDefault(0) |
| 45 | + .EqualGreaterThan(0); |
| 46 | + AddComment(R"DOC(Create LoDRanTable by LoDTensor |
| 47 | +
|
| 48 | +LoD Rank Table stores the `level` of `lod` which is ordered by sequence |
| 49 | +length in descending order. It is useful when implement dynamic RNN and is |
| 50 | +shared by dynamic RNN memory, dynamic RNN slice input and dynamic RNN slice |
| 51 | +output operators. |
| 52 | +)DOC"); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +class LoDRankTableInferShape : public framework::InferShapeBase { |
| 57 | + public: |
| 58 | + void operator()(framework::InferShapeContext *context) const override { |
| 59 | + PADDLE_ENFORCE(context->HasInput("X"), "LoDRankTable must has input X"); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +class LoDRankTableInferVarType : public framework::VarTypeInference { |
| 64 | + public: |
| 65 | + void operator()(const framework::OpDescBind &op_desc, |
| 66 | + framework::BlockDescBind *block) const override { |
| 67 | + for (auto &o : op_desc.Output("Out")) { |
| 68 | + block->Var(o)->SetType(framework::VarDesc::LOD_RANK_TABLE); |
| 69 | + } |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +} // namespace operators |
| 74 | +} // namespace paddle |
| 75 | + |
| 76 | +REGISTER_OPERATOR(lod_rank_table, paddle::operators::LoDRankTableOp, |
| 77 | + paddle::operators::LoDRankTableOpProtoMaker, |
| 78 | + paddle::operators::LoDRankTableInferShape, |
| 79 | + paddle::operators::LoDRankTableInferVarType, |
| 80 | + paddle::framework::EmptyGradOpMaker); |
0 commit comments