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