Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_SIMPLE_TENSOR_H |
| 25 | #define ARM_COMPUTE_TEST_SIMPLE_TENSOR_H |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/Utils.h" |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 30 | #include "tests/IAccessor.h" |
| 31 | #include "tests/Utils.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <array> |
| 35 | #include <cstddef> |
| 36 | #include <cstdint> |
| 37 | #include <functional> |
| 38 | #include <memory> |
| 39 | #include <stdexcept> |
| 40 | #include <utility> |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace test |
| 45 | { |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 46 | class RawTensor; |
| 47 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 48 | /** Simple tensor object that stores elements in a consecutive chunk of memory. |
| 49 | * |
| 50 | * It can be created by either loading an image from a file which also |
| 51 | * initialises the content of the tensor or by explcitly specifying the size. |
| 52 | * The latter leaves the content uninitialised. |
| 53 | * |
| 54 | * Furthermore, the class provides methods to convert the tensor's values into |
| 55 | * different image format. |
| 56 | */ |
| 57 | template <typename T> |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 58 | class SimpleTensor : public IAccessor |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 59 | { |
| 60 | public: |
| 61 | /** Create an uninitialised tensor. */ |
| 62 | SimpleTensor() = default; |
| 63 | |
| 64 | /** Create an uninitialised tensor of the given @p shape and @p format. |
| 65 | * |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 66 | * @param[in] shape Shape of the new raw tensor. |
| 67 | * @param[in] format Format of the new raw tensor. |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 68 | */ |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 69 | SimpleTensor(TensorShape shape, Format format); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 70 | |
| 71 | /** Create an uninitialised tensor of the given @p shape and @p data type. |
| 72 | * |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 73 | * @param[in] shape Shape of the new raw tensor. |
| 74 | * @param[in] data_type Data type of the new raw tensor. |
| 75 | * @param[in] num_channels (Optional) Number of channels (default = 1). |
| 76 | * @param[in] quantization_info (Optional) Quantization info for asymmetric quantization (default = empty). |
| 77 | * @param[in] data_layout (Optional) Data layout of the tensor (default = NCHW). |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 78 | */ |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 79 | SimpleTensor(TensorShape shape, DataType data_type, |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 80 | int num_channels = 1, |
| 81 | QuantizationInfo quantization_info = QuantizationInfo(), |
| 82 | DataLayout data_layout = DataLayout::NCHW); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 83 | |
| 84 | /** Create a deep copy of the given @p tensor. |
| 85 | * |
| 86 | * @param[in] tensor To be copied tensor. |
| 87 | */ |
| 88 | SimpleTensor(const SimpleTensor &tensor); |
| 89 | |
| 90 | /** Create a deep copy of the given @p tensor. |
| 91 | * |
| 92 | * @param[in] tensor To be copied tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 93 | * |
| 94 | * @return a copy of the given tensor. |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 95 | */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 96 | SimpleTensor &operator=(SimpleTensor tensor); |
| 97 | /** Allow instances of this class to be move constructed */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 98 | SimpleTensor(SimpleTensor &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 99 | /** Default destructor. */ |
| 100 | ~SimpleTensor() = default; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 101 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 102 | /** Tensor value type */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 103 | using value_type = T; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 104 | /** Tensor buffer pointer type */ |
| 105 | using Buffer = std::unique_ptr<value_type[]>; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 106 | |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 107 | friend class RawTensor; |
| 108 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 109 | /** Return value at @p offset in the buffer. |
| 110 | * |
| 111 | * @param[in] offset Offset within the buffer. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 112 | * |
| 113 | * @return value in the buffer. |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 114 | */ |
| 115 | T &operator[](size_t offset); |
| 116 | |
| 117 | /** Return constant value at @p offset in the buffer. |
| 118 | * |
| 119 | * @param[in] offset Offset within the buffer. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 120 | * |
| 121 | * @return constant value in the buffer. |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 122 | */ |
| 123 | const T &operator[](size_t offset) const; |
| 124 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 125 | /** Shape of the tensor. |
| 126 | * |
| 127 | * @return the shape of the tensor. |
| 128 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 129 | TensorShape shape() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 130 | /** Size of each element in the tensor in bytes. |
| 131 | * |
| 132 | * @return the size of each element in the tensor in bytes. |
| 133 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 134 | size_t element_size() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 135 | /** Total size of the tensor in bytes. |
| 136 | * |
| 137 | * @return the total size of the tensor in bytes. |
| 138 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 139 | size_t size() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 140 | /** Image format of the tensor. |
| 141 | * |
| 142 | * @return the format of the tensor. |
| 143 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 144 | Format format() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 145 | /** Data layout of the tensor. |
| 146 | * |
| 147 | * @return the data layout of the tensor. |
| 148 | */ |
| 149 | DataLayout data_layout() const override; |
| 150 | /** Data type of the tensor. |
| 151 | * |
| 152 | * @return the data type of the tensor. |
| 153 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 154 | DataType data_type() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 155 | /** Number of channels of the tensor. |
| 156 | * |
| 157 | * @return the number of channels of the tensor. |
| 158 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 159 | int num_channels() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 160 | /** Number of elements of the tensor. |
| 161 | * |
| 162 | * @return the number of elements of the tensor. |
| 163 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 164 | int num_elements() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 165 | /** Available padding around the tensor. |
| 166 | * |
| 167 | * @return the available padding around the tensor. |
| 168 | */ |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 169 | PaddingSize padding() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 170 | /** Quantization info in case of asymmetric quantized type |
| 171 | * |
| 172 | * @return |
| 173 | */ |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 174 | QuantizationInfo quantization_info() const override; |
| 175 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 176 | /** Constant pointer to the underlying buffer. |
| 177 | * |
| 178 | * @return a constant pointer to the data. |
| 179 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 180 | const T *data() const; |
| 181 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 182 | /** Pointer to the underlying buffer. |
| 183 | * |
| 184 | * @return a pointer to the data. |
| 185 | */ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 186 | T *data(); |
| 187 | |
| 188 | /** Read only access to the specified element. |
| 189 | * |
| 190 | * @param[in] coord Coordinates of the desired element. |
| 191 | * |
| 192 | * @return A pointer to the desired element. |
| 193 | */ |
| 194 | const void *operator()(const Coordinates &coord) const override; |
| 195 | |
| 196 | /** Access to the specified element. |
| 197 | * |
| 198 | * @param[in] coord Coordinates of the desired element. |
| 199 | * |
| 200 | * @return A pointer to the desired element. |
| 201 | */ |
| 202 | void *operator()(const Coordinates &coord) override; |
| 203 | |
| 204 | /** Swaps the content of the provided tensors. |
| 205 | * |
| 206 | * @param[in, out] tensor1 Tensor to be swapped. |
| 207 | * @param[in, out] tensor2 Tensor to be swapped. |
| 208 | */ |
| 209 | template <typename U> |
| 210 | friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2); |
| 211 | |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 212 | protected: |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 213 | Buffer _buffer{ nullptr }; |
| 214 | TensorShape _shape{}; |
| 215 | Format _format{ Format::UNKNOWN }; |
| 216 | DataType _data_type{ DataType::UNKNOWN }; |
| 217 | int _num_channels{ 0 }; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 218 | QuantizationInfo _quantization_info{}; |
Michele Di Giorgio | 4a65b98 | 2018-03-02 11:21:38 +0000 | [diff] [blame] | 219 | DataLayout _data_layout{ DataLayout::UNKNOWN }; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
Vidhya Sudhan Loganathan | a25d16c | 2018-11-16 11:33:12 +0000 | [diff] [blame] | 222 | template <typename T1, typename T2> |
| 223 | SimpleTensor<T1> copy_tensor(const SimpleTensor<T2> &tensor) |
| 224 | { |
| 225 | SimpleTensor<T1> st(tensor.shape(), tensor.data_type(), |
| 226 | tensor.num_channels(), |
| 227 | tensor.quantization_info(), |
| 228 | tensor.data_layout()); |
| 229 | for(size_t n = 0; n < size_t(st.num_elements()); n++) |
| 230 | { |
| 231 | st.data()[n] = static_cast<T1>(tensor.data()[n]); |
| 232 | } |
| 233 | return st; |
| 234 | } |
| 235 | |
| 236 | template <typename T1, typename T2, typename std::enable_if<std::is_same<T1, T2>::value, int>::type = 0> |
| 237 | SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor) |
| 238 | { |
| 239 | SimpleTensor<T1> st(tensor.shape(), tensor.data_type(), |
| 240 | tensor.num_channels(), |
| 241 | tensor.quantization_info(), |
| 242 | tensor.data_layout()); |
| 243 | memcpy((void *)st.data(), (const void *)tensor.data(), size_t(st.num_elements() * sizeof(T1))); |
| 244 | return st; |
| 245 | } |
| 246 | |
| 247 | template < typename T1, typename T2, typename std::enable_if < (std::is_same<T1, half>::value || std::is_same<T2, half>::value), int >::type = 0 > |
| 248 | SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor) |
| 249 | { |
| 250 | SimpleTensor<T1> st(tensor.shape(), tensor.data_type(), |
| 251 | tensor.num_channels(), |
| 252 | tensor.quantization_info(), |
| 253 | tensor.data_layout()); |
| 254 | for(size_t n = 0; n < size_t(st.num_elements()); n++) |
| 255 | { |
| 256 | st.data()[n] = half_float::detail::half_cast<T1, T2>(tensor.data()[n]); |
| 257 | } |
| 258 | return st; |
| 259 | } |
| 260 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 261 | template <typename T> |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 262 | SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 263 | : _buffer(nullptr), |
| 264 | _shape(shape), |
| 265 | _format(format), |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 266 | _quantization_info(), |
| 267 | _data_layout(DataLayout::NCHW) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 268 | { |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 269 | _num_channels = num_channels(); |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 270 | _buffer = std::make_unique<T[]>(num_elements() * _num_channels); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | template <typename T> |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 274 | SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, QuantizationInfo quantization_info, DataLayout data_layout) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 275 | : _buffer(nullptr), |
| 276 | _shape(shape), |
| 277 | _data_type(data_type), |
| 278 | _num_channels(num_channels), |
Michele Di Giorgio | 4a65b98 | 2018-03-02 11:21:38 +0000 | [diff] [blame] | 279 | _quantization_info(quantization_info), |
| 280 | _data_layout(data_layout) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 281 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 282 | _buffer = std::make_unique<T[]>(this->_shape.total_size() * _num_channels); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | template <typename T> |
| 286 | SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor) |
| 287 | : _buffer(nullptr), |
| 288 | _shape(tensor.shape()), |
| 289 | _format(tensor.format()), |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 290 | _data_type(tensor.data_type()), |
| 291 | _num_channels(tensor.num_channels()), |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 292 | _quantization_info(tensor.quantization_info()), |
| 293 | _data_layout(tensor.data_layout()) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 294 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 295 | _buffer = std::make_unique<T[]>(tensor.num_elements() * _num_channels); |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 296 | std::copy_n(tensor.data(), this->_shape.total_size() * _num_channels, _buffer.get()); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | template <typename T> |
| 300 | SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor) |
| 301 | { |
| 302 | swap(*this, tensor); |
| 303 | |
| 304 | return *this; |
| 305 | } |
| 306 | |
| 307 | template <typename T> |
| 308 | T &SimpleTensor<T>::operator[](size_t offset) |
| 309 | { |
| 310 | return _buffer[offset]; |
| 311 | } |
| 312 | |
| 313 | template <typename T> |
| 314 | const T &SimpleTensor<T>::operator[](size_t offset) const |
| 315 | { |
| 316 | return _buffer[offset]; |
| 317 | } |
| 318 | |
| 319 | template <typename T> |
| 320 | TensorShape SimpleTensor<T>::shape() const |
| 321 | { |
| 322 | return _shape; |
| 323 | } |
| 324 | |
| 325 | template <typename T> |
| 326 | size_t SimpleTensor<T>::element_size() const |
| 327 | { |
| 328 | return num_channels() * element_size_from_data_type(data_type()); |
| 329 | } |
| 330 | |
| 331 | template <typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 332 | QuantizationInfo SimpleTensor<T>::quantization_info() const |
| 333 | { |
| 334 | return _quantization_info; |
| 335 | } |
| 336 | |
| 337 | template <typename T> |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 338 | size_t SimpleTensor<T>::size() const |
| 339 | { |
| 340 | const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>()); |
| 341 | return size * element_size(); |
| 342 | } |
| 343 | |
| 344 | template <typename T> |
| 345 | Format SimpleTensor<T>::format() const |
| 346 | { |
| 347 | return _format; |
| 348 | } |
| 349 | |
| 350 | template <typename T> |
Michele Di Giorgio | 4a65b98 | 2018-03-02 11:21:38 +0000 | [diff] [blame] | 351 | DataLayout SimpleTensor<T>::data_layout() const |
| 352 | { |
| 353 | return _data_layout; |
| 354 | } |
| 355 | |
| 356 | template <typename T> |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 357 | DataType SimpleTensor<T>::data_type() const |
| 358 | { |
| 359 | if(_format != Format::UNKNOWN) |
| 360 | { |
| 361 | return data_type_from_format(_format); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | return _data_type; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | template <typename T> |
| 370 | int SimpleTensor<T>::num_channels() const |
| 371 | { |
| 372 | switch(_format) |
| 373 | { |
| 374 | case Format::U8: |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 375 | case Format::U16: |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 376 | case Format::S16: |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 377 | case Format::U32: |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 378 | case Format::S32: |
| 379 | case Format::F16: |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 380 | case Format::F32: |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 381 | return 1; |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 382 | // Because the U and V channels are subsampled |
| 383 | // these formats appear like having only 2 channels: |
| 384 | case Format::YUYV422: |
| 385 | case Format::UYVY422: |
| 386 | return 2; |
| 387 | case Format::UV88: |
| 388 | return 2; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 389 | case Format::RGB888: |
| 390 | return 3; |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 391 | case Format::RGBA8888: |
| 392 | return 4; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 393 | case Format::UNKNOWN: |
| 394 | return _num_channels; |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 395 | //Doesn't make sense for planar formats: |
| 396 | case Format::NV12: |
| 397 | case Format::NV21: |
| 398 | case Format::IYUV: |
| 399 | case Format::YUV444: |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 400 | default: |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 401 | return 0; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | template <typename T> |
| 406 | int SimpleTensor<T>::num_elements() const |
| 407 | { |
| 408 | return _shape.total_size(); |
| 409 | } |
| 410 | |
| 411 | template <typename T> |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 412 | PaddingSize SimpleTensor<T>::padding() const |
| 413 | { |
| 414 | return PaddingSize(0); |
| 415 | } |
| 416 | |
| 417 | template <typename T> |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 418 | const T *SimpleTensor<T>::data() const |
| 419 | { |
| 420 | return _buffer.get(); |
| 421 | } |
| 422 | |
| 423 | template <typename T> |
| 424 | T *SimpleTensor<T>::data() |
| 425 | { |
| 426 | return _buffer.get(); |
| 427 | } |
| 428 | |
| 429 | template <typename T> |
| 430 | const void *SimpleTensor<T>::operator()(const Coordinates &coord) const |
| 431 | { |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 432 | return _buffer.get() + coord2index(_shape, coord) * _num_channels; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | template <typename T> |
| 436 | void *SimpleTensor<T>::operator()(const Coordinates &coord) |
| 437 | { |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 438 | return _buffer.get() + coord2index(_shape, coord) * _num_channels; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | template <typename U> |
| 442 | void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2) |
| 443 | { |
| 444 | // Use unqualified call to swap to enable ADL. But make std::swap available |
| 445 | // as backup. |
| 446 | using std::swap; |
| 447 | swap(tensor1._shape, tensor2._shape); |
| 448 | swap(tensor1._format, tensor2._format); |
| 449 | swap(tensor1._data_type, tensor2._data_type); |
| 450 | swap(tensor1._num_channels, tensor2._num_channels); |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 451 | swap(tensor1._quantization_info, tensor2._quantization_info); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 452 | swap(tensor1._buffer, tensor2._buffer); |
| 453 | } |
| 454 | } // namespace test |
| 455 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 456 | #endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_H */ |