|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 12 | +#include <executorch/runtime/core/result.h> |
| 13 | + |
| 14 | +namespace executorch { |
| 15 | +namespace extension { |
| 16 | +namespace internal { |
| 17 | + |
| 18 | +/** |
| 19 | + * Base class template storing the underlying data with size and stride helpers. |
| 20 | + * Inherited by TensorAccessor<> which requires specialization on rank. |
| 21 | + */ |
| 22 | +template <typename T, ssize_t N> |
| 23 | +class TensorAccessorBase { |
| 24 | + public: |
| 25 | + /// Returns the size of the underlying tensor at the given dimension. |
| 26 | + executorch::aten::SizesType size(ssize_t i) const { |
| 27 | + ET_CHECK_MSG( |
| 28 | + i < dim_ && i >= 0, |
| 29 | + "Dimension outside of [0, %zd], got %zd", |
| 30 | + dim_ - 1, |
| 31 | + i); |
| 32 | + return sizes_[i]; |
| 33 | + } |
| 34 | + |
| 35 | + /// Returns the stride of the underlying tensor at the given dimension. |
| 36 | + executorch::aten::StridesType stride(ssize_t i) const { |
| 37 | + ET_CHECK_MSG( |
| 38 | + i < dim_ && i >= 0, |
| 39 | + "Dimension outside of [0, %zd], got %zd", |
| 40 | + dim_ - 1, |
| 41 | + i); |
| 42 | + return strides_[i]; |
| 43 | + } |
| 44 | + |
| 45 | + protected: |
| 46 | + TensorAccessorBase( |
| 47 | + T* data, |
| 48 | + const executorch::aten::SizesType* sizes, |
| 49 | + const executorch::aten::StridesType* strides, |
| 50 | + ssize_t dim) |
| 51 | + : data_(data), sizes_(sizes), strides_(strides), dim_(dim) {} |
| 52 | + |
| 53 | + T* data_; |
| 54 | + const executorch::aten::SizesType* sizes_; |
| 55 | + const executorch::aten::StridesType* strides_; |
| 56 | + ssize_t dim_; |
| 57 | +}; |
| 58 | + |
| 59 | +} // namespace internal |
| 60 | + |
| 61 | +/** |
| 62 | + * TensorAccessor template with data type and rank as template parameters. No |
| 63 | + * public constructors, can only be created using make_tensor_accessor from a |
| 64 | + * given executorch::aten::Tensor. Use operator[] to index and obtain a lower |
| 65 | + * rank accessor or the underlying scalar value. |
| 66 | + */ |
| 67 | +template <typename T, ssize_t N> |
| 68 | +class TensorAccessor : public internal::TensorAccessorBase<T, N> { |
| 69 | + public: |
| 70 | + /** |
| 71 | + * Index into the the outer most dimension. |
| 72 | + * |
| 73 | + * @param i Index. |
| 74 | + * @return If N > 1, a TensorAccessor with N-1 dimensions. If N == 1, a |
| 75 | + * reference to the underlying scalar. Refer to the TensorAccessor<T, 1> |
| 76 | + * specialization. |
| 77 | + */ |
| 78 | + TensorAccessor<T, N - 1> operator[](ssize_t i) { |
| 79 | + return TensorAccessor<T, N - 1>( |
| 80 | + this->data_ + this->strides_[0] * i, |
| 81 | + this->sizes_ + 1, |
| 82 | + this->strides_ + 1, |
| 83 | + N - 1); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Index into the the outer most dimension. |
| 88 | + * |
| 89 | + * @param i Index. |
| 90 | + * @return If N > 1, a constant TensorAccessor with N-1 dimensions. If N == 1, |
| 91 | + * a constant reference to the underlying scalar. Refer to the |
| 92 | + * TensorAccessor<T, 1> specialization. |
| 93 | + */ |
| 94 | + const TensorAccessor<T, N - 1> operator[](ssize_t i) const { |
| 95 | + return TensorAccessor<T, N - 1>( |
| 96 | + this->data_ + this->strides_[0] * i, |
| 97 | + this->sizes_ + 1, |
| 98 | + this->strides_ + 1, |
| 99 | + N - 1); |
| 100 | + } |
| 101 | + |
| 102 | + private: |
| 103 | + TensorAccessor( |
| 104 | + T* data, |
| 105 | + const executorch::aten::SizesType* sizes, |
| 106 | + const executorch::aten::StridesType* strides, |
| 107 | + ssize_t dim) |
| 108 | + : internal::TensorAccessorBase<T, N>(data, sizes, strides, dim) {} |
| 109 | + |
| 110 | + template <typename T2, ssize_t N2> |
| 111 | + friend class TensorAccessor; |
| 112 | + |
| 113 | + template <typename T2, ssize_t N2> |
| 114 | + friend executorch::runtime::Result<TensorAccessor<T2, N2>> |
| 115 | + make_tensor_accessor(const executorch::aten::Tensor& t); |
| 116 | +}; |
| 117 | + |
| 118 | +/** |
| 119 | + * TensorAccessor specialization for N == 1, where operator[] returns a |
| 120 | + * reference to the underlying scalar. |
| 121 | + */ |
| 122 | +template <typename T> |
| 123 | +class TensorAccessor<T, 1> : public internal::TensorAccessorBase<T, 1> { |
| 124 | + public: |
| 125 | + /** |
| 126 | + * Index into the the outer most dimension. |
| 127 | + * |
| 128 | + * @param i Index. |
| 129 | + * @return Reference to the underlying scalar. |
| 130 | + */ |
| 131 | + T& operator[](ssize_t i) { |
| 132 | + return this->data_[this->strides_[0] * i]; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Index into the the outer most dimension. |
| 137 | + * |
| 138 | + * @param i Index. |
| 139 | + * @return Constant reference to the underlying scalar. |
| 140 | + */ |
| 141 | + const T& operator[](ssize_t i) const { |
| 142 | + return this->data_[this->strides_[0] * i]; |
| 143 | + } |
| 144 | + |
| 145 | + private: |
| 146 | + TensorAccessor( |
| 147 | + T* data, |
| 148 | + const executorch::aten::SizesType* sizes, |
| 149 | + const executorch::aten::StridesType* strides, |
| 150 | + ssize_t dim) |
| 151 | + : internal::TensorAccessorBase<T, 1>(data, sizes, strides, dim) {} |
| 152 | + |
| 153 | + template <typename T2, ssize_t N2> |
| 154 | + friend class TensorAccessor; |
| 155 | + |
| 156 | + template <typename T2, ssize_t N2> |
| 157 | + friend executorch::runtime::Result<TensorAccessor<T2, N2>> |
| 158 | + make_tensor_accessor(const executorch::aten::Tensor& t); |
| 159 | +}; |
| 160 | + |
| 161 | +/** |
| 162 | + * Creates a TensorAccessor<T, N> from the given tensor. The number of dimension |
| 163 | + * N and the data type T's size must match those of the input tensor. For |
| 164 | + * Executorch tensors, non-trivial dimension order is not supported. |
| 165 | + * |
| 166 | + * @param tensor Origin tensor. The TensorImpl inside must outlive the returned |
| 167 | + * TensorAccessor. |
| 168 | + * @return TensorAccessor of the input tensor. |
| 169 | + * @retval Error::InvalidArgument Mismatch on data type or number of dimensions. |
| 170 | + * @retval Error::NotSupported Input tensor has non-trivial dimension onrder. |
| 171 | + */ |
| 172 | +template <typename T, ssize_t N> |
| 173 | +executorch::runtime::Result<TensorAccessor<T, N>> make_tensor_accessor( |
| 174 | + const executorch::aten::Tensor& tensor) { |
| 175 | + static_assert( |
| 176 | + N > 0, |
| 177 | + "TensorAccessor is used for indexing tensors, for scalar use *_data_ptr<T>()"); |
| 178 | + |
| 179 | + if (N != tensor.dim()) { |
| 180 | + ET_LOG( |
| 181 | + Error, "Expecting %zd dimensions but tensor has %zd.", N, tensor.dim()); |
| 182 | + return executorch::runtime::Error::InvalidArgument; |
| 183 | + } |
| 184 | + |
| 185 | + if (sizeof(T) != tensor.element_size()) { |
| 186 | + ET_LOG( |
| 187 | + Error, |
| 188 | + "Size of data type template argument (%zd) not equal to tensor element size (%zd)", |
| 189 | + sizeof(T), |
| 190 | + tensor.element_size()); |
| 191 | + return executorch::runtime::Error::InvalidArgument; |
| 192 | + } |
| 193 | + |
| 194 | +#ifndef USE_ATEN_LIB |
| 195 | + auto dim_order = tensor.dim_order(); |
| 196 | + for (ssize_t i = 0; i < dim_order.size(); i++) { |
| 197 | + if (dim_order[i] != i) { |
| 198 | + ET_LOG(Error, "Non-trival dim_order not supported."); |
| 199 | + return executorch::runtime::Error::NotSupported; |
| 200 | + } |
| 201 | + } |
| 202 | +#endif |
| 203 | + |
| 204 | + T* ptr = nullptr; |
| 205 | + if constexpr (std::is_const_v<T>) { |
| 206 | + ptr = tensor.const_data_ptr<T>(); |
| 207 | + } else { |
| 208 | + ptr = tensor.mutable_data_ptr<T>(); |
| 209 | + } |
| 210 | + return TensorAccessor<T, N>( |
| 211 | + ptr, tensor.sizes().data(), tensor.strides().data(), N); |
| 212 | +} |
| 213 | + |
| 214 | +} // namespace extension |
| 215 | +} // namespace executorch |
0 commit comments