blob: 49e3a83399e1740c819e94dcf02d674a138f616e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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_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
John Richardson70f946b2017-10-02 16:52:16 +010069 /** Path to assets directory used to initialise library. */
70 std::string path() const;
71
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 /** Seed that is used to fill tensors with random values. */
73 std::random_device::result_type seed() const;
74
Giorgio Arenafda46182017-06-16 13:57:33 +010075 /** Provides a tensor shape for the specified image.
76 *
77 * @param[in] name Image file used to look up the raw tensor.
78 */
79 TensorShape get_image_shape(const std::string &name);
80
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 /** Provides a contant raw tensor for the specified image.
82 *
83 * @param[in] name Image file used to look up the raw tensor.
84 */
85 const RawTensor &get(const std::string &name) const;
86
87 /** Provides a raw tensor for the specified image.
88 *
89 * @param[in] name Image file used to look up the raw tensor.
90 */
91 RawTensor get(const std::string &name);
92
93 /** Creates an uninitialised raw tensor with the given @p data_type and @p
94 * num_channels. The shape is derived from the specified image.
95 *
96 * @param[in] name Image file used to initialise the tensor.
97 * @param[in] data_type Data type used to initialise the tensor.
98 * @param[in] num_channels Number of channels used to initialise the tensor.
99 */
100 RawTensor get(const std::string &name, DataType data_type, int num_channels = 1) const;
101
102 /** Provides a contant raw tensor for the specified image after it has been
103 * converted to @p format.
104 *
105 * @param[in] name Image file used to look up the raw tensor.
106 * @param[in] format Format used to look up the raw tensor.
107 */
108 const RawTensor &get(const std::string &name, Format format) const;
109
110 /** Provides a raw tensor for the specified image after it has been
111 * converted to @p format.
112 *
113 * @param[in] name Image file used to look up the raw tensor.
114 * @param[in] format Format used to look up the raw tensor.
115 */
116 RawTensor get(const std::string &name, Format format);
117
118 /** Provides a contant raw tensor for the specified channel after it has
119 * been extracted form the given image.
120 *
121 * @param[in] name Image file used to look up the raw tensor.
122 * @param[in] channel Channel used to look up the raw tensor.
123 *
124 * @note The channel has to be unambiguous so that the format can be
125 * inferred automatically.
126 */
127 const RawTensor &get(const std::string &name, Channel channel) const;
128
129 /** Provides a raw tensor for the specified channel after it has been
130 * extracted form the given image.
131 *
132 * @param[in] name Image file used to look up the raw tensor.
133 * @param[in] channel Channel used to look up the raw tensor.
134 *
135 * @note The channel has to be unambiguous so that the format can be
136 * inferred automatically.
137 */
138 RawTensor get(const std::string &name, Channel channel);
139
140 /** Provides a constant raw tensor for the specified channel after it has
141 * been extracted form the given image formatted to @p format.
142 *
143 * @param[in] name Image file used to look up the raw tensor.
144 * @param[in] format Format used to look up the raw tensor.
145 * @param[in] channel Channel used to look up the raw tensor.
146 */
147 const RawTensor &get(const std::string &name, Format format, Channel channel) const;
148
149 /** Provides a raw tensor for the specified channel after it has been
150 * extracted form the given image formatted to @p format.
151 *
152 * @param[in] name Image file used to look up the raw tensor.
153 * @param[in] format Format used to look up the raw tensor.
154 * @param[in] channel Channel used to look up the raw tensor.
155 */
156 RawTensor get(const std::string &name, Format format, Channel channel);
157
Giorgio Arenaa2611812017-07-21 10:08:48 +0100158 /** Puts garbage values all around the tensor for testing purposes
159 *
160 * @param[in, out] tensor To be filled tensor.
161 * @param[in] distribution Distribution used to fill the tensor's surroundings.
162 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
163 */
164 template <typename T, typename D>
165 void fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const;
166
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 /** Fills the specified @p tensor with random values drawn from @p
168 * distribution.
169 *
170 * @param[in, out] tensor To be filled tensor.
171 * @param[in] distribution Distribution used to fill the tensor.
172 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
173 *
174 * @note The @p distribution has to provide operator(Generator &) which
175 * will be used to draw samples.
176 */
177 template <typename T, typename D>
178 void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const;
179
180 /** Fills the specified @p raw tensor with random values drawn from @p
181 * distribution.
182 *
183 * @param[in, out] raw To be filled raw.
184 * @param[in] distribution Distribution used to fill the tensor.
185 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
186 *
187 * @note The @p distribution has to provide operator(Generator &) which
188 * will be used to draw samples.
189 */
190 template <typename D>
191 void fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const;
192
193 /** Fills the specified @p tensor with the content of the specified image
194 * converted to the given format.
195 *
196 * @param[in, out] tensor To be filled tensor.
197 * @param[in] name Image file used to fill the tensor.
198 * @param[in] format Format of the image used to fill the tensor.
199 *
200 * @warning No check is performed that the specified format actually
201 * matches the format of the tensor.
202 */
203 template <typename T>
204 void fill(T &&tensor, const std::string &name, Format format) const;
205
206 /** Fills the raw tensor with the content of the specified image
207 * converted to the given format.
208 *
209 * @param[in, out] raw To be filled raw tensor.
210 * @param[in] name Image file used to fill the tensor.
211 * @param[in] format Format of the image used to fill the tensor.
212 *
213 * @warning No check is performed that the specified format actually
214 * matches the format of the tensor.
215 */
216 void fill(RawTensor &raw, const std::string &name, Format format) const;
217
218 /** Fills the specified @p tensor with the content of the specified channel
219 * extracted from the given image.
220 *
221 * @param[in, out] tensor To be filled tensor.
222 * @param[in] name Image file used to fill the tensor.
223 * @param[in] channel Channel of the image used to fill the tensor.
224 *
225 * @note The channel has to be unambiguous so that the format can be
226 * inferred automatically.
227 *
228 * @warning No check is performed that the specified format actually
229 * matches the format of the tensor.
230 */
231 template <typename T>
232 void fill(T &&tensor, const std::string &name, Channel channel) const;
233
234 /** Fills the raw tensor with the content of the specified channel
235 * extracted from the given image.
236 *
237 * @param[in, out] raw To be filled raw tensor.
238 * @param[in] name Image file used to fill the tensor.
239 * @param[in] channel Channel of the image used to fill the tensor.
240 *
241 * @note The channel has to be unambiguous so that the format can be
242 * inferred automatically.
243 *
244 * @warning No check is performed that the specified format actually
245 * matches the format of the tensor.
246 */
247 void fill(RawTensor &raw, const std::string &name, Channel channel) const;
248
249 /** Fills the specified @p tensor with the content of the specified channel
250 * extracted from the given image after it has been converted to the given
251 * format.
252 *
253 * @param[in, out] tensor To be filled tensor.
254 * @param[in] name Image file used to fill the tensor.
255 * @param[in] format Format of the image used to fill the tensor.
256 * @param[in] channel Channel of the image used to fill the tensor.
257 *
258 * @warning No check is performed that the specified format actually
259 * matches the format of the tensor.
260 */
261 template <typename T>
262 void fill(T &&tensor, const std::string &name, Format format, Channel channel) const;
263
264 /** Fills the raw tensor with the content of the specified channel
265 * extracted from the given image after it has been converted to the given
266 * format.
267 *
268 * @param[in, out] raw To be filled raw tensor.
269 * @param[in] name Image file used to fill the tensor.
270 * @param[in] format Format of the image used to fill the tensor.
271 * @param[in] channel Channel of the image used to fill the tensor.
272 *
273 * @warning No check is performed that the specified format actually
274 * matches the format of the tensor.
275 */
276 void fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const;
277
278 /** Fill a tensor with uniform distribution across the range of its type
279 *
280 * @param[in, out] tensor To be filled tensor.
281 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
282 */
283 template <typename T>
284 void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const;
285
286 /** Fill a tensor with uniform distribution across the a specified range
287 *
288 * @param[in, out] tensor To be filled tensor.
289 * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
290 * @param[in] low lowest value in the range (inclusive)
291 * @param[in] high highest value in the range (inclusive)
292 *
293 * @note @p low and @p high must be of the same type as the data type of @p tensor
294 */
295 template <typename T, typename D>
296 void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const;
297
SiCong Li86b53332017-08-23 11:02:43 +0100298 /** Fills the specified @p tensor with data loaded from .npy (numpy binary) in specified path.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299 *
300 * @param[in, out] tensor To be filled tensor.
301 * @param[in] name Data file.
SiCong Li86b53332017-08-23 11:02:43 +0100302 *
303 * @note The numpy array stored in the binary .npy file must be row-major in the sense that it
304 * must store elements within a row consecutively in the memory, then rows within a 2D slice,
305 * then 2D slices within a 3D slice and so on. Note that it imposes no restrictions on what
306 * indexing convention is used in the numpy array. That is, the numpy array can be either fortran
307 * style or C style as long as it adheres to the rule above.
308 *
309 * More concretely, the orders of dimensions for each style are as follows:
310 * C-style (numpy default):
311 * array[HigherDims..., Z, Y, X]
312 * Fortran style:
313 * array[X, Y, Z, HigherDims...]
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100314 */
315 template <typename T>
316 void fill_layer_data(T &&tensor, std::string name) const;
317
318private:
319 // Function prototype to convert between image formats.
320 using Converter = void (*)(const RawTensor &src, RawTensor &dst);
321 // Function prototype to extract a channel from an image.
322 using Extractor = void (*)(const RawTensor &src, RawTensor &dst);
323 // Function prototype to load an image file.
324 using Loader = RawTensor (*)(const std::string &path);
325
326 const Converter &get_converter(Format src, Format dst) const;
327 const Converter &get_converter(DataType src, Format dst) const;
328 const Converter &get_converter(Format src, DataType dst) const;
329 const Converter &get_converter(DataType src, DataType dst) const;
330 const Extractor &get_extractor(Format format, Channel) const;
331 const Loader &get_loader(const std::string &extension) const;
332
333 /** Creates a raw tensor from the specified image.
334 *
335 * @param[in] name To be loaded image file.
336 *
337 * @note If use_single_image is true @p name is ignored and the user image
338 * is loaded instead.
339 */
340 RawTensor load_image(const std::string &name) const;
341
342 /** Provides a raw tensor for the specified image and format.
343 *
344 * @param[in] name Image file used to look up the raw tensor.
345 * @param[in] format Format used to look up the raw tensor.
346 *
347 * If the tensor has already been requested before the cached version will
348 * be returned. Otherwise the tensor will be added to the cache.
349 *
350 * @note If use_single_image is true @p name is ignored and the user image
351 * is loaded instead.
352 */
353 const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format) const;
354
355 /** Provides a raw tensor for the specified image, format and channel.
356 *
357 * @param[in] name Image file used to look up the raw tensor.
358 * @param[in] format Format used to look up the raw tensor.
359 * @param[in] channel Channel used to look up the raw tensor.
360 *
361 * If the tensor has already been requested before the cached version will
362 * be returned. Otherwise the tensor will be added to the cache.
363 *
364 * @note If use_single_image is true @p name is ignored and the user image
365 * is loaded instead.
366 */
367 const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const;
368
369 mutable TensorCache _cache{};
370 mutable std::mutex _format_lock{};
371 mutable std::mutex _channel_lock{};
Anthony Barbierac69aa12017-07-03 17:39:37 +0100372 const std::string _library_path;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373 std::random_device::result_type _seed;
374};
375
376template <typename T, typename D>
Giorgio Arenaa2611812017-07-21 10:08:48 +0100377void AssetsLibrary::fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
378{
379 const PaddingSize padding_size = tensor.padding();
380
381 Window window;
382 window.set(0, Window::Dimension(-padding_size.left, tensor.shape()[0] + padding_size.right, 1));
Gian Marco5420b282017-11-29 10:41:38 +0000383 if(tensor.shape().num_dimensions() > 1)
384 {
385 window.set(1, Window::Dimension(-padding_size.top, tensor.shape()[1] + padding_size.bottom, 1));
386 }
Giorgio Arenaa2611812017-07-21 10:08:48 +0100387
388 std::mt19937 gen(_seed);
389
390 execute_window_loop(window, [&](const Coordinates & id)
391 {
392 TensorShape shape = tensor.shape();
393
394 // If outside of valid region
395 if(id.x() < 0 || id.x() >= static_cast<int>(shape.x()) || id.y() < 0 || id.y() >= static_cast<int>(shape.y()))
396 {
397 using ResultType = typename std::remove_reference<D>::type::result_type;
398 const ResultType value = distribution(gen);
399 void *const out_ptr = tensor(id);
400 store_value_with_data_type(out_ptr, value, tensor.data_type());
401 }
402 });
403}
404
405template <typename T, typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100406void AssetsLibrary::fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407{
408 Window window;
409 for(unsigned int d = 0; d < tensor.shape().num_dimensions(); ++d)
410 {
411 window.set(d, Window::Dimension(0, tensor.shape()[d], 1));
412 }
413
414 std::mt19937 gen(_seed + seed_offset);
415
416 //FIXME: Replace with normal loop
417 execute_window_loop(window, [&](const Coordinates & id)
418 {
419 using ResultType = typename std::remove_reference<D>::type::result_type;
420 const ResultType value = distribution(gen);
421 void *const out_ptr = tensor(id);
422 store_value_with_data_type(out_ptr, value, tensor.data_type());
423 });
Giorgio Arenaa2611812017-07-21 10:08:48 +0100424
425 fill_borders_with_garbage(tensor, distribution, seed_offset);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426}
427
428template <typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100429void AssetsLibrary::fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100430{
431 std::mt19937 gen(_seed + seed_offset);
432
433 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
434 {
435 using ResultType = typename std::remove_reference<D>::type::result_type;
436 const ResultType value = distribution(gen);
437 store_value_with_data_type(raw.data() + offset, value, raw.data_type());
438 }
439}
440
441template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100442void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100443{
444 const RawTensor &raw = get(name, format);
445
446 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
447 {
448 const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
449
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100450 const RawTensor::value_type *const raw_ptr = raw.data() + offset;
451 const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100452 std::copy_n(raw_ptr, raw.element_size(), out_ptr);
453 }
454}
455
456template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100457void AssetsLibrary::fill(T &&tensor, const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458{
459 fill(std::forward<T>(tensor), name, get_format_for_channel(channel), channel);
460}
461
462template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100463void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100464{
465 const RawTensor &raw = get(name, format, channel);
466
467 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
468 {
469 const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
470
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100471 const RawTensor::value_type *const raw_ptr = raw.data() + offset;
472 const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100473 std::copy_n(raw_ptr, raw.element_size(), out_ptr);
474 }
475}
476
477template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100478void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100479{
480 switch(tensor.data_type())
481 {
482 case DataType::U8:
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000483 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100484 {
485 std::uniform_int_distribution<uint8_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(), std::numeric_limits<uint8_t>::max());
486 fill(tensor, distribution_u8, seed_offset);
487 break;
488 }
489 case DataType::S8:
490 case DataType::QS8:
491 {
492 std::uniform_int_distribution<int8_t> distribution_s8(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max());
493 fill(tensor, distribution_s8, seed_offset);
494 break;
495 }
496 case DataType::U16:
497 {
498 std::uniform_int_distribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), std::numeric_limits<uint16_t>::max());
499 fill(tensor, distribution_u16, seed_offset);
500 break;
501 }
502 case DataType::S16:
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +0100503 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100504 {
505 std::uniform_int_distribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), std::numeric_limits<int16_t>::max());
506 fill(tensor, distribution_s16, seed_offset);
507 break;
508 }
509 case DataType::U32:
510 {
511 std::uniform_int_distribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max());
512 fill(tensor, distribution_u32, seed_offset);
513 break;
514 }
515 case DataType::S32:
516 {
517 std::uniform_int_distribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max());
518 fill(tensor, distribution_s32, seed_offset);
519 break;
520 }
521 case DataType::U64:
522 {
523 std::uniform_int_distribution<uint64_t> distribution_u64(std::numeric_limits<uint64_t>::lowest(), std::numeric_limits<uint64_t>::max());
524 fill(tensor, distribution_u64, seed_offset);
525 break;
526 }
527 case DataType::S64:
528 {
529 std::uniform_int_distribution<int64_t> distribution_s64(std::numeric_limits<int64_t>::lowest(), std::numeric_limits<int64_t>::max());
530 fill(tensor, distribution_s64, seed_offset);
531 break;
532 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100533 case DataType::F16:
SiCong Li02dfb2c2017-07-27 17:59:20 +0100534 {
535 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
536 std::uniform_real_distribution<float> distribution_f16(-100.f, 100.f);
537 fill(tensor, distribution_f16, seed_offset);
538 break;
539 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100540 case DataType::F32:
541 {
542 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
543 std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f);
544 fill(tensor, distribution_f32, seed_offset);
545 break;
546 }
547 case DataType::F64:
548 {
549 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
550 std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f);
551 fill(tensor, distribution_f64, seed_offset);
552 break;
553 }
554 case DataType::SIZET:
555 {
556 std::uniform_int_distribution<size_t> distribution_sizet(std::numeric_limits<size_t>::lowest(), std::numeric_limits<size_t>::max());
557 fill(tensor, distribution_sizet, seed_offset);
558 break;
559 }
560 default:
561 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
562 }
563}
564
565template <typename T, typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100566void 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 +0100567{
568 switch(tensor.data_type())
569 {
570 case DataType::U8:
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000571 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100572 {
573 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint8_t, D>::value));
574 std::uniform_int_distribution<uint8_t> distribution_u8(low, high);
575 fill(tensor, distribution_u8, seed_offset);
576 break;
577 }
578 case DataType::S8:
579 case DataType::QS8:
580 {
581 ARM_COMPUTE_ERROR_ON(!(std::is_same<int8_t, D>::value));
582 std::uniform_int_distribution<int8_t> distribution_s8(low, high);
583 fill(tensor, distribution_s8, seed_offset);
584 break;
585 }
586 case DataType::U16:
587 {
588 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint16_t, D>::value));
589 std::uniform_int_distribution<uint16_t> distribution_u16(low, high);
590 fill(tensor, distribution_u16, seed_offset);
591 break;
592 }
593 case DataType::S16:
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100594 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100595 {
596 ARM_COMPUTE_ERROR_ON(!(std::is_same<int16_t, D>::value));
597 std::uniform_int_distribution<int16_t> distribution_s16(low, high);
598 fill(tensor, distribution_s16, seed_offset);
599 break;
600 }
601 case DataType::U32:
602 {
603 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint32_t, D>::value));
604 std::uniform_int_distribution<uint32_t> distribution_u32(low, high);
605 fill(tensor, distribution_u32, seed_offset);
606 break;
607 }
608 case DataType::S32:
609 {
610 ARM_COMPUTE_ERROR_ON(!(std::is_same<int32_t, D>::value));
611 std::uniform_int_distribution<int32_t> distribution_s32(low, high);
612 fill(tensor, distribution_s32, seed_offset);
613 break;
614 }
615 case DataType::U64:
616 {
617 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint64_t, D>::value));
618 std::uniform_int_distribution<uint64_t> distribution_u64(low, high);
619 fill(tensor, distribution_u64, seed_offset);
620 break;
621 }
622 case DataType::S64:
623 {
624 ARM_COMPUTE_ERROR_ON(!(std::is_same<int64_t, D>::value));
625 std::uniform_int_distribution<int64_t> distribution_s64(low, high);
626 fill(tensor, distribution_s64, seed_offset);
627 break;
628 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100629 case DataType::F16:
630 {
Moritz Pflanzere49e2662017-07-21 15:55:28 +0100631 std::uniform_real_distribution<float> distribution_f16(low, high);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100632 fill(tensor, distribution_f16, seed_offset);
633 break;
634 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100635 case DataType::F32:
636 {
637 ARM_COMPUTE_ERROR_ON(!(std::is_same<float, D>::value));
638 std::uniform_real_distribution<float> distribution_f32(low, high);
639 fill(tensor, distribution_f32, seed_offset);
640 break;
641 }
642 case DataType::F64:
643 {
644 ARM_COMPUTE_ERROR_ON(!(std::is_same<double, D>::value));
645 std::uniform_real_distribution<double> distribution_f64(low, high);
646 fill(tensor, distribution_f64, seed_offset);
647 break;
648 }
649 case DataType::SIZET:
650 {
651 ARM_COMPUTE_ERROR_ON(!(std::is_same<size_t, D>::value));
652 std::uniform_int_distribution<size_t> distribution_sizet(low, high);
653 fill(tensor, distribution_sizet, seed_offset);
654 break;
655 }
656 default:
657 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
658 }
659}
660
661template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100662void AssetsLibrary::fill_layer_data(T &&tensor, std::string name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100663{
664#ifdef _WIN32
665 const std::string path_separator("\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100666#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100667 const std::string path_separator("/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100668#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100669 const std::string path = _library_path + path_separator + name;
670
SiCong Li86b53332017-08-23 11:02:43 +0100671 std::vector<unsigned long> shape;
672
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100673 // Open file
SiCong Li86b53332017-08-23 11:02:43 +0100674 std::ifstream stream(path, std::ios::in | std::ios::binary);
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100675 if(!stream.good())
676 {
677 throw framework::FileNotFound("Could not load npy file: " + path);
678 }
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000679 std::string header = npy::read_header(stream);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100680
SiCong Li86b53332017-08-23 11:02:43 +0100681 // Parse header
682 bool fortran_order = false;
683 std::string typestr;
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000684 npy::parse_header(header, typestr, fortran_order, shape);
SiCong Li86b53332017-08-23 11:02:43 +0100685
686 // Check if the typestring matches the given one
687 std::string expect_typestr = get_typestring(tensor.data_type());
688 ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
689
690 // Validate tensor shape
691 ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch");
692 if(fortran_order)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100693 {
SiCong Li86b53332017-08-23 11:02:43 +0100694 for(size_t i = 0; i < shape.size(); ++i)
695 {
696 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[i], "Tensor dimensions mismatch");
697 }
698 }
699 else
700 {
701 for(size_t i = 0; i < shape.size(); ++i)
702 {
703 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
704 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100705 }
706
SiCong Li86b53332017-08-23 11:02:43 +0100707 // Read data
708 if(tensor.padding().empty())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100709 {
SiCong Li86b53332017-08-23 11:02:43 +0100710 // If tensor has no padding read directly from stream.
711 stream.read(reinterpret_cast<char *>(tensor.data()), tensor.size());
712 }
713 else
714 {
715 // If tensor has padding accessing tensor elements through execution window.
716 Window window;
717 window.use_tensor_dimensions(tensor.shape());
718
719 //FIXME : Replace with normal loop
720 execute_window_loop(window, [&](const Coordinates & id)
721 {
722 stream.read(reinterpret_cast<char *>(tensor(id)), tensor.element_size());
723 });
724 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100725}
726} // namespace test
727} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100728#endif /* __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__ */