lod_tensor_array.h 3.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yu Yang 已提交
14 15 16

#pragma once
#include <vector>
Y
Yi Wang 已提交
17
#include "paddle/fluid/framework/lod_tensor.h"
Y
Yu Yang 已提交
18 19 20

namespace paddle {
namespace framework {
Y
Yan Chunwei 已提交
21 22 23 24 25 26

// NOTE The vector<LoDTensor> can't be replaced with the class LoDTensorArray
// directly, because there are many vector<LoDTensor> used accross the project,
// and some of them are treated as LoDTensorArray.
#if !defined(PADDLE_ON_INFERENCE)

Y
Yu Yang 已提交
27
using LoDTensorArray = std::vector<LoDTensor>;
Y
Yan Chunwei 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

#else  // !PADDLE_ON_INFERENCE

#pragma message "LoDTensorArray is replaced with the inference one."
/*
 * A LoDTensorArray which will not deallocate buffer when resized, fix the data
 * diff in inference, and more performance friendly in the concurrency
 * scenerios.
 */
class LoDTensorArray {
 public:
  LoDTensorArray() = default;

  using iterator = std::vector<LoDTensor>::iterator;
  using const_iterator = std::vector<LoDTensor>::const_iterator;

  const_iterator begin() const { return array_.begin(); }
  const_iterator end() const { return array_.begin() + size_; }
  iterator begin() { return array_.begin(); }
  iterator end() { return array_.begin() + size_; }

  void push_back(const LoDTensor& x) {
    if (size_ < array_.size()) {
      array_[size_++] = x;
    } else {
      array_.push_back(x);
      ++size_;
    }
  }
  void resize(size_t size) {
    if (array_.size() < size) {
      array_.resize(size);
    }
    size_ = size;
  }

  void emplace_back() { array_.emplace_back(); }

  void emplace_back(LoDTensor&& x) { array_.emplace_back(std::move(x)); }

  LoDTensor& back() { return array_.back(); }

  size_t space() const { return array_.size(); }

  void reserve(size_t size) {
    // Naive warning to tell user this array might be to large. The memory and
    // buffer used by this TensorArray will not be deleted during the training
    // and inference phase, so attention not to make it expand too long.
    if (size > 800UL) {
      LOG(WARNING) << "TensorArray has more than 800 items";
    }
    array_.reserve(size);
  }

  bool empty() const { return size_ == 0UL; }
  void clear() { size_ = 0UL; }

  LoDTensor& operator[](size_t id) { return array_[id]; }
  const LoDTensor& operator[](size_t id) const { return array_[id]; }
  LoDTensor& at(size_t id) { return array_.at(id); }
  const LoDTensor& at(size_t id) const { return array_.at(id); }

  size_t size() const { return size_; }

 private:
  size_t size_{0};
  std::vector<LoDTensor> array_;
};
#endif  // !PADDLE_ON_INFERENCE

}  // namespace framework
Y
Yu Yang 已提交
99
}  // namespace paddle