|
| 1 | +#define TORCH_ASSERT_NO_OPERATORS |
| 2 | +#include <caffe2/core/IValueInterface.h> |
| 3 | +#undef TORCH_ASSERT_NO_OPERATORS |
| 4 | +#include <ATen/core/ivalue.h> |
| 5 | +#include <c10/util/irange.h> |
| 6 | + |
| 7 | +#include <memory> |
| 8 | + |
| 9 | +namespace caffe2 { |
| 10 | +namespace detail { |
| 11 | + |
| 12 | +caffe2::Tensor contiguous(caffe2::Tensor t) { |
| 13 | + return caffe2::Tensor(at::Tensor(std::move(t)).contiguous()); |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +IValueInterface::IValueInterface(ArrayRef<c10::IValue> values): |
| 18 | + size_(values.size()) { |
| 19 | + auto values_tmp = std::make_unique<c10::IValue[]>(size_); |
| 20 | + std::copy_n(values.data(), size_, values_tmp.get()); |
| 21 | + values_ = values_tmp.release(); |
| 22 | +} |
| 23 | + |
| 24 | +IValueInterface::~IValueInterface() { |
| 25 | + delete[] values_; |
| 26 | +} |
| 27 | + |
| 28 | +bool IValueInterface::isTensorList(size_t idx) const { |
| 29 | + return at(idx).isTensorList(); |
| 30 | +} |
| 31 | + |
| 32 | +bool IValueInterface::isTensor(size_t idx) const { |
| 33 | + return at(idx).isTensor(); |
| 34 | +} |
| 35 | + |
| 36 | +const c10::IValue& IValueInterface::at(size_t idx) const { |
| 37 | + TORCH_INTERNAL_ASSERT(idx < size_, "caffe2: Input index ", idx, |
| 38 | + " is out of range for call with ", size_, " arguments"); |
| 39 | + return values_[idx]; |
| 40 | +} |
| 41 | + |
| 42 | +std::vector<caffe2::Tensor> IValueInterface::toTensorVector(size_t idx) const { |
| 43 | + auto list = at(idx).toTensorList(); |
| 44 | + std::vector<caffe2::Tensor> ret(list.size()); |
| 45 | + for (auto i : c10::irange(list.size())) { |
| 46 | + ret[i] = caffe2::Tensor(list.get(i)); |
| 47 | + } |
| 48 | + return ret; |
| 49 | +} |
| 50 | + |
| 51 | +caffe2::Tensor IValueInterface::toTensor(size_t idx) const { |
| 52 | + return caffe2::Tensor(at(idx).toTensor()); |
| 53 | +} |
| 54 | + |
| 55 | +int IValueInterface::compute_input_size() const { |
| 56 | + if (empty()) { |
| 57 | + return 0; |
| 58 | + } |
| 59 | + if (values_[0].isTensorList()) { |
| 60 | + // if the first input is a tensor list, we get input tensors by indexing |
| 61 | + // into that list. currently, this means that only tensors from that list |
| 62 | + // are accessible as inputs. any hypothetical input tensors that come after |
| 63 | + // the list are not accessible. |
| 64 | + return values_[0].toTensorList().size(); |
| 65 | + } |
| 66 | + // it's not a tensor list. Count the number of tensor inputs and return them. |
| 67 | + int num_tensor_inputs = 0; |
| 68 | + bool found_nontensor = false; |
| 69 | + for (auto i: c10::irange(size())) { |
| 70 | + if (values_[i].isTensor()) { |
| 71 | + TORCH_INTERNAL_ASSERT( |
| 72 | + !found_nontensor, |
| 73 | + "All tensor arguments must come before non-tensor arguments"); |
| 74 | + ++num_tensor_inputs; |
| 75 | + } else { |
| 76 | + found_nontensor = true; |
| 77 | + } |
| 78 | + } |
| 79 | + return num_tensor_inputs; |
| 80 | +} |
| 81 | + |
| 82 | +template <typename T> |
| 83 | +typename c10::detail::ivalue_to_const_ref_overload_return<T>::type |
| 84 | +IValueInterface::to(size_t idx) const { |
| 85 | + return at(idx).template to<T>(); |
| 86 | +} |
| 87 | + |
| 88 | +#define INSTANTIATE_TO(Type) \ |
| 89 | + template typename c10::detail::ivalue_to_const_ref_overload_return<Type>::type \ |
| 90 | + IValueInterface::to<Type>(size_t idx) const |
| 91 | + |
| 92 | +INSTANTIATE_TO(at::Tensor); |
| 93 | +INSTANTIATE_TO(at::Storage); |
| 94 | +INSTANTIATE_TO(c10::Stream); |
| 95 | +INSTANTIATE_TO(float); |
| 96 | +INSTANTIATE_TO(double); |
| 97 | +INSTANTIATE_TO(c10::complex<double>); |
| 98 | +INSTANTIATE_TO(unsigned char); |
| 99 | +INSTANTIATE_TO(signed char); |
| 100 | +INSTANTIATE_TO(unsigned short); |
| 101 | +INSTANTIATE_TO(short); |
| 102 | +INSTANTIATE_TO(int); |
| 103 | +INSTANTIATE_TO(uint32_t); |
| 104 | +INSTANTIATE_TO(uint64_t); |
| 105 | +INSTANTIATE_TO(int64_t); |
| 106 | +INSTANTIATE_TO(bool); |
| 107 | +INSTANTIATE_TO(c10::intrusive_ptr<caffe2::Blob>);; |
| 108 | +INSTANTIATE_TO(c10::intrusive_ptr<ivalue::ConstantString>); |
| 109 | +INSTANTIATE_TO(c10::intrusive_ptr<ivalue::Object>); |
| 110 | +INSTANTIATE_TO(at::Scalar); |
| 111 | +INSTANTIATE_TO(c10::List<int64_t>); |
| 112 | +INSTANTIATE_TO(c10::List<double>); |
| 113 | +INSTANTIATE_TO(c10::List<c10::complex<double>>); |
| 114 | +INSTANTIATE_TO(c10::List<bool>); |
| 115 | +INSTANTIATE_TO(c10::List<at::Tensor>); |
| 116 | +INSTANTIATE_TO(c10::impl::GenericList); |
| 117 | +INSTANTIATE_TO(c10::impl::GenericDict); |
| 118 | +INSTANTIATE_TO(c10::intrusive_ptr<ivalue::Tuple>); |
| 119 | +INSTANTIATE_TO(std::string); |
| 120 | +INSTANTIATE_TO(c10::string_view); |
| 121 | +INSTANTIATE_TO(c10::intrusive_ptr<ivalue::Future>); |
| 122 | +INSTANTIATE_TO(c10::intrusive_ptr<c10::RRefInterface>); |
| 123 | +INSTANTIATE_TO(c10::intrusive_ptr<at::Quantizer>); |
| 124 | +INSTANTIATE_TO(IValue); |
| 125 | +INSTANTIATE_TO(c10::Device); |
| 126 | +INSTANTIATE_TO(at::ScalarType); |
| 127 | +INSTANTIATE_TO(at::Layout); |
| 128 | +INSTANTIATE_TO(at::MemoryFormat); |
| 129 | +INSTANTIATE_TO(at::QScheme); |
| 130 | +INSTANTIATE_TO(at::Dimname); |
| 131 | +INSTANTIATE_TO(at::Generator); |
| 132 | + |
| 133 | +template <typename T> |
| 134 | +struct list_value_type { |
| 135 | + using type = T; |
| 136 | +}; |
| 137 | + |
| 138 | +template <> |
| 139 | +struct list_value_type<int> { |
| 140 | + using type = int64_t; |
| 141 | +}; |
| 142 | + |
| 143 | +template <> |
| 144 | +struct list_value_type<int16_t> { |
| 145 | + using type = int64_t; |
| 146 | +}; |
| 147 | + |
| 148 | +template <> |
| 149 | +struct list_value_type<float> { |
| 150 | + using type = double; |
| 151 | +}; |
| 152 | + |
| 153 | +template <typename T, typename U> |
| 154 | +static std::vector<T> to_vector(const c10::List<U> &list) { |
| 155 | + std::vector<T> ret; |
| 156 | + for (auto i: c10::irange(list.size())) { |
| 157 | + ret.push_back(list.get(i)); |
| 158 | + } |
| 159 | + return ret; |
| 160 | +} |
| 161 | + |
| 162 | +template <typename T> |
| 163 | +std::vector<T> IValueInterface::toVec(size_t idx) const { |
| 164 | + using list_value_t = typename list_value_type<T>::type; |
| 165 | + return to_vector<T>(to<c10::List<list_value_t>>(idx)); |
| 166 | +} |
| 167 | + |
| 168 | +#define INSTANTIATE_TO_VEC(Type) \ |
| 169 | + template std::vector<Type> IValueInterface::toVec<Type>(size_t idx) const; |
| 170 | + |
| 171 | +INSTANTIATE_TO_VEC(int64_t); |
| 172 | +INSTANTIATE_TO_VEC(int32_t); |
| 173 | +INSTANTIATE_TO_VEC(int16_t); |
| 174 | +INSTANTIATE_TO_VEC(float); |
| 175 | +INSTANTIATE_TO_VEC(double); |
| 176 | +INSTANTIATE_TO_VEC(bool); |
| 177 | + |
| 178 | +}} // namespace caffe2::detail |
0 commit comments