tensor_array.h 3.3 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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 <vector>

#include "paddle/framework/lod_tensor.h"

namespace paddle {
namespace framework {

/*
 * DyBatchSeqPosition stores indices of the basic element in tensor. It is used
 * after lod-tensor's re-assembling, its info can be used to recover the order
 * in original lod-tensor.
 */
struct DySeqMeta {
29 30 31
  DySeqMeta(size_t begin, size_t end, size_t ori_idx)
      : begin(begin), end(end), ori_idx(ori_idx) {}

Y
Yan Chunwei 已提交
32 33 34 35 36
  size_t begin;
  size_t end;  // not included
  size_t ori_idx;
};

37 38 39 40 41 42 43
using DySeqMetaBatch = std::vector<DySeqMeta>;

/*
 * Extract the indices of instances.
 */
std::vector<size_t> GenDyBatchIndice(const DySeqMetaBatch &metas, int batch_id);

Y
Yan Chunwei 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/*
 * TensorArray is a C-array-like array of tensors, it is meant to be used with
 * dynamic iteration primitives such as while_loop. It is used to segment inputs
 * and store states in all time steps.
 *
 * By providing some methods similar to a C++ array, the difinition of some
 * state-based dynamic models such as RNN cound be more natural and highly
 * flexible.
 */
class TensorArray {
 public:
  using value_type = float;

  // max number of values allowed to store.
  const size_t MAX_SIZE{100000};

  /*
   * Read the value at location `index` in the `TensorArray`.
   */
  const LoDTensor &Read(size_t index) const;

  /*
   * Write value into the index of the TensorArray.
   */
  void Write(size_t index, const LoDTensor &value);

  /*
   * Write value into the index of the TensorArray, with memory shared.
   */
  void WriteShared(size_t index, const LoDTensor &value);

  /*
   * Recover the original LoD-arranged LoDTensor with the `values`, `level` and
   * `indice_map`.
   */
79
  LoDTensor Pack(size_t level, const DySeqMetaBatch &meta,
Y
Yan Chunwei 已提交
80 81 82 83 84 85 86
                 const LoD &lod) const;

  /*
   * Split LoDTensor in some `level` and write the generated batches to
   * `values`, if set `desend`, will sort by length in descending order else in
   * ascending order.
   */
87
  DySeqMetaBatch Unpack(const LoDTensor &source, int level, bool length_desend);
Y
Yan Chunwei 已提交
88 89 90 91 92 93 94 95

  /*
   * Pack the values into a tensor with rank one higher than each tensor in
   * values.
   */
  LoDTensor Stack() const;

  /*
96
   * Unstacks the given division of a rank-`R` tensor into rank-`(R-1)` tensors.
Y
Yan Chunwei 已提交
97 98 99 100
   */
  void Unstack(const LoDTensor &source) const;

  /*
101
   * Unstacks the given division of a rank-`R` tensor into rank-`(R-1)` tensors,
Y
Yan Chunwei 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
   * with memory of tensors shared.
   */
  void UnstackShared(const LoDTensor &source) const;

  /*
   * Return the number of values.
   */
  size_t size() const;

 protected:
  void Unstack(const LoDTensor &source, bool data_shared) const;

 private:
  mutable std::vector<LoDTensor> values_;
};  // class TensorArray

}  // namespace framework
}  // namespace paddle