lod_tensor.h 3.0 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>
Q
qijun 已提交
18
#ifndef PADDLE_ONLY_CPU
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 28 29 30 31
#include "paddle/framework/ddim.h"
#include "paddle/framework/tensor.h"
#include "paddle/platform/enforce.h"

namespace paddle {
namespace framework {

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

41
using LoD = std::vector<Vector<size_t>>;
Q
qijun 已提交
42

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

45
LoD SliceInLevel(const LoD& in, size_t level, size_t elem_begin,
Q
qijun 已提交
46 47
                 size_t elem_end);

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

50
/*
51
 * LoDTensor (Level of details Tensor)
52 53
 * see https://en.wikipedia.org/wiki/Level_of_details for reference.
 */
54
class LoDTensor : public Tensor {
55
 public:
56
  LoDTensor() {}
57

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

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

62
  LoD lod() { return lod_; }
Q
qijun 已提交
63

64
  /*
65
   * Get a element from LoD.
66 67 68 69 70 71 72
   */
  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));
73
    return (lod_)[level][elem];
74 75 76
  }

  /*
77
   * Number of LoDTensor's levels, each level has units of data, for example,
78 79
   * in the sentence's view, article, paragraph, sentence are 3 levels.
   */
80
  size_t NumLevels() const { return lod_.size(); }
81 82 83 84 85 86 87
  /*
   * 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
Q
qijun 已提交
88
    return (lod_)[level].size() - 1;
89 90 91
  }

  /*
Q
qijun 已提交
92
   * Slice of levels[level_begin:level_end]
93
   */
Q
qijun 已提交
94
  void SliceLevels(size_t level_begin, size_t level_end);
95 96

  /*
Q
qijun 已提交
97
   * Slice of elements of a level, [elem_begin: elem_end]
98
   * @note: low performance in slice lod_.
99
   */
Q
qijun 已提交
100
  void SliceInLevel(size_t level, size_t elem_begin, size_t elem_end);
101

Q
qijun 已提交
102
 private:
103
  LoD lod_;
104 105 106
};
}  // namespace framework
}  // namespace paddle