blob: ee136447ee598f5e8aa6be492a6fe456c1caf627 [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));
383 window.set(1, Window::Dimension(-padding_size.top, tensor.shape()[1] + padding_size.bottom, 1));
384
385 std::mt19937 gen(_seed);
386
387 execute_window_loop(window, [&](const Coordinates & id)
388 {
389 TensorShape shape = tensor.shape();
390
391 // If outside of valid region
392 if(id.x() < 0 || id.x() >= static_cast<int>(shape.x()) || id.y() < 0 || id.y() >= static_cast<int>(shape.y()))
393 {
394 using ResultType = typename std::remove_reference<D>::type::result_type;
395 const ResultType value = distribution(gen);
396 void *const out_ptr = tensor(id);
397 store_value_with_data_type(out_ptr, value, tensor.data_type());
398 }
399 });
400}
401
402template <typename T, typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100403void AssetsLibrary::fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100404{
405 Window window;
406 for(unsigned int d = 0; d < tensor.shape().num_dimensions(); ++d)
407 {
408 window.set(d, Window::Dimension(0, tensor.shape()[d], 1));
409 }
410
411 std::mt19937 gen(_seed + seed_offset);
412
413 //FIXME: Replace with normal loop
414 execute_window_loop(window, [&](const Coordinates & id)
415 {
416 using ResultType = typename std::remove_reference<D>::type::result_type;
417 const ResultType value = distribution(gen);
418 void *const out_ptr = tensor(id);
419 store_value_with_data_type(out_ptr, value, tensor.data_type());
420 });
Giorgio Arenaa2611812017-07-21 10:08:48 +0100421
422 fill_borders_with_garbage(tensor, distribution, seed_offset);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100423}
424
425template <typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100426void AssetsLibrary::fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100427{
428 std::mt19937 gen(_seed + seed_offset);
429
430 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
431 {
432 using ResultType = typename std::remove_reference<D>::type::result_type;
433 const ResultType value = distribution(gen);
434 store_value_with_data_type(raw.data() + offset, value, raw.data_type());
435 }
436}
437
438template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100439void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100440{
441 const RawTensor &raw = get(name, format);
442
443 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
444 {
445 const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
446
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100447 const RawTensor::value_type *const raw_ptr = raw.data() + offset;
448 const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449 std::copy_n(raw_ptr, raw.element_size(), out_ptr);
450 }
451}
452
453template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100454void AssetsLibrary::fill(T &&tensor, const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100455{
456 fill(std::forward<T>(tensor), name, get_format_for_channel(channel), channel);
457}
458
459template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100460void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100461{
462 const RawTensor &raw = get(name, format, channel);
463
464 for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
465 {
466 const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
467
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100468 const RawTensor::value_type *const raw_ptr = raw.data() + offset;
469 const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100470 std::copy_n(raw_ptr, raw.element_size(), out_ptr);
471 }
472}
473
474template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100475void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100476{
477 switch(tensor.data_type())
478 {
479 case DataType::U8:
480 {
481 std::uniform_int_distribution<uint8_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(), std::numeric_limits<uint8_t>::max());
482 fill(tensor, distribution_u8, seed_offset);
483 break;
484 }
485 case DataType::S8:
486 case DataType::QS8:
487 {
488 std::uniform_int_distribution<int8_t> distribution_s8(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max());
489 fill(tensor, distribution_s8, seed_offset);
490 break;
491 }
492 case DataType::U16:
493 {
494 std::uniform_int_distribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), std::numeric_limits<uint16_t>::max());
495 fill(tensor, distribution_u16, seed_offset);
496 break;
497 }
498 case DataType::S16:
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +0100499 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100500 {
501 std::uniform_int_distribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), std::numeric_limits<int16_t>::max());
502 fill(tensor, distribution_s16, seed_offset);
503 break;
504 }
505 case DataType::U32:
506 {
507 std::uniform_int_distribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max());
508 fill(tensor, distribution_u32, seed_offset);
509 break;
510 }
511 case DataType::S32:
512 {
513 std::uniform_int_distribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max());
514 fill(tensor, distribution_s32, seed_offset);
515 break;
516 }
517 case DataType::U64:
518 {
519 std::uniform_int_distribution<uint64_t> distribution_u64(std::numeric_limits<uint64_t>::lowest(), std::numeric_limits<uint64_t>::max());
520 fill(tensor, distribution_u64, seed_offset);
521 break;
522 }
523 case DataType::S64:
524 {
525 std::uniform_int_distribution<int64_t> distribution_s64(std::numeric_limits<int64_t>::lowest(), std::numeric_limits<int64_t>::max());
526 fill(tensor, distribution_s64, seed_offset);
527 break;
528 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100529 case DataType::F16:
SiCong Li02dfb2c2017-07-27 17:59:20 +0100530 {
531 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
532 std::uniform_real_distribution<float> distribution_f16(-100.f, 100.f);
533 fill(tensor, distribution_f16, seed_offset);
534 break;
535 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100536 case DataType::F32:
537 {
538 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
539 std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f);
540 fill(tensor, distribution_f32, seed_offset);
541 break;
542 }
543 case DataType::F64:
544 {
545 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
546 std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f);
547 fill(tensor, distribution_f64, seed_offset);
548 break;
549 }
550 case DataType::SIZET:
551 {
552 std::uniform_int_distribution<size_t> distribution_sizet(std::numeric_limits<size_t>::lowest(), std::numeric_limits<size_t>::max());
553 fill(tensor, distribution_sizet, seed_offset);
554 break;
555 }
556 default:
557 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
558 }
559}
560
561template <typename T, typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100562void 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 +0100563{
564 switch(tensor.data_type())
565 {
566 case DataType::U8:
567 {
568 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint8_t, D>::value));
569 std::uniform_int_distribution<uint8_t> distribution_u8(low, high);
570 fill(tensor, distribution_u8, seed_offset);
571 break;
572 }
573 case DataType::S8:
574 case DataType::QS8:
575 {
576 ARM_COMPUTE_ERROR_ON(!(std::is_same<int8_t, D>::value));
577 std::uniform_int_distribution<int8_t> distribution_s8(low, high);
578 fill(tensor, distribution_s8, seed_offset);
579 break;
580 }
581 case DataType::U16:
582 {
583 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint16_t, D>::value));
584 std::uniform_int_distribution<uint16_t> distribution_u16(low, high);
585 fill(tensor, distribution_u16, seed_offset);
586 break;
587 }
588 case DataType::S16:
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100589 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100590 {
591 ARM_COMPUTE_ERROR_ON(!(std::is_same<int16_t, D>::value));
592 std::uniform_int_distribution<int16_t> distribution_s16(low, high);
593 fill(tensor, distribution_s16, seed_offset);
594 break;
595 }
596 case DataType::U32:
597 {
598 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint32_t, D>::value));
599 std::uniform_int_distribution<uint32_t> distribution_u32(low, high);
600 fill(tensor, distribution_u32, seed_offset);
601 break;
602 }
603 case DataType::S32:
604 {
605 ARM_COMPUTE_ERROR_ON(!(std::is_same<int32_t, D>::value));
606 std::uniform_int_distribution<int32_t> distribution_s32(low, high);
607 fill(tensor, distribution_s32, seed_offset);
608 break;
609 }
610 case DataType::U64:
611 {
612 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint64_t, D>::value));
613 std::uniform_int_distribution<uint64_t> distribution_u64(low, high);
614 fill(tensor, distribution_u64, seed_offset);
615 break;
616 }
617 case DataType::S64:
618 {
619 ARM_COMPUTE_ERROR_ON(!(std::is_same<int64_t, D>::value));
620 std::uniform_int_distribution<int64_t> distribution_s64(low, high);
621 fill(tensor, distribution_s64, seed_offset);
622 break;
623 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100624 case DataType::F16:
625 {
Moritz Pflanzere49e2662017-07-21 15:55:28 +0100626 std::uniform_real_distribution<float> distribution_f16(low, high);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100627 fill(tensor, distribution_f16, seed_offset);
628 break;
629 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100630 case DataType::F32:
631 {
632 ARM_COMPUTE_ERROR_ON(!(std::is_same<float, D>::value));
633 std::uniform_real_distribution<float> distribution_f32(low, high);
634 fill(tensor, distribution_f32, seed_offset);
635 break;
636 }
637 case DataType::F64:
638 {
639 ARM_COMPUTE_ERROR_ON(!(std::is_same<double, D>::value));
640 std::uniform_real_distribution<double> distribution_f64(low, high);
641 fill(tensor, distribution_f64, seed_offset);
642 break;
643 }
644 case DataType::SIZET:
645 {
646 ARM_COMPUTE_ERROR_ON(!(std::is_same<size_t, D>::value));
647 std::uniform_int_distribution<size_t> distribution_sizet(low, high);
648 fill(tensor, distribution_sizet, seed_offset);
649 break;
650 }
651 default:
652 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
653 }
654}
655
656template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100657void AssetsLibrary::fill_layer_data(T &&tensor, std::string name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100658{
659#ifdef _WIN32
660 const std::string path_separator("\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100661#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100662 const std::string path_separator("/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100663#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100664 const std::string path = _library_path + path_separator + name;
665
SiCong Li86b53332017-08-23 11:02:43 +0100666 std::vector<unsigned long> shape;
667
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100668 // Open file
SiCong Li86b53332017-08-23 11:02:43 +0100669 std::ifstream stream(path, std::ios::in | std::ios::binary);
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100670 if(!stream.good())
671 {
672 throw framework::FileNotFound("Could not load npy file: " + path);
673 }
SiCong Li86b53332017-08-23 11:02:43 +0100674 // Check magic bytes and version number
675 unsigned char v_major = 0;
676 unsigned char v_minor = 0;
677 npy::read_magic(stream, &v_major, &v_minor);
678
679 // Read header
680 std::string header;
681 if(v_major == 1 && v_minor == 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100682 {
SiCong Li86b53332017-08-23 11:02:43 +0100683 header = npy::read_header_1_0(stream);
684 }
685 else if(v_major == 2 && v_minor == 0)
686 {
687 header = npy::read_header_2_0(stream);
688 }
689 else
690 {
691 ARM_COMPUTE_ERROR("Unsupported file format version");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100692 }
693
SiCong Li86b53332017-08-23 11:02:43 +0100694 // Parse header
695 bool fortran_order = false;
696 std::string typestr;
697 npy::ParseHeader(header, typestr, &fortran_order, shape);
698
699 // Check if the typestring matches the given one
700 std::string expect_typestr = get_typestring(tensor.data_type());
701 ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
702
703 // Validate tensor shape
704 ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch");
705 if(fortran_order)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100706 {
SiCong Li86b53332017-08-23 11:02:43 +0100707 for(size_t i = 0; i < shape.size(); ++i)
708 {
709 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[i], "Tensor dimensions mismatch");
710 }
711 }
712 else
713 {
714 for(size_t i = 0; i < shape.size(); ++i)
715 {
716 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
717 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100718 }
719
SiCong Li86b53332017-08-23 11:02:43 +0100720 // Read data
721 if(tensor.padding().empty())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100722 {
SiCong Li86b53332017-08-23 11:02:43 +0100723 // If tensor has no padding read directly from stream.
724 stream.read(reinterpret_cast<char *>(tensor.data()), tensor.size());
725 }
726 else
727 {
728 // If tensor has padding accessing tensor elements through execution window.
729 Window window;
730 window.use_tensor_dimensions(tensor.shape());
731
732 //FIXME : Replace with normal loop
733 execute_window_loop(window, [&](const Coordinates & id)
734 {
735 stream.read(reinterpret_cast<char *>(tensor(id)), tensor.element_size());
736 });
737 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100738}
739} // namespace test
740} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100741#endif /* __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__ */