blob: 6eb1e3c5f42fbbadc44357494ebe5ae858821d41 [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:
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000480 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100481 {
482 std::uniform_int_distribution<uint8_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(), std::numeric_limits<uint8_t>::max());
483 fill(tensor, distribution_u8, seed_offset);
484 break;
485 }
486 case DataType::S8:
487 case DataType::QS8:
488 {
489 std::uniform_int_distribution<int8_t> distribution_s8(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max());
490 fill(tensor, distribution_s8, seed_offset);
491 break;
492 }
493 case DataType::U16:
494 {
495 std::uniform_int_distribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), std::numeric_limits<uint16_t>::max());
496 fill(tensor, distribution_u16, seed_offset);
497 break;
498 }
499 case DataType::S16:
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +0100500 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501 {
502 std::uniform_int_distribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), std::numeric_limits<int16_t>::max());
503 fill(tensor, distribution_s16, seed_offset);
504 break;
505 }
506 case DataType::U32:
507 {
508 std::uniform_int_distribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max());
509 fill(tensor, distribution_u32, seed_offset);
510 break;
511 }
512 case DataType::S32:
513 {
514 std::uniform_int_distribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max());
515 fill(tensor, distribution_s32, seed_offset);
516 break;
517 }
518 case DataType::U64:
519 {
520 std::uniform_int_distribution<uint64_t> distribution_u64(std::numeric_limits<uint64_t>::lowest(), std::numeric_limits<uint64_t>::max());
521 fill(tensor, distribution_u64, seed_offset);
522 break;
523 }
524 case DataType::S64:
525 {
526 std::uniform_int_distribution<int64_t> distribution_s64(std::numeric_limits<int64_t>::lowest(), std::numeric_limits<int64_t>::max());
527 fill(tensor, distribution_s64, seed_offset);
528 break;
529 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100530 case DataType::F16:
SiCong Li02dfb2c2017-07-27 17:59:20 +0100531 {
532 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
533 std::uniform_real_distribution<float> distribution_f16(-100.f, 100.f);
534 fill(tensor, distribution_f16, seed_offset);
535 break;
536 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100537 case DataType::F32:
538 {
539 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
540 std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f);
541 fill(tensor, distribution_f32, seed_offset);
542 break;
543 }
544 case DataType::F64:
545 {
546 // It doesn't make sense to check [-inf, inf], so hard code it to a big number
547 std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f);
548 fill(tensor, distribution_f64, seed_offset);
549 break;
550 }
551 case DataType::SIZET:
552 {
553 std::uniform_int_distribution<size_t> distribution_sizet(std::numeric_limits<size_t>::lowest(), std::numeric_limits<size_t>::max());
554 fill(tensor, distribution_sizet, seed_offset);
555 break;
556 }
557 default:
558 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
559 }
560}
561
562template <typename T, typename D>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100563void 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 +0100564{
565 switch(tensor.data_type())
566 {
567 case DataType::U8:
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000568 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100569 {
570 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint8_t, D>::value));
571 std::uniform_int_distribution<uint8_t> distribution_u8(low, high);
572 fill(tensor, distribution_u8, seed_offset);
573 break;
574 }
575 case DataType::S8:
576 case DataType::QS8:
577 {
578 ARM_COMPUTE_ERROR_ON(!(std::is_same<int8_t, D>::value));
579 std::uniform_int_distribution<int8_t> distribution_s8(low, high);
580 fill(tensor, distribution_s8, seed_offset);
581 break;
582 }
583 case DataType::U16:
584 {
585 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint16_t, D>::value));
586 std::uniform_int_distribution<uint16_t> distribution_u16(low, high);
587 fill(tensor, distribution_u16, seed_offset);
588 break;
589 }
590 case DataType::S16:
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100591 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100592 {
593 ARM_COMPUTE_ERROR_ON(!(std::is_same<int16_t, D>::value));
594 std::uniform_int_distribution<int16_t> distribution_s16(low, high);
595 fill(tensor, distribution_s16, seed_offset);
596 break;
597 }
598 case DataType::U32:
599 {
600 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint32_t, D>::value));
601 std::uniform_int_distribution<uint32_t> distribution_u32(low, high);
602 fill(tensor, distribution_u32, seed_offset);
603 break;
604 }
605 case DataType::S32:
606 {
607 ARM_COMPUTE_ERROR_ON(!(std::is_same<int32_t, D>::value));
608 std::uniform_int_distribution<int32_t> distribution_s32(low, high);
609 fill(tensor, distribution_s32, seed_offset);
610 break;
611 }
612 case DataType::U64:
613 {
614 ARM_COMPUTE_ERROR_ON(!(std::is_same<uint64_t, D>::value));
615 std::uniform_int_distribution<uint64_t> distribution_u64(low, high);
616 fill(tensor, distribution_u64, seed_offset);
617 break;
618 }
619 case DataType::S64:
620 {
621 ARM_COMPUTE_ERROR_ON(!(std::is_same<int64_t, D>::value));
622 std::uniform_int_distribution<int64_t> distribution_s64(low, high);
623 fill(tensor, distribution_s64, seed_offset);
624 break;
625 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100626 case DataType::F16:
627 {
Moritz Pflanzere49e2662017-07-21 15:55:28 +0100628 std::uniform_real_distribution<float> distribution_f16(low, high);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100629 fill(tensor, distribution_f16, seed_offset);
630 break;
631 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100632 case DataType::F32:
633 {
634 ARM_COMPUTE_ERROR_ON(!(std::is_same<float, D>::value));
635 std::uniform_real_distribution<float> distribution_f32(low, high);
636 fill(tensor, distribution_f32, seed_offset);
637 break;
638 }
639 case DataType::F64:
640 {
641 ARM_COMPUTE_ERROR_ON(!(std::is_same<double, D>::value));
642 std::uniform_real_distribution<double> distribution_f64(low, high);
643 fill(tensor, distribution_f64, seed_offset);
644 break;
645 }
646 case DataType::SIZET:
647 {
648 ARM_COMPUTE_ERROR_ON(!(std::is_same<size_t, D>::value));
649 std::uniform_int_distribution<size_t> distribution_sizet(low, high);
650 fill(tensor, distribution_sizet, seed_offset);
651 break;
652 }
653 default:
654 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
655 }
656}
657
658template <typename T>
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100659void AssetsLibrary::fill_layer_data(T &&tensor, std::string name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100660{
661#ifdef _WIN32
662 const std::string path_separator("\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100663#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100664 const std::string path_separator("/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100665#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100666 const std::string path = _library_path + path_separator + name;
667
SiCong Li86b53332017-08-23 11:02:43 +0100668 std::vector<unsigned long> shape;
669
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100670 // Open file
SiCong Li86b53332017-08-23 11:02:43 +0100671 std::ifstream stream(path, std::ios::in | std::ios::binary);
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100672 if(!stream.good())
673 {
674 throw framework::FileNotFound("Could not load npy file: " + path);
675 }
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000676 std::string header = npy::read_header(stream);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100677
SiCong Li86b53332017-08-23 11:02:43 +0100678 // Parse header
679 bool fortran_order = false;
680 std::string typestr;
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000681 npy::parse_header(header, typestr, fortran_order, shape);
SiCong Li86b53332017-08-23 11:02:43 +0100682
683 // Check if the typestring matches the given one
684 std::string expect_typestr = get_typestring(tensor.data_type());
685 ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
686
687 // Validate tensor shape
688 ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch");
689 if(fortran_order)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100690 {
SiCong Li86b53332017-08-23 11:02:43 +0100691 for(size_t i = 0; i < shape.size(); ++i)
692 {
693 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[i], "Tensor dimensions mismatch");
694 }
695 }
696 else
697 {
698 for(size_t i = 0; i < shape.size(); ++i)
699 {
700 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
701 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100702 }
703
SiCong Li86b53332017-08-23 11:02:43 +0100704 // Read data
705 if(tensor.padding().empty())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100706 {
SiCong Li86b53332017-08-23 11:02:43 +0100707 // If tensor has no padding read directly from stream.
708 stream.read(reinterpret_cast<char *>(tensor.data()), tensor.size());
709 }
710 else
711 {
712 // If tensor has padding accessing tensor elements through execution window.
713 Window window;
714 window.use_tensor_dimensions(tensor.shape());
715
716 //FIXME : Replace with normal loop
717 execute_window_loop(window, [&](const Coordinates & id)
718 {
719 stream.read(reinterpret_cast<char *>(tensor(id)), tensor.element_size());
720 });
721 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100722}
723} // namespace test
724} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100725#endif /* __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__ */