2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__
25 #define __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__
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"
49 /** Simple tensor object that stores elements in a consecutive chunk of memory.
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.
55 * Furthermore, the class provides methods to convert the tensor's values into
56 * different image format.
59 class SimpleTensor : public IAccessor
62 /** Create an uninitialised tensor. */
63 SimpleTensor() = default;
65 /** Create an uninitialised tensor of the given @p shape and @p format.
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
71 SimpleTensor(TensorShape shape, Format format, int fixed_point_position = 0);
73 /** Create an uninitialised tensor of the given @p shape and @p data type.
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).
80 SimpleTensor(TensorShape shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0);
82 /** Create a deep copy of the given @p tensor.
84 * @param[in] tensor To be copied tensor.
86 SimpleTensor(const SimpleTensor &tensor);
88 /** Create a deep copy of the given @p tensor.
90 * @param[in] tensor To be copied tensor.
92 SimpleTensor &operator =(SimpleTensor tensor);
93 SimpleTensor(SimpleTensor &&) = default;
94 ~SimpleTensor() = default;
97 using Buffer = std::unique_ptr<value_type[]>;
99 friend class RawTensor;
101 /** Return value at @p offset in the buffer.
103 * @param[in] offset Offset within the buffer.
105 T &operator[](size_t offset);
107 /** Return constant value at @p offset in the buffer.
109 * @param[in] offset Offset within the buffer.
111 const T &operator[](size_t offset) const;
113 /** Shape of the tensor. */
114 TensorShape shape() const override;
116 /** Size of each element in the tensor in bytes. */
117 size_t element_size() const override;
119 /** Total size of the tensor in bytes. */
120 size_t size() const override;
122 /** Image format of the tensor. */
123 Format format() const override;
125 /** Data type of the tensor. */
126 DataType data_type() const override;
128 /** Number of channels of the tensor. */
129 int num_channels() const override;
131 /** Number of elements of the tensor. */
132 int num_elements() const override;
134 /** Available padding around the tensor. */
135 PaddingSize padding() const override;
137 /** The number of bits for the fractional part of the fixed point numbers. */
138 int fixed_point_position() const override;
140 /** Constant pointer to the underlying buffer. */
141 const T *data() const;
143 /** Pointer to the underlying buffer. */
146 /** Read only access to the specified element.
148 * @param[in] coord Coordinates of the desired element.
150 * @return A pointer to the desired element.
152 const void *operator()(const Coordinates &coord) const override;
154 /** Access to the specified element.
156 * @param[in] coord Coordinates of the desired element.
158 * @return A pointer to the desired element.
160 void *operator()(const Coordinates &coord) override;
162 /** Swaps the content of the provided tensors.
164 * @param[in, out] tensor1 Tensor to be swapped.
165 * @param[in, out] tensor2 Tensor to be swapped.
167 template <typename U>
168 friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2);
171 Buffer _buffer{ nullptr };
172 TensorShape _shape{};
173 Format _format{ Format::UNKNOWN };
174 DataType _data_type{ DataType::UNKNOWN };
175 int _num_channels{ 0 };
176 int _fixed_point_position{ 0 };
179 template <typename T>
180 SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format, int fixed_point_position)
184 _fixed_point_position(fixed_point_position)
186 _buffer = support::cpp14::make_unique<T[]>(num_elements() * num_channels());
189 template <typename T>
190 SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position)
193 _data_type(data_type),
194 _num_channels(num_channels),
195 _fixed_point_position(fixed_point_position)
197 _buffer = support::cpp14::make_unique<T[]>(num_elements() * this->num_channels());
200 template <typename T>
201 SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
203 _shape(tensor.shape()),
204 _format(tensor.format()),
205 _data_type(tensor.data_type()),
206 _num_channels(tensor.num_channels()),
207 _fixed_point_position(tensor.fixed_point_position())
209 _buffer = support::cpp14::make_unique<T[]>(tensor.num_elements() * num_channels());
210 std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get());
213 template <typename T>
214 SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
221 template <typename T>
222 T &SimpleTensor<T>::operator[](size_t offset)
224 return _buffer[offset];
227 template <typename T>
228 const T &SimpleTensor<T>::operator[](size_t offset) const
230 return _buffer[offset];
233 template <typename T>
234 TensorShape SimpleTensor<T>::shape() const
239 template <typename T>
240 size_t SimpleTensor<T>::element_size() const
242 return num_channels() * element_size_from_data_type(data_type());
245 template <typename T>
246 int SimpleTensor<T>::fixed_point_position() const
248 return _fixed_point_position;
251 template <typename T>
252 size_t SimpleTensor<T>::size() const
254 const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
255 return size * element_size();
258 template <typename T>
259 Format SimpleTensor<T>::format() const
264 template <typename T>
265 DataType SimpleTensor<T>::data_type() const
267 if(_format != Format::UNKNOWN)
269 return data_type_from_format(_format);
277 template <typename T>
278 int SimpleTensor<T>::num_channels() const
291 case Format::UNKNOWN:
292 return _num_channels;
294 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
298 template <typename T>
299 int SimpleTensor<T>::num_elements() const
301 return _shape.total_size();
304 template <typename T>
305 PaddingSize SimpleTensor<T>::padding() const
307 return PaddingSize(0);
310 template <typename T>
311 const T *SimpleTensor<T>::data() const
313 return _buffer.get();
316 template <typename T>
317 T *SimpleTensor<T>::data()
319 return _buffer.get();
322 template <typename T>
323 const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
325 return _buffer.get() + coord2index(_shape, coord);
328 template <typename T>
329 void *SimpleTensor<T>::operator()(const Coordinates &coord)
331 return _buffer.get() + coord2index(_shape, coord);
334 template <typename U>
335 void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2)
337 // Use unqualified call to swap to enable ADL. But make std::swap available
340 swap(tensor1._shape, tensor2._shape);
341 swap(tensor1._format, tensor2._format);
342 swap(tensor1._data_type, tensor2._data_type);
343 swap(tensor1._num_channels, tensor2._num_channels);
344 swap(tensor1._buffer, tensor2._buffer);
347 } // namespace arm_compute
348 #endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */