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