Add LoDRankTable (#5349)
* Add LoDRankTable LoD Rank Table stores the `level` of `lod` which is ordered by sequence length in descending order. It is useful when implement dynamic RNN and is shared by dynamic RNN memory, dynamic RNN slice input and dynamic RNN slice output operators. * Add InferVarTypemobile_baidu
parent
73632deea0
commit
74849158e3
@ -0,0 +1,43 @@
|
||||
/* Copyright (c) 2016 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/framework/lod_rank_table.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
void LoDRankTable::Reset(const LoD& lod, size_t level) {
|
||||
this->coarse_lod_.clear();
|
||||
this->items_.clear();
|
||||
PADDLE_ENFORCE(level < lod.size(),
|
||||
"Cannot rank lod since the level %d is less than lod size %d",
|
||||
level, lod.size());
|
||||
coarse_lod_.reserve(level);
|
||||
for (size_t i = 0; i < level; ++i) {
|
||||
coarse_lod_.push_back(lod[i]);
|
||||
}
|
||||
auto& vec = lod[level];
|
||||
for (size_t i = 0; i < vec.size() - 1; ++i) {
|
||||
TableItem item;
|
||||
item.index = i;
|
||||
item.length = vec[i + 1] - vec[i];
|
||||
items_.emplace_back(item);
|
||||
}
|
||||
std::sort(items_.begin(), items_.end(),
|
||||
[](const TableItem& a, const TableItem& b) {
|
||||
return a.length > b.length;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,55 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
|
||||
#pragma once
|
||||
#include "paddle/framework/lod_tensor.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
// LoD Rank Table stores the `level` of `lod` which is ordered by sequence
|
||||
// length in descending order. It is useful when implement dynamic RNN and is
|
||||
// shared by dynamic RNN memory, dynamic RNN slice input and dynamic RNN slice
|
||||
// output operators.
|
||||
//
|
||||
// The table item contains two element. The length of sequence and the index of
|
||||
// sequence in that level.
|
||||
//
|
||||
// LoDRankTable also stores the coarse_lod, which is the lod information whose
|
||||
// level is less than input level, in order to restore the output LoD
|
||||
// information.
|
||||
class LoDRankTable {
|
||||
public:
|
||||
struct TableItem {
|
||||
size_t index;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
LoDRankTable() {}
|
||||
|
||||
void Reset(const LoD& lod, size_t level);
|
||||
|
||||
const std::vector<TableItem>& items() const { return this->items_; }
|
||||
|
||||
const LoD& coarse_lod() const { return this->coarse_lod_; }
|
||||
|
||||
size_t level() const { return coarse_lod_.size(); }
|
||||
|
||||
private:
|
||||
LoD coarse_lod_;
|
||||
std::vector<TableItem> items_;
|
||||
};
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,80 @@
|
||||
/* Copyright (c) 2016 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/framework/lod_rank_table.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class LoDRankTableOp : public framework::OperatorBase {
|
||||
public:
|
||||
LoDRankTableOp(const std::string &type,
|
||||
const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: OperatorBase(type, inputs, outputs, attrs) {}
|
||||
void Run(const framework::Scope &scope,
|
||||
const platform::DeviceContext &dev_ctx) const override {
|
||||
auto x = scope.FindVar(Input("X"))->Get<framework::LoDTensor>();
|
||||
auto *out =
|
||||
scope.FindVar(Output("Out"))->GetMutable<framework::LoDRankTable>();
|
||||
out->Reset(x.lod(), static_cast<size_t>(Attr<int>("level")));
|
||||
}
|
||||
};
|
||||
|
||||
class LoDRankTableOpProtoMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
LoDRankTableOpProtoMaker(framework::OpProto *proto,
|
||||
framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X",
|
||||
"(LoDTensor) input lod tensor, must contain lod information.");
|
||||
AddOutput("Out", "(LoDRankTable) The rank table of specific level.");
|
||||
AddAttr<int>("level", "(int) the specific lod level to rank.")
|
||||
.SetDefault(0)
|
||||
.EqualGreaterThan(0);
|
||||
AddComment(R"DOC(Create LoDRanTable by LoDTensor
|
||||
|
||||
LoD Rank Table stores the `level` of `lod` which is ordered by sequence
|
||||
length in descending order. It is useful when implement dynamic RNN and is
|
||||
shared by dynamic RNN memory, dynamic RNN slice input and dynamic RNN slice
|
||||
output operators.
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
class LoDRankTableInferShape : public framework::InferShapeBase {
|
||||
public:
|
||||
void operator()(framework::InferShapeContext *context) const override {
|
||||
PADDLE_ENFORCE(context->HasInput("X"), "LoDRankTable must has input X");
|
||||
}
|
||||
};
|
||||
|
||||
class LoDRankTableInferVarType : public framework::VarTypeInference {
|
||||
public:
|
||||
void operator()(const framework::OpDescBind &op_desc,
|
||||
framework::BlockDescBind *block) const override {
|
||||
for (auto &o : op_desc.Output("Out")) {
|
||||
block->Var(o)->SetType(framework::VarDesc::LOD_RANK_TABLE);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
REGISTER_OPERATOR(lod_rank_table, paddle::operators::LoDRankTableOp,
|
||||
paddle::operators::LoDRankTableOpProtoMaker,
|
||||
paddle::operators::LoDRankTableInferShape,
|
||||
paddle::operators::LoDRankTableInferVarType,
|
||||
paddle::framework::EmptyGradOpMaker);
|
@ -0,0 +1,29 @@
|
||||
from paddle.v2.framework.layers import lod_rank_table, data
|
||||
from paddle.v2.framework.executor import Executor
|
||||
from paddle.v2.framework.framework import g_program
|
||||
import paddle.v2.framework.core as core
|
||||
import numpy
|
||||
import unittest
|
||||
|
||||
|
||||
class TestLoDRankTable(unittest.TestCase):
|
||||
def test_lod_rank_table(self):
|
||||
x = data(name='x', shape=[100])
|
||||
cpu = core.CPUPlace()
|
||||
rank_table = lod_rank_table(x=x, level=1)
|
||||
rank_table.persistable = True
|
||||
exe = Executor(cpu)
|
||||
scope = core.Scope()
|
||||
|
||||
tensor = core.LoDTensor()
|
||||
tensor.set(numpy.random.random(size=(17, 100)), cpu)
|
||||
tensor.set_lod([[0, 1, 3], [0, 5, 6, 7], [0, 3, 4, 9, 10, 13, 16, 17]])
|
||||
|
||||
exe.run(g_program, scope=scope, feed={'x': tensor})
|
||||
var = scope.find_var(rank_table.name)
|
||||
table = var.get_lod_rank_table()
|
||||
self.assertEqual([(0, 5), (1, 1), (2, 1)], table.items())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue