lod_tensor.h 4.7 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>
L
liaogang 已提交
18
#if !defined(PADDLE_ONLY_CPU)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#endif

#include "paddle/framework/ddim.h"
#include "paddle/framework/tensor.h"
#include "paddle/platform/enforce.h"

namespace paddle {
namespace framework {

/*
 * LODTensor (Level of details Tensor)
 * see https://en.wikipedia.org/wiki/Level_of_details for reference.
 */
34
class LODTensor : public Tensor {
35 36 37
 public:
// Level save offsets of each unit.
#ifdef PADDLE_ONLY_CPU
38 39
  template <typename T>
  using Vector = std::vector<T>;
40
#else
41 42
  template <typename T>
  using Vector = thrust::host_vector<T>;
43
#endif
44
  // LoD stores offsets of each level of units, the largest units level first,
45 46
  // then the smaller units level. Each Level stores the offsets of units in
  // Tesor.
47 48 49 50 51
  class LOD : public std::vector<Vector<size_t>> {
   public:
    LOD SliceLevels(size_t level_begin, size_t level_end) const;
    LOD SliceInLevel(size_t level, size_t elem_begin, size_t elem_end) const;
  };
52 53

  LODTensor() {}
54
  explicit LODTensor(const LOD &lod) : lod_(lod) {}
55

56
  virtual Tensor *Clone() const { return new LODTensor(lod_); }
57 58 59 60 61 62 63 64 65 66

  /*
   * Get a element from LOD.
   */
  size_t lod_element(size_t level, size_t elem) const {
    PADDLE_ENFORCE(level < NumLevels(), "level [%d] out of range [%d]", level,
                   NumLevels());
    PADDLE_ENFORCE(elem < NumElements(level),
                   "element begin [%d] out of range [%d]", elem,
                   NumElements(level));
67
    return (lod_)[level][elem];
68 69 70 71 72 73
  }

  /*
   * Number of LODTensor's levels, each level has units of data, for example,
   * in the sentence's view, article, paragraph, sentence are 3 levels.
   */
74
  size_t NumLevels() const { return lod_.size(); }
75 76 77 78 79 80 81
  /*
   * Number of elements in a level.
   */
  size_t NumElements(size_t level = 0) const {
    PADDLE_ENFORCE(level < NumLevels(), "level [%d] out of range [%d]", level,
                   NumLevels());
    // the last offset is the end of last element
82
    return lod_[level].size() - 1;
83 84 85 86 87 88
  }

  /*
   * Slice of levels[level_begin:level_end], with tensor shared.
   */
  template <typename T>
89
  LODTensor SliceLevels(size_t level_begin, size_t level_end) const;
90 91 92

  /*
   * Slice of elements of a level, [elem_begin: elem_end], with tensor shared.
93
   * @note: low performance in slice lod_.
94
   */
95 96 97
  template <typename T>
  LODTensor SliceInLevel(size_t level, size_t elem_begin,
                         size_t elem_end) const;
98 99

  /*
100
   * Copy other's lod_'s content, free to mutate.
101
   */
102
  void CopyLOD(const LODTensor &other) { lod_ = other.lod_; }
103 104 105
  /*
   * Determine whether LODTensor has a valid LOD info.
   */
106 107
  const LOD &lod() const { return lod_; }
  LOD *mutable_lod() { return &lod_; }
108

109
  virtual ~LODTensor() {}
110 111

 private:
112
  LOD lod_;
113 114
};

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
bool operator==(const LODTensor::LOD &a, const LODTensor::LOD &b);

template <typename T>
LODTensor LODTensor::SliceLevels(size_t level_begin, size_t level_end) const {
  auto new_lod = lod_.SliceLevels(level_begin, level_end);
  // slice levels just need to update LOD info, each level will contains the
  // whole tensor_, so no need to modify tensor_.
  LODTensor new_tensor(new_lod);
  new_tensor.ShareDataWith<T>(*this);
  return new_tensor;
}

template <typename T>
LODTensor LODTensor::SliceInLevel(size_t level, size_t elem_begin,
                                  size_t elem_end) const {
  PADDLE_ENFORCE(level < NumLevels(), "level [%d] out of range [%d]", level,
                 NumLevels());
  PADDLE_ENFORCE(elem_begin < NumElements(level),
                 "element begin [%d] out of range [%d]", elem_begin,
                 NumElements(level));
  PADDLE_ENFORCE(elem_end < NumElements(level) + 1,
                 "element end [%d] out of range [%d]", elem_end,
                 NumElements(level));

  auto new_lod = lod_.SliceInLevel(level, elem_begin, elem_end);

  // slice elements just need to update LOD info, because offsets are not
  // changed, so the original tensor_ can be reused.
  LODTensor new_tensor(new_lod);
  new_tensor.ShareDataWith<T>(*this);
  return new_tensor;
}

148 149
}  // namespace framework
}  // namespace paddle