1 /*
2 * Copyright (c) 2016-2023 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 __UTILS_UTILS_H__
25 #define __UTILS_UTILS_H__
26
27 /** @dir .
28 * brief Boiler plate code used by examples. Various utilities to print types, load / store assets, etc.
29 */
30
31 #include "arm_compute/core/Helpers.h"
32 #include "arm_compute/core/ITensor.h"
33 #include "arm_compute/core/Types.h"
34 #include "arm_compute/core/Window.h"
35 #include "arm_compute/runtime/Tensor.h"
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wunused-parameter"
38 #pragma GCC diagnostic ignored "-Wstrict-overflow"
39 #include "libnpy/npy.hpp"
40 #pragma GCC diagnostic pop
41 #include "support/StringSupport.h"
42
43 #ifdef ARM_COMPUTE_CL
44 #include "arm_compute/core/CL/OpenCL.h"
45 #include "arm_compute/runtime/CL/CLTensor.h"
46 #endif /* ARM_COMPUTE_CL */
47
48 #include <cstdlib>
49 #include <cstring>
50 #include <fstream>
51 #include <iostream>
52 #include <memory>
53 #include <random>
54 #include <string>
55 #include <tuple>
56 #include <vector>
57
58 namespace arm_compute
59 {
60 namespace utils
61 {
62 /** Supported image types */
63 enum class ImageType
64 {
65 UNKNOWN,
66 PPM,
67 JPEG
68 };
69
70 /** Abstract Example class.
71 *
72 * All examples have to inherit from this class.
73 */
74 class Example
75 {
76 public:
77 /** Setup the example.
78 *
79 * @param[in] argc Argument count.
80 * @param[in] argv Argument values.
81 *
82 * @return True in case of no errors in setup else false
83 */
do_setup(int argc,char ** argv)84 virtual bool do_setup(int argc, char **argv)
85 {
86 ARM_COMPUTE_UNUSED(argc, argv);
87 return true;
88 };
89 /** Run the example. */
do_run()90 virtual void do_run() {};
91 /** Teardown the example. */
do_teardown()92 virtual void do_teardown() {};
93
94 /** Default destructor. */
95 virtual ~Example() = default;
96 };
97
98 /** Run an example and handle the potential exceptions it throws
99 *
100 * @param[in] argc Number of command line arguments
101 * @param[in] argv Command line arguments
102 * @param[in] example Example to run
103 */
104 int run_example(int argc, char **argv, std::unique_ptr<Example> example);
105
106 template <typename T>
run_example(int argc,char ** argv)107 int run_example(int argc, char **argv)
108 {
109 return run_example(argc, argv, std::make_unique<T>());
110 }
111
112 /** Draw a RGB rectangular window for the detected object
113 *
114 * @param[in, out] tensor Input tensor where the rectangle will be drawn on. Format supported: RGB888
115 * @param[in] rect Geometry of the rectangular window
116 * @param[in] r Red colour to use
117 * @param[in] g Green colour to use
118 * @param[in] b Blue colour to use
119 */
120 void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b);
121
122 /** Gets image type given a file
123 *
124 * @param[in] filename File to identify its image type
125 *
126 * @return Image type
127 */
128 ImageType get_image_type_from_file(const std::string &filename);
129
130 /** Parse the ppm header from an input file stream. At the end of the execution,
131 * the file position pointer will be located at the first pixel stored in the ppm file
132 *
133 * @param[in] fs Input file stream to parse
134 *
135 * @return The width, height and max value stored in the header of the PPM file
136 */
137 std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
138
139 /** Parse the npy header from an input file stream. At the end of the execution,
140 * the file position pointer will be located at the first pixel stored in the npy file //TODO
141 *
142 * @param[in] fs Input file stream to parse
143 *
144 * @return The width and height stored in the header of the NPY file
145 */
146 npy::header_t parse_npy_header(std::ifstream &fs);
147
148 /** Obtain numpy type string from DataType.
149 *
150 * @param[in] data_type Data type.
151 *
152 * @return numpy type string.
153 */
get_typestring(DataType data_type)154 inline std::string get_typestring(DataType data_type)
155 {
156 // Check endianness
157 const unsigned int i = 1;
158 const char *c = reinterpret_cast<const char *>(&i);
159 std::string endianness;
160 if(*c == 1)
161 {
162 endianness = std::string("<");
163 }
164 else
165 {
166 endianness = std::string(">");
167 }
168 const std::string no_endianness("|");
169
170 switch(data_type)
171 {
172 case DataType::U8:
173 case DataType::QASYMM8:
174 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
175 case DataType::S8:
176 case DataType::QSYMM8:
177 case DataType::QSYMM8_PER_CHANNEL:
178 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
179 case DataType::U16:
180 case DataType::QASYMM16:
181 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
182 case DataType::S16:
183 case DataType::QSYMM16:
184 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
185 case DataType::U32:
186 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
187 case DataType::S32:
188 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
189 case DataType::U64:
190 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
191 case DataType::S64:
192 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
193 case DataType::F16:
194 return endianness + "f" + support::cpp11::to_string(sizeof(half));
195 case DataType::F32:
196 return endianness + "f" + support::cpp11::to_string(sizeof(float));
197 case DataType::F64:
198 return endianness + "f" + support::cpp11::to_string(sizeof(double));
199 case DataType::SIZET:
200 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
201 default:
202 ARM_COMPUTE_ERROR("Data type not supported");
203 }
204 }
205
206 /** Maps a tensor if needed
207 *
208 * @param[in] tensor Tensor to be mapped
209 * @param[in] blocking Specified if map is blocking or not
210 */
211 template <typename T>
map(T & tensor,bool blocking)212 inline void map(T &tensor, bool blocking)
213 {
214 ARM_COMPUTE_UNUSED(tensor);
215 ARM_COMPUTE_UNUSED(blocking);
216 }
217
218 /** Unmaps a tensor if needed
219 *
220 * @param tensor Tensor to be unmapped
221 */
222 template <typename T>
unmap(T & tensor)223 inline void unmap(T &tensor)
224 {
225 ARM_COMPUTE_UNUSED(tensor);
226 }
227
228 #ifdef ARM_COMPUTE_CL
229 /** Maps a tensor if needed
230 *
231 * @param[in] tensor Tensor to be mapped
232 * @param[in] blocking Specified if map is blocking or not
233 */
map(CLTensor & tensor,bool blocking)234 inline void map(CLTensor &tensor, bool blocking)
235 {
236 tensor.map(blocking);
237 }
238
239 /** Unmaps a tensor if needed
240 *
241 * @param tensor Tensor to be unmapped
242 */
unmap(CLTensor & tensor)243 inline void unmap(CLTensor &tensor)
244 {
245 tensor.unmap();
246 }
247 #endif /* ARM_COMPUTE_CL */
248
249 /** Specialized class to generate random non-zero FP16 values.
250 * uniform_real_distribution<half> generates values that get rounded off to zero, causing
251 * differences between ACL and reference implementation
252 */
253 template <typename T>
254 class uniform_real_distribution_16bit
255 {
256 static_assert(std::is_same<T, half>::value || std::is_same<T, bfloat16>::value, "Only half and bfloat16 data types supported");
257
258 public:
259 using result_type = T;
260 /** Constructor
261 *
262 * @param[in] min Minimum value of the distribution
263 * @param[in] max Maximum value of the distribution
264 */
265 explicit uniform_real_distribution_16bit(float min = 0.f, float max = 1.0)
dist(min,max)266 : dist(min, max)
267 {
268 }
269
270 /** () operator to generate next value
271 *
272 * @param[in] gen an uniform random bit generator object
273 */
operator()274 T operator()(std::mt19937 &gen)
275 {
276 return T(dist(gen));
277 }
278
279 private:
280 std::uniform_real_distribution<float> dist;
281 };
282
283 /** Numpy data loader */
284 class NPYLoader
285 {
286 public:
287 /** Default constructor */
NPYLoader()288 NPYLoader()
289 : _fs(), _shape(), _fortran_order(false), _typestring(), _file_layout(DataLayout::NCHW)
290 {
291 }
292
293 /** Open a NPY file and reads its metadata
294 *
295 * @param[in] npy_filename File to open
296 * @param[in] file_layout (Optional) Layout in which the weights are stored in the file.
297 */
298 void open(const std::string &npy_filename, DataLayout file_layout = DataLayout::NCHW)
299 {
300 ARM_COMPUTE_ERROR_ON(is_open());
301 try
302 {
303 _fs.open(npy_filename, std::ios::in | std::ios::binary);
304 ARM_COMPUTE_EXIT_ON_MSG_VAR(!_fs.good(), "Failed to load binary data from %s", npy_filename.c_str());
305 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
306 _file_layout = file_layout;
307
308 npy::header_t header = parse_npy_header(_fs);
309 _shape = header.shape;
310 _fortran_order = header.fortran_order;
311 _typestring = header.dtype.str();
312 }
catch(const std::ifstream::failure & e)313 catch(const std::ifstream::failure &e)
314 {
315 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", npy_filename.c_str(), e.what());
316 }
317 }
318 /** Return true if a NPY file is currently open */
is_open()319 bool is_open()
320 {
321 return _fs.is_open();
322 }
323
324 /** Return true if a NPY file is in fortran order */
is_fortran()325 bool is_fortran()
326 {
327 return _fortran_order;
328 }
329
330 /** Initialise the tensor's metadata with the dimensions of the NPY file currently open
331 *
332 * @param[out] tensor Tensor to initialise
333 * @param[in] dt Data type to use for the tensor
334 */
335 template <typename T>
init_tensor(T & tensor,arm_compute::DataType dt)336 void init_tensor(T &tensor, arm_compute::DataType dt)
337 {
338 ARM_COMPUTE_ERROR_ON(!is_open());
339 ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32);
340
341 // Use the size of the input NPY tensor
342 TensorShape shape;
343 shape.set_num_dimensions(_shape.size());
344 for(size_t i = 0; i < _shape.size(); ++i)
345 {
346 size_t src = i;
347 if(_fortran_order)
348 {
349 src = _shape.size() - 1 - i;
350 }
351 shape.set(i, _shape.at(src));
352 }
353
354 arm_compute::TensorInfo tensor_info(shape, 1, dt);
355 tensor.allocator()->init(tensor_info);
356 }
357
358 /** Fill a tensor with the content of the currently open NPY file.
359 *
360 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
361 *
362 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
363 */
364 template <typename T>
fill_tensor(T & tensor)365 void fill_tensor(T &tensor)
366 {
367 ARM_COMPUTE_ERROR_ON(!is_open());
368 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::QASYMM8, arm_compute::DataType::S32, arm_compute::DataType::F32, arm_compute::DataType::F16);
369 try
370 {
371 // Map buffer if creating a CLTensor
372 map(tensor, true);
373
374 // Check if the file is large enough to fill the tensor
375 const size_t current_position = _fs.tellg();
376 _fs.seekg(0, std::ios_base::end);
377 const size_t end_position = _fs.tellg();
378 _fs.seekg(current_position, std::ios_base::beg);
379
380 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
381 "Not enough data in file");
382 ARM_COMPUTE_UNUSED(end_position);
383
384 // Check if the typestring matches the given one
385 std::string expect_typestr = get_typestring(tensor.info()->data_type());
386 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
387
388 bool are_layouts_different = (_file_layout != tensor.info()->data_layout());
389 // Correct dimensions (Needs to match TensorShape dimension corrections)
390 if(_shape.size() != tensor.info()->tensor_shape().num_dimensions())
391 {
392 for(int i = static_cast<int>(_shape.size()) - 1; i > 0; --i)
393 {
394 if(_shape[i] == 1)
395 {
396 _shape.pop_back();
397 }
398 else
399 {
400 break;
401 }
402 }
403 }
404
405 TensorShape permuted_shape = tensor.info()->tensor_shape();
406 arm_compute::PermutationVector perm;
407 if(are_layouts_different && tensor.info()->tensor_shape().num_dimensions() > 2)
408 {
409 perm = (tensor.info()->data_layout() == arm_compute::DataLayout::NHWC) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
410 arm_compute::PermutationVector perm_vec = (tensor.info()->data_layout() == arm_compute::DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
411
412 arm_compute::permute(permuted_shape, perm_vec);
413 }
414
415 // Validate tensor shape
416 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch");
417 for(size_t i = 0; i < _shape.size(); ++i)
418 {
419 ARM_COMPUTE_ERROR_ON_MSG(permuted_shape[i] != _shape[i], "Tensor dimensions mismatch");
420 }
421
422 switch(tensor.info()->data_type())
423 {
424 case arm_compute::DataType::QASYMM8:
425 case arm_compute::DataType::S32:
426 case arm_compute::DataType::F32:
427 case arm_compute::DataType::F16:
428 {
429 // Read data
430 if(!are_layouts_different && !_fortran_order && tensor.info()->padding().empty())
431 {
432 // If tensor has no padding read directly from stream.
433 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
434 }
435 else
436 {
437 // If tensor has padding or is in fortran order accessing tensor elements through execution window.
438 Window window;
439 const unsigned int num_dims = _shape.size();
440 if(_fortran_order)
441 {
442 for(unsigned int dim = 0; dim < num_dims; dim++)
443 {
444 permuted_shape.set(dim, _shape[num_dims - dim - 1]);
445 perm.set(dim, num_dims - dim - 1);
446 }
447 if(are_layouts_different)
448 {
449 // Permute only if num_dimensions greater than 2
450 if(num_dims > 2)
451 {
452 if(_file_layout == DataLayout::NHWC) // i.e destination is NCHW --> permute(1,2,0)
453 {
454 arm_compute::permute(perm, arm_compute::PermutationVector(1U, 2U, 0U));
455 }
456 else
457 {
458 arm_compute::permute(perm, arm_compute::PermutationVector(2U, 0U, 1U));
459 }
460 }
461 }
462 }
463 window.use_tensor_dimensions(permuted_shape);
464
465 execute_window_loop(window, [&](const Coordinates & id)
466 {
467 Coordinates dst(id);
468 arm_compute::permute(dst, perm);
469 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(dst)), tensor.info()->element_size());
470 });
471 }
472
473 break;
474 }
475 default:
476 ARM_COMPUTE_ERROR("Unsupported data type");
477 }
478
479 // Unmap buffer if creating a CLTensor
480 unmap(tensor);
481 }
482 catch(const std::ifstream::failure &e)
483 {
484 ARM_COMPUTE_ERROR_VAR("Loading NPY file: %s", e.what());
485 }
486 }
487
488 private:
489 std::ifstream _fs;
490 std::vector<unsigned long> _shape;
491 bool _fortran_order;
492 std::string _typestring;
493 DataLayout _file_layout;
494 };
495
496 /** Template helper function to save a tensor image to a PPM file.
497 *
498 * @note Only U8 and RGB888 formats supported.
499 * @note Only works with 2D tensors.
500 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
501 *
502 * @param[in] tensor The tensor to save as PPM file
503 * @param[in] ppm_filename Filename of the file to create.
504 */
505 template <typename T>
save_to_ppm(T & tensor,const std::string & ppm_filename)506 void save_to_ppm(T &tensor, const std::string &ppm_filename)
507 {
508 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
509 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
510
511 std::ofstream fs;
512
513 try
514 {
515 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
516 fs.open(ppm_filename, std::ios::out | std::ios::binary);
517
518 const unsigned int width = tensor.info()->tensor_shape()[0];
519 const unsigned int height = tensor.info()->tensor_shape()[1];
520
521 fs << "P6\n"
522 << width << " " << height << " 255\n";
523
524 // Map buffer if creating a CLTensor
525 map(tensor, true);
526
527 switch(tensor.info()->format())
528 {
529 case arm_compute::Format::U8:
530 {
531 arm_compute::Window window;
532 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
533 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
534
535 arm_compute::Iterator in(&tensor, window);
536
537 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
538 {
539 const unsigned char value = *in.ptr();
540
541 fs << value << value << value;
542 },
543 in);
544
545 break;
546 }
547 case arm_compute::Format::RGB888:
548 {
549 arm_compute::Window window;
550 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
551 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
552
553 arm_compute::Iterator in(&tensor, window);
554
555 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
556 {
557 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
558 },
559 in);
560
561 break;
562 }
563 default:
564 ARM_COMPUTE_ERROR("Unsupported format");
565 }
566
567 // Unmap buffer if creating a CLTensor
568 unmap(tensor);
569 }
570 catch(const std::ofstream::failure &e)
571 {
572 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
573 }
574 }
575
576 /** Template helper function to save a tensor image to a NPY file.
577 *
578 * @note Only F32 data type supported.
579 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
580 *
581 * @param[in] tensor The tensor to save as NPY file
582 * @param[in] npy_filename Filename of the file to create.
583 * @param[in] fortran_order If true, save matrix in fortran order.
584 */
585 template <typename T, typename U = float>
save_to_npy(T & tensor,const std::string & npy_filename,bool fortran_order)586 void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
587 {
588 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32, arm_compute::DataType::QASYMM8);
589
590 std::ofstream fs;
591 try
592 {
593 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
594 fs.open(npy_filename, std::ios::out | std::ios::binary);
595
596 std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
597
598 for(unsigned int i = 0, j = tensor.info()->num_dimensions() - 1; i < tensor.info()->num_dimensions(); ++i, --j)
599 {
600 shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i];
601 }
602
603 // Map buffer if creating a CLTensor
604 map(tensor, true);
605
606 using typestring_type = typename std::conditional<std::is_floating_point<U>::value, float, qasymm8_t>::type;
607
608 std::vector<typestring_type> tmp; /* Used only to get the typestring */
609 const npy::dtype_t dtype = npy::dtype_map.at(std::type_index(typeid(tmp)));
610
611 std::ofstream stream(npy_filename, std::ofstream::binary);
612 npy::header_t header{ dtype, fortran_order, shape };
613 npy::write_header(stream, header);
614
615 arm_compute::Window window;
616 window.use_tensor_dimensions(tensor.info()->tensor_shape());
617
618 arm_compute::Iterator in(&tensor, window);
619
620 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
621 {
622 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(typestring_type));
623 },
624 in);
625
626 // Unmap buffer if creating a CLTensor
627 unmap(tensor);
628 }
629 catch(const std::ofstream::failure &e)
630 {
631 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", npy_filename.c_str(), e.what());
632 }
633 }
634
635 /** Load the tensor with pre-trained data from a binary file
636 *
637 * @param[in] tensor The tensor to be filled. Data type supported: F32.
638 * @param[in] filename Filename of the binary file to load from.
639 */
640 template <typename T>
load_trained_data(T & tensor,const std::string & filename)641 void load_trained_data(T &tensor, const std::string &filename)
642 {
643 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
644
645 std::ifstream fs;
646
647 try
648 {
649 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
650 // Open file
651 fs.open(filename, std::ios::in | std::ios::binary);
652
653 if(!fs.good())
654 {
655 throw std::runtime_error("Could not load binary data: " + filename);
656 }
657
658 // Map buffer if creating a CLTensor
659 map(tensor, true);
660
661 Window window;
662
663 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
664
665 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
666 {
667 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
668 }
669
670 arm_compute::Iterator in(&tensor, window);
671
672 execute_window_loop(window, [&](const Coordinates &)
673 {
674 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
675 },
676 in);
677
678 // Unmap buffer if creating a CLTensor
679 unmap(tensor);
680 }
681 catch(const std::ofstream::failure &e)
682 {
683 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", filename.c_str(), e.what());
684 }
685 }
686
687 template <typename T, typename TensorType>
fill_tensor_value(TensorType & tensor,T value)688 void fill_tensor_value(TensorType &tensor, T value)
689 {
690 map(tensor, true);
691
692 Window window;
693 window.use_tensor_dimensions(tensor.info()->tensor_shape());
694
695 Iterator it_tensor(&tensor, window);
696 execute_window_loop(window, [&](const Coordinates &)
697 {
698 *reinterpret_cast<T *>(it_tensor.ptr()) = value;
699 },
700 it_tensor);
701
702 unmap(tensor);
703 }
704
705 template <typename T, typename TensorType>
fill_tensor_zero(TensorType & tensor)706 void fill_tensor_zero(TensorType &tensor)
707 {
708 fill_tensor_value(tensor, T(0));
709 }
710
711 template <typename T, typename TensorType>
fill_tensor_vector(TensorType & tensor,std::vector<T> vec)712 void fill_tensor_vector(TensorType &tensor, std::vector<T> vec)
713 {
714 ARM_COMPUTE_ERROR_ON(tensor.info()->tensor_shape().total_size() != vec.size());
715
716 map(tensor, true);
717
718 Window window;
719 window.use_tensor_dimensions(tensor.info()->tensor_shape());
720
721 int i = 0;
722 Iterator it_tensor(&tensor, window);
723 execute_window_loop(window, [&](const Coordinates &)
724 {
725 *reinterpret_cast<T *>(it_tensor.ptr()) = vec.at(i++);
726 },
727 it_tensor);
728
729 unmap(tensor);
730 }
731
732 template <typename T, typename TensorType>
733 void fill_random_tensor(TensorType &tensor, std::random_device::result_type seed, T lower_bound = std::numeric_limits<T>::lowest(), T upper_bound = std::numeric_limits<T>::max())
734 {
735 constexpr bool is_fp_16bit = std::is_same<T, half>::value || std::is_same<T, bfloat16>::value;
736 constexpr bool is_integral = std::is_integral<T>::value && !is_fp_16bit;
737
738 using fp_dist_type = typename std::conditional<is_fp_16bit, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
739 using dist_type = typename std::conditional<is_integral, std::uniform_int_distribution<T>, fp_dist_type>::type;
740
741 std::mt19937 gen(seed);
742 dist_type dist(lower_bound, upper_bound);
743
744 map(tensor, true);
745
746 Window window;
747 window.use_tensor_dimensions(tensor.info()->tensor_shape());
748
749 Iterator it(&tensor, window);
750 execute_window_loop(window, [&](const Coordinates &)
751 {
752 *reinterpret_cast<T *>(it.ptr()) = dist(gen);
753 },
754 it);
755
756 unmap(tensor);
757 }
758
759 template <typename T, typename TensorType>
760 void fill_random_tensor(TensorType &tensor, T lower_bound = std::numeric_limits<T>::lowest(), T upper_bound = std::numeric_limits<T>::max())
761 {
762 std::random_device rd;
763 fill_random_tensor(tensor, rd(), lower_bound, upper_bound);
764 }
765
766 template <typename T>
init_sgemm_output(T & dst,T & src0,T & src1,arm_compute::DataType dt)767 void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
768 {
769 dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1), src0.info()->dimension(2)), 1, dt));
770 }
771 /** This function returns the amount of memory free reading from /proc/meminfo
772 *
773 * @return The free memory in kB
774 */
775 uint64_t get_mem_free_from_meminfo();
776
777 /** Compare two tensors
778 *
779 * @param[in] tensor1 First tensor to be compared.
780 * @param[in] tensor2 Second tensor to be compared.
781 * @param[in] tolerance Tolerance used for the comparison.
782 *
783 * @return The number of mismatches
784 */
785 template <typename T>
compare_tensor(ITensor & tensor1,ITensor & tensor2,T tolerance)786 int compare_tensor(ITensor &tensor1, ITensor &tensor2, T tolerance)
787 {
788 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2);
789 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
790
791 int num_mismatches = 0;
792 Window window;
793 window.use_tensor_dimensions(tensor1.info()->tensor_shape());
794
795 map(tensor1, true);
796 map(tensor2, true);
797
798 Iterator itensor1(&tensor1, window);
799 Iterator itensor2(&tensor2, window);
800
801 execute_window_loop(window, [&](const Coordinates &)
802 {
803 if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > tolerance)
804 {
805 ++num_mismatches;
806 }
807 },
808 itensor1, itensor2);
809
810 unmap(itensor1);
811 unmap(itensor2);
812
813 return num_mismatches;
814 }
815 } // namespace utils
816 } // namespace arm_compute
817 #endif /* __UTILS_UTILS_H__*/
818