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 | |
| 130 | /** The number of bits for the fractional part of the fixed point numbers. */ |
| 131 | int fixed_point_position() const override; |
| 132 | |
| 133 | /** Constant pointer to the underlying buffer. */ |
| 134 | const T *data() const; |
| 135 | |
| 136 | /** Pointer to the underlying buffer. */ |
| 137 | T *data(); |
| 138 | |
| 139 | /** Read only access to the specified element. |
| 140 | * |
| 141 | * @param[in] coord Coordinates of the desired element. |
| 142 | * |
| 143 | * @return A pointer to the desired element. |
| 144 | */ |
| 145 | const void *operator()(const Coordinates &coord) const override; |
| 146 | |
| 147 | /** Access to the specified element. |
| 148 | * |
| 149 | * @param[in] coord Coordinates of the desired element. |
| 150 | * |
| 151 | * @return A pointer to the desired element. |
| 152 | */ |
| 153 | void *operator()(const Coordinates &coord) override; |
| 154 | |
| 155 | /** Swaps the content of the provided tensors. |
| 156 | * |
| 157 | * @param[in, out] tensor1 Tensor to be swapped. |
| 158 | * @param[in, out] tensor2 Tensor to be swapped. |
| 159 | */ |
| 160 | template <typename U> |
| 161 | friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2); |
| 162 | |
| 163 | private: |
| 164 | Buffer _buffer{ nullptr }; |
| 165 | TensorShape _shape{}; |
| 166 | Format _format{ Format::UNKNOWN }; |
| 167 | DataType _data_type{ DataType::UNKNOWN }; |
| 168 | int _num_channels{ 0 }; |
| 169 | int _fixed_point_position{ 0 }; |
| 170 | }; |
| 171 | |
| 172 | template <typename T> |
| 173 | SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format, int fixed_point_position) |
| 174 | : _buffer(nullptr), |
| 175 | _shape(shape), |
| 176 | _format(format), |
| 177 | _fixed_point_position(fixed_point_position) |
| 178 | { |
| 179 | _buffer = support::cpp14::make_unique<T[]>(num_elements() * num_channels()); |
| 180 | } |
| 181 | |
| 182 | template <typename T> |
| 183 | SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position) |
| 184 | : _buffer(nullptr), |
| 185 | _shape(shape), |
| 186 | _data_type(data_type), |
| 187 | _num_channels(num_channels), |
| 188 | _fixed_point_position(fixed_point_position) |
| 189 | { |
| 190 | _buffer = support::cpp14::make_unique<T[]>(num_elements() * this->num_channels()); |
| 191 | } |
| 192 | |
| 193 | template <typename T> |
| 194 | SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor) |
| 195 | : _buffer(nullptr), |
| 196 | _shape(tensor.shape()), |
| 197 | _format(tensor.format()), |
| 198 | _fixed_point_position(tensor.fixed_point_position()) |
| 199 | { |
| 200 | _buffer = support::cpp14::make_unique<T[]>(tensor.num_elements() * num_channels()); |
| 201 | std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get()); |
| 202 | } |
| 203 | |
| 204 | template <typename T> |
| 205 | SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor) |
| 206 | { |
| 207 | swap(*this, tensor); |
| 208 | |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | template <typename T> |
| 213 | T &SimpleTensor<T>::operator[](size_t offset) |
| 214 | { |
| 215 | return _buffer[offset]; |
| 216 | } |
| 217 | |
| 218 | template <typename T> |
| 219 | const T &SimpleTensor<T>::operator[](size_t offset) const |
| 220 | { |
| 221 | return _buffer[offset]; |
| 222 | } |
| 223 | |
| 224 | template <typename T> |
| 225 | TensorShape SimpleTensor<T>::shape() const |
| 226 | { |
| 227 | return _shape; |
| 228 | } |
| 229 | |
| 230 | template <typename T> |
| 231 | size_t SimpleTensor<T>::element_size() const |
| 232 | { |
| 233 | return num_channels() * element_size_from_data_type(data_type()); |
| 234 | } |
| 235 | |
| 236 | template <typename T> |
| 237 | int SimpleTensor<T>::fixed_point_position() const |
| 238 | { |
| 239 | return _fixed_point_position; |
| 240 | } |
| 241 | |
| 242 | template <typename T> |
| 243 | size_t SimpleTensor<T>::size() const |
| 244 | { |
| 245 | const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>()); |
| 246 | return size * element_size(); |
| 247 | } |
| 248 | |
| 249 | template <typename T> |
| 250 | Format SimpleTensor<T>::format() const |
| 251 | { |
| 252 | return _format; |
| 253 | } |
| 254 | |
| 255 | template <typename T> |
| 256 | DataType SimpleTensor<T>::data_type() const |
| 257 | { |
| 258 | if(_format != Format::UNKNOWN) |
| 259 | { |
| 260 | return data_type_from_format(_format); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | return _data_type; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | template <typename T> |
| 269 | int SimpleTensor<T>::num_channels() const |
| 270 | { |
| 271 | switch(_format) |
| 272 | { |
| 273 | case Format::U8: |
| 274 | case Format::S16: |
| 275 | case Format::U16: |
| 276 | case Format::S32: |
| 277 | case Format::U32: |
| 278 | return 1; |
| 279 | case Format::RGB888: |
| 280 | return 3; |
| 281 | case Format::UNKNOWN: |
| 282 | return _num_channels; |
| 283 | default: |
| 284 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | template <typename T> |
| 289 | int SimpleTensor<T>::num_elements() const |
| 290 | { |
| 291 | return _shape.total_size(); |
| 292 | } |
| 293 | |
| 294 | template <typename T> |
| 295 | const T *SimpleTensor<T>::data() const |
| 296 | { |
| 297 | return _buffer.get(); |
| 298 | } |
| 299 | |
| 300 | template <typename T> |
| 301 | T *SimpleTensor<T>::data() |
| 302 | { |
| 303 | return _buffer.get(); |
| 304 | } |
| 305 | |
| 306 | template <typename T> |
| 307 | const void *SimpleTensor<T>::operator()(const Coordinates &coord) const |
| 308 | { |
| 309 | return _buffer.get() + coord2index(_shape, coord); |
| 310 | } |
| 311 | |
| 312 | template <typename T> |
| 313 | void *SimpleTensor<T>::operator()(const Coordinates &coord) |
| 314 | { |
| 315 | return _buffer.get() + coord2index(_shape, coord); |
| 316 | } |
| 317 | |
| 318 | template <typename U> |
| 319 | void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2) |
| 320 | { |
| 321 | // Use unqualified call to swap to enable ADL. But make std::swap available |
| 322 | // as backup. |
| 323 | using std::swap; |
| 324 | swap(tensor1._shape, tensor2._shape); |
| 325 | swap(tensor1._format, tensor2._format); |
| 326 | swap(tensor1._data_type, tensor2._data_type); |
| 327 | swap(tensor1._num_channels, tensor2._num_channels); |
| 328 | swap(tensor1._buffer, tensor2._buffer); |
| 329 | } |
| 330 | } // namespace test |
| 331 | } // namespace arm_compute |
| 332 | #endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */ |