int_array.h 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

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

17 18
#include "paddle/phi/api/ext/exception.h"
#include "paddle/phi/api/include/tensor.h"
19 20 21 22 23

namespace paddle {
namespace experimental {

template <typename T>
24
class IntArrayBase {
25 26
 public:
  // Constructor support implicit
27
  IntArrayBase() = default;
28

29
  IntArrayBase(const std::vector<int64_t>& vec) : array_(vec) {}  // NOLINT
30

31
  IntArrayBase(const std::vector<int32_t>& vec) {  // NOLINT
32 33 34
    array_.insert(array_.begin(), vec.begin(), vec.end());
  }

35
  IntArrayBase(std::initializer_list<int64_t> array_list)
36 37
      : array_(array_list) {}

38
  IntArrayBase(const int64_t* date_value, int64_t n) {
39 40 41
    AssignData(date_value, n);
  }

42
  IntArrayBase(const int32_t* date_value, int64_t n) {
43 44 45
    AssignData(date_value, n);
  }

46
  bool FromTensor() const { return is_from_tensor_; }
C
chentianyu03 已提交
47

48
  void SetFromTensor(bool val) { is_from_tensor_ = val; }
C
chentianyu03 已提交
49

50
  // The Tensor must have one dim
51
  IntArrayBase(const T& tensor) {  // NOLINT
52
    is_from_tensor_ = true;
53 54
    size_t n = tensor.numel();
    array_.reserve(n);
55
    switch (tensor.dtype()) {
56 57 58 59 60 61 62 63
      case DataType::INT32:
        AssignData(tensor.template data<int32_t>(), n);
        break;
      case DataType::INT64:
        AssignData(tensor.template data<int64_t>(), n);
        break;
      default:
        PD_THROW(
64
            "Data type error. Currently, The data type of IntArrayBase "
65 66
            "only supports Tensor with int32 and int64, "
            "but now received `",
67
            tensor.dtype(),
68 69 70 71 72
            "`.");
    }
  }

  // The Tensor in vec must have only one element
73
  IntArrayBase(const std::vector<T>& tensor_list) {  // NOLINT
74
    is_from_tensor_ = true;
C
chentianyu03 已提交
75 76 77

    for (size_t i = 0; i < tensor_list.size(); ++i) {
      DataType data_type = tensor_list[i].dtype();
78
      switch (data_type) {
C
chentianyu03 已提交
79 80
        case DataType::INT32:
          array_.push_back(*tensor_list[i].template data<int32_t>());
81
          break;
C
chentianyu03 已提交
82 83
        case DataType::INT64:
          array_.push_back(*tensor_list[i].template data<int64_t>());
84 85 86
          break;
        default:
          PD_THROW(
87
              "Data type error. Currently, The data type of IntArrayBase "
88 89 90 91 92 93 94 95 96
              "only supports Tensor with int32 and int64, "
              "but now received `",
              data_type,
              "`.");
      }
    }
  }

  template <typename OtherT>
97
  IntArrayBase(const IntArrayBase<OtherT>& other) : array_(other.GetData()) {}
98

99 100
  size_t size() const { return array_.size(); }

101 102 103 104 105 106
  const std::vector<int64_t>& GetData() const { return array_; }

 private:
  /// \brief Assign the data_ from const data pointer value of type T.
  template <typename TYPE>
  void AssignData(const TYPE* value_data, int64_t n) {
107
    if (value_data || n == 0) {
108 109 110 111 112 113 114 115 116 117 118 119 120
      array_.reserve(n);
      for (auto i = 0; i < n; ++i) {
        array_.push_back(static_cast<int64_t>(value_data[i]));
      }
    } else {
      PD_THROW("The input data pointer is null.");
    }
  }

 private:
  // TODO(zhangyunfei) Replace std::vector with a more efficient container
  // structure.
  std::vector<int64_t> array_;
121
  bool is_from_tensor_{false};
122 123
};

124 125
using IntArray =
    paddle::experimental::IntArrayBase<paddle::experimental::Tensor>;
126 127 128 129

}  // namespace experimental
}  // namespace paddle

130
namespace phi {
131 132

class DenseTensor;
133
using IntArray = paddle::experimental::IntArrayBase<DenseTensor>;
134

135
}  // namespace phi