lod_tensor.cc 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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. */

#include "paddle/framework/lod_tensor.h"

#include <glog/logging.h>

namespace paddle {
namespace framework {

22 23
LoD SliceLevels(const LoD& in, size_t level_begin, size_t level_end) {
  LoD new_lod;
24 25
  new_lod.reserve(level_end - level_begin);
  for (size_t i = level_begin; i < level_end; i++) {
Q
qijun 已提交
26
    new_lod.emplace_back(in.at(i));
27 28
  }
  return new_lod;
29 30
}

31
LoD SliceInLevel(const LoD& in, size_t level, size_t elem_begin,
Q
qijun 已提交
32
                 size_t elem_end) {
33
  // slice the lod.
34
  LoD new_lod;
Q
qijun 已提交
35 36 37
  new_lod.reserve(in.size() - level);
  auto start = in.at(level)[elem_begin];
  auto end = in.at(level)[elem_end];
38

Q
qijun 已提交
39
  for (auto it = in.begin() + level; it != in.end(); it++) {
40 41 42 43 44 45 46 47 48
    auto it_begin = std::find(it->begin(), it->end(), start);
    auto it_end = std::find(it_begin, it->end(), end);
    PADDLE_ENFORCE(it_begin != it->end(), "error in parsing lod info");
    PADDLE_ENFORCE(it_end != it->end(), "error in parsing lod info");
    new_lod.emplace_back(it_begin, it_end + 1);
    // reset offset if tensor is copyed and sliced.
    std::transform(new_lod.back().begin(), new_lod.back().end(),
                   new_lod.back().begin(),
                   [start](int v) { return v - start; });
49
    PADDLE_ENFORCE_EQ(new_lod.back().front(), 0, "error in slice LoD");
50
  }
Q
qijun 已提交
51
  PADDLE_ENFORCE_LE(new_lod.size(), in.size());
52 53 54
  return new_lod;
}

55
bool operator==(const LoD& a, const LoD& b) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  if (a.size() != b.size()) {
    return false;
  }

  for (size_t i = 0; i < a.size(); i++) {
    const auto& a_level = a[i];
    const auto& b_level = b[i];
    if (a_level.size() != b_level.size()) {
      return false;
    }
    for (size_t j = 0; j < a_level.size(); j++) {
      if (a_level[j] != b_level[j]) {
        return false;
      }
    }
  }
  return true;
73 74
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
size_t LoDTensor::NumElements(size_t level, size_t idx) const {
  PADDLE_ENFORCE_LT(level, NumLevels());
  PADDLE_ENFORCE_LT(idx, NumElements(level));
  // the last level of LoD, just return number of records in Tensor
  if (level == NumLevels() - 1) {
    return lod_[level][idx + 1] - lod_[level][idx];
  }
  // high level of LoD, and there is another lower level, return number of
  // lower-level elements
  auto tmp = SliceInLevel(lod_, level, idx, idx + 1);
  PADDLE_ENFORCE_GE(tmp.size(), 2);
  // there is a 0 as a placeholder stored in LoD, so the number of elements
  // equals lod.size() - 1
  return tmp[1].size() - 1;
}

91
void LoDTensor::ShrinkLevels(size_t level_begin, size_t level_end) {
Q
qijun 已提交
92 93 94 95
  auto new_lod = framework::SliceLevels(lod_, level_begin, level_end);
  lod_ = new_lod;
}

96 97 98 99 100
void LoDTensor::ShrinkInLevel(size_t level, size_t elem_begin,
                              size_t elem_end) {
  PADDLE_ENFORCE_LT(level, NumLevels());
  PADDLE_ENFORCE_LT(elem_begin, NumElements(level));
  PADDLE_ENFORCE_LT(elem_end, NumElements(level) + 1);
Q
qijun 已提交
101 102 103 104 105

  auto new_lod = framework::SliceInLevel(lod_, level, elem_begin, elem_end);
  lod_ = new_lod;
}

W
wanghaoshuang 已提交
106 107 108 109 110 111 112
Vector<size_t> repeat_lod(Vector<size_t> data, Vector<size_t> starts,
                          Vector<size_t> times, bool is_first) {
  Vector<size_t> result;
  result.push_back(data[0]);
  size_t p = 0, start = 0, end = 0;
  if (is_first == true) {
    for (size_t i = 0; i < times.size(); ++i) {
W
wanghaoshuang 已提交
113
      result.push_back(result.back() + times[i] * (data[i + 1] - data[i]));
W
wanghaoshuang 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }
  } else {
    for (size_t i = 0; i < times.size(); ++i) {
      while (starts[i] != data[p] && p < data.size()) {
        ++p;
      }
      start = p;
      while (starts[i + 1] != data[p] && p < data.size()) {
        ++p;
      }
      end = p + 1;
      for (size_t j = 0; j < times[i]; ++j) {
        for (size_t index = start; index < end - 1; ++index) {
          result.push_back(result.back() + data[index + 1] - data[index]);
        }
      }
    }
  }
  return result;
}

135 136
}  // namespace framework
}  // namespace paddle