blob: d181022ffe0cfba1d868f0734829b43c30776669 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Jakub Sujak3b504ef2022-12-07 23:55:22 +00002 * Copyright (c) 2016-2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
Michele Di Giorgio552e11d2020-09-23 15:08:38 +010027/** @dir .
28 * brief Boiler plate code used by examples. Various utilities to print types, load / store assets, etc.
29 */
30
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/ITensor.h"
33#include "arm_compute/core/Types.h"
steniu01bee466b2017-06-21 16:45:41 +010034#include "arm_compute/core/Window.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/runtime/Tensor.h"
Michalis Spyrou6bff1952019-10-02 17:22:11 +010036#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wunused-parameter"
Michalis Spyroufae513c2019-10-16 17:41:33 +010038#pragma GCC diagnostic ignored "-Wstrict-overflow"
Giorgio Arenacf3935f2017-10-26 17:14:13 +010039#include "libnpy/npy.hpp"
Michalis Spyrou6bff1952019-10-02 17:22:11 +010040#pragma GCC diagnostic pop
Matthew Bentham758b5ba2020-03-05 23:37:48 +000041#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042
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>
Georgios Pinitas40f51a62020-11-21 03:04:18 +000052#include <memory>
Giorgio Arenacf3935f2017-10-26 17:14:13 +010053#include <random>
54#include <string>
55#include <tuple>
56#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057
58namespace arm_compute
59{
60namespace utils
61{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010062/** Supported image types */
63enum class ImageType
64{
65 UNKNOWN,
66 PPM,
67 JPEG
68};
69
Anthony Barbier6db0ff52018-01-05 10:59:12 +000070/** Abstract Example class.
71 *
72 * All examples have to inherit from this class.
73 */
74class Example
75{
76public:
Alex Gildayc357c472018-03-21 13:54:09 +000077 /** Setup the example.
78 *
79 * @param[in] argc Argument count.
80 * @param[in] argv Argument values.
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010081 *
82 * @return True in case of no errors in setup else false
Alex Gildayc357c472018-03-21 13:54:09 +000083 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010084 virtual bool do_setup(int argc, char **argv)
85 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +010086 ARM_COMPUTE_UNUSED(argc, argv);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010087 return true;
88 };
Alex Gildayc357c472018-03-21 13:54:09 +000089 /** Run the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000090 virtual void do_run() {};
Alex Gildayc357c472018-03-21 13:54:09 +000091 /** Teardown the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000092 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 */
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100104int run_example(int argc, char **argv, std::unique_ptr<Example> example);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000105
106template <typename T>
107int run_example(int argc, char **argv)
108{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000109 return run_example(argc, argv, std::make_unique<T>());
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000110}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
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 */
120void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b);
121
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100122/** Gets image type given a file
123 *
124 * @param[in] filename File to identify its image type
125 *
126 * @return Image type
127 */
128ImageType get_image_type_from_file(const std::string &filename);
129
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130/** 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 */
137std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
138
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100139/** 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 */
Jakub Sujak3b504ef2022-12-07 23:55:22 +0000146npy::header_t parse_npy_header(std::ifstream &fs);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100147
148/** Obtain numpy type string from DataType.
149 *
150 * @param[in] data_type Data type.
151 *
152 * @return numpy type string.
153 */
154inline 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:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000173 case DataType::QASYMM8:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100174 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
175 case DataType::S8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100176 case DataType::QSYMM8:
177 case DataType::QSYMM8_PER_CHANNEL:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100178 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
179 case DataType::U16:
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100180 case DataType::QASYMM16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100181 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
182 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100183 case DataType::QSYMM16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100184 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));
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000193 case DataType::F16:
194 return endianness + "f" + support::cpp11::to_string(sizeof(half));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100195 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:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100202 ARM_COMPUTE_ERROR("Data type not supported");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100203 }
204}
205
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100206/** 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 */
211template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100212inline void map(T &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100213{
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 */
222template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100223inline void unmap(T &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100224{
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 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100234inline void map(CLTensor &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100235{
236 tensor.map(blocking);
237}
238
239/** Unmaps a tensor if needed
240 *
241 * @param tensor Tensor to be unmapped
242 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100243inline void unmap(CLTensor &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100244{
245 tensor.unmap();
246}
247#endif /* ARM_COMPUTE_CL */
248
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000249/** 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*/
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000253template <typename T>
254class uniform_real_distribution_16bit
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000255{
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000256 static_assert(std::is_same<T, half>::value || std::is_same<T, bfloat16>::value, "Only half and bfloat16 data types supported");
257
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000258public:
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000259 using result_type = T;
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000260 /** Constructor
261 *
Giorgio Arena6aeb2172020-12-15 15:45:43 +0000262 * @param[in] min Minimum value of the distribution
263 * @param[in] max Maximum value of the distribution
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000264 */
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000265 explicit uniform_real_distribution_16bit(float min = 0.f, float max = 1.0)
Giorgio Arena6aeb2172020-12-15 15:45:43 +0000266 : dist(min, max)
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000267 {
268 }
269
270 /** () operator to generate next value
271 *
272 * @param[in] gen an uniform random bit generator object
273 */
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000274 T operator()(std::mt19937 &gen)
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000275 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000276 return T(dist(gen));
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000277 }
Giorgio Arena6aeb2172020-12-15 15:45:43 +0000278
279private:
280 std::uniform_real_distribution<float> dist;
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000281};
282
Alex Gildayc357c472018-03-21 13:54:09 +0000283/** Numpy data loader */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100284class NPYLoader
285{
286public:
Alex Gildayc357c472018-03-21 13:54:09 +0000287 /** Default constructor */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100288 NPYLoader()
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100289 : _fs(), _shape(), _fortran_order(false), _typestring(), _file_layout(DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100290 {
291 }
292
293 /** Open a NPY file and reads its metadata
294 *
295 * @param[in] npy_filename File to open
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100296 * @param[in] file_layout (Optional) Layout in which the weights are stored in the file.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100297 */
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100298 void open(const std::string &npy_filename, DataLayout file_layout = DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100299 {
300 ARM_COMPUTE_ERROR_ON(is_open());
301 try
302 {
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100303 _fs.open(npy_filename, std::ios::in | std::ios::binary);
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100304 ARM_COMPUTE_EXIT_ON_MSG_VAR(!_fs.good(), "Failed to load binary data from %s", npy_filename.c_str());
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100305 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
306 _file_layout = file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100307
Jakub Sujak3b504ef2022-12-07 23:55:22 +0000308 npy::header_t header = parse_npy_header(_fs);
309 _shape = header.shape;
310 _fortran_order = header.fortran_order;
311 _typestring = header.dtype.str();
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100312 }
313 catch(const std::ifstream::failure &e)
314 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100315 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", npy_filename.c_str(), e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100316 }
317 }
318 /** Return true if a NPY file is currently open */
319 bool is_open()
320 {
321 return _fs.is_open();
322 }
323
324 /** Return true if a NPY file is in fortran order */
325 bool is_fortran()
326 {
327 return _fortran_order;
328 }
329
Gian Marco0bc5a252017-12-04 13:55:08 +0000330 /** Initialise the tensor's metadata with the dimensions of the NPY file currently open
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100331 *
332 * @param[out] tensor Tensor to initialise
Gian Marco0bc5a252017-12-04 13:55:08 +0000333 * @param[in] dt Data type to use for the tensor
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100334 */
335 template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000336 void init_tensor(T &tensor, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100337 {
338 ARM_COMPUTE_ERROR_ON(!is_open());
Gian Marco0bc5a252017-12-04 13:55:08 +0000339 ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100340
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 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100346 size_t src = i;
347 if(_fortran_order)
348 {
349 src = _shape.size() - 1 - i;
350 }
351 shape.set(i, _shape.at(src));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100352 }
353
Gian Marco0bc5a252017-12-04 13:55:08 +0000354 arm_compute::TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100355 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>
365 void fill_tensor(T &tensor)
366 {
367 ARM_COMPUTE_ERROR_ON(!is_open());
giuros01351bd132019-08-23 14:27:30 +0100368 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);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100369 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());
Gian Marco Iodice08dfba32023-06-08 15:59:28 +0100386
387 bool enable_f32_to_f16_conversion = false;
388 if(_typestring != expect_typestr)
389 {
390 const std::string f32_typestring = "<f4";
391 const std::string f16_typestring = "<f2";
392 // if typestring does not match, check whether _typestring is F32 and can be downcasted to expect_typestr
393 if(_typestring == f32_typestring && expect_typestr == f16_typestring)
394 {
395 enable_f32_to_f16_conversion = true;
396 }
397 else
398 {
399 ARM_COMPUTE_ERROR("Typestrings mismatch");
400 }
401 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100402
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100403 bool are_layouts_different = (_file_layout != tensor.info()->data_layout());
404 // Correct dimensions (Needs to match TensorShape dimension corrections)
405 if(_shape.size() != tensor.info()->tensor_shape().num_dimensions())
406 {
407 for(int i = static_cast<int>(_shape.size()) - 1; i > 0; --i)
408 {
409 if(_shape[i] == 1)
410 {
411 _shape.pop_back();
412 }
413 else
414 {
415 break;
416 }
417 }
418 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100419
420 TensorShape permuted_shape = tensor.info()->tensor_shape();
421 arm_compute::PermutationVector perm;
422 if(are_layouts_different && tensor.info()->tensor_shape().num_dimensions() > 2)
423 {
424 perm = (tensor.info()->data_layout() == arm_compute::DataLayout::NHWC) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
425 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);
426
427 arm_compute::permute(permuted_shape, perm_vec);
428 }
429
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100430 // Validate tensor shape
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000431 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch");
Michalis Spyrou39412952018-08-14 17:06:16 +0100432 for(size_t i = 0; i < _shape.size(); ++i)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100433 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100434 ARM_COMPUTE_ERROR_ON_MSG(permuted_shape[i] != _shape[i], "Tensor dimensions mismatch");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100435 }
436
Gian Marco0bc5a252017-12-04 13:55:08 +0000437 switch(tensor.info()->data_type())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100438 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100439 case arm_compute::DataType::QASYMM8:
440 case arm_compute::DataType::S32:
Gian Marco0bc5a252017-12-04 13:55:08 +0000441 case arm_compute::DataType::F32:
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000442 case arm_compute::DataType::F16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100443 {
444 // Read data
Gian Marco Iodice08dfba32023-06-08 15:59:28 +0100445 if(!are_layouts_different && !_fortran_order && tensor.info()->padding().empty() && !enable_f32_to_f16_conversion)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100446 {
447 // If tensor has no padding read directly from stream.
448 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
449 }
450 else
451 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100452 // If tensor has padding or is in fortran order accessing tensor elements through execution window.
Michalis Spyrou39412952018-08-14 17:06:16 +0100453 Window window;
454 const unsigned int num_dims = _shape.size();
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100455 if(_fortran_order)
456 {
457 for(unsigned int dim = 0; dim < num_dims; dim++)
458 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100459 permuted_shape.set(dim, _shape[num_dims - dim - 1]);
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100460 perm.set(dim, num_dims - dim - 1);
461 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100462 if(are_layouts_different)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100463 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100464 // Permute only if num_dimensions greater than 2
465 if(num_dims > 2)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100466 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100467 if(_file_layout == DataLayout::NHWC) // i.e destination is NCHW --> permute(1,2,0)
468 {
469 arm_compute::permute(perm, arm_compute::PermutationVector(1U, 2U, 0U));
470 }
471 else
472 {
473 arm_compute::permute(perm, arm_compute::PermutationVector(2U, 0U, 1U));
474 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100475 }
476 }
477 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100478 window.use_tensor_dimensions(permuted_shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100479
480 execute_window_loop(window, [&](const Coordinates & id)
481 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100482 Coordinates dst(id);
483 arm_compute::permute(dst, perm);
Gian Marco Iodice08dfba32023-06-08 15:59:28 +0100484 if(enable_f32_to_f16_conversion)
485 {
486 float f32_val = 0;
487 _fs.read(reinterpret_cast<char *>(&f32_val), 4u);
488 half f16_val = half_float::half_cast<half, std::round_to_nearest>(f32_val);
489 *(reinterpret_cast<half *>(tensor.ptr_to_element(dst))) = f16_val;
490 }
491 else
492 {
493 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(dst)), tensor.info()->element_size());
494 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100495 });
496 }
497
498 break;
499 }
500 default:
Gian Marco0bc5a252017-12-04 13:55:08 +0000501 ARM_COMPUTE_ERROR("Unsupported data type");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100502 }
503
504 // Unmap buffer if creating a CLTensor
505 unmap(tensor);
506 }
507 catch(const std::ifstream::failure &e)
508 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100509 ARM_COMPUTE_ERROR_VAR("Loading NPY file: %s", e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100510 }
511 }
512
513private:
514 std::ifstream _fs;
515 std::vector<unsigned long> _shape;
516 bool _fortran_order;
517 std::string _typestring;
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100518 DataLayout _file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100519};
520
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100521/** Template helper function to save a tensor image to a PPM file.
522 *
523 * @note Only U8 and RGB888 formats supported.
524 * @note Only works with 2D tensors.
525 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
526 *
527 * @param[in] tensor The tensor to save as PPM file
528 * @param[in] ppm_filename Filename of the file to create.
529 */
530template <typename T>
531void save_to_ppm(T &tensor, const std::string &ppm_filename)
532{
533 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
534 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
535
536 std::ofstream fs;
537
538 try
539 {
540 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
541 fs.open(ppm_filename, std::ios::out | std::ios::binary);
542
543 const unsigned int width = tensor.info()->tensor_shape()[0];
544 const unsigned int height = tensor.info()->tensor_shape()[1];
545
546 fs << "P6\n"
547 << width << " " << height << " 255\n";
548
Michele Di Giorgio40efd532021-03-18 17:32:00 +0000549 // Map buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100550 map(tensor, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100551
552 switch(tensor.info()->format())
553 {
554 case arm_compute::Format::U8:
555 {
556 arm_compute::Window window;
557 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
558 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
559
560 arm_compute::Iterator in(&tensor, window);
561
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100562 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100563 {
564 const unsigned char value = *in.ptr();
565
566 fs << value << value << value;
567 },
568 in);
569
570 break;
571 }
572 case arm_compute::Format::RGB888:
573 {
574 arm_compute::Window window;
575 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
576 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
577
578 arm_compute::Iterator in(&tensor, window);
579
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100580 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100581 {
582 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
583 },
584 in);
585
586 break;
587 }
588 default:
589 ARM_COMPUTE_ERROR("Unsupported format");
590 }
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100591
Michele Di Giorgio40efd532021-03-18 17:32:00 +0000592 // Unmap buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100593 unmap(tensor);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100594 }
595 catch(const std::ofstream::failure &e)
596 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100597 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100598 }
599}
steniu01bee466b2017-06-21 16:45:41 +0100600
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100601/** Template helper function to save a tensor image to a NPY file.
602 *
Gian Marcobfa3b522017-12-12 10:08:38 +0000603 * @note Only F32 data type supported.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100604 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
605 *
606 * @param[in] tensor The tensor to save as NPY file
607 * @param[in] npy_filename Filename of the file to create.
608 * @param[in] fortran_order If true, save matrix in fortran order.
609 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000610template <typename T, typename U = float>
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100611void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
612{
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000613 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32, arm_compute::DataType::QASYMM8);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100614
615 std::ofstream fs;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100616 try
617 {
618 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
619 fs.open(npy_filename, std::ios::out | std::ios::binary);
620
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100621 std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100622
Pablo Tello32521432018-11-15 14:43:10 +0000623 for(unsigned int i = 0, j = tensor.info()->num_dimensions() - 1; i < tensor.info()->num_dimensions(); ++i, --j)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100624 {
Pablo Tello32521432018-11-15 14:43:10 +0000625 shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i];
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100626 }
627
628 // Map buffer if creating a CLTensor
629 map(tensor, true);
630
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000631 using typestring_type = typename std::conditional<std::is_floating_point<U>::value, float, qasymm8_t>::type;
632
633 std::vector<typestring_type> tmp; /* Used only to get the typestring */
Jakub Sujak3b504ef2022-12-07 23:55:22 +0000634 const npy::dtype_t dtype = npy::dtype_map.at(std::type_index(typeid(tmp)));
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000635
636 std::ofstream stream(npy_filename, std::ofstream::binary);
Jakub Sujak3b504ef2022-12-07 23:55:22 +0000637 npy::header_t header{ dtype, fortran_order, shape };
638 npy::write_header(stream, header);
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000639
640 arm_compute::Window window;
641 window.use_tensor_dimensions(tensor.info()->tensor_shape());
642
643 arm_compute::Iterator in(&tensor, window);
644
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100645 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100646 {
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000647 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(typestring_type));
648 },
649 in);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100650
651 // Unmap buffer if creating a CLTensor
652 unmap(tensor);
653 }
654 catch(const std::ofstream::failure &e)
655 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100656 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", npy_filename.c_str(), e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100657 }
658}
659
steniu01bee466b2017-06-21 16:45:41 +0100660/** Load the tensor with pre-trained data from a binary file
661 *
662 * @param[in] tensor The tensor to be filled. Data type supported: F32.
663 * @param[in] filename Filename of the binary file to load from.
664 */
665template <typename T>
666void load_trained_data(T &tensor, const std::string &filename)
667{
668 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
669
670 std::ifstream fs;
671
672 try
673 {
674 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
675 // Open file
676 fs.open(filename, std::ios::in | std::ios::binary);
677
678 if(!fs.good())
679 {
680 throw std::runtime_error("Could not load binary data: " + filename);
681 }
682
Michele Di Giorgio40efd532021-03-18 17:32:00 +0000683 // Map buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100684 map(tensor, true);
685
steniu01bee466b2017-06-21 16:45:41 +0100686 Window window;
687
688 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
689
690 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
691 {
692 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
693 }
694
695 arm_compute::Iterator in(&tensor, window);
696
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100697 execute_window_loop(window, [&](const Coordinates &)
steniu01bee466b2017-06-21 16:45:41 +0100698 {
699 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
700 },
701 in);
702
Michele Di Giorgio40efd532021-03-18 17:32:00 +0000703 // Unmap buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100704 unmap(tensor);
steniu01bee466b2017-06-21 16:45:41 +0100705 }
706 catch(const std::ofstream::failure &e)
707 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100708 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", filename.c_str(), e.what());
steniu01bee466b2017-06-21 16:45:41 +0100709 }
710}
711
Giorgio Arena82c0d7f2020-12-15 17:15:43 +0000712template <typename T, typename TensorType>
713void fill_tensor_value(TensorType &tensor, T value)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100714{
Giorgio Arena82c0d7f2020-12-15 17:15:43 +0000715 map(tensor, true);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100716
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100717 Window window;
Michalis Spyrou5e69bb42018-03-09 16:36:00 +0000718 window.use_tensor_dimensions(tensor.info()->tensor_shape());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100719
Giorgio Arena82c0d7f2020-12-15 17:15:43 +0000720 Iterator it_tensor(&tensor, window);
721 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100722 {
Giorgio Arena82c0d7f2020-12-15 17:15:43 +0000723 *reinterpret_cast<T *>(it_tensor.ptr()) = value;
724 },
725 it_tensor);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100726
727 unmap(tensor);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100728}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100729
Giorgio Arena82c0d7f2020-12-15 17:15:43 +0000730template <typename T, typename TensorType>
731void fill_tensor_zero(TensorType &tensor)
732{
733 fill_tensor_value(tensor, T(0));
734}
735
736template <typename T, typename TensorType>
737void fill_tensor_vector(TensorType &tensor, std::vector<T> vec)
738{
739 ARM_COMPUTE_ERROR_ON(tensor.info()->tensor_shape().total_size() != vec.size());
740
741 map(tensor, true);
742
743 Window window;
744 window.use_tensor_dimensions(tensor.info()->tensor_shape());
745
746 int i = 0;
747 Iterator it_tensor(&tensor, window);
748 execute_window_loop(window, [&](const Coordinates &)
749 {
750 *reinterpret_cast<T *>(it_tensor.ptr()) = vec.at(i++);
751 },
752 it_tensor);
753
754 unmap(tensor);
755}
756
757template <typename T, typename TensorType>
758void 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())
759{
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000760 constexpr bool is_fp_16bit = std::is_same<T, half>::value || std::is_same<T, bfloat16>::value;
761 constexpr bool is_integral = std::is_integral<T>::value && !is_fp_16bit;
Giorgio Arena82c0d7f2020-12-15 17:15:43 +0000762
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000763 using fp_dist_type = typename std::conditional<is_fp_16bit, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
Giorgio Arena82c0d7f2020-12-15 17:15:43 +0000764 using dist_type = typename std::conditional<is_integral, std::uniform_int_distribution<T>, fp_dist_type>::type;
765
766 std::mt19937 gen(seed);
767 dist_type dist(lower_bound, upper_bound);
768
769 map(tensor, true);
770
771 Window window;
772 window.use_tensor_dimensions(tensor.info()->tensor_shape());
773
774 Iterator it(&tensor, window);
775 execute_window_loop(window, [&](const Coordinates &)
776 {
777 *reinterpret_cast<T *>(it.ptr()) = dist(gen);
778 },
779 it);
780
781 unmap(tensor);
782}
783
784template <typename T, typename TensorType>
785void fill_random_tensor(TensorType &tensor, T lower_bound = std::numeric_limits<T>::lowest(), T upper_bound = std::numeric_limits<T>::max())
786{
787 std::random_device rd;
788 fill_random_tensor(tensor, rd(), lower_bound, upper_bound);
789}
790
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100791template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000792void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100793{
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000794 dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1), src0.info()->dimension(2)), 1, dt));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100795}
Gian Marco5ca74092018-02-08 16:21:54 +0000796/** This function returns the amount of memory free reading from /proc/meminfo
797 *
798 * @return The free memory in kB
799 */
800uint64_t get_mem_free_from_meminfo();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100801
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000802/** Compare two tensors
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100803 *
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000804 * @param[in] tensor1 First tensor to be compared.
805 * @param[in] tensor2 Second tensor to be compared.
806 * @param[in] tolerance Tolerance used for the comparison.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100807 *
808 * @return The number of mismatches
809 */
810template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000811int compare_tensor(ITensor &tensor1, ITensor &tensor2, T tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100812{
813 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2);
814 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
815
816 int num_mismatches = 0;
817 Window window;
818 window.use_tensor_dimensions(tensor1.info()->tensor_shape());
819
820 map(tensor1, true);
821 map(tensor2, true);
Pablo Tello32521432018-11-15 14:43:10 +0000822
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100823 Iterator itensor1(&tensor1, window);
824 Iterator itensor2(&tensor2, window);
825
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100826 execute_window_loop(window, [&](const Coordinates &)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100827 {
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000828 if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100829 {
830 ++num_mismatches;
831 }
832 },
833 itensor1, itensor2);
834
835 unmap(itensor1);
836 unmap(itensor2);
837
838 return num_mismatches;
839}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100840} // namespace utils
841} // namespace arm_compute
842#endif /* __UTILS_UTILS_H__*/