lod_tensor.h 3.5 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>
W
wanghaoshuang 已提交
18 19 20
#include "paddle/memory/memcpy.h"
#include "paddle/platform/device_context.h"
#include "paddle/platform/place.h"
21
#ifdef PADDLE_WITH_CUDA
22 23
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
24
#include <thrust/system/cuda/experimental/pinned_allocator.h>
25 26
#endif

27
#include <glog/logging.h>
28 29 30 31 32 33 34
#include "paddle/framework/ddim.h"
#include "paddle/framework/tensor.h"
#include "paddle/platform/enforce.h"

namespace paddle {
namespace framework {

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

44 45 46 47 48 49 50 51 52 53 54 55
/*
 * 3-level LoD stores
 *
 * 0 10 20
 * 0 5 10 15 20
 * 0 2 5 7 10 12 15 20
 *
 * - in a level, each element indicates offset in the underlying Tensor
 * - 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]
 */
56
using LoD = std::vector<Vector<size_t>>;
Q
qijun 已提交
57

58
LoD SliceLevels(const LoD& in, size_t level_begin, size_t level_end);
Q
qijun 已提交
59

60
LoD SliceInLevel(const LoD& in, size_t level, size_t elem_begin,
Q
qijun 已提交
61 62
                 size_t elem_end);

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

65
/*
66
 * LoDTensor (Level of details Tensor)
67 68
 * see https://en.wikipedia.org/wiki/Level_of_details for reference.
 */
69
class LoDTensor : public Tensor {
70
 public:
71
  LoDTensor() {}
72

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

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

77
  LoD lod() const { return lod_; }
Q
qijun 已提交
78

79
  /*
80
   * Get a element from LoD.
81 82
   */
  size_t lod_element(size_t level, size_t elem) const {
83 84
    PADDLE_ENFORCE_LT(level, NumLevels());
    PADDLE_ENFORCE_LT(elem, NumElements(level));
85
    return (lod_)[level][elem];
86 87 88
  }

  /*
89
   * Number of LoDTensor's levels, each level has units of data, for example,
90 91
   * in the sentence's view, article, paragraph, sentence are 3 levels.
   */
92
  size_t NumLevels() const { return lod_.size(); }
93 94 95 96
  /*
   * Number of elements in a level.
   */
  size_t NumElements(size_t level = 0) const {
97
    PADDLE_ENFORCE_LT(level, NumLevels());
98
    // the last offset is the end of last element
Q
qijun 已提交
99
    return (lod_)[level].size() - 1;
100 101
  }

102 103 104 105 106 107 108 109 110 111 112 113
  /*
   * 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;

114
  /*
115
   * Shrink levels[level_begin:level_end]
116
   */
117
  void ShrinkLevels(size_t level_begin, size_t level_end);
118 119

  /*
120
   * Shrink elements of a level, [elem_begin: elem_end]
121
   * @note: low performance in slice lod_.
122
   */
123
  void ShrinkInLevel(size_t level, size_t elem_begin, size_t elem_end);
124

Q
qijun 已提交
125
 private:
126
  LoD lod_;
127
};
W
wanghaoshuang 已提交
128 129 130 131

Vector<size_t> repeat_lod(Vector<size_t> data, Vector<size_t> starts,
                          Vector<size_t> times, bool is_first);

132 133
}  // namespace framework
}  // namespace paddle