blob: 4bbe4c56f612673c4fcf4c690a2c56c4a12c5a8f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ioan-Cristian Szabo91d20d92017-10-27 17:35:40 +01002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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_TENSOR_LIBRARY_H__
25#define __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__
26
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/TensorShape.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Window.h"
SiCong Li86b53332017-08-23 11:02:43 +010034#include "libnpy/npy.hpp"
Moritz Pflanzere49e2662017-07-21 15:55:28 +010035#include "tests/RawTensor.h"
36#include "tests/TensorCache.h"
37#include "tests/Utils.h"
Anthony Barbierf6705ec2017-09-28 12:01:10 +010038#include "tests/framework/Exceptions.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <algorithm>
41#include <cstddef>
42#include <fstream>
43#include <random>
44#include <string>
45#include <type_traits>
SiCong Li86b53332017-08-23 11:02:43 +010046#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48namespace arm_compute
49{
50namespace test
51{
52/** Factory class to create and fill tensors.
53 *
54 * Allows to initialise tensors from loaded images or by specifying the shape
55 * explicitly. Furthermore, provides methods to fill tensors with the content of
56 * loaded images or with random values.
57 */
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010058class AssetsLibrary final
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059{
60public:
John Richardson70f946b2017-10-02 16:52:16 +010061 /** Initialises the library with a @p path to the assets directory.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 * Furthermore, sets the seed for the random generator to @p seed.
63 *
John Richardson70f946b2017-10-02 16:52:16 +010064 * @param[in] path Path to load assets from.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 * @param[in] seed Seed used to initialise the random number generator.
66 */
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010067 AssetsLibrary(std::string path, std::random_device::result_type seed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068
Alex Gildayc357c472018-03-21 13:54:09 +000069 /** Path to assets directory used to initialise library.
70 *
71 * @return the path to the assets directory.
72 */
John Richardson70f946b2017-10-02 16:52:16 +010073 std::string path() const;
74
Alex Gildayc357c472018-03-21 13:54:09 +000075 /** Seed that is used to fill tensors with random values.
76 *
77 * @return the initial random seed.
78 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 std::random_device::result_type seed() const;
80
Giorgio Arenafda46182017-06-16 13:57:33 +010081 /** Provides a tensor shape for the specified image.
82 *
83 * @param[in] name Image file used to look up the raw tensor.
Alex Gildayc357c472018-03-21 13:54:09 +000084 *
85 * @return the tensor shape for the specified image.
Giorgio Arenafda46182017-06-16 13:57:33 +010086 */
87 TensorShape get_image_shape(const std::string &name);
88
Alex Gildayc357c472018-03-21 13:54:09 +000089 /** Provides a constant raw tensor for the specified image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 *
91 * @param[in] name Image file used to look up the raw tensor.
Alex Gildayc357c472018-03-21 13:54:09 +000092 *
93 * @return a raw tensor for the specified image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 */
95 const RawTensor &get(const std::string &name) const;
96
97 /** Provides a raw tensor for the specified image.
98 *
99 * @param[in] name Image file used to look up the raw tensor.
Alex Gildayc357c472018-03-21 13:54:09 +0000100 *
101 * @return a raw tensor for the specified image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 */
103 RawTensor get(const std::string &name);
104
105 /** Creates an uninitialised raw tensor with the given @p data_type and @p
106 * num_channels. The shape is derived from the specified image.
107 *
108 * @param[in] name Image file used to initialise the tensor.
109 * @param[in] data_type Data type used to initialise the tensor.
110 * @param[in] num_channels Number of channels used to initialise the tensor.
Alex Gildayc357c472018-03-21 13:54:09 +0000111 *
112 * @return a raw tensor for the specified image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 */
114 RawTensor get(const std::string &name, DataType data_type, int num_channels = 1) const;
115
116 /** Provides a contant raw tensor for the specified image after it has been
117 * converted to @p format.
118 *
119 * @param[in] name Image file used to look up the raw tensor.
120 * @param[in] format Format used to look up the raw tensor.
Alex Gildayc357c472018-03-21 13:54:09 +0000121 *
122 * @return a raw tensor for the specified image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 */
124 const RawTensor &get(const std::string &name, Format format) const;
125
126 /** Provides a raw tensor for the specified image after it has been
127 * converted to @p format.
128 *
129 * @param[in] name Image file used to look up the raw tensor.
130 * @param[in] format Format used to look up the raw tensor.
Alex Gildayc357c472018-03-21 13:54:09 +0000131 *
132 * @return a raw tensor for the specified image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 */
134 RawTensor get(const std::string &name, Format format);
135
136 /** Provides a contant raw tensor for the specified channel after it has
137 * been extracted form the given image.
138 *
139 * @param[in] name Image file used to look up the raw tensor.
140 * @param[in] channel Channel used to look up the raw tensor.
141 *
142 * @note The channel has to be unambiguous so that the format can be
143 * inferred automatically.
Alex Gildayc357c472018-03-21 13:54:09 +0000144 *
145 * @return a raw tensor for the specified image channel.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 */
147 const RawTensor &get(const std::string &name, Channel channel) const;
148
149 /** Provides a raw tensor for the specified channel after it has been
150 * extracted form the given image.
151 *
152 * @param[in] name Image file used to look up the raw tensor.
153 * @param[in] channel Channel used to look up the raw tensor.
154 *
155 * @note The channel has to be unambiguous so that the format can be
156 * inferred automatically.
Alex Gildayc357c472018-03-21 13:54:09 +0000157 *
158 * @return a raw tensor for the specified image channel.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 */
160 RawTensor get(const std::string &name, Channel channel);
161
162 /** Provides a constant raw tensor for the specified channel after it has
163 * been extracted form the given image formatted to @p format.
164 *
165 * @param[in] name Image file used to look up the raw tensor.
166 * @param[in] format Format used to look up the raw tensor.
167 * @param[in] channel Channel used to look up the raw tensor.
Alex Gildayc357c472018-03-21 13:54:09 +0000168 *
169 * @return a raw tensor for the specified image channel.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 */
171 const RawTensor &get(const std::string &name, Format format, Channel channel) const;
172
173 /** Provides a raw tensor for the specified channel after it has been
174 * extracted form the given image formatted to @p format.
175 *
176 * @param[in] name Image file used to look up the raw tensor.
177 * @param[in] format Format used to look up the raw tensor.
178 * @param[in] channel Channel used to look up the raw tensor.
Alex Gildayc357c472018-03-21 13:54:09 +0000179 *
180 * @return a raw tensor for the specified image channel.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 */
182 RawTensor get(const std::string &name, Format format, Channel channel);
183
Giorgio Arenaa2611812017-07-21 10:08:48 +0100184 /** Puts garbage values all around the tensor for testing purposes
185 *
186 * @param[in, out] tensor To be filled tensor.
187 * @param[in] distribution Distribution used to fill the tensor's surroundings.
188 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
189 */
190 template <typename T, typename D>
191 void fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const;
192
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 /** Fills the specified @p tensor with random values drawn from @p
194 * distribution.
195 *
196 * @param[in, out] tensor To be filled tensor.
197 * @param[in] distribution Distribution used to fill the tensor.
198 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
199 *
200 * @note The @p distribution has to provide operator(Generator &) which
201 * will be used to draw samples.
202 */
203 template <typename T, typename D>
204 void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const;
205
206 /** Fills the specified @p raw tensor with random values drawn from @p
207 * distribution.
208 *
209 * @param[in, out] raw To be filled raw.
210 * @param[in] distribution Distribution used to fill the tensor.
211 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
212 *
213 * @note The @p distribution has to provide operator(Generator &) which
214 * will be used to draw samples.
215 */
216 template <typename D>
217 void fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const;
218
219 /** Fills the specified @p tensor with the content of the specified image
220 * converted to the given format.
221 *
222 * @param[in, out] tensor To be filled tensor.
223 * @param[in] name Image file used to fill the tensor.
224 * @param[in] format Format of the image used to fill the tensor.
225 *
226 * @warning No check is performed that the specified format actually
227 * matches the format of the tensor.
228 */
229 template <typename T>
230 void fill(T &&tensor, const std::string &name, Format format) const;
231
232 /** Fills the raw tensor with the content of the specified image
233 * converted to the given format.
234 *
235 * @param[in, out] raw To be filled raw tensor.
236 * @param[in] name Image file used to fill the tensor.
237 * @param[in] format Format of the image used to fill the tensor.
238 *
239 * @warning No check is performed that the specified format actually
240 * matches the format of the tensor.
241 */
242 void fill(RawTensor &raw, const std::string &name, Format format) const;
243
244 /** Fills the specified @p tensor with the content of the specified channel
245 * extracted from the given image.
246 *
247 * @param[in, out] tensor To be filled tensor.
248 * @param[in] name Image file used to fill the tensor.
249 * @param[in] channel Channel of the image used to fill the tensor.
250 *
251 * @note The channel has to be unambiguous so that the format can be
252 * inferred automatically.
253 *
254 * @warning No check is performed that the specified format actually
255 * matches the format of the tensor.
256 */
257 template <typename T>
258 void fill(T &&tensor, const std::string &name, Channel channel) const;
259
260 /** Fills the raw tensor with the content of the specified channel
261 * extracted from the given image.
262 *
263 * @param[in, out] raw To be filled raw tensor.
264 * @param[in] name Image file used to fill the tensor.
265 * @param[in] channel Channel of the image used to fill the tensor.
266 *
267 * @note The channel has to be unambiguous so that the format can be
268 * inferred automatically.
269 *
270 * @warning No check is performed that the specified format actually
271 * matches the format of the tensor.
272 */
273 void fill(RawTensor &raw, const std::string &name, Channel channel) const;
274
275 /** Fills the specified @p tensor with the content of the specified channel
276 * extracted from the given image after it has been converted to the given
277 * format.
278 *
279 * @param[in, out] tensor To be filled tensor.
280 * @param[in] name Image file used to fill the tensor.
281 * @param[in] format Format of the image used to fill the tensor.
282 * @param[in] channel Channel of the image used to fill the tensor.
283 *
284 * @warning No check is performed that the specified format actually
285 * matches the format of the tensor.
286 */
287 template <typename T>
288 void fill(T &&tensor, const std::string &name, Format format, Channel channel) const;
289
290 /** Fills the raw tensor with the content of the specified channel
291 * extracted from the given image after it has been converted to the given
292 * format.
293 *
294 * @param[in, out] raw To be filled raw tensor.
295 * @param[in] name Image file used to fill the tensor.
296 * @param[in] format Format of the image used to fill the tensor.
297 * @param[in] channel Channel of the image used to fill the tensor.
298 *
299 * @warning No check is performed that the specified format actually
300 * matches the format of the tensor.
301 */
302 void fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const;
303
Alex Gilday345ab182018-01-09 11:40:19 +0000304 /** Fills the specified @p tensor with the content of the raw tensor.
305 *
306 * @param[in, out] tensor To be filled tensor.
307 * @param[in] raw Raw tensor used to fill the tensor.
308 *
309 * @warning No check is performed that the specified format actually
310 * matches the format of the tensor.
311 */
312 template <typename T>
313 void fill(T &&tensor, RawTensor raw) const;
314
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315 /** Fill a tensor with uniform distribution across the range of its type
316 *
317 * @param[in, out] tensor To be filled tensor.
318 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
319 */
320 template <typename T>
321 void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const;
322
323 /** Fill a tensor with uniform distribution across the a specified range
324 *
325 * @param[in, out] tensor To be filled tensor.
326 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
327 * @param[in] low lowest value in the range (inclusive)
328 * @param[in] high highest value in the range (inclusive)
329 *
330 * @note @p low and @p high must be of the same type as the data type of @p tensor
331 */
332 template <typename T, typename D>
333 void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const;
334
SiCong Li86b53332017-08-23 11:02:43 +0100335 /** Fills the specified @p tensor with data loaded from .npy (numpy binary) in specified path.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336 *
337 * @param[in, out] tensor To be filled tensor.
338 * @param[in] name Data file.
SiCong Li86b53332017-08-23 11:02:43 +0100339 *
340 * @note The numpy array stored in the binary .npy file must be row-major in the sense that it
341 * must store elements within a row consecutively in the memory, then rows within a 2D slice,
342 * then 2D slices within a 3D slice and so on. Note that it imposes no restrictions on what
343 * indexing convention is used in the numpy array. That is, the numpy array can be either fortran
344 * style or C style as long as it adheres to the rule above.
345 *
346 * More concretely, the orders of dimensions for each style are as follows:
347 * C-style (numpy default):
348 * array[HigherDims..., Z, Y, X]
349 * Fortran style:
350 * array[X, Y, Z, HigherDims...]
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351 */
352 template <typename T>
353 void fill_layer_data(T &&tensor, std::string name) const;
354
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000355 /** Fill a tensor with a constant value
356 *
357 * @param[in, out] tensor To be filled tensor.
358 * @param[in] value Value to be assigned to all elements of the input tensor.
359 *
360 * @note @p value must be of the same type as the data type of @p tensor
361 */
362 template <typename T, typename D>
363 void fill_tensor_value(T &&tensor, D value) const;
364
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365private:
366 // Function prototype to convert between image formats.
367 using Converter = void (*)(const RawTensor &src, RawTensor &dst);
368 // Function prototype to extract a channel from an image.
369 using Extractor = void (*)(const RawTensor &src, RawTensor &dst);
370 // Function prototype to load an image file.
371 using Loader = RawTensor (*)(const std::string &path);
372
373 const Converter &get_converter(Format src, Format dst) const;
374 const Converter &get_converter(DataType src, Format dst) const;
375 const Converter &get_converter(Format src, DataType dst) const;
376 const Converter &get_converter(DataType src, DataType dst) const;
377 const Extractor &get_extractor(Format format, Channel) const;
378 const Loader &get_loader(const std::string &extension) const;
379
380 /** Creates a raw tensor from the specified image.
381 *
382 * @param[in] name To be loaded image file.
383 *
384 * @note If use_single_image is true @p name is ignored and the user image
385 * is loaded instead.
386 */
387 RawTensor load_image(const std::string &name) const;
388
389 /** Provides a raw tensor for the specified image and format.
390 *
391 * @param[in] name Image file used to look up the raw tensor.
392 * @param[in] format Format used to look up the raw tensor.
393 *
394 * If the tensor has already been requested before the cached version will
395 * be returned. Otherwise the tensor will be added to the cache.
396 *
397 * @note If use_single_image is true @p name is ignored and the user image
398 * is loaded instead.
399 */
400 const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format) const;
401
402 /** Provides a raw tensor for the specified image, format and channel.
403 *
404 * @param[in] name Image file used to look up the raw tensor.
405 * @param[in] format Format used to look up the raw tensor.
406 * @param[in] channel Channel used to look up the raw tensor.
407 *
408 * If the tensor has already been requested before the cached version will
409 * be returned. Otherwise the tensor will be added to the cache.
410 *
411 * @note If use_single_image is true @p name is ignored and the user image
412 * is loaded instead.
413 */
414 const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const;
415
416 mutable TensorCache _cache{};
417 mutable std::mutex _format_lock{};
418 mutable std::mutex _channel_lock{};
Anthony Barbierac69aa12017-07-03 17:39:37 +0100419 const std::string _library_path;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100420 std::random_device::result_type _seed;
421};
422
423template <typename T, typename D>
Giorgio Arenaa2611812017-07-21 10:08:48 +0100424void AssetsLibrary::fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
425{
426 const PaddingSize padding_size = tensor.padding();
427
428 Window window;
429 window.set(0, Window::Dimension(-padding_size.left, tensor.shape()[0] + padding_size.right, 1));
Gian Marco5420b282017-11-29 10:41:38 +0000430 if(tensor.shape().num_dimensions() > 1)
431 {
432 window.set(1, Window::Dimension(-padding_size.top, tensor.shape()[1] + padding_size.bottom, 1));
433 }
Giorgio Arenaa2611812017-07-21 10:08:48 +0100434
435 std::mt19937 gen(_seed);
436
437 execute_window_loop(window, [&](const Coordinates & id)
438 {
439 TensorShape shape = tensor.shape();
440
441 // If outside of valid region
442 if(id.x() < 0 || id.x() >= static_cast<int>(shape.x()) || id.y() < 0 || id.y() >= static_cast<int>(shape.y()))
443 {
444 using ResultType = typename std::remove_reference<D>::type::result_type;
445 const ResultType value = distribution(gen);
446 void *const out_ptr = tensor(id);
447 store_value_with_data_type(out_ptr, value, tensor.data_type());
448 }
449 });
450}
451
452template <typename T, typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100453void AssetsLibrary::fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100454{
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100455 using ResultType = typename std::remove_reference<D>::type::result_type;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100456
457 std::mt19937 gen(_seed + seed_offset);
458
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100459 // Iterate over all elements
460 for(int element_idx = 0; element_idx < tensor.num_elements(); ++element_idx)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100461 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100462 const Coordinates id = index2coord(tensor.shape(), element_idx);
463
464 // Iterate over all channels
465 for(int channel = 0; channel < tensor.num_channels(); ++channel)
466 {
467 const ResultType value = distribution(gen);
468 ResultType &target_value = reinterpret_cast<ResultType *const>(tensor(id))[channel];
469
470 store_value_with_data_type(&target_value, value, tensor.data_type());
471 }
472 }
Giorgio Arenaa2611812017-07-21 10:08:48 +0100473
474 fill_borders_with_garbage(tensor, distribution, seed_offset);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100475}
476
477template <typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100478void AssetsLibrary::fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100479{
480 std::mt19937 gen(_seed + seed_offset);
481
482 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
483 {
484 using ResultType = typename std::remove_reference<D>::type::result_type;
485 const ResultType value = distribution(gen);
486 store_value_with_data_type(raw.data() + offset, value, raw.data_type());
487 }
488}
489
490template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100491void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100492{
493 const RawTensor &raw = get(name, format);
494
495 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
496 {
497 const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
498
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100499 const RawTensor::value_type *const raw_ptr = raw.data() + offset;
500 const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501 std::copy_n(raw_ptr, raw.element_size(), out_ptr);
502 }
503}
504
505template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100506void AssetsLibrary::fill(T &&tensor, const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100507{
508 fill(std::forward<T>(tensor), name, get_format_for_channel(channel), channel);
509}
510
511template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100512void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100513{
514 const RawTensor &raw = get(name, format, channel);
515
516 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
517 {
518 const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
519
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100520 const RawTensor::value_type *const raw_ptr = raw.data() + offset;
521 const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100522 std::copy_n(raw_ptr, raw.element_size(), out_ptr);
523 }
524}
525
526template <typename T>
Alex Gilday345ab182018-01-09 11:40:19 +0000527void AssetsLibrary::fill(T &&tensor, RawTensor raw) const
528{
529 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
530 {
531 const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
532
533 const RawTensor::value_type *const raw_ptr = raw.data() + offset;
534 const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
535 std::copy_n(raw_ptr, raw.element_size(), out_ptr);
536 }
537}
538
539template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100540void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100541{
542 switch(tensor.data_type())
543 {
544 case DataType::U8:
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000545 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100546 {
547 std::uniform_int_distribution<uint8_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(), std::numeric_limits<uint8_t>::max());
548 fill(tensor, distribution_u8, seed_offset);
549 break;
550 }
551 case DataType::S8:
552 case DataType::QS8:
553 {
554 std::uniform_int_distribution<int8_t> distribution_s8(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max());
555 fill(tensor, distribution_s8, seed_offset);
556 break;
557 }
558 case DataType::U16:
559 {
560 std::uniform_int_distribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), std::numeric_limits<uint16_t>::max());
561 fill(tensor, distribution_u16, seed_offset);
562 break;
563 }
564 case DataType::S16:
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +0100565 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100566 {
567 std::uniform_int_distribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), std::numeric_limits<int16_t>::max());
568 fill(tensor, distribution_s16, seed_offset);
569 break;
570 }
571 case DataType::U32:
572 {
573 std::uniform_int_distribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max());
574 fill(tensor, distribution_u32, seed_offset);
575 break;
576 }
577 case DataType::S32:
578 {
579 std::uniform_int_distribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max());
580 fill(tensor, distribution_s32, seed_offset);
581 break;
582 }
583 case DataType::U64:
584 {
585 std::uniform_int_distribution<uint64_t> distribution_u64(std::numeric_limits<uint64_t>::lowest(), std::numeric_limits<uint64_t>::max());
586 fill(tensor, distribution_u64, seed_offset);
587 break;
588 }
589 case DataType::S64:
590 {
591 std::uniform_int_distribution<int64_t> distribution_s64(std::numeric_limits<int64_t>::lowest(), std::numeric_limits<int64_t>::max());
592 fill(tensor, distribution_s64, seed_offset);
593 break;
594 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100595 case DataType::F16:
SiCong Li02dfb2c2017-07-27 17:59:20 +0100596 {
597 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
598 std::uniform_real_distribution<float> distribution_f16(-100.f, 100.f);
599 fill(tensor, distribution_f16, seed_offset);
600 break;
601 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100602 case DataType::F32:
603 {
604 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
605 std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f);
606 fill(tensor, distribution_f32, seed_offset);
607 break;
608 }
609 case DataType::F64:
610 {
611 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
612 std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f);
613 fill(tensor, distribution_f64, seed_offset);
614 break;
615 }
616 case DataType::SIZET:
617 {
618 std::uniform_int_distribution<size_t> distribution_sizet(std::numeric_limits<size_t>::lowest(), std::numeric_limits<size_t>::max());
619 fill(tensor, distribution_sizet, seed_offset);
620 break;
621 }
622 default:
623 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
624 }
625}
626
627template <typename T, typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100628void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100629{
630 switch(tensor.data_type())
631 {
632 case DataType::U8:
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000633 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100634 {
635 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint8_t, D>::value));
636 std::uniform_int_distribution<uint8_t> distribution_u8(low, high);
637 fill(tensor, distribution_u8, seed_offset);
638 break;
639 }
640 case DataType::S8:
641 case DataType::QS8:
642 {
643 ARM_COMPUTE_ERROR_ON(!(std::is_same<int8_t, D>::value));
644 std::uniform_int_distribution<int8_t> distribution_s8(low, high);
645 fill(tensor, distribution_s8, seed_offset);
646 break;
647 }
648 case DataType::U16:
649 {
650 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint16_t, D>::value));
651 std::uniform_int_distribution<uint16_t> distribution_u16(low, high);
652 fill(tensor, distribution_u16, seed_offset);
653 break;
654 }
655 case DataType::S16:
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100656 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100657 {
658 ARM_COMPUTE_ERROR_ON(!(std::is_same<int16_t, D>::value));
659 std::uniform_int_distribution<int16_t> distribution_s16(low, high);
660 fill(tensor, distribution_s16, seed_offset);
661 break;
662 }
663 case DataType::U32:
664 {
665 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint32_t, D>::value));
666 std::uniform_int_distribution<uint32_t> distribution_u32(low, high);
667 fill(tensor, distribution_u32, seed_offset);
668 break;
669 }
670 case DataType::S32:
671 {
672 ARM_COMPUTE_ERROR_ON(!(std::is_same<int32_t, D>::value));
673 std::uniform_int_distribution<int32_t> distribution_s32(low, high);
674 fill(tensor, distribution_s32, seed_offset);
675 break;
676 }
677 case DataType::U64:
678 {
679 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint64_t, D>::value));
680 std::uniform_int_distribution<uint64_t> distribution_u64(low, high);
681 fill(tensor, distribution_u64, seed_offset);
682 break;
683 }
684 case DataType::S64:
685 {
686 ARM_COMPUTE_ERROR_ON(!(std::is_same<int64_t, D>::value));
687 std::uniform_int_distribution<int64_t> distribution_s64(low, high);
688 fill(tensor, distribution_s64, seed_offset);
689 break;
690 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100691 case DataType::F16:
692 {
Moritz Pflanzere49e2662017-07-21 15:55:28 +0100693 std::uniform_real_distribution<float> distribution_f16(low, high);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100694 fill(tensor, distribution_f16, seed_offset);
695 break;
696 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100697 case DataType::F32:
698 {
699 ARM_COMPUTE_ERROR_ON(!(std::is_same<float, D>::value));
700 std::uniform_real_distribution<float> distribution_f32(low, high);
701 fill(tensor, distribution_f32, seed_offset);
702 break;
703 }
704 case DataType::F64:
705 {
706 ARM_COMPUTE_ERROR_ON(!(std::is_same<double, D>::value));
707 std::uniform_real_distribution<double> distribution_f64(low, high);
708 fill(tensor, distribution_f64, seed_offset);
709 break;
710 }
711 case DataType::SIZET:
712 {
713 ARM_COMPUTE_ERROR_ON(!(std::is_same<size_t, D>::value));
714 std::uniform_int_distribution<size_t> distribution_sizet(low, high);
715 fill(tensor, distribution_sizet, seed_offset);
716 break;
717 }
718 default:
719 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
720 }
721}
722
723template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100724void AssetsLibrary::fill_layer_data(T &&tensor, std::string name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100725{
726#ifdef _WIN32
727 const std::string path_separator("\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100728#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100729 const std::string path_separator("/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100730#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100731 const std::string path = _library_path + path_separator + name;
732
SiCong Li86b53332017-08-23 11:02:43 +0100733 std::vector<unsigned long> shape;
734
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100735 // Open file
SiCong Li86b53332017-08-23 11:02:43 +0100736 std::ifstream stream(path, std::ios::in | std::ios::binary);
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100737 if(!stream.good())
738 {
739 throw framework::FileNotFound("Could not load npy file: " + path);
740 }
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000741 std::string header = npy::read_header(stream);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100742
SiCong Li86b53332017-08-23 11:02:43 +0100743 // Parse header
744 bool fortran_order = false;
745 std::string typestr;
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000746 npy::parse_header(header, typestr, fortran_order, shape);
SiCong Li86b53332017-08-23 11:02:43 +0100747
748 // Check if the typestring matches the given one
749 std::string expect_typestr = get_typestring(tensor.data_type());
750 ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
751
752 // Validate tensor shape
753 ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch");
754 if(fortran_order)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100755 {
SiCong Li86b53332017-08-23 11:02:43 +0100756 for(size_t i = 0; i < shape.size(); ++i)
757 {
758 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[i], "Tensor dimensions mismatch");
759 }
760 }
761 else
762 {
763 for(size_t i = 0; i < shape.size(); ++i)
764 {
765 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
766 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100767 }
768
SiCong Li86b53332017-08-23 11:02:43 +0100769 // Read data
770 if(tensor.padding().empty())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100771 {
SiCong Li86b53332017-08-23 11:02:43 +0100772 // If tensor has no padding read directly from stream.
773 stream.read(reinterpret_cast<char *>(tensor.data()), tensor.size());
774 }
775 else
776 {
777 // If tensor has padding accessing tensor elements through execution window.
778 Window window;
779 window.use_tensor_dimensions(tensor.shape());
780
SiCong Li86b53332017-08-23 11:02:43 +0100781 execute_window_loop(window, [&](const Coordinates & id)
782 {
783 stream.read(reinterpret_cast<char *>(tensor(id)), tensor.element_size());
784 });
785 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100786}
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000787
788template <typename T, typename D>
789void AssetsLibrary::fill_tensor_value(T &&tensor, D value) const
790{
791 fill_tensor_uniform(tensor, 0, value, value);
792}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100793} // namespace test
794} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100795#endif /* __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__ */