Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | { |
| 47 | /** Simple tensor object that stores elements in a consecutive chunk of memory. |
| 48 | * |
| 49 | * It can be created by either loading an image from a file which also |
| 50 | * initialises the content of the tensor or by explcitly specifying the size. |
| 51 | * The latter leaves the content uninitialised. |
| 52 | * |
| 53 | * Furthermore, the class provides methods to convert the tensor's values into |
| 54 | * different image format. |
| 55 | */ |
| 56 | template <typename T> |
| 57 | class SimpleTensor final : public IAccessor |
| 58 | { |
| 59 | public: |
| 60 | /** Create an uninitialised tensor. */ |
| 61 | SimpleTensor() = default; |
| 62 | |
| 63 | /** Create an uninitialised tensor of the given @p shape and @p format. |
| 64 | * |
| 65 | * @param[in] shape Shape of the new raw tensor. |
| 66 | * @param[in] format Format of the new raw tensor. |
| 67 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers |
| 68 | */ |
| 69 | SimpleTensor(TensorShape shape, Format format, int fixed_point_position = 0); |
| 70 | |
| 71 | /** Create an uninitialised tensor of the given @p shape and @p data type. |
| 72 | * |
| 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] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers (default = 0). |
| 77 | */ |
| 78 | SimpleTensor(TensorShape shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0); |
| 79 | |
| 80 | /** Create a deep copy of the given @p tensor. |
| 81 | * |
| 82 | * @param[in] tensor To be copied tensor. |
| 83 | */ |
| 84 | SimpleTensor(const SimpleTensor &tensor); |
| 85 | |
| 86 | /** Create a deep copy of the given @p tensor. |
| 87 | * |
| 88 | * @param[in] tensor To be copied tensor. |
| 89 | */ |
| 90 | SimpleTensor &operator =(SimpleTensor tensor); |
| 91 | SimpleTensor(SimpleTensor &&) = default; |
| 92 | ~SimpleTensor() = default; |
| 93 | |
| 94 | using value_type = T; |
| 95 | using Buffer = std::unique_ptr<value_type[]>; |
| 96 | |
| 97 | /** Return value at @p offset in the buffer. |
| 98 | * |
| 99 | * @param[in] offset Offset within the buffer. |
| 100 | */ |
| 101 | T &operator[](size_t offset); |
| 102 | |
| 103 | /** Return constant value at @p offset in the buffer. |
| 104 | * |
| 105 | * @param[in] offset Offset within the buffer. |
| 106 | */ |
| 107 | const T &operator[](size_t offset) const; |
| 108 | |
| 109 | /** Shape of the tensor. */ |
| 110 | TensorShape shape() const override; |
| 111 | |
| 112 | /** Size of each element in the tensor in bytes. */ |
| 113 | size_t element_size() const override; |
| 114 | |
| 115 | /** Total size of the tensor in bytes. */ |
| 116 | size_t size() const override; |
| 117 | |
| 118 | /** Image format of the tensor. */ |
| 119 | Format format() const override; |
| 120 | |
| 121 | /** Data type of the tensor. */ |
| 122 | DataType data_type() const override; |
| 123 | |
| 124 | /** Number of channels of the tensor. */ |
| 125 | int num_channels() const override; |
| 126 | |
| 127 | /** Number of elements of the tensor. */ |
| 128 | int num_elements() const override; |
| 129 | |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 130 | /** Available padding around the tensor. */ |
| 131 | PaddingSize padding() const override; |
| 132 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 133 | /** The number of bits for the fractional part of the fixed point numbers. */ |
| 134 | int fixed_point_position() const override; |
| 135 | |
| 136 | /** Constant pointer to the underlying buffer. */ |
| 137 | const T *data() const; |
| 138 | |
| 139 | /** Pointer to the underlying buffer. */ |
| 140 | T *data(); |
| 141 | |
| 142 | /** Read only access to the specified element. |
| 143 | * |
| 144 | * @param[in] coord Coordinates of the desired element. |
| 145 | * |
| 146 | * @return A pointer to the desired element. |
| 147 | */ |
| 148 | const void *operator()(const Coordinates &coord) const override; |
| 149 | |
| 150 | /** Access to the specified element. |
| 151 | * |
| 152 | * @param[in] coord Coordinates of the desired element. |
| 153 | * |
| 154 | * @return A pointer to the desired element. |
| 155 | */ |
| 156 | void *operator()(const Coordinates &coord) override; |
| 157 | |
| 158 | /** Swaps the content of the provided tensors. |
| 159 | * |
| 160 | * @param[in, out] tensor1 Tensor to be swapped. |
| 161 | * @param[in, out] tensor2 Tensor to be swapped. |
| 162 | */ |
| 163 | template <typename U> |
| 164 | friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2); |
| 165 | |
| 166 | private: |
| 167 | Buffer _buffer{ nullptr }; |
| 168 | TensorShape _shape{}; |
| 169 | Format _format{ Format::UNKNOWN }; |
| 170 | DataType _data_type{ DataType::UNKNOWN }; |
| 171 | int _num_channels{ 0 }; |
| 172 | int _fixed_point_position{ 0 }; |
| 173 | }; |
| 174 | |
| 175 | template <typename T> |
| 176 | SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format, int fixed_point_position) |
| 177 | : _buffer(nullptr), |
| 178 | _shape(shape), |
| 179 | _format(format), |
| 180 | _fixed_point_position(fixed_point_position) |
| 181 | { |
| 182 | _buffer = support::cpp14::make_unique<T[]>(num_elements() * num_channels()); |
| 183 | } |
| 184 | |
| 185 | template <typename T> |
| 186 | SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position) |
| 187 | : _buffer(nullptr), |
| 188 | _shape(shape), |
| 189 | _data_type(data_type), |
| 190 | _num_channels(num_channels), |
| 191 | _fixed_point_position(fixed_point_position) |
| 192 | { |
| 193 | _buffer = support::cpp14::make_unique<T[]>(num_elements() * this->num_channels()); |
| 194 | } |
| 195 | |
| 196 | template <typename T> |
| 197 | SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor) |
| 198 | : _buffer(nullptr), |
| 199 | _shape(tensor.shape()), |
| 200 | _format(tensor.format()), |
| 201 | _fixed_point_position(tensor.fixed_point_position()) |
| 202 | { |
| 203 | _buffer = support::cpp14::make_unique<T[]>(tensor.num_elements() * num_channels()); |
| 204 | std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get()); |
| 205 | } |
| 206 | |
| 207 | template <typename T> |
| 208 | SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor) |
| 209 | { |
| 210 | swap(*this, tensor); |
| 211 | |
| 212 | return *this; |
| 213 | } |
| 214 | |
| 215 | template <typename T> |
| 216 | T &SimpleTensor<T>::operator[](size_t offset) |
| 217 | { |
| 218 | return _buffer[offset]; |
| 219 | } |
| 220 | |
| 221 | template <typename T> |
| 222 | const T &SimpleTensor<T>::operator[](size_t offset) const |
| 223 | { |
| 224 | return _buffer[offset]; |
| 225 | } |
| 226 | |
| 227 | template <typename T> |
| 228 | TensorShape SimpleTensor<T>::shape() const |
| 229 | { |
| 230 | return _shape; |
| 231 | } |
| 232 | |
| 233 | template <typename T> |
| 234 | size_t SimpleTensor<T>::element_size() const |
| 235 | { |
| 236 | return num_channels() * element_size_from_data_type(data_type()); |
| 237 | } |
| 238 | |
| 239 | template <typename T> |
| 240 | int SimpleTensor<T>::fixed_point_position() const |
| 241 | { |
| 242 | return _fixed_point_position; |
| 243 | } |
| 244 | |
| 245 | template <typename T> |
| 246 | size_t SimpleTensor<T>::size() const |
| 247 | { |
| 248 | const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>()); |
| 249 | return size * element_size(); |
| 250 | } |
| 251 | |
| 252 | template <typename T> |
| 253 | Format SimpleTensor<T>::format() const |
| 254 | { |
| 255 | return _format; |
| 256 | } |
| 257 | |
| 258 | template <typename T> |
| 259 | DataType SimpleTensor<T>::data_type() const |
| 260 | { |
| 261 | if(_format != Format::UNKNOWN) |
| 262 | { |
| 263 | return data_type_from_format(_format); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | return _data_type; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | template <typename T> |
| 272 | int SimpleTensor<T>::num_channels() const |
| 273 | { |
| 274 | switch(_format) |
| 275 | { |
| 276 | case Format::U8: |
| 277 | case Format::S16: |
| 278 | case Format::U16: |
| 279 | case Format::S32: |
| 280 | case Format::U32: |
| 281 | return 1; |
| 282 | case Format::RGB888: |
| 283 | return 3; |
| 284 | case Format::UNKNOWN: |
| 285 | return _num_channels; |
| 286 | default: |
| 287 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | template <typename T> |
| 292 | int SimpleTensor<T>::num_elements() const |
| 293 | { |
| 294 | return _shape.total_size(); |
| 295 | } |
| 296 | |
| 297 | template <typename T> |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 298 | PaddingSize SimpleTensor<T>::padding() const |
| 299 | { |
| 300 | return PaddingSize(0); |
| 301 | } |
| 302 | |
| 303 | template <typename T> |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 304 | const T *SimpleTensor<T>::data() const |
| 305 | { |
| 306 | return _buffer.get(); |
| 307 | } |
| 308 | |
| 309 | template <typename T> |
| 310 | T *SimpleTensor<T>::data() |
| 311 | { |
| 312 | return _buffer.get(); |
| 313 | } |
| 314 | |
| 315 | template <typename T> |
| 316 | const void *SimpleTensor<T>::operator()(const Coordinates &coord) const |
| 317 | { |
| 318 | return _buffer.get() + coord2index(_shape, coord); |
| 319 | } |
| 320 | |
| 321 | template <typename T> |
| 322 | void *SimpleTensor<T>::operator()(const Coordinates &coord) |
| 323 | { |
| 324 | return _buffer.get() + coord2index(_shape, coord); |
| 325 | } |
| 326 | |
| 327 | template <typename U> |
| 328 | void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2) |
| 329 | { |
| 330 | // Use unqualified call to swap to enable ADL. But make std::swap available |
| 331 | // as backup. |
| 332 | using std::swap; |
| 333 | swap(tensor1._shape, tensor2._shape); |
| 334 | swap(tensor1._format, tensor2._format); |
| 335 | swap(tensor1._data_type, tensor2._data_type); |
| 336 | swap(tensor1._num_channels, tensor2._num_channels); |
| 337 | swap(tensor1._buffer, tensor2._buffer); |
| 338 | } |
| 339 | } // namespace test |
| 340 | } // namespace arm_compute |
| 341 | #endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */ |