lod_tensor.h 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 <memory>
18
#ifdef PADDLE_WITH_CUDA
19 20
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
21
#include <thrust/system/cuda/experimental/pinned_allocator.h>
22 23
#endif

24
#include <glog/logging.h>
25 26 27
#include "paddle/framework/ddim.h"
#include "paddle/framework/tensor.h"
#include "paddle/platform/enforce.h"
28
#include "paddle/platform/place.h"
29 30 31 32

namespace paddle {
namespace framework {

33
#ifndef PADDLE_WITH_CUDA
Q
qijun 已提交
34 35 36 37
template <typename T>
using Vector = std::vector<T>;
#else
template <typename T>
38 39
using Vector = thrust::host_vector<
    T, thrust::system::cuda::experimental::pinned_allocator<T>>;
Q
qijun 已提交
40 41
#endif

42
/*
43
 * LoD is short for Level of Details.
44
 *
45
 * - in a level, each element indicates relative offset of the lower level
46 47 48
 * - the first element should be 0 and that indicates that this sequence start
 * from 0
 * - each sequence's begin and end(no-inclusive) is level[id, id+1]
49 50 51 52 53 54 55
 *
 * For example:
 *    3-level LoD stores
 *
 *    0 2 3
 *    0 2 4 7
 *    0 2 5 7 10 12 15 20
56
 */
57
using LoD = std::vector<Vector<size_t>>;
Q
qijun 已提交
58

59 60
std::ostream& operator<<(std::ostream& os, const LoD& lod);

61 62 63 64 65 66
/*
 * Slice levels from a LoD.
 * NOTE the lowest level should always be the absolute offsets of the underlying
 * tensor instances. So if higher layers are sliced without the lowest level,
 * the lower level of the sliced LoD will be transformed to the absolute offset.
 */
67
LoD SliceLevels(const LoD& in, size_t level_begin, size_t level_end);
Q
qijun 已提交
68

69
LoD SliceInLevel(const LoD& in, size_t level, size_t elem_begin,
Q
qijun 已提交
70
                 size_t elem_end);
71 72 73 74
/*
 * Transform an LoD from relative offsets to absolute offsets.
 */
LoD ToAbsOffset(const LoD& in);
Q
qijun 已提交
75

76
bool operator==(const LoD& a, const LoD& b);
Q
qijun 已提交
77

78
/*
79
 * LoDTensor (Level of details Tensor)
80 81
 * see https://en.wikipedia.org/wiki/Level_of_details for reference.
 */
82
class LoDTensor : public Tensor {
83
 public:
84
  LoDTensor() {}
85

86
  explicit LoDTensor(const LoD& lod) : lod_(lod) {}
Q
qijun 已提交
87

88
  void set_lod(const LoD& lod) { lod_ = lod; }
Q
qijun 已提交
89

Y
Yu Yang 已提交
90 91 92
  const LoD& lod() const { return lod_; }

  LoD* mutable_lod() { return &lod_; }
Q
qijun 已提交
93

94
  /*
W
wanghaoshuang 已提交
95
   * Get the start offset and end offset of an  element from LoD.
96
   */
W
wanghaoshuang 已提交
97
  std::pair<size_t, size_t> lod_element(size_t level, size_t elem) const {
98
    PADDLE_ENFORCE_LT(level, NumLevels());
W
wanghaoshuang 已提交
99 100
    PADDLE_ENFORCE_LT(elem, NumElements(level));
    return std::make_pair((lod_)[level][elem], (lod_)[level][elem + 1]);
101 102 103
  }

  /*
104
   * Number of LoDTensor's levels, each level has units of data, for example,
105 106
   * in the sentence's view, article, paragraph, sentence are 3 levels.
   */
107
  size_t NumLevels() const { return lod_.size(); }
108 109 110 111
  /*
   * Number of elements in a level.
   */
  size_t NumElements(size_t level = 0) const {
112
    PADDLE_ENFORCE_LT(level, NumLevels());
113
    // the last offset is the end of last element
Q
qijun 已提交
114
    return (lod_)[level].size() - 1;
115 116
  }

117 118 119 120 121 122 123 124 125 126 127 128
  /*
   * Number of lower-level elements.
   * For example, a 2-level lod-tensor
   *
   * 0-th level   |   |
   * 1-th level   ||  |||
   *
   * NumElements(0, 0) get 2
   * NumElements(0, 1) get 3
   */
  size_t NumElements(size_t level, size_t idx) const;

129 130 131 132 133 134
  /*
   * Get the number of instances in the underlying tensor in the `idx`-th
   * element.
   */
  size_t NumInstancesInElement(size_t level, size_t idx) const;

135
  /*
136
   * Shrink levels[level_begin:level_end]
137
   */
138
  void ShrinkLevels(size_t level_begin, size_t level_end);
139 140

  /*
141
   * Shrink elements of a level, [elem_begin: elem_end]
142
   * @note: low performance in slice lod_.
143
   */
144
  void ShrinkInLevel(size_t level, size_t elem_begin, size_t elem_end);
145

Q
qijun 已提交
146
 private:
147
  LoD lod_;
148
};
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

/*
 * Expand the `source` to fit the LoD of `lod`. For example, a `source`
 * LoDTensor is
 *  - LoD: [0, 2]
 *  - tensor: [a0, a1]
 * a `lod` is
 *  - LoD: [0 3 5]
 * returns a new LoDTensor
 *  - [a0 a0 a0 a1 a1]
 */
template <typename T>
LoDTensor LodExpand(const LoDTensor& source, const LoD& lod, size_t level,
                    const platform::Place& place) {
  LoD abs_lod = ToAbsOffset(lod);
  const auto& lod_level = lod[level];
  size_t num_instances = source.dims()[0];

  // new tensor
  LoDTensor tensor;
  tensor.set_lod(lod);
  auto dims = source.dims();
  dims[0] = lod_level.back();
  tensor.Resize(dims);
  tensor.mutable_data<T>(place);

  PADDLE_ENFORCE_EQ(num_instances, lod_level.size() - 1);
  for (size_t ins = 0; ins < num_instances; ins++) {
    for (size_t elem = lod_level[ins]; elem < lod_level[ins + 1]; elem++) {
      tensor.Slice(elem, elem + 1)
          .CopyFrom(source.Slice(ins, ins + 1), platform::CPUPlace(),
                    platform::CPUDeviceContext());
    }
  }
  return tensor;
}

186 187
std::pair<LoD, std::pair<size_t, size_t>> GetSubLoDAndAbsoluteOffset(
    const LoD& lod, size_t start_idx, size_t end_idx, size_t start_level);
188

189
void AppendLoD(LoD* lod, const LoD& lod_length);
190

191 192
}  // namespace framework
}  // namespace paddle