lod_rank_table.h 1.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yu Yang 已提交
14 15

#pragma once
Y
Yu Yang 已提交
16
#include <iosfwd>
17
#include <vector>
Y
Yi Wang 已提交
18
#include "paddle/fluid/framework/lod_tensor.h"
Y
Yu Yang 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

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
Y
Yu Yang 已提交
57 58 59 60

std::ostream& operator<<(std::ostream& out,
                         const framework::LoDRankTable& table);

Y
Yu Yang 已提交
61
}  // namespace paddle