blob: 1941d374767e60f38077a8d9c26b1454099b9993 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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"
34#include "arm_compute/core/Validate.h"
steniu01bee466b2017-06-21 16:45:41 +010035#include "arm_compute/core/Window.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/runtime/Tensor.h"
Michalis Spyrou6bff1952019-10-02 17:22:11 +010037#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Wunused-parameter"
Michalis Spyroufae513c2019-10-16 17:41:33 +010039#pragma GCC diagnostic ignored "-Wstrict-overflow"
Giorgio Arenacf3935f2017-10-26 17:14:13 +010040#include "libnpy/npy.hpp"
Michalis Spyrou6bff1952019-10-02 17:22:11 +010041#pragma GCC diagnostic pop
Matthew Bentham758b5ba2020-03-05 23:37:48 +000042#include "support/MemorySupport.h"
43#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45#ifdef ARM_COMPUTE_CL
46#include "arm_compute/core/CL/OpenCL.h"
Isabella Gottardi02aabcc2017-10-12 17:28:51 +010047#include "arm_compute/runtime/CL/CLDistribution1D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048#include "arm_compute/runtime/CL/CLTensor.h"
49#endif /* ARM_COMPUTE_CL */
Anthony Barbier7068f992017-10-26 15:23:08 +010050#ifdef ARM_COMPUTE_GC
51#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
52#endif /* ARM_COMPUTE_GC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
54#include <cstdlib>
55#include <cstring>
56#include <fstream>
57#include <iostream>
Giorgio Arenacf3935f2017-10-26 17:14:13 +010058#include <random>
59#include <string>
60#include <tuple>
61#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062
63namespace arm_compute
64{
65namespace utils
66{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010067/** Supported image types */
68enum class ImageType
69{
70 UNKNOWN,
71 PPM,
72 JPEG
73};
74
Anthony Barbier6db0ff52018-01-05 10:59:12 +000075/** Abstract Example class.
76 *
77 * All examples have to inherit from this class.
78 */
79class Example
80{
81public:
Alex Gildayc357c472018-03-21 13:54:09 +000082 /** Setup the example.
83 *
84 * @param[in] argc Argument count.
85 * @param[in] argv Argument values.
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010086 *
87 * @return True in case of no errors in setup else false
Alex Gildayc357c472018-03-21 13:54:09 +000088 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010089 virtual bool do_setup(int argc, char **argv)
90 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +010091 ARM_COMPUTE_UNUSED(argc, argv);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010092 return true;
93 };
Alex Gildayc357c472018-03-21 13:54:09 +000094 /** Run the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000095 virtual void do_run() {};
Alex Gildayc357c472018-03-21 13:54:09 +000096 /** Teardown the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000097 virtual void do_teardown() {};
98
99 /** Default destructor. */
100 virtual ~Example() = default;
101};
102
103/** Run an example and handle the potential exceptions it throws
104 *
105 * @param[in] argc Number of command line arguments
106 * @param[in] argv Command line arguments
107 * @param[in] example Example to run
108 */
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100109int run_example(int argc, char **argv, std::unique_ptr<Example> example);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000110
111template <typename T>
112int run_example(int argc, char **argv)
113{
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100114 return run_example(argc, argv, support::cpp14::make_unique<T>());
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000115}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116
117/** Draw a RGB rectangular window for the detected object
118 *
119 * @param[in, out] tensor Input tensor where the rectangle will be drawn on. Format supported: RGB888
120 * @param[in] rect Geometry of the rectangular window
121 * @param[in] r Red colour to use
122 * @param[in] g Green colour to use
123 * @param[in] b Blue colour to use
124 */
125void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b);
126
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100127/** Gets image type given a file
128 *
129 * @param[in] filename File to identify its image type
130 *
131 * @return Image type
132 */
133ImageType get_image_type_from_file(const std::string &filename);
134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135/** Parse the ppm header from an input file stream. At the end of the execution,
136 * the file position pointer will be located at the first pixel stored in the ppm file
137 *
138 * @param[in] fs Input file stream to parse
139 *
140 * @return The width, height and max value stored in the header of the PPM file
141 */
142std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
143
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100144/** Parse the npy header from an input file stream. At the end of the execution,
145 * the file position pointer will be located at the first pixel stored in the npy file //TODO
146 *
147 * @param[in] fs Input file stream to parse
148 *
149 * @return The width and height stored in the header of the NPY file
150 */
151std::tuple<std::vector<unsigned long>, bool, std::string> parse_npy_header(std::ifstream &fs);
152
153/** Obtain numpy type string from DataType.
154 *
155 * @param[in] data_type Data type.
156 *
157 * @return numpy type string.
158 */
159inline std::string get_typestring(DataType data_type)
160{
161 // Check endianness
162 const unsigned int i = 1;
163 const char *c = reinterpret_cast<const char *>(&i);
164 std::string endianness;
165 if(*c == 1)
166 {
167 endianness = std::string("<");
168 }
169 else
170 {
171 endianness = std::string(">");
172 }
173 const std::string no_endianness("|");
174
175 switch(data_type)
176 {
177 case DataType::U8:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000178 case DataType::QASYMM8:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100179 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
180 case DataType::S8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100181 case DataType::QSYMM8:
182 case DataType::QSYMM8_PER_CHANNEL:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100183 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
184 case DataType::U16:
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100185 case DataType::QASYMM16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100186 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
187 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100188 case DataType::QSYMM16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100189 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
190 case DataType::U32:
191 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
192 case DataType::S32:
193 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
194 case DataType::U64:
195 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
196 case DataType::S64:
197 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000198 case DataType::F16:
199 return endianness + "f" + support::cpp11::to_string(sizeof(half));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100200 case DataType::F32:
201 return endianness + "f" + support::cpp11::to_string(sizeof(float));
202 case DataType::F64:
203 return endianness + "f" + support::cpp11::to_string(sizeof(double));
204 case DataType::SIZET:
205 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
206 default:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100207 ARM_COMPUTE_ERROR("Data type not supported");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100208 }
209}
210
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100211/** Maps a tensor if needed
212 *
213 * @param[in] tensor Tensor to be mapped
214 * @param[in] blocking Specified if map is blocking or not
215 */
216template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100217inline void map(T &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100218{
219 ARM_COMPUTE_UNUSED(tensor);
220 ARM_COMPUTE_UNUSED(blocking);
221}
222
223/** Unmaps a tensor if needed
224 *
225 * @param tensor Tensor to be unmapped
226 */
227template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100228inline void unmap(T &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100229{
230 ARM_COMPUTE_UNUSED(tensor);
231}
232
233#ifdef ARM_COMPUTE_CL
234/** Maps a tensor if needed
235 *
236 * @param[in] tensor Tensor to be mapped
237 * @param[in] blocking Specified if map is blocking or not
238 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100239inline void map(CLTensor &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100240{
241 tensor.map(blocking);
242}
243
244/** Unmaps a tensor if needed
245 *
246 * @param tensor Tensor to be unmapped
247 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100248inline void unmap(CLTensor &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100249{
250 tensor.unmap();
251}
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100252
253/** Maps a distribution if needed
254 *
255 * @param[in] distribution Distribution to be mapped
256 * @param[in] blocking Specified if map is blocking or not
257 */
258inline void map(CLDistribution1D &distribution, bool blocking)
259{
260 distribution.map(blocking);
261}
262
263/** Unmaps a distribution if needed
264 *
265 * @param distribution Distribution to be unmapped
266 */
267inline void unmap(CLDistribution1D &distribution)
268{
269 distribution.unmap();
270}
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100271#endif /* ARM_COMPUTE_CL */
272
Anthony Barbier7068f992017-10-26 15:23:08 +0100273#ifdef ARM_COMPUTE_GC
274/** Maps a tensor if needed
275 *
276 * @param[in] tensor Tensor to be mapped
277 * @param[in] blocking Specified if map is blocking or not
278 */
279inline void map(GCTensor &tensor, bool blocking)
280{
281 tensor.map(blocking);
282}
283
284/** Unmaps a tensor if needed
285 *
286 * @param tensor Tensor to be unmapped
287 */
288inline void unmap(GCTensor &tensor)
289{
290 tensor.unmap();
291}
292#endif /* ARM_COMPUTE_GC */
293
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000294/** Specialized class to generate random non-zero FP16 values.
295 * uniform_real_distribution<half> generates values that get rounded off to zero, causing
296 * differences between ACL and reference implementation
297*/
298class uniform_real_distribution_fp16
299{
300 half min{ 0.0f }, max{ 0.0f };
301 std::uniform_real_distribution<float> neg{ min, -0.3f };
302 std::uniform_real_distribution<float> pos{ 0.3f, max };
303 std::uniform_int_distribution<uint8_t> sign_picker{ 0, 1 };
304
305public:
306 using result_type = half;
307 /** Constructor
308 *
309 * @param[in] a Minimum value of the distribution
310 * @param[in] b Maximum value of the distribution
311 */
312 explicit uniform_real_distribution_fp16(half a = half(0.0), half b = half(1.0))
313 : min(a), max(b)
314 {
315 }
316
317 /** () operator to generate next value
318 *
319 * @param[in] gen an uniform random bit generator object
320 */
321 half operator()(std::mt19937 &gen)
322 {
323 if(sign_picker(gen))
324 {
325 return (half)neg(gen);
326 }
327 return (half)pos(gen);
328 }
329};
330
Alex Gildayc357c472018-03-21 13:54:09 +0000331/** Numpy data loader */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100332class NPYLoader
333{
334public:
Alex Gildayc357c472018-03-21 13:54:09 +0000335 /** Default constructor */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100336 NPYLoader()
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100337 : _fs(), _shape(), _fortran_order(false), _typestring(), _file_layout(DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100338 {
339 }
340
341 /** Open a NPY file and reads its metadata
342 *
343 * @param[in] npy_filename File to open
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100344 * @param[in] file_layout (Optional) Layout in which the weights are stored in the file.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100345 */
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100346 void open(const std::string &npy_filename, DataLayout file_layout = DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100347 {
348 ARM_COMPUTE_ERROR_ON(is_open());
349 try
350 {
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100351 _fs.open(npy_filename, std::ios::in | std::ios::binary);
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100352 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 +0100353 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
354 _file_layout = file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100355
356 std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs);
357 }
358 catch(const std::ifstream::failure &e)
359 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100360 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", npy_filename.c_str(), e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100361 }
362 }
363 /** Return true if a NPY file is currently open */
364 bool is_open()
365 {
366 return _fs.is_open();
367 }
368
369 /** Return true if a NPY file is in fortran order */
370 bool is_fortran()
371 {
372 return _fortran_order;
373 }
374
Gian Marco0bc5a252017-12-04 13:55:08 +0000375 /** Initialise the tensor's metadata with the dimensions of the NPY file currently open
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100376 *
377 * @param[out] tensor Tensor to initialise
Gian Marco0bc5a252017-12-04 13:55:08 +0000378 * @param[in] dt Data type to use for the tensor
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100379 */
380 template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000381 void init_tensor(T &tensor, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100382 {
383 ARM_COMPUTE_ERROR_ON(!is_open());
Gian Marco0bc5a252017-12-04 13:55:08 +0000384 ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100385
386 // Use the size of the input NPY tensor
387 TensorShape shape;
388 shape.set_num_dimensions(_shape.size());
389 for(size_t i = 0; i < _shape.size(); ++i)
390 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100391 size_t src = i;
392 if(_fortran_order)
393 {
394 src = _shape.size() - 1 - i;
395 }
396 shape.set(i, _shape.at(src));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100397 }
398
Gian Marco0bc5a252017-12-04 13:55:08 +0000399 arm_compute::TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100400 tensor.allocator()->init(tensor_info);
401 }
402
403 /** Fill a tensor with the content of the currently open NPY file.
404 *
405 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
406 *
407 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
408 */
409 template <typename T>
410 void fill_tensor(T &tensor)
411 {
412 ARM_COMPUTE_ERROR_ON(!is_open());
giuros01351bd132019-08-23 14:27:30 +0100413 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 +0100414 try
415 {
416 // Map buffer if creating a CLTensor
417 map(tensor, true);
418
419 // Check if the file is large enough to fill the tensor
420 const size_t current_position = _fs.tellg();
421 _fs.seekg(0, std::ios_base::end);
422 const size_t end_position = _fs.tellg();
423 _fs.seekg(current_position, std::ios_base::beg);
424
425 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
426 "Not enough data in file");
427 ARM_COMPUTE_UNUSED(end_position);
428
429 // Check if the typestring matches the given one
430 std::string expect_typestr = get_typestring(tensor.info()->data_type());
431 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
432
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100433 bool are_layouts_different = (_file_layout != tensor.info()->data_layout());
434 // Correct dimensions (Needs to match TensorShape dimension corrections)
435 if(_shape.size() != tensor.info()->tensor_shape().num_dimensions())
436 {
437 for(int i = static_cast<int>(_shape.size()) - 1; i > 0; --i)
438 {
439 if(_shape[i] == 1)
440 {
441 _shape.pop_back();
442 }
443 else
444 {
445 break;
446 }
447 }
448 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100449
450 TensorShape permuted_shape = tensor.info()->tensor_shape();
451 arm_compute::PermutationVector perm;
452 if(are_layouts_different && tensor.info()->tensor_shape().num_dimensions() > 2)
453 {
454 perm = (tensor.info()->data_layout() == arm_compute::DataLayout::NHWC) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
455 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);
456
457 arm_compute::permute(permuted_shape, perm_vec);
458 }
459
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100460 // Validate tensor shape
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000461 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch");
Michalis Spyrou39412952018-08-14 17:06:16 +0100462 for(size_t i = 0; i < _shape.size(); ++i)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100463 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100464 ARM_COMPUTE_ERROR_ON_MSG(permuted_shape[i] != _shape[i], "Tensor dimensions mismatch");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100465 }
466
Gian Marco0bc5a252017-12-04 13:55:08 +0000467 switch(tensor.info()->data_type())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100468 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100469 case arm_compute::DataType::QASYMM8:
470 case arm_compute::DataType::S32:
Gian Marco0bc5a252017-12-04 13:55:08 +0000471 case arm_compute::DataType::F32:
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000472 case arm_compute::DataType::F16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100473 {
474 // Read data
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100475 if(!are_layouts_different && !_fortran_order && tensor.info()->padding().empty())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100476 {
477 // If tensor has no padding read directly from stream.
478 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
479 }
480 else
481 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100482 // If tensor has padding or is in fortran order accessing tensor elements through execution window.
Michalis Spyrou39412952018-08-14 17:06:16 +0100483 Window window;
484 const unsigned int num_dims = _shape.size();
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100485 if(_fortran_order)
486 {
487 for(unsigned int dim = 0; dim < num_dims; dim++)
488 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100489 permuted_shape.set(dim, _shape[num_dims - dim - 1]);
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100490 perm.set(dim, num_dims - dim - 1);
491 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100492 if(are_layouts_different)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100493 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100494 // Permute only if num_dimensions greater than 2
495 if(num_dims > 2)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100496 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100497 if(_file_layout == DataLayout::NHWC) // i.e destination is NCHW --> permute(1,2,0)
498 {
499 arm_compute::permute(perm, arm_compute::PermutationVector(1U, 2U, 0U));
500 }
501 else
502 {
503 arm_compute::permute(perm, arm_compute::PermutationVector(2U, 0U, 1U));
504 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100505 }
506 }
507 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100508 window.use_tensor_dimensions(permuted_shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100509
510 execute_window_loop(window, [&](const Coordinates & id)
511 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100512 Coordinates dst(id);
513 arm_compute::permute(dst, perm);
514 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(dst)), tensor.info()->element_size());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100515 });
516 }
517
518 break;
519 }
520 default:
Gian Marco0bc5a252017-12-04 13:55:08 +0000521 ARM_COMPUTE_ERROR("Unsupported data type");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100522 }
523
524 // Unmap buffer if creating a CLTensor
525 unmap(tensor);
526 }
527 catch(const std::ifstream::failure &e)
528 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100529 ARM_COMPUTE_ERROR_VAR("Loading NPY file: %s", e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100530 }
531 }
532
533private:
534 std::ifstream _fs;
535 std::vector<unsigned long> _shape;
536 bool _fortran_order;
537 std::string _typestring;
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100538 DataLayout _file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100539};
540
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100541/** Template helper function to save a tensor image to a PPM file.
542 *
543 * @note Only U8 and RGB888 formats supported.
544 * @note Only works with 2D tensors.
545 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
546 *
547 * @param[in] tensor The tensor to save as PPM file
548 * @param[in] ppm_filename Filename of the file to create.
549 */
550template <typename T>
551void save_to_ppm(T &tensor, const std::string &ppm_filename)
552{
553 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
554 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
555
556 std::ofstream fs;
557
558 try
559 {
560 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
561 fs.open(ppm_filename, std::ios::out | std::ios::binary);
562
563 const unsigned int width = tensor.info()->tensor_shape()[0];
564 const unsigned int height = tensor.info()->tensor_shape()[1];
565
566 fs << "P6\n"
567 << width << " " << height << " 255\n";
568
Anthony Barbier7068f992017-10-26 15:23:08 +0100569 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100570 map(tensor, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100571
572 switch(tensor.info()->format())
573 {
574 case arm_compute::Format::U8:
575 {
576 arm_compute::Window window;
577 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
578 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
579
580 arm_compute::Iterator in(&tensor, window);
581
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100582 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100583 {
584 const unsigned char value = *in.ptr();
585
586 fs << value << value << value;
587 },
588 in);
589
590 break;
591 }
592 case arm_compute::Format::RGB888:
593 {
594 arm_compute::Window window;
595 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
596 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
597
598 arm_compute::Iterator in(&tensor, window);
599
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100600 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100601 {
602 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
603 },
604 in);
605
606 break;
607 }
608 default:
609 ARM_COMPUTE_ERROR("Unsupported format");
610 }
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100611
Anthony Barbier7068f992017-10-26 15:23:08 +0100612 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100613 unmap(tensor);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100614 }
615 catch(const std::ofstream::failure &e)
616 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100617 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100618 }
619}
steniu01bee466b2017-06-21 16:45:41 +0100620
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100621/** Template helper function to save a tensor image to a NPY file.
622 *
Gian Marcobfa3b522017-12-12 10:08:38 +0000623 * @note Only F32 data type supported.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100624 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
625 *
626 * @param[in] tensor The tensor to save as NPY file
627 * @param[in] npy_filename Filename of the file to create.
628 * @param[in] fortran_order If true, save matrix in fortran order.
629 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000630template <typename T, typename U = float>
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100631void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
632{
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000633 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32, arm_compute::DataType::QASYMM8);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100634
635 std::ofstream fs;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100636 try
637 {
638 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
639 fs.open(npy_filename, std::ios::out | std::ios::binary);
640
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100641 std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100642
Pablo Tello32521432018-11-15 14:43:10 +0000643 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 +0100644 {
Pablo Tello32521432018-11-15 14:43:10 +0000645 shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i];
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100646 }
647
648 // Map buffer if creating a CLTensor
649 map(tensor, true);
650
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000651 using typestring_type = typename std::conditional<std::is_floating_point<U>::value, float, qasymm8_t>::type;
652
653 std::vector<typestring_type> tmp; /* Used only to get the typestring */
654 npy::Typestring typestring_o{ tmp };
655 std::string typestring = typestring_o.str();
656
657 std::ofstream stream(npy_filename, std::ofstream::binary);
658 npy::write_header(stream, typestring, fortran_order, shape);
659
660 arm_compute::Window window;
661 window.use_tensor_dimensions(tensor.info()->tensor_shape());
662
663 arm_compute::Iterator in(&tensor, window);
664
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100665 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100666 {
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000667 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(typestring_type));
668 },
669 in);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100670
671 // Unmap buffer if creating a CLTensor
672 unmap(tensor);
673 }
674 catch(const std::ofstream::failure &e)
675 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100676 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", npy_filename.c_str(), e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100677 }
678}
679
steniu01bee466b2017-06-21 16:45:41 +0100680/** Load the tensor with pre-trained data from a binary file
681 *
682 * @param[in] tensor The tensor to be filled. Data type supported: F32.
683 * @param[in] filename Filename of the binary file to load from.
684 */
685template <typename T>
686void load_trained_data(T &tensor, const std::string &filename)
687{
688 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
689
690 std::ifstream fs;
691
692 try
693 {
694 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
695 // Open file
696 fs.open(filename, std::ios::in | std::ios::binary);
697
698 if(!fs.good())
699 {
700 throw std::runtime_error("Could not load binary data: " + filename);
701 }
702
Anthony Barbier7068f992017-10-26 15:23:08 +0100703 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100704 map(tensor, true);
705
steniu01bee466b2017-06-21 16:45:41 +0100706 Window window;
707
708 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
709
710 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
711 {
712 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
713 }
714
715 arm_compute::Iterator in(&tensor, window);
716
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100717 execute_window_loop(window, [&](const Coordinates &)
steniu01bee466b2017-06-21 16:45:41 +0100718 {
719 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
720 },
721 in);
722
Anthony Barbier7068f992017-10-26 15:23:08 +0100723 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100724 unmap(tensor);
steniu01bee466b2017-06-21 16:45:41 +0100725 }
726 catch(const std::ofstream::failure &e)
727 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100728 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", filename.c_str(), e.what());
steniu01bee466b2017-06-21 16:45:41 +0100729 }
730}
731
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100732template <typename T>
733void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100734{
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100735 std::random_device rd;
736 std::mt19937 gen(rd());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100737
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100738 Window window;
Michalis Spyrou5e69bb42018-03-09 16:36:00 +0000739 window.use_tensor_dimensions(tensor.info()->tensor_shape());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100740
741 map(tensor, true);
742
743 Iterator it(&tensor, window);
744
Gian Marcobfa3b522017-12-12 10:08:38 +0000745 switch(tensor.info()->data_type())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100746 {
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000747 case arm_compute::DataType::F16:
748 {
749 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
750
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100751 execute_window_loop(window, [&](const Coordinates &)
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000752 {
753 *reinterpret_cast<half *>(it.ptr()) = (half)dist(gen);
754 },
755 it);
756
757 break;
758 }
Gian Marcobfa3b522017-12-12 10:08:38 +0000759 case arm_compute::DataType::F32:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100760 {
761 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
762
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100763 execute_window_loop(window, [&](const Coordinates &)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100764 {
765 *reinterpret_cast<float *>(it.ptr()) = dist(gen);
766 },
767 it);
768
769 break;
770 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100771 default:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100772 {
773 ARM_COMPUTE_ERROR("Unsupported format");
774 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100775 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100776
777 unmap(tensor);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100778}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100779
780template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000781void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100782{
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000783 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 +0100784}
Gian Marco5ca74092018-02-08 16:21:54 +0000785/** This function returns the amount of memory free reading from /proc/meminfo
786 *
787 * @return The free memory in kB
788 */
789uint64_t get_mem_free_from_meminfo();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100790
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000791/** Compare two tensors
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100792 *
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000793 * @param[in] tensor1 First tensor to be compared.
794 * @param[in] tensor2 Second tensor to be compared.
795 * @param[in] tolerance Tolerance used for the comparison.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100796 *
797 * @return The number of mismatches
798 */
799template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000800int compare_tensor(ITensor &tensor1, ITensor &tensor2, T tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100801{
802 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2);
803 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
804
805 int num_mismatches = 0;
806 Window window;
807 window.use_tensor_dimensions(tensor1.info()->tensor_shape());
808
809 map(tensor1, true);
810 map(tensor2, true);
Pablo Tello32521432018-11-15 14:43:10 +0000811
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100812 Iterator itensor1(&tensor1, window);
813 Iterator itensor2(&tensor2, window);
814
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100815 execute_window_loop(window, [&](const Coordinates &)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100816 {
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000817 if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100818 {
819 ++num_mismatches;
820 }
821 },
822 itensor1, itensor2);
823
824 unmap(itensor1);
825 unmap(itensor2);
826
827 return num_mismatches;
828}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100829} // namespace utils
830} // namespace arm_compute
831#endif /* __UTILS_UTILS_H__*/