1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_TEST_TENSOR_LIBRARY_H
25*c217d954SCole Faust #define ARM_COMPUTE_TEST_TENSOR_LIBRARY_H
26*c217d954SCole Faust
27*c217d954SCole Faust #include "arm_compute/core/Coordinates.h"
28*c217d954SCole Faust #include "arm_compute/core/Error.h"
29*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
30*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
31*c217d954SCole Faust #include "arm_compute/core/TensorShape.h"
32*c217d954SCole Faust #include "arm_compute/core/Types.h"
33*c217d954SCole Faust #include "arm_compute/core/Window.h"
34*c217d954SCole Faust #include "support/Random.h"
35*c217d954SCole Faust #include "tests/RawTensor.h"
36*c217d954SCole Faust #include "tests/TensorCache.h"
37*c217d954SCole Faust #include "tests/Utils.h"
38*c217d954SCole Faust #include "tests/framework/Exceptions.h"
39*c217d954SCole Faust #include "utils/Utils.h"
40*c217d954SCole Faust
41*c217d954SCole Faust #include <algorithm>
42*c217d954SCole Faust #include <cstddef>
43*c217d954SCole Faust #include <fstream>
44*c217d954SCole Faust #include <random>
45*c217d954SCole Faust #include <string>
46*c217d954SCole Faust #include <type_traits>
47*c217d954SCole Faust #include <vector>
48*c217d954SCole Faust
49*c217d954SCole Faust namespace arm_compute
50*c217d954SCole Faust {
51*c217d954SCole Faust namespace test
52*c217d954SCole Faust {
53*c217d954SCole Faust /** Factory class to create and fill tensors.
54*c217d954SCole Faust *
55*c217d954SCole Faust * Allows to initialise tensors from loaded images or by specifying the shape
56*c217d954SCole Faust * explicitly. Furthermore, provides methods to fill tensors with the content of
57*c217d954SCole Faust * loaded images or with random values.
58*c217d954SCole Faust */
59*c217d954SCole Faust class AssetsLibrary final
60*c217d954SCole Faust {
61*c217d954SCole Faust public:
62*c217d954SCole Faust using RangePair = std::pair<float, float>;
63*c217d954SCole Faust
64*c217d954SCole Faust public:
65*c217d954SCole Faust /** Initialises the library with a @p path to the assets directory.
66*c217d954SCole Faust * Furthermore, sets the seed for the random generator to @p seed.
67*c217d954SCole Faust *
68*c217d954SCole Faust * @param[in] path Path to load assets from.
69*c217d954SCole Faust * @param[in] seed Seed used to initialise the random number generator.
70*c217d954SCole Faust */
71*c217d954SCole Faust AssetsLibrary(std::string path, std::random_device::result_type seed);
72*c217d954SCole Faust
73*c217d954SCole Faust /** Path to assets directory used to initialise library.
74*c217d954SCole Faust *
75*c217d954SCole Faust * @return the path to the assets directory.
76*c217d954SCole Faust */
77*c217d954SCole Faust std::string path() const;
78*c217d954SCole Faust
79*c217d954SCole Faust /** Seed that is used to fill tensors with random values.
80*c217d954SCole Faust *
81*c217d954SCole Faust * @return the initial random seed.
82*c217d954SCole Faust */
83*c217d954SCole Faust std::random_device::result_type seed() const;
84*c217d954SCole Faust
85*c217d954SCole Faust /** Provides a tensor shape for the specified image.
86*c217d954SCole Faust *
87*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
88*c217d954SCole Faust *
89*c217d954SCole Faust * @return the tensor shape for the specified image.
90*c217d954SCole Faust */
91*c217d954SCole Faust TensorShape get_image_shape(const std::string &name);
92*c217d954SCole Faust
93*c217d954SCole Faust /** Provides a constant raw tensor for the specified image.
94*c217d954SCole Faust *
95*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
96*c217d954SCole Faust *
97*c217d954SCole Faust * @return a raw tensor for the specified image.
98*c217d954SCole Faust */
99*c217d954SCole Faust const RawTensor &get(const std::string &name) const;
100*c217d954SCole Faust
101*c217d954SCole Faust /** Provides a raw tensor for the specified image.
102*c217d954SCole Faust *
103*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
104*c217d954SCole Faust *
105*c217d954SCole Faust * @return a raw tensor for the specified image.
106*c217d954SCole Faust */
107*c217d954SCole Faust RawTensor get(const std::string &name);
108*c217d954SCole Faust
109*c217d954SCole Faust /** Creates an uninitialised raw tensor with the given @p data_type and @p
110*c217d954SCole Faust * num_channels. The shape is derived from the specified image.
111*c217d954SCole Faust *
112*c217d954SCole Faust * @param[in] name Image file used to initialise the tensor.
113*c217d954SCole Faust * @param[in] data_type Data type used to initialise the tensor.
114*c217d954SCole Faust * @param[in] num_channels Number of channels used to initialise the tensor.
115*c217d954SCole Faust *
116*c217d954SCole Faust * @return a raw tensor for the specified image.
117*c217d954SCole Faust */
118*c217d954SCole Faust RawTensor get(const std::string &name, DataType data_type, int num_channels = 1) const;
119*c217d954SCole Faust
120*c217d954SCole Faust /** Provides a contant raw tensor for the specified image after it has been
121*c217d954SCole Faust * converted to @p format.
122*c217d954SCole Faust *
123*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
124*c217d954SCole Faust * @param[in] format Format used to look up the raw tensor.
125*c217d954SCole Faust *
126*c217d954SCole Faust * @return a raw tensor for the specified image.
127*c217d954SCole Faust */
128*c217d954SCole Faust const RawTensor &get(const std::string &name, Format format) const;
129*c217d954SCole Faust
130*c217d954SCole Faust /** Provides a raw tensor for the specified image after it has been
131*c217d954SCole Faust * converted to @p format.
132*c217d954SCole Faust *
133*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
134*c217d954SCole Faust * @param[in] format Format used to look up the raw tensor.
135*c217d954SCole Faust *
136*c217d954SCole Faust * @return a raw tensor for the specified image.
137*c217d954SCole Faust */
138*c217d954SCole Faust RawTensor get(const std::string &name, Format format);
139*c217d954SCole Faust
140*c217d954SCole Faust /** Provides a contant raw tensor for the specified channel after it has
141*c217d954SCole Faust * been extracted form the given image.
142*c217d954SCole Faust *
143*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
144*c217d954SCole Faust * @param[in] channel Channel used to look up the raw tensor.
145*c217d954SCole Faust *
146*c217d954SCole Faust * @note The channel has to be unambiguous so that the format can be
147*c217d954SCole Faust * inferred automatically.
148*c217d954SCole Faust *
149*c217d954SCole Faust * @return a raw tensor for the specified image channel.
150*c217d954SCole Faust */
151*c217d954SCole Faust const RawTensor &get(const std::string &name, Channel channel) const;
152*c217d954SCole Faust
153*c217d954SCole Faust /** Provides a raw tensor for the specified channel after it has been
154*c217d954SCole Faust * extracted form the given image.
155*c217d954SCole Faust *
156*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
157*c217d954SCole Faust * @param[in] channel Channel used to look up the raw tensor.
158*c217d954SCole Faust *
159*c217d954SCole Faust * @note The channel has to be unambiguous so that the format can be
160*c217d954SCole Faust * inferred automatically.
161*c217d954SCole Faust *
162*c217d954SCole Faust * @return a raw tensor for the specified image channel.
163*c217d954SCole Faust */
164*c217d954SCole Faust RawTensor get(const std::string &name, Channel channel);
165*c217d954SCole Faust
166*c217d954SCole Faust /** Provides a constant raw tensor for the specified channel after it has
167*c217d954SCole Faust * been extracted form the given image formatted to @p format.
168*c217d954SCole Faust *
169*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
170*c217d954SCole Faust * @param[in] format Format used to look up the raw tensor.
171*c217d954SCole Faust * @param[in] channel Channel used to look up the raw tensor.
172*c217d954SCole Faust *
173*c217d954SCole Faust * @return a raw tensor for the specified image channel.
174*c217d954SCole Faust */
175*c217d954SCole Faust const RawTensor &get(const std::string &name, Format format, Channel channel) const;
176*c217d954SCole Faust
177*c217d954SCole Faust /** Provides a raw tensor for the specified channel after it has been
178*c217d954SCole Faust * extracted form the given image formatted to @p format.
179*c217d954SCole Faust *
180*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
181*c217d954SCole Faust * @param[in] format Format used to look up the raw tensor.
182*c217d954SCole Faust * @param[in] channel Channel used to look up the raw tensor.
183*c217d954SCole Faust *
184*c217d954SCole Faust * @return a raw tensor for the specified image channel.
185*c217d954SCole Faust */
186*c217d954SCole Faust RawTensor get(const std::string &name, Format format, Channel channel);
187*c217d954SCole Faust
188*c217d954SCole Faust /** Puts garbage values all around the tensor for testing purposes
189*c217d954SCole Faust *
190*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
191*c217d954SCole Faust * @param[in] distribution Distribution used to fill the tensor's surroundings.
192*c217d954SCole Faust * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
193*c217d954SCole Faust */
194*c217d954SCole Faust template <typename T, typename D>
195*c217d954SCole Faust void fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const;
196*c217d954SCole Faust
197*c217d954SCole Faust /** Fills the specified @p tensor with random values drawn from @p
198*c217d954SCole Faust * distribution.
199*c217d954SCole Faust *
200*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
201*c217d954SCole Faust * @param[in] distribution Distribution used to fill the tensor.
202*c217d954SCole Faust * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
203*c217d954SCole Faust *
204*c217d954SCole Faust * @note The @p distribution has to provide operator(Generator &) which
205*c217d954SCole Faust * will be used to draw samples.
206*c217d954SCole Faust */
207*c217d954SCole Faust template <typename T, typename D>
208*c217d954SCole Faust void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const;
209*c217d954SCole Faust
210*c217d954SCole Faust template <typename T, typename D>
211*c217d954SCole Faust void fill_boxes(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const;
212*c217d954SCole Faust
213*c217d954SCole Faust /** Fills the specified @p raw tensor with random values drawn from @p
214*c217d954SCole Faust * distribution.
215*c217d954SCole Faust *
216*c217d954SCole Faust * @param[in, out] vec To be filled vector.
217*c217d954SCole Faust * @param[in] distribution Distribution used to fill the tensor.
218*c217d954SCole Faust * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
219*c217d954SCole Faust *
220*c217d954SCole Faust * @note The @p distribution has to provide operator(Generator &) which
221*c217d954SCole Faust * will be used to draw samples.
222*c217d954SCole Faust */
223*c217d954SCole Faust template <typename T, typename D>
224*c217d954SCole Faust void fill(std::vector<T> &vec, D &&distribution, std::random_device::result_type seed_offset) const;
225*c217d954SCole Faust
226*c217d954SCole Faust /** Fills the specified @p raw tensor with random values drawn from @p
227*c217d954SCole Faust * distribution.
228*c217d954SCole Faust *
229*c217d954SCole Faust * @param[in, out] raw To be filled raw.
230*c217d954SCole Faust * @param[in] distribution Distribution used to fill the tensor.
231*c217d954SCole Faust * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
232*c217d954SCole Faust *
233*c217d954SCole Faust * @note The @p distribution has to provide operator(Generator &) which
234*c217d954SCole Faust * will be used to draw samples.
235*c217d954SCole Faust */
236*c217d954SCole Faust template <typename D>
237*c217d954SCole Faust void fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const;
238*c217d954SCole Faust
239*c217d954SCole Faust /** Fills the specified @p tensor with the content of the specified image
240*c217d954SCole Faust * converted to the given format.
241*c217d954SCole Faust *
242*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
243*c217d954SCole Faust * @param[in] name Image file used to fill the tensor.
244*c217d954SCole Faust * @param[in] format Format of the image used to fill the tensor.
245*c217d954SCole Faust *
246*c217d954SCole Faust * @warning No check is performed that the specified format actually
247*c217d954SCole Faust * matches the format of the tensor.
248*c217d954SCole Faust */
249*c217d954SCole Faust template <typename T>
250*c217d954SCole Faust void fill(T &&tensor, const std::string &name, Format format) const;
251*c217d954SCole Faust
252*c217d954SCole Faust /** Fills the raw tensor with the content of the specified image
253*c217d954SCole Faust * converted to the given format.
254*c217d954SCole Faust *
255*c217d954SCole Faust * @param[in, out] raw To be filled raw tensor.
256*c217d954SCole Faust * @param[in] name Image file used to fill the tensor.
257*c217d954SCole Faust * @param[in] format Format of the image used to fill the tensor.
258*c217d954SCole Faust *
259*c217d954SCole Faust * @warning No check is performed that the specified format actually
260*c217d954SCole Faust * matches the format of the tensor.
261*c217d954SCole Faust */
262*c217d954SCole Faust void fill(RawTensor &raw, const std::string &name, Format format) const;
263*c217d954SCole Faust
264*c217d954SCole Faust /** Fills the specified @p tensor with the content of the specified channel
265*c217d954SCole Faust * extracted from the given image.
266*c217d954SCole Faust *
267*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
268*c217d954SCole Faust * @param[in] name Image file used to fill the tensor.
269*c217d954SCole Faust * @param[in] channel Channel of the image used to fill the tensor.
270*c217d954SCole Faust *
271*c217d954SCole Faust * @note The channel has to be unambiguous so that the format can be
272*c217d954SCole Faust * inferred automatically.
273*c217d954SCole Faust *
274*c217d954SCole Faust * @warning No check is performed that the specified format actually
275*c217d954SCole Faust * matches the format of the tensor.
276*c217d954SCole Faust */
277*c217d954SCole Faust template <typename T>
278*c217d954SCole Faust void fill(T &&tensor, const std::string &name, Channel channel) const;
279*c217d954SCole Faust
280*c217d954SCole Faust /** Fills the raw tensor with the content of the specified channel
281*c217d954SCole Faust * extracted from the given image.
282*c217d954SCole Faust *
283*c217d954SCole Faust * @param[in, out] raw To be filled raw tensor.
284*c217d954SCole Faust * @param[in] name Image file used to fill the tensor.
285*c217d954SCole Faust * @param[in] channel Channel of the image used to fill the tensor.
286*c217d954SCole Faust *
287*c217d954SCole Faust * @note The channel has to be unambiguous so that the format can be
288*c217d954SCole Faust * inferred automatically.
289*c217d954SCole Faust *
290*c217d954SCole Faust * @warning No check is performed that the specified format actually
291*c217d954SCole Faust * matches the format of the tensor.
292*c217d954SCole Faust */
293*c217d954SCole Faust void fill(RawTensor &raw, const std::string &name, Channel channel) const;
294*c217d954SCole Faust
295*c217d954SCole Faust /** Fills the specified @p tensor with the content of the specified channel
296*c217d954SCole Faust * extracted from the given image after it has been converted to the given
297*c217d954SCole Faust * format.
298*c217d954SCole Faust *
299*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
300*c217d954SCole Faust * @param[in] name Image file used to fill the tensor.
301*c217d954SCole Faust * @param[in] format Format of the image used to fill the tensor.
302*c217d954SCole Faust * @param[in] channel Channel of the image used to fill the tensor.
303*c217d954SCole Faust *
304*c217d954SCole Faust * @warning No check is performed that the specified format actually
305*c217d954SCole Faust * matches the format of the tensor.
306*c217d954SCole Faust */
307*c217d954SCole Faust template <typename T>
308*c217d954SCole Faust void fill(T &&tensor, const std::string &name, Format format, Channel channel) const;
309*c217d954SCole Faust
310*c217d954SCole Faust /** Fills the raw tensor with the content of the specified channel
311*c217d954SCole Faust * extracted from the given image after it has been converted to the given
312*c217d954SCole Faust * format.
313*c217d954SCole Faust *
314*c217d954SCole Faust * @param[in, out] raw To be filled raw tensor.
315*c217d954SCole Faust * @param[in] name Image file used to fill the tensor.
316*c217d954SCole Faust * @param[in] format Format of the image used to fill the tensor.
317*c217d954SCole Faust * @param[in] channel Channel of the image used to fill the tensor.
318*c217d954SCole Faust *
319*c217d954SCole Faust * @warning No check is performed that the specified format actually
320*c217d954SCole Faust * matches the format of the tensor.
321*c217d954SCole Faust */
322*c217d954SCole Faust void fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const;
323*c217d954SCole Faust
324*c217d954SCole Faust /** Fills the specified @p tensor with the content of the raw tensor.
325*c217d954SCole Faust *
326*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
327*c217d954SCole Faust * @param[in] raw Raw tensor used to fill the tensor.
328*c217d954SCole Faust *
329*c217d954SCole Faust * @warning No check is performed that the specified format actually
330*c217d954SCole Faust * matches the format of the tensor.
331*c217d954SCole Faust */
332*c217d954SCole Faust template <typename T>
333*c217d954SCole Faust void fill(T &&tensor, RawTensor raw) const;
334*c217d954SCole Faust
335*c217d954SCole Faust /** Fill a tensor with uniform distribution
336*c217d954SCole Faust *
337*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
338*c217d954SCole Faust * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
339*c217d954SCole Faust */
340*c217d954SCole Faust template <typename T>
341*c217d954SCole Faust void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const;
342*c217d954SCole Faust
343*c217d954SCole Faust /** Fill a tensor with uniform distribution
344*c217d954SCole Faust *
345*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
346*c217d954SCole Faust * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
347*c217d954SCole Faust * @param[in] low lowest value in the range (inclusive)
348*c217d954SCole Faust * @param[in] high highest value in the range (inclusive)
349*c217d954SCole Faust *
350*c217d954SCole Faust * @note @p low and @p high must be of the same type as the data type of @p tensor
351*c217d954SCole Faust */
352*c217d954SCole Faust template <typename T, typename D>
353*c217d954SCole Faust void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const;
354*c217d954SCole Faust
355*c217d954SCole Faust /** Fill a tensor with uniform distribution across the specified range
356*c217d954SCole Faust *
357*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
358*c217d954SCole Faust * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator.
359*c217d954SCole Faust * @param[in] excluded_range_pairs Ranges to exclude from the generator
360*c217d954SCole Faust */
361*c217d954SCole Faust template <typename T>
362*c217d954SCole Faust void fill_tensor_uniform_ranged(T &&tensor,
363*c217d954SCole Faust std::random_device::result_type seed_offset,
364*c217d954SCole Faust const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs) const;
365*c217d954SCole Faust
366*c217d954SCole Faust /** Fills the specified @p tensor with data loaded from .npy (numpy binary) in specified path.
367*c217d954SCole Faust *
368*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
369*c217d954SCole Faust * @param[in] name Data file.
370*c217d954SCole Faust *
371*c217d954SCole Faust * @note The numpy array stored in the binary .npy file must be row-major in the sense that it
372*c217d954SCole Faust * must store elements within a row consecutively in the memory, then rows within a 2D slice,
373*c217d954SCole Faust * then 2D slices within a 3D slice and so on. Note that it imposes no restrictions on what
374*c217d954SCole Faust * indexing convention is used in the numpy array. That is, the numpy array can be either fortran
375*c217d954SCole Faust * style or C style as long as it adheres to the rule above.
376*c217d954SCole Faust *
377*c217d954SCole Faust * More concretely, the orders of dimensions for each style are as follows:
378*c217d954SCole Faust * C-style (numpy default):
379*c217d954SCole Faust * array[HigherDims..., Z, Y, X]
380*c217d954SCole Faust * Fortran style:
381*c217d954SCole Faust * array[X, Y, Z, HigherDims...]
382*c217d954SCole Faust */
383*c217d954SCole Faust template <typename T>
384*c217d954SCole Faust void fill_layer_data(T &&tensor, std::string name) const;
385*c217d954SCole Faust
386*c217d954SCole Faust /** Fill a tensor with a constant value
387*c217d954SCole Faust *
388*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
389*c217d954SCole Faust * @param[in] value Value to be assigned to all elements of the input tensor.
390*c217d954SCole Faust *
391*c217d954SCole Faust * @note @p value must be of the same type as the data type of @p tensor
392*c217d954SCole Faust */
393*c217d954SCole Faust template <typename T, typename D>
394*c217d954SCole Faust void fill_tensor_value(T &&tensor, D value) const;
395*c217d954SCole Faust
396*c217d954SCole Faust /** Fill a tensor with a given vector with static values.
397*c217d954SCole Faust *
398*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
399*c217d954SCole Faust * @param[in] values A vector containing values
400*c217d954SCole Faust *
401*c217d954SCole Faust * To cope with various size tensors, the vector size doens't have to be
402*c217d954SCole Faust * the same as tensor's size. If the size of the tensor is larger than the vector,
403*c217d954SCole Faust * the iterator the vector will keep iterating and wrap around. If the vector is
404*c217d954SCole Faust * larger, values located after the required size won't be used.
405*c217d954SCole Faust */
406*c217d954SCole Faust template <typename T, typename DataType>
407*c217d954SCole Faust void fill_static_values(T &&tensor, const std::vector<DataType> &values) const;
408*c217d954SCole Faust
409*c217d954SCole Faust // Function type to generate a number to fill tensors.
410*c217d954SCole Faust template <typename ResultType>
411*c217d954SCole Faust using GeneratorFunctionType = std::function<ResultType(void)>;
412*c217d954SCole Faust /** Fill a tensor with a value generator function.
413*c217d954SCole Faust *
414*c217d954SCole Faust * @param[in, out] tensor To be filled tensor.
415*c217d954SCole Faust * @param[in] generate_value A function that generates values.
416*c217d954SCole Faust */
417*c217d954SCole Faust template <typename T, typename ResultType>
418*c217d954SCole Faust void fill_with_generator(T &&tensor, const GeneratorFunctionType<ResultType> &generate_value) const;
419*c217d954SCole Faust
420*c217d954SCole Faust private:
421*c217d954SCole Faust // Function prototype to convert between image formats.
422*c217d954SCole Faust using Converter = void (*)(const RawTensor &src, RawTensor &dst);
423*c217d954SCole Faust // Function prototype to extract a channel from an image.
424*c217d954SCole Faust using Extractor = void (*)(const RawTensor &src, RawTensor &dst);
425*c217d954SCole Faust // Function prototype to load an image file.
426*c217d954SCole Faust using Loader = RawTensor (*)(const std::string &path);
427*c217d954SCole Faust
428*c217d954SCole Faust const Converter &get_converter(Format src, Format dst) const;
429*c217d954SCole Faust const Converter &get_converter(DataType src, Format dst) const;
430*c217d954SCole Faust const Converter &get_converter(Format src, DataType dst) const;
431*c217d954SCole Faust const Converter &get_converter(DataType src, DataType dst) const;
432*c217d954SCole Faust const Extractor &get_extractor(Format format, Channel) const;
433*c217d954SCole Faust const Loader &get_loader(const std::string &extension) const;
434*c217d954SCole Faust
435*c217d954SCole Faust /** Creates a raw tensor from the specified image.
436*c217d954SCole Faust *
437*c217d954SCole Faust * @param[in] name To be loaded image file.
438*c217d954SCole Faust *
439*c217d954SCole Faust * @note If use_single_image is true @p name is ignored and the user image
440*c217d954SCole Faust * is loaded instead.
441*c217d954SCole Faust */
442*c217d954SCole Faust RawTensor load_image(const std::string &name) const;
443*c217d954SCole Faust
444*c217d954SCole Faust /** Provides a raw tensor for the specified image and format.
445*c217d954SCole Faust *
446*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
447*c217d954SCole Faust * @param[in] format Format used to look up the raw tensor.
448*c217d954SCole Faust *
449*c217d954SCole Faust * If the tensor has already been requested before the cached version will
450*c217d954SCole Faust * be returned. Otherwise the tensor will be added to the cache.
451*c217d954SCole Faust *
452*c217d954SCole Faust * @note If use_single_image is true @p name is ignored and the user image
453*c217d954SCole Faust * is loaded instead.
454*c217d954SCole Faust */
455*c217d954SCole Faust const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format) const;
456*c217d954SCole Faust
457*c217d954SCole Faust /** Provides a raw tensor for the specified image, format and channel.
458*c217d954SCole Faust *
459*c217d954SCole Faust * @param[in] name Image file used to look up the raw tensor.
460*c217d954SCole Faust * @param[in] format Format used to look up the raw tensor.
461*c217d954SCole Faust * @param[in] channel Channel used to look up the raw tensor.
462*c217d954SCole Faust *
463*c217d954SCole Faust * If the tensor has already been requested before the cached version will
464*c217d954SCole Faust * be returned. Otherwise the tensor will be added to the cache.
465*c217d954SCole Faust *
466*c217d954SCole Faust * @note If use_single_image is true @p name is ignored and the user image
467*c217d954SCole Faust * is loaded instead.
468*c217d954SCole Faust */
469*c217d954SCole Faust const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const;
470*c217d954SCole Faust
471*c217d954SCole Faust mutable TensorCache _cache{};
472*c217d954SCole Faust mutable arm_compute::Mutex _format_lock{};
473*c217d954SCole Faust mutable arm_compute::Mutex _channel_lock{};
474*c217d954SCole Faust const std::string _library_path;
475*c217d954SCole Faust std::random_device::result_type _seed;
476*c217d954SCole Faust };
477*c217d954SCole Faust
478*c217d954SCole Faust namespace detail
479*c217d954SCole Faust {
480*c217d954SCole Faust template <typename T>
convert_range_pair(const std::vector<AssetsLibrary::RangePair> & excluded_range_pairs)481*c217d954SCole Faust inline std::vector<std::pair<T, T>> convert_range_pair(const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs)
482*c217d954SCole Faust {
483*c217d954SCole Faust std::vector<std::pair<T, T>> converted;
484*c217d954SCole Faust std::transform(excluded_range_pairs.begin(),
485*c217d954SCole Faust excluded_range_pairs.end(),
486*c217d954SCole Faust std::back_inserter(converted),
487*c217d954SCole Faust [](const AssetsLibrary::RangePair & p)
488*c217d954SCole Faust {
489*c217d954SCole Faust return std::pair<T, T>(static_cast<T>(p.first), static_cast<T>(p.second));
490*c217d954SCole Faust });
491*c217d954SCole Faust return converted;
492*c217d954SCole Faust }
493*c217d954SCole Faust
494*c217d954SCole Faust /* Read npy header and check the payload is suitable for the specified type and shape
495*c217d954SCole Faust *
496*c217d954SCole Faust * @param[in] stream ifstream of the npy file
497*c217d954SCole Faust * @param[in] expect_typestr Expected typestr
498*c217d954SCole Faust * @param[in] expect_shape Shape of tensor expected to receive the data
499*c217d954SCole Faust *
500*c217d954SCole Faust * @note Advances stream to the beginning of the data payload
501*c217d954SCole Faust */
502*c217d954SCole Faust void validate_npy_header(std::ifstream &stream, const std::string &expect_typestr, const TensorShape &expect_shape);
503*c217d954SCole Faust } // namespace detail
504*c217d954SCole Faust
505*c217d954SCole Faust template <typename T, typename D>
fill_borders_with_garbage(T && tensor,D && distribution,std::random_device::result_type seed_offset)506*c217d954SCole Faust void AssetsLibrary::fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
507*c217d954SCole Faust {
508*c217d954SCole Faust const PaddingSize padding_size = tensor.padding();
509*c217d954SCole Faust
510*c217d954SCole Faust Window window;
511*c217d954SCole Faust window.set(0, Window::Dimension(-padding_size.left, tensor.shape()[0] + padding_size.right, 1));
512*c217d954SCole Faust if(tensor.shape().num_dimensions() > 1)
513*c217d954SCole Faust {
514*c217d954SCole Faust window.set(1, Window::Dimension(-padding_size.top, tensor.shape()[1] + padding_size.bottom, 1));
515*c217d954SCole Faust }
516*c217d954SCole Faust
517*c217d954SCole Faust std::mt19937 gen(_seed + seed_offset);
518*c217d954SCole Faust
519*c217d954SCole Faust execute_window_loop(window, [&](const Coordinates & id)
520*c217d954SCole Faust {
521*c217d954SCole Faust TensorShape shape = tensor.shape();
522*c217d954SCole Faust
523*c217d954SCole Faust // If outside of valid region
524*c217d954SCole Faust if(id.x() < 0 || id.x() >= static_cast<int>(shape.x()) || id.y() < 0 || id.y() >= static_cast<int>(shape.y()))
525*c217d954SCole Faust {
526*c217d954SCole Faust using ResultType = typename std::remove_reference<D>::type::result_type;
527*c217d954SCole Faust const ResultType value = distribution(gen);
528*c217d954SCole Faust void *const out_ptr = tensor(id);
529*c217d954SCole Faust store_value_with_data_type(out_ptr, value, tensor.data_type());
530*c217d954SCole Faust }
531*c217d954SCole Faust });
532*c217d954SCole Faust }
533*c217d954SCole Faust
534*c217d954SCole Faust template <typename T, typename D>
fill_boxes(T && tensor,D && distribution,std::random_device::result_type seed_offset)535*c217d954SCole Faust void AssetsLibrary::fill_boxes(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
536*c217d954SCole Faust {
537*c217d954SCole Faust using DistributionType = typename std::remove_reference<D>::type;
538*c217d954SCole Faust using ResultType = typename DistributionType::result_type;
539*c217d954SCole Faust
540*c217d954SCole Faust std::mt19937 gen(_seed + seed_offset);
541*c217d954SCole Faust TensorShape shape(tensor.shape());
542*c217d954SCole Faust const uint32_t num_boxes = tensor.num_elements() / 4;
543*c217d954SCole Faust // Iterate over all elements
544*c217d954SCole Faust DistributionType size_dist{ ResultType(0.f), ResultType(1.f) };
545*c217d954SCole Faust for(uint32_t element_idx = 0; element_idx < num_boxes * 4; element_idx += 4)
546*c217d954SCole Faust {
547*c217d954SCole Faust const ResultType delta = size_dist(gen);
548*c217d954SCole Faust const ResultType epsilon = size_dist(gen);
549*c217d954SCole Faust const ResultType left = distribution(gen);
550*c217d954SCole Faust const ResultType top = distribution(gen);
551*c217d954SCole Faust const ResultType right = left + delta;
552*c217d954SCole Faust const ResultType bottom = top + epsilon;
553*c217d954SCole Faust const std::tuple<ResultType, ResultType, ResultType, ResultType> box(left, top, right, bottom);
554*c217d954SCole Faust Coordinates x1 = index2coord(shape, element_idx);
555*c217d954SCole Faust Coordinates y1 = index2coord(shape, element_idx + 1);
556*c217d954SCole Faust Coordinates x2 = index2coord(shape, element_idx + 2);
557*c217d954SCole Faust Coordinates y2 = index2coord(shape, element_idx + 3);
558*c217d954SCole Faust ResultType &target_value_x1 = reinterpret_cast<ResultType *>(tensor(x1))[0];
559*c217d954SCole Faust ResultType &target_value_y1 = reinterpret_cast<ResultType *>(tensor(y1))[0];
560*c217d954SCole Faust ResultType &target_value_x2 = reinterpret_cast<ResultType *>(tensor(x2))[0];
561*c217d954SCole Faust ResultType &target_value_y2 = reinterpret_cast<ResultType *>(tensor(y2))[0];
562*c217d954SCole Faust store_value_with_data_type(&target_value_x1, std::get<0>(box), tensor.data_type());
563*c217d954SCole Faust store_value_with_data_type(&target_value_y1, std::get<1>(box), tensor.data_type());
564*c217d954SCole Faust store_value_with_data_type(&target_value_x2, std::get<2>(box), tensor.data_type());
565*c217d954SCole Faust store_value_with_data_type(&target_value_y2, std::get<3>(box), tensor.data_type());
566*c217d954SCole Faust }
567*c217d954SCole Faust fill_borders_with_garbage(tensor, distribution, seed_offset);
568*c217d954SCole Faust }
569*c217d954SCole Faust
570*c217d954SCole Faust template <typename T, typename D>
fill(std::vector<T> & vec,D && distribution,std::random_device::result_type seed_offset)571*c217d954SCole Faust void AssetsLibrary::fill(std::vector<T> &vec, D &&distribution, std::random_device::result_type seed_offset) const
572*c217d954SCole Faust {
573*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(vec.empty(), "Vector must not be empty");
574*c217d954SCole Faust
575*c217d954SCole Faust using ResultType = typename std::remove_reference<D>::type::result_type;
576*c217d954SCole Faust
577*c217d954SCole Faust std::mt19937 gen(_seed + seed_offset);
578*c217d954SCole Faust for(size_t i = 0; i < vec.size(); ++i)
579*c217d954SCole Faust {
580*c217d954SCole Faust const ResultType value = distribution(gen);
581*c217d954SCole Faust
582*c217d954SCole Faust vec[i] = value;
583*c217d954SCole Faust }
584*c217d954SCole Faust }
585*c217d954SCole Faust
586*c217d954SCole Faust template <typename T, typename ResultType>
fill_with_generator(T && tensor,const GeneratorFunctionType<ResultType> & generate_value)587*c217d954SCole Faust void AssetsLibrary::fill_with_generator(T &&tensor, const GeneratorFunctionType<ResultType> &generate_value) const
588*c217d954SCole Faust {
589*c217d954SCole Faust const bool is_nhwc = tensor.data_layout() == DataLayout::NHWC;
590*c217d954SCole Faust TensorShape shape(tensor.shape());
591*c217d954SCole Faust
592*c217d954SCole Faust if(is_nhwc)
593*c217d954SCole Faust {
594*c217d954SCole Faust // Ensure that the equivalent tensors will be filled for both data layouts
595*c217d954SCole Faust permute(shape, PermutationVector(1U, 2U, 0U));
596*c217d954SCole Faust }
597*c217d954SCole Faust
598*c217d954SCole Faust // Iterate over all elements
599*c217d954SCole Faust const uint32_t num_elements = tensor.num_elements();
600*c217d954SCole Faust for(uint32_t element_idx = 0; element_idx < num_elements; ++element_idx)
601*c217d954SCole Faust {
602*c217d954SCole Faust Coordinates id = index2coord(shape, element_idx);
603*c217d954SCole Faust
604*c217d954SCole Faust if(is_nhwc)
605*c217d954SCole Faust {
606*c217d954SCole Faust // Write in the correct id for permuted shapes
607*c217d954SCole Faust permute(id, PermutationVector(2U, 0U, 1U));
608*c217d954SCole Faust }
609*c217d954SCole Faust
610*c217d954SCole Faust // Iterate over all channels
611*c217d954SCole Faust for(int channel = 0; channel < tensor.num_channels(); ++channel)
612*c217d954SCole Faust {
613*c217d954SCole Faust const ResultType value = generate_value();
614*c217d954SCole Faust ResultType &target_value = reinterpret_cast<ResultType *>(tensor(id))[channel];
615*c217d954SCole Faust
616*c217d954SCole Faust store_value_with_data_type(&target_value, value, tensor.data_type());
617*c217d954SCole Faust }
618*c217d954SCole Faust }
619*c217d954SCole Faust }
620*c217d954SCole Faust
621*c217d954SCole Faust template <typename T, typename D>
fill(T && tensor,D && distribution,std::random_device::result_type seed_offset)622*c217d954SCole Faust void AssetsLibrary::fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
623*c217d954SCole Faust {
624*c217d954SCole Faust using ResultType = typename std::remove_reference<D>::type::result_type;
625*c217d954SCole Faust std::mt19937 gen(_seed + seed_offset);
626*c217d954SCole Faust
627*c217d954SCole Faust GeneratorFunctionType<ResultType> number_generator = [&]()
628*c217d954SCole Faust {
629*c217d954SCole Faust const ResultType value = distribution(gen);
630*c217d954SCole Faust return value;
631*c217d954SCole Faust };
632*c217d954SCole Faust
633*c217d954SCole Faust fill_with_generator(tensor, number_generator);
634*c217d954SCole Faust fill_borders_with_garbage(tensor, distribution, seed_offset);
635*c217d954SCole Faust }
636*c217d954SCole Faust
637*c217d954SCole Faust template <typename T, typename DataType>
fill_static_values(T && tensor,const std::vector<DataType> & values)638*c217d954SCole Faust void AssetsLibrary::fill_static_values(T &&tensor, const std::vector<DataType> &values) const
639*c217d954SCole Faust {
640*c217d954SCole Faust auto it = values.begin();
641*c217d954SCole Faust GeneratorFunctionType<DataType> get_next_value = [&]()
642*c217d954SCole Faust {
643*c217d954SCole Faust const DataType value = *it;
644*c217d954SCole Faust ++it;
645*c217d954SCole Faust
646*c217d954SCole Faust if(it == values.end())
647*c217d954SCole Faust {
648*c217d954SCole Faust it = values.begin();
649*c217d954SCole Faust }
650*c217d954SCole Faust
651*c217d954SCole Faust return value;
652*c217d954SCole Faust };
653*c217d954SCole Faust
654*c217d954SCole Faust fill_with_generator(tensor, get_next_value);
655*c217d954SCole Faust }
656*c217d954SCole Faust
657*c217d954SCole Faust template <typename D>
fill(RawTensor & raw,D && distribution,std::random_device::result_type seed_offset)658*c217d954SCole Faust void AssetsLibrary::fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const
659*c217d954SCole Faust {
660*c217d954SCole Faust std::mt19937 gen(_seed + seed_offset);
661*c217d954SCole Faust
662*c217d954SCole Faust for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
663*c217d954SCole Faust {
664*c217d954SCole Faust using ResultType = typename std::remove_reference<D>::type::result_type;
665*c217d954SCole Faust const ResultType value = distribution(gen);
666*c217d954SCole Faust
667*c217d954SCole Faust store_value_with_data_type(raw.data() + offset, value, raw.data_type());
668*c217d954SCole Faust }
669*c217d954SCole Faust }
670*c217d954SCole Faust
671*c217d954SCole Faust template <typename T>
fill(T && tensor,const std::string & name,Format format)672*c217d954SCole Faust void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format) const
673*c217d954SCole Faust {
674*c217d954SCole Faust const RawTensor &raw = get(name, format);
675*c217d954SCole Faust
676*c217d954SCole Faust for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
677*c217d954SCole Faust {
678*c217d954SCole Faust const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
679*c217d954SCole Faust
680*c217d954SCole Faust const RawTensor::value_type *const raw_ptr = raw.data() + offset;
681*c217d954SCole Faust const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
682*c217d954SCole Faust std::copy_n(raw_ptr, raw.element_size(), out_ptr);
683*c217d954SCole Faust }
684*c217d954SCole Faust }
685*c217d954SCole Faust
686*c217d954SCole Faust template <typename T>
fill(T && tensor,const std::string & name,Channel channel)687*c217d954SCole Faust void AssetsLibrary::fill(T &&tensor, const std::string &name, Channel channel) const
688*c217d954SCole Faust {
689*c217d954SCole Faust fill(std::forward<T>(tensor), name, get_format_for_channel(channel), channel);
690*c217d954SCole Faust }
691*c217d954SCole Faust
692*c217d954SCole Faust template <typename T>
fill(T && tensor,const std::string & name,Format format,Channel channel)693*c217d954SCole Faust void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format, Channel channel) const
694*c217d954SCole Faust {
695*c217d954SCole Faust const RawTensor &raw = get(name, format, channel);
696*c217d954SCole Faust
697*c217d954SCole Faust for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
698*c217d954SCole Faust {
699*c217d954SCole Faust const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
700*c217d954SCole Faust
701*c217d954SCole Faust const RawTensor::value_type *const raw_ptr = raw.data() + offset;
702*c217d954SCole Faust const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
703*c217d954SCole Faust std::copy_n(raw_ptr, raw.element_size(), out_ptr);
704*c217d954SCole Faust }
705*c217d954SCole Faust }
706*c217d954SCole Faust
707*c217d954SCole Faust template <typename T>
fill(T && tensor,RawTensor raw)708*c217d954SCole Faust void AssetsLibrary::fill(T &&tensor, RawTensor raw) const
709*c217d954SCole Faust {
710*c217d954SCole Faust for(size_t offset = 0; offset < raw.size(); offset += raw.element_size())
711*c217d954SCole Faust {
712*c217d954SCole Faust const Coordinates id = index2coord(raw.shape(), offset / raw.element_size());
713*c217d954SCole Faust
714*c217d954SCole Faust const RawTensor::value_type *const raw_ptr = raw.data() + offset;
715*c217d954SCole Faust const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id));
716*c217d954SCole Faust std::copy_n(raw_ptr, raw.element_size(), out_ptr);
717*c217d954SCole Faust }
718*c217d954SCole Faust }
719*c217d954SCole Faust
720*c217d954SCole Faust template <typename T>
fill_tensor_uniform(T && tensor,std::random_device::result_type seed_offset)721*c217d954SCole Faust void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const
722*c217d954SCole Faust {
723*c217d954SCole Faust switch(tensor.data_type())
724*c217d954SCole Faust {
725*c217d954SCole Faust case DataType::U8:
726*c217d954SCole Faust case DataType::QASYMM8:
727*c217d954SCole Faust {
728*c217d954SCole Faust std::uniform_int_distribution<unsigned int> distribution_u8(std::numeric_limits<uint8_t>::lowest(), std::numeric_limits<uint8_t>::max());
729*c217d954SCole Faust fill(tensor, distribution_u8, seed_offset);
730*c217d954SCole Faust break;
731*c217d954SCole Faust }
732*c217d954SCole Faust case DataType::S8:
733*c217d954SCole Faust case DataType::QSYMM8:
734*c217d954SCole Faust case DataType::QSYMM8_PER_CHANNEL:
735*c217d954SCole Faust case DataType::QASYMM8_SIGNED:
736*c217d954SCole Faust {
737*c217d954SCole Faust std::uniform_int_distribution<int> distribution_s8(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max());
738*c217d954SCole Faust fill(tensor, distribution_s8, seed_offset);
739*c217d954SCole Faust break;
740*c217d954SCole Faust }
741*c217d954SCole Faust case DataType::U16:
742*c217d954SCole Faust {
743*c217d954SCole Faust std::uniform_int_distribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), std::numeric_limits<uint16_t>::max());
744*c217d954SCole Faust fill(tensor, distribution_u16, seed_offset);
745*c217d954SCole Faust break;
746*c217d954SCole Faust }
747*c217d954SCole Faust case DataType::S16:
748*c217d954SCole Faust case DataType::QSYMM16:
749*c217d954SCole Faust {
750*c217d954SCole Faust std::uniform_int_distribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), std::numeric_limits<int16_t>::max());
751*c217d954SCole Faust fill(tensor, distribution_s16, seed_offset);
752*c217d954SCole Faust break;
753*c217d954SCole Faust }
754*c217d954SCole Faust case DataType::U32:
755*c217d954SCole Faust {
756*c217d954SCole Faust std::uniform_int_distribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max());
757*c217d954SCole Faust fill(tensor, distribution_u32, seed_offset);
758*c217d954SCole Faust break;
759*c217d954SCole Faust }
760*c217d954SCole Faust case DataType::S32:
761*c217d954SCole Faust {
762*c217d954SCole Faust std::uniform_int_distribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max());
763*c217d954SCole Faust fill(tensor, distribution_s32, seed_offset);
764*c217d954SCole Faust break;
765*c217d954SCole Faust }
766*c217d954SCole Faust case DataType::U64:
767*c217d954SCole Faust {
768*c217d954SCole Faust std::uniform_int_distribution<uint64_t> distribution_u64(std::numeric_limits<uint64_t>::lowest(), std::numeric_limits<uint64_t>::max());
769*c217d954SCole Faust fill(tensor, distribution_u64, seed_offset);
770*c217d954SCole Faust break;
771*c217d954SCole Faust }
772*c217d954SCole Faust case DataType::S64:
773*c217d954SCole Faust {
774*c217d954SCole Faust std::uniform_int_distribution<int64_t> distribution_s64(std::numeric_limits<int64_t>::lowest(), std::numeric_limits<int64_t>::max());
775*c217d954SCole Faust fill(tensor, distribution_s64, seed_offset);
776*c217d954SCole Faust break;
777*c217d954SCole Faust }
778*c217d954SCole Faust case DataType::BFLOAT16:
779*c217d954SCole Faust {
780*c217d954SCole Faust // It doesn't make sense to check [-inf, inf], so hard code it to a big number
781*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<bfloat16> distribution_bf16{ -1000.f, 1000.f };
782*c217d954SCole Faust fill(tensor, distribution_bf16, seed_offset);
783*c217d954SCole Faust break;
784*c217d954SCole Faust }
785*c217d954SCole Faust case DataType::F16:
786*c217d954SCole Faust {
787*c217d954SCole Faust // It doesn't make sense to check [-inf, inf], so hard code it to a big number
788*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<half> distribution_f16{ -100.f, 100.f };
789*c217d954SCole Faust fill(tensor, distribution_f16, seed_offset);
790*c217d954SCole Faust break;
791*c217d954SCole Faust }
792*c217d954SCole Faust case DataType::F32:
793*c217d954SCole Faust {
794*c217d954SCole Faust // It doesn't make sense to check [-inf, inf], so hard code it to a big number
795*c217d954SCole Faust std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f);
796*c217d954SCole Faust fill(tensor, distribution_f32, seed_offset);
797*c217d954SCole Faust break;
798*c217d954SCole Faust }
799*c217d954SCole Faust case DataType::F64:
800*c217d954SCole Faust {
801*c217d954SCole Faust // It doesn't make sense to check [-inf, inf], so hard code it to a big number
802*c217d954SCole Faust std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f);
803*c217d954SCole Faust fill(tensor, distribution_f64, seed_offset);
804*c217d954SCole Faust break;
805*c217d954SCole Faust }
806*c217d954SCole Faust case DataType::SIZET:
807*c217d954SCole Faust {
808*c217d954SCole Faust std::uniform_int_distribution<size_t> distribution_sizet(std::numeric_limits<size_t>::lowest(), std::numeric_limits<size_t>::max());
809*c217d954SCole Faust fill(tensor, distribution_sizet, seed_offset);
810*c217d954SCole Faust break;
811*c217d954SCole Faust }
812*c217d954SCole Faust default:
813*c217d954SCole Faust ARM_COMPUTE_ERROR("NOT SUPPORTED!");
814*c217d954SCole Faust }
815*c217d954SCole Faust }
816*c217d954SCole Faust
817*c217d954SCole Faust template <typename T>
fill_tensor_uniform_ranged(T && tensor,std::random_device::result_type seed_offset,const std::vector<AssetsLibrary::RangePair> & excluded_range_pairs)818*c217d954SCole Faust void AssetsLibrary::fill_tensor_uniform_ranged(T &&tensor,
819*c217d954SCole Faust std::random_device::result_type seed_offset,
820*c217d954SCole Faust const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs) const
821*c217d954SCole Faust {
822*c217d954SCole Faust using namespace arm_compute::utils::random;
823*c217d954SCole Faust
824*c217d954SCole Faust switch(tensor.data_type())
825*c217d954SCole Faust {
826*c217d954SCole Faust case DataType::U8:
827*c217d954SCole Faust case DataType::QASYMM8:
828*c217d954SCole Faust {
829*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<uint32_t>(excluded_range_pairs);
830*c217d954SCole Faust RangedUniformDistribution<uint32_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(),
831*c217d954SCole Faust std::numeric_limits<uint8_t>::max(),
832*c217d954SCole Faust converted_pairs);
833*c217d954SCole Faust fill(tensor, distribution_u8, seed_offset);
834*c217d954SCole Faust break;
835*c217d954SCole Faust }
836*c217d954SCole Faust case DataType::S8:
837*c217d954SCole Faust case DataType::QSYMM8:
838*c217d954SCole Faust {
839*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<int32_t>(excluded_range_pairs);
840*c217d954SCole Faust RangedUniformDistribution<int32_t> distribution_s8(std::numeric_limits<int8_t>::lowest(),
841*c217d954SCole Faust std::numeric_limits<int8_t>::max(),
842*c217d954SCole Faust converted_pairs);
843*c217d954SCole Faust fill(tensor, distribution_s8, seed_offset);
844*c217d954SCole Faust break;
845*c217d954SCole Faust }
846*c217d954SCole Faust case DataType::U16:
847*c217d954SCole Faust {
848*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<uint16_t>(excluded_range_pairs);
849*c217d954SCole Faust RangedUniformDistribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(),
850*c217d954SCole Faust std::numeric_limits<uint16_t>::max(),
851*c217d954SCole Faust converted_pairs);
852*c217d954SCole Faust fill(tensor, distribution_u16, seed_offset);
853*c217d954SCole Faust break;
854*c217d954SCole Faust }
855*c217d954SCole Faust case DataType::S16:
856*c217d954SCole Faust case DataType::QSYMM16:
857*c217d954SCole Faust {
858*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<int16_t>(excluded_range_pairs);
859*c217d954SCole Faust RangedUniformDistribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(),
860*c217d954SCole Faust std::numeric_limits<int16_t>::max(),
861*c217d954SCole Faust converted_pairs);
862*c217d954SCole Faust fill(tensor, distribution_s16, seed_offset);
863*c217d954SCole Faust break;
864*c217d954SCole Faust }
865*c217d954SCole Faust case DataType::U32:
866*c217d954SCole Faust {
867*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<uint32_t>(excluded_range_pairs);
868*c217d954SCole Faust RangedUniformDistribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(),
869*c217d954SCole Faust std::numeric_limits<uint32_t>::max(),
870*c217d954SCole Faust converted_pairs);
871*c217d954SCole Faust fill(tensor, distribution_u32, seed_offset);
872*c217d954SCole Faust break;
873*c217d954SCole Faust }
874*c217d954SCole Faust case DataType::S32:
875*c217d954SCole Faust {
876*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<int32_t>(excluded_range_pairs);
877*c217d954SCole Faust RangedUniformDistribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(),
878*c217d954SCole Faust std::numeric_limits<int32_t>::max(),
879*c217d954SCole Faust converted_pairs);
880*c217d954SCole Faust fill(tensor, distribution_s32, seed_offset);
881*c217d954SCole Faust break;
882*c217d954SCole Faust }
883*c217d954SCole Faust case DataType::BFLOAT16:
884*c217d954SCole Faust {
885*c217d954SCole Faust // It doesn't make sense to check [-inf, inf], so hard code it to a big number
886*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<bfloat16>(excluded_range_pairs);
887*c217d954SCole Faust RangedUniformDistribution<bfloat16> distribution_bf16(bfloat16(-1000.f), bfloat16(1000.f), converted_pairs);
888*c217d954SCole Faust fill(tensor, distribution_bf16, seed_offset);
889*c217d954SCole Faust break;
890*c217d954SCole Faust }
891*c217d954SCole Faust case DataType::F16:
892*c217d954SCole Faust {
893*c217d954SCole Faust // It doesn't make sense to check [-inf, inf], so hard code it to a big number
894*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<half>(excluded_range_pairs);
895*c217d954SCole Faust RangedUniformDistribution<half> distribution_f16(half(-100.f), half(100.f), converted_pairs);
896*c217d954SCole Faust fill(tensor, distribution_f16, seed_offset);
897*c217d954SCole Faust break;
898*c217d954SCole Faust }
899*c217d954SCole Faust case DataType::F32:
900*c217d954SCole Faust {
901*c217d954SCole Faust // It doesn't make sense to check [-inf, inf], so hard code it to a big number
902*c217d954SCole Faust const auto converted_pairs = detail::convert_range_pair<float>(excluded_range_pairs);
903*c217d954SCole Faust RangedUniformDistribution<float> distribution_f32(-1000.f, 1000.f, converted_pairs);
904*c217d954SCole Faust fill(tensor, distribution_f32, seed_offset);
905*c217d954SCole Faust break;
906*c217d954SCole Faust }
907*c217d954SCole Faust default:
908*c217d954SCole Faust ARM_COMPUTE_ERROR("NOT SUPPORTED!");
909*c217d954SCole Faust }
910*c217d954SCole Faust }
911*c217d954SCole Faust
912*c217d954SCole Faust template <typename T, typename D>
fill_tensor_uniform(T && tensor,std::random_device::result_type seed_offset,D low,D high)913*c217d954SCole Faust void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const
914*c217d954SCole Faust {
915*c217d954SCole Faust switch(tensor.data_type())
916*c217d954SCole Faust {
917*c217d954SCole Faust case DataType::U8:
918*c217d954SCole Faust case DataType::QASYMM8:
919*c217d954SCole Faust {
920*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<uint8_t, D>::value));
921*c217d954SCole Faust std::uniform_int_distribution<uint32_t> distribution_u8(low, high);
922*c217d954SCole Faust fill(tensor, distribution_u8, seed_offset);
923*c217d954SCole Faust break;
924*c217d954SCole Faust }
925*c217d954SCole Faust case DataType::S8:
926*c217d954SCole Faust case DataType::QSYMM8:
927*c217d954SCole Faust case DataType::QASYMM8_SIGNED:
928*c217d954SCole Faust {
929*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<int8_t, D>::value));
930*c217d954SCole Faust std::uniform_int_distribution<int32_t> distribution_s8(low, high);
931*c217d954SCole Faust fill(tensor, distribution_s8, seed_offset);
932*c217d954SCole Faust break;
933*c217d954SCole Faust }
934*c217d954SCole Faust case DataType::U16:
935*c217d954SCole Faust {
936*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<uint16_t, D>::value));
937*c217d954SCole Faust std::uniform_int_distribution<uint16_t> distribution_u16(low, high);
938*c217d954SCole Faust fill(tensor, distribution_u16, seed_offset);
939*c217d954SCole Faust break;
940*c217d954SCole Faust }
941*c217d954SCole Faust case DataType::S16:
942*c217d954SCole Faust case DataType::QSYMM16:
943*c217d954SCole Faust {
944*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<int16_t, D>::value));
945*c217d954SCole Faust std::uniform_int_distribution<int16_t> distribution_s16(low, high);
946*c217d954SCole Faust fill(tensor, distribution_s16, seed_offset);
947*c217d954SCole Faust break;
948*c217d954SCole Faust }
949*c217d954SCole Faust case DataType::U32:
950*c217d954SCole Faust {
951*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<uint32_t, D>::value));
952*c217d954SCole Faust std::uniform_int_distribution<uint32_t> distribution_u32(low, high);
953*c217d954SCole Faust fill(tensor, distribution_u32, seed_offset);
954*c217d954SCole Faust break;
955*c217d954SCole Faust }
956*c217d954SCole Faust case DataType::S32:
957*c217d954SCole Faust {
958*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<int32_t, D>::value));
959*c217d954SCole Faust std::uniform_int_distribution<int32_t> distribution_s32(low, high);
960*c217d954SCole Faust fill(tensor, distribution_s32, seed_offset);
961*c217d954SCole Faust break;
962*c217d954SCole Faust }
963*c217d954SCole Faust case DataType::U64:
964*c217d954SCole Faust {
965*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<uint64_t, D>::value));
966*c217d954SCole Faust std::uniform_int_distribution<uint64_t> distribution_u64(low, high);
967*c217d954SCole Faust fill(tensor, distribution_u64, seed_offset);
968*c217d954SCole Faust break;
969*c217d954SCole Faust }
970*c217d954SCole Faust case DataType::S64:
971*c217d954SCole Faust {
972*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<int64_t, D>::value));
973*c217d954SCole Faust std::uniform_int_distribution<int64_t> distribution_s64(low, high);
974*c217d954SCole Faust fill(tensor, distribution_s64, seed_offset);
975*c217d954SCole Faust break;
976*c217d954SCole Faust }
977*c217d954SCole Faust case DataType::BFLOAT16:
978*c217d954SCole Faust {
979*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<bfloat16> distribution_bf16{ float(low), float(high) };
980*c217d954SCole Faust fill(tensor, distribution_bf16, seed_offset);
981*c217d954SCole Faust break;
982*c217d954SCole Faust }
983*c217d954SCole Faust case DataType::F16:
984*c217d954SCole Faust {
985*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<half> distribution_f16{ float(low), float(high) };
986*c217d954SCole Faust fill(tensor, distribution_f16, seed_offset);
987*c217d954SCole Faust break;
988*c217d954SCole Faust }
989*c217d954SCole Faust case DataType::F32:
990*c217d954SCole Faust {
991*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<float, D>::value));
992*c217d954SCole Faust std::uniform_real_distribution<float> distribution_f32(low, high);
993*c217d954SCole Faust fill(tensor, distribution_f32, seed_offset);
994*c217d954SCole Faust break;
995*c217d954SCole Faust }
996*c217d954SCole Faust case DataType::F64:
997*c217d954SCole Faust {
998*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<double, D>::value));
999*c217d954SCole Faust std::uniform_real_distribution<double> distribution_f64(low, high);
1000*c217d954SCole Faust fill(tensor, distribution_f64, seed_offset);
1001*c217d954SCole Faust break;
1002*c217d954SCole Faust }
1003*c217d954SCole Faust case DataType::SIZET:
1004*c217d954SCole Faust {
1005*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!(std::is_same<size_t, D>::value));
1006*c217d954SCole Faust std::uniform_int_distribution<size_t> distribution_sizet(low, high);
1007*c217d954SCole Faust fill(tensor, distribution_sizet, seed_offset);
1008*c217d954SCole Faust break;
1009*c217d954SCole Faust }
1010*c217d954SCole Faust default:
1011*c217d954SCole Faust ARM_COMPUTE_ERROR("NOT SUPPORTED!");
1012*c217d954SCole Faust }
1013*c217d954SCole Faust }
1014*c217d954SCole Faust
1015*c217d954SCole Faust template <typename T>
fill_layer_data(T && tensor,std::string name)1016*c217d954SCole Faust void AssetsLibrary::fill_layer_data(T &&tensor, std::string name) const
1017*c217d954SCole Faust {
1018*c217d954SCole Faust #ifdef _WIN32
1019*c217d954SCole Faust const std::string path_separator("\\");
1020*c217d954SCole Faust #else /* _WIN32 */
1021*c217d954SCole Faust const std::string path_separator("/");
1022*c217d954SCole Faust #endif /* _WIN32 */
1023*c217d954SCole Faust const std::string path = _library_path + path_separator + name;
1024*c217d954SCole Faust
1025*c217d954SCole Faust // Open file
1026*c217d954SCole Faust std::ifstream stream(path, std::ios::in | std::ios::binary);
1027*c217d954SCole Faust if(!stream.good())
1028*c217d954SCole Faust {
1029*c217d954SCole Faust throw framework::FileNotFound("Could not load npy file: " + path);
1030*c217d954SCole Faust }
1031*c217d954SCole Faust
1032*c217d954SCole Faust validate_npy_header(stream, tensor.data_type(), tensor.shape());
1033*c217d954SCole Faust
1034*c217d954SCole Faust // Read data
1035*c217d954SCole Faust if(tensor.padding().empty())
1036*c217d954SCole Faust {
1037*c217d954SCole Faust // If tensor has no padding read directly from stream.
1038*c217d954SCole Faust stream.read(reinterpret_cast<char *>(tensor.data()), tensor.size());
1039*c217d954SCole Faust }
1040*c217d954SCole Faust else
1041*c217d954SCole Faust {
1042*c217d954SCole Faust // If tensor has padding accessing tensor elements through execution window.
1043*c217d954SCole Faust Window window;
1044*c217d954SCole Faust window.use_tensor_dimensions(tensor.shape());
1045*c217d954SCole Faust
1046*c217d954SCole Faust execute_window_loop(window, [&](const Coordinates & id)
1047*c217d954SCole Faust {
1048*c217d954SCole Faust stream.read(reinterpret_cast<char *>(tensor(id)), tensor.element_size());
1049*c217d954SCole Faust });
1050*c217d954SCole Faust }
1051*c217d954SCole Faust }
1052*c217d954SCole Faust
1053*c217d954SCole Faust template <typename T, typename D>
fill_tensor_value(T && tensor,D value)1054*c217d954SCole Faust void AssetsLibrary::fill_tensor_value(T &&tensor, D value) const
1055*c217d954SCole Faust {
1056*c217d954SCole Faust fill_tensor_uniform(tensor, 0, value, value);
1057*c217d954SCole Faust }
1058*c217d954SCole Faust } // namespace test
1059*c217d954SCole Faust } // namespace arm_compute
1060*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_TENSOR_LIBRARY_H */
1061