blob: bc2fef4eb09a650b9d522cc3082cdc10be1235a0 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini053e7512018-12-28 15:05:20 +00002 * Copyright (c) 2016-2019 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
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
steniu01bee466b2017-06-21 16:45:41 +010031#include "arm_compute/core/Window.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/runtime/Tensor.h"
Michalis Spyrou6bff1952019-10-02 17:22:11 +010033#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Wunused-parameter"
Giorgio Arenacf3935f2017-10-26 17:14:13 +010035#include "libnpy/npy.hpp"
Michalis Spyrou6bff1952019-10-02 17:22:11 +010036#pragma GCC diagnostic pop
Anthony Barbier2a07e182017-08-04 18:20:27 +010037#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#ifdef ARM_COMPUTE_CL
40#include "arm_compute/core/CL/OpenCL.h"
Isabella Gottardi02aabcc2017-10-12 17:28:51 +010041#include "arm_compute/runtime/CL/CLDistribution1D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042#include "arm_compute/runtime/CL/CLTensor.h"
43#endif /* ARM_COMPUTE_CL */
Anthony Barbier7068f992017-10-26 15:23:08 +010044#ifdef ARM_COMPUTE_GC
45#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
46#endif /* ARM_COMPUTE_GC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48#include <cstdlib>
49#include <cstring>
50#include <fstream>
51#include <iostream>
Giorgio Arenacf3935f2017-10-26 17:14:13 +010052#include <random>
53#include <string>
54#include <tuple>
55#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
57namespace arm_compute
58{
59namespace utils
60{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010061/** Supported image types */
62enum class ImageType
63{
64 UNKNOWN,
65 PPM,
66 JPEG
67};
68
Anthony Barbier6db0ff52018-01-05 10:59:12 +000069/** Abstract Example class.
70 *
71 * All examples have to inherit from this class.
72 */
73class Example
74{
75public:
Alex Gildayc357c472018-03-21 13:54:09 +000076 /** Setup the example.
77 *
78 * @param[in] argc Argument count.
79 * @param[in] argv Argument values.
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010080 *
81 * @return True in case of no errors in setup else false
Alex Gildayc357c472018-03-21 13:54:09 +000082 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010083 virtual bool do_setup(int argc, char **argv)
84 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +010085 ARM_COMPUTE_UNUSED(argc, argv);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010086 return true;
87 };
Alex Gildayc357c472018-03-21 13:54:09 +000088 /** Run the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000089 virtual void do_run() {};
Alex Gildayc357c472018-03-21 13:54:09 +000090 /** Teardown the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000091 virtual void do_teardown() {};
92
93 /** Default destructor. */
94 virtual ~Example() = default;
95};
96
97/** Run an example and handle the potential exceptions it throws
98 *
99 * @param[in] argc Number of command line arguments
100 * @param[in] argv Command line arguments
101 * @param[in] example Example to run
102 */
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100103int run_example(int argc, char **argv, std::unique_ptr<Example> example);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000104
105template <typename T>
106int run_example(int argc, char **argv)
107{
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100108 return run_example(argc, argv, support::cpp14::make_unique<T>());
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000109}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
111/** Draw a RGB rectangular window for the detected object
112 *
113 * @param[in, out] tensor Input tensor where the rectangle will be drawn on. Format supported: RGB888
114 * @param[in] rect Geometry of the rectangular window
115 * @param[in] r Red colour to use
116 * @param[in] g Green colour to use
117 * @param[in] b Blue colour to use
118 */
119void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b);
120
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100121/** Gets image type given a file
122 *
123 * @param[in] filename File to identify its image type
124 *
125 * @return Image type
126 */
127ImageType get_image_type_from_file(const std::string &filename);
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129/** Parse the ppm header from an input file stream. At the end of the execution,
130 * the file position pointer will be located at the first pixel stored in the ppm file
131 *
132 * @param[in] fs Input file stream to parse
133 *
134 * @return The width, height and max value stored in the header of the PPM file
135 */
136std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
137
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100138/** Parse the npy header from an input file stream. At the end of the execution,
139 * the file position pointer will be located at the first pixel stored in the npy file //TODO
140 *
141 * @param[in] fs Input file stream to parse
142 *
143 * @return The width and height stored in the header of the NPY file
144 */
145std::tuple<std::vector<unsigned long>, bool, std::string> parse_npy_header(std::ifstream &fs);
146
147/** Obtain numpy type string from DataType.
148 *
149 * @param[in] data_type Data type.
150 *
151 * @return numpy type string.
152 */
153inline std::string get_typestring(DataType data_type)
154{
155 // Check endianness
156 const unsigned int i = 1;
157 const char *c = reinterpret_cast<const char *>(&i);
158 std::string endianness;
159 if(*c == 1)
160 {
161 endianness = std::string("<");
162 }
163 else
164 {
165 endianness = std::string(">");
166 }
167 const std::string no_endianness("|");
168
169 switch(data_type)
170 {
171 case DataType::U8:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000172 case DataType::QASYMM8:
Michalis Spyrouc8530212019-08-22 11:44:04 +0100173 case DataType::QASYMM8_PER_CHANNEL:
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}
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100247
248/** Maps a distribution if needed
249 *
250 * @param[in] distribution Distribution to be mapped
251 * @param[in] blocking Specified if map is blocking or not
252 */
253inline void map(CLDistribution1D &distribution, bool blocking)
254{
255 distribution.map(blocking);
256}
257
258/** Unmaps a distribution if needed
259 *
260 * @param distribution Distribution to be unmapped
261 */
262inline void unmap(CLDistribution1D &distribution)
263{
264 distribution.unmap();
265}
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100266#endif /* ARM_COMPUTE_CL */
267
Anthony Barbier7068f992017-10-26 15:23:08 +0100268#ifdef ARM_COMPUTE_GC
269/** Maps a tensor if needed
270 *
271 * @param[in] tensor Tensor to be mapped
272 * @param[in] blocking Specified if map is blocking or not
273 */
274inline void map(GCTensor &tensor, bool blocking)
275{
276 tensor.map(blocking);
277}
278
279/** Unmaps a tensor if needed
280 *
281 * @param tensor Tensor to be unmapped
282 */
283inline void unmap(GCTensor &tensor)
284{
285 tensor.unmap();
286}
287#endif /* ARM_COMPUTE_GC */
288
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000289/** Specialized class to generate random non-zero FP16 values.
290 * uniform_real_distribution<half> generates values that get rounded off to zero, causing
291 * differences between ACL and reference implementation
292*/
293class uniform_real_distribution_fp16
294{
295 half min{ 0.0f }, max{ 0.0f };
296 std::uniform_real_distribution<float> neg{ min, -0.3f };
297 std::uniform_real_distribution<float> pos{ 0.3f, max };
298 std::uniform_int_distribution<uint8_t> sign_picker{ 0, 1 };
299
300public:
301 using result_type = half;
302 /** Constructor
303 *
304 * @param[in] a Minimum value of the distribution
305 * @param[in] b Maximum value of the distribution
306 */
307 explicit uniform_real_distribution_fp16(half a = half(0.0), half b = half(1.0))
308 : min(a), max(b)
309 {
310 }
311
312 /** () operator to generate next value
313 *
314 * @param[in] gen an uniform random bit generator object
315 */
316 half operator()(std::mt19937 &gen)
317 {
318 if(sign_picker(gen))
319 {
320 return (half)neg(gen);
321 }
322 return (half)pos(gen);
323 }
324};
325
Alex Gildayc357c472018-03-21 13:54:09 +0000326/** Numpy data loader */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100327class NPYLoader
328{
329public:
Alex Gildayc357c472018-03-21 13:54:09 +0000330 /** Default constructor */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100331 NPYLoader()
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100332 : _fs(), _shape(), _fortran_order(false), _typestring(), _file_layout(DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100333 {
334 }
335
336 /** Open a NPY file and reads its metadata
337 *
338 * @param[in] npy_filename File to open
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100339 * @param[in] file_layout (Optional) Layout in which the weights are stored in the file.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100340 */
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100341 void open(const std::string &npy_filename, DataLayout file_layout = DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100342 {
343 ARM_COMPUTE_ERROR_ON(is_open());
344 try
345 {
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100346 _fs.open(npy_filename, std::ios::in | std::ios::binary);
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100347 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 +0100348 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
349 _file_layout = file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100350
351 std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs);
352 }
353 catch(const std::ifstream::failure &e)
354 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100355 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", npy_filename.c_str(), e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100356 }
357 }
358 /** Return true if a NPY file is currently open */
359 bool is_open()
360 {
361 return _fs.is_open();
362 }
363
364 /** Return true if a NPY file is in fortran order */
365 bool is_fortran()
366 {
367 return _fortran_order;
368 }
369
Gian Marco0bc5a252017-12-04 13:55:08 +0000370 /** Initialise the tensor's metadata with the dimensions of the NPY file currently open
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100371 *
372 * @param[out] tensor Tensor to initialise
Gian Marco0bc5a252017-12-04 13:55:08 +0000373 * @param[in] dt Data type to use for the tensor
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100374 */
375 template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000376 void init_tensor(T &tensor, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100377 {
378 ARM_COMPUTE_ERROR_ON(!is_open());
Gian Marco0bc5a252017-12-04 13:55:08 +0000379 ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100380
381 // Use the size of the input NPY tensor
382 TensorShape shape;
383 shape.set_num_dimensions(_shape.size());
384 for(size_t i = 0; i < _shape.size(); ++i)
385 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100386 size_t src = i;
387 if(_fortran_order)
388 {
389 src = _shape.size() - 1 - i;
390 }
391 shape.set(i, _shape.at(src));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100392 }
393
Gian Marco0bc5a252017-12-04 13:55:08 +0000394 arm_compute::TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100395 tensor.allocator()->init(tensor_info);
396 }
397
398 /** Fill a tensor with the content of the currently open NPY file.
399 *
400 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
401 *
402 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
403 */
404 template <typename T>
405 void fill_tensor(T &tensor)
406 {
407 ARM_COMPUTE_ERROR_ON(!is_open());
giuros01351bd132019-08-23 14:27:30 +0100408 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 +0100409 try
410 {
411 // Map buffer if creating a CLTensor
412 map(tensor, true);
413
414 // Check if the file is large enough to fill the tensor
415 const size_t current_position = _fs.tellg();
416 _fs.seekg(0, std::ios_base::end);
417 const size_t end_position = _fs.tellg();
418 _fs.seekg(current_position, std::ios_base::beg);
419
420 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
421 "Not enough data in file");
422 ARM_COMPUTE_UNUSED(end_position);
423
424 // Check if the typestring matches the given one
425 std::string expect_typestr = get_typestring(tensor.info()->data_type());
426 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
427
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100428 bool are_layouts_different = (_file_layout != tensor.info()->data_layout());
429 // Correct dimensions (Needs to match TensorShape dimension corrections)
430 if(_shape.size() != tensor.info()->tensor_shape().num_dimensions())
431 {
432 for(int i = static_cast<int>(_shape.size()) - 1; i > 0; --i)
433 {
434 if(_shape[i] == 1)
435 {
436 _shape.pop_back();
437 }
438 else
439 {
440 break;
441 }
442 }
443 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100444
445 TensorShape permuted_shape = tensor.info()->tensor_shape();
446 arm_compute::PermutationVector perm;
447 if(are_layouts_different && tensor.info()->tensor_shape().num_dimensions() > 2)
448 {
449 perm = (tensor.info()->data_layout() == arm_compute::DataLayout::NHWC) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
450 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);
451
452 arm_compute::permute(permuted_shape, perm_vec);
453 }
454
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100455 // Validate tensor shape
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000456 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch");
Michalis Spyrou39412952018-08-14 17:06:16 +0100457 for(size_t i = 0; i < _shape.size(); ++i)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100458 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100459 ARM_COMPUTE_ERROR_ON_MSG(permuted_shape[i] != _shape[i], "Tensor dimensions mismatch");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100460 }
461
Gian Marco0bc5a252017-12-04 13:55:08 +0000462 switch(tensor.info()->data_type())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100463 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100464 case arm_compute::DataType::QASYMM8:
465 case arm_compute::DataType::S32:
Gian Marco0bc5a252017-12-04 13:55:08 +0000466 case arm_compute::DataType::F32:
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000467 case arm_compute::DataType::F16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100468 {
469 // Read data
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100470 if(!are_layouts_different && !_fortran_order && tensor.info()->padding().empty())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100471 {
472 // If tensor has no padding read directly from stream.
473 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
474 }
475 else
476 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100477 // If tensor has padding or is in fortran order accessing tensor elements through execution window.
Michalis Spyrou39412952018-08-14 17:06:16 +0100478 Window window;
479 const unsigned int num_dims = _shape.size();
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100480 if(_fortran_order)
481 {
482 for(unsigned int dim = 0; dim < num_dims; dim++)
483 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100484 permuted_shape.set(dim, _shape[num_dims - dim - 1]);
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100485 perm.set(dim, num_dims - dim - 1);
486 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100487 if(are_layouts_different)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100488 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100489 // Permute only if num_dimensions greater than 2
490 if(num_dims > 2)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100491 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100492 if(_file_layout == DataLayout::NHWC) // i.e destination is NCHW --> permute(1,2,0)
493 {
494 arm_compute::permute(perm, arm_compute::PermutationVector(1U, 2U, 0U));
495 }
496 else
497 {
498 arm_compute::permute(perm, arm_compute::PermutationVector(2U, 0U, 1U));
499 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100500 }
501 }
502 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100503 window.use_tensor_dimensions(permuted_shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100504
505 execute_window_loop(window, [&](const Coordinates & id)
506 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100507 Coordinates dst(id);
508 arm_compute::permute(dst, perm);
509 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(dst)), tensor.info()->element_size());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100510 });
511 }
512
513 break;
514 }
515 default:
Gian Marco0bc5a252017-12-04 13:55:08 +0000516 ARM_COMPUTE_ERROR("Unsupported data type");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100517 }
518
519 // Unmap buffer if creating a CLTensor
520 unmap(tensor);
521 }
522 catch(const std::ifstream::failure &e)
523 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100524 ARM_COMPUTE_ERROR_VAR("Loading NPY file: %s", e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100525 }
526 }
527
528private:
529 std::ifstream _fs;
530 std::vector<unsigned long> _shape;
531 bool _fortran_order;
532 std::string _typestring;
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100533 DataLayout _file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100534};
535
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100536/** Template helper function to save a tensor image to a PPM file.
537 *
538 * @note Only U8 and RGB888 formats supported.
539 * @note Only works with 2D tensors.
540 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
541 *
542 * @param[in] tensor The tensor to save as PPM file
543 * @param[in] ppm_filename Filename of the file to create.
544 */
545template <typename T>
546void save_to_ppm(T &tensor, const std::string &ppm_filename)
547{
548 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
549 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
550
551 std::ofstream fs;
552
553 try
554 {
555 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
556 fs.open(ppm_filename, std::ios::out | std::ios::binary);
557
558 const unsigned int width = tensor.info()->tensor_shape()[0];
559 const unsigned int height = tensor.info()->tensor_shape()[1];
560
561 fs << "P6\n"
562 << width << " " << height << " 255\n";
563
Anthony Barbier7068f992017-10-26 15:23:08 +0100564 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100565 map(tensor, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100566
567 switch(tensor.info()->format())
568 {
569 case arm_compute::Format::U8:
570 {
571 arm_compute::Window window;
572 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
573 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
574
575 arm_compute::Iterator in(&tensor, window);
576
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100577 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100578 {
579 const unsigned char value = *in.ptr();
580
581 fs << value << value << value;
582 },
583 in);
584
585 break;
586 }
587 case arm_compute::Format::RGB888:
588 {
589 arm_compute::Window window;
590 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
591 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
592
593 arm_compute::Iterator in(&tensor, window);
594
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100595 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100596 {
597 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
598 },
599 in);
600
601 break;
602 }
603 default:
604 ARM_COMPUTE_ERROR("Unsupported format");
605 }
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100606
Anthony Barbier7068f992017-10-26 15:23:08 +0100607 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100608 unmap(tensor);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100609 }
610 catch(const std::ofstream::failure &e)
611 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100612 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100613 }
614}
steniu01bee466b2017-06-21 16:45:41 +0100615
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100616/** Template helper function to save a tensor image to a NPY file.
617 *
Gian Marcobfa3b522017-12-12 10:08:38 +0000618 * @note Only F32 data type supported.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100619 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
620 *
621 * @param[in] tensor The tensor to save as NPY file
622 * @param[in] npy_filename Filename of the file to create.
623 * @param[in] fortran_order If true, save matrix in fortran order.
624 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000625template <typename T, typename U = float>
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100626void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
627{
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000628 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32, arm_compute::DataType::QASYMM8);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100629
630 std::ofstream fs;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100631 try
632 {
633 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
634 fs.open(npy_filename, std::ios::out | std::ios::binary);
635
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100636 std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100637
Pablo Tello32521432018-11-15 14:43:10 +0000638 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 +0100639 {
Pablo Tello32521432018-11-15 14:43:10 +0000640 shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i];
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100641 }
642
643 // Map buffer if creating a CLTensor
644 map(tensor, true);
645
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000646 using typestring_type = typename std::conditional<std::is_floating_point<U>::value, float, qasymm8_t>::type;
647
648 std::vector<typestring_type> tmp; /* Used only to get the typestring */
649 npy::Typestring typestring_o{ tmp };
650 std::string typestring = typestring_o.str();
651
652 std::ofstream stream(npy_filename, std::ofstream::binary);
653 npy::write_header(stream, typestring, fortran_order, shape);
654
655 arm_compute::Window window;
656 window.use_tensor_dimensions(tensor.info()->tensor_shape());
657
658 arm_compute::Iterator in(&tensor, window);
659
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100660 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates &)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100661 {
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000662 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(typestring_type));
663 },
664 in);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100665
666 // Unmap buffer if creating a CLTensor
667 unmap(tensor);
668 }
669 catch(const std::ofstream::failure &e)
670 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100671 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", npy_filename.c_str(), e.what());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100672 }
673}
674
steniu01bee466b2017-06-21 16:45:41 +0100675/** Load the tensor with pre-trained data from a binary file
676 *
677 * @param[in] tensor The tensor to be filled. Data type supported: F32.
678 * @param[in] filename Filename of the binary file to load from.
679 */
680template <typename T>
681void load_trained_data(T &tensor, const std::string &filename)
682{
683 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
684
685 std::ifstream fs;
686
687 try
688 {
689 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
690 // Open file
691 fs.open(filename, std::ios::in | std::ios::binary);
692
693 if(!fs.good())
694 {
695 throw std::runtime_error("Could not load binary data: " + filename);
696 }
697
Anthony Barbier7068f992017-10-26 15:23:08 +0100698 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100699 map(tensor, true);
700
steniu01bee466b2017-06-21 16:45:41 +0100701 Window window;
702
703 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
704
705 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
706 {
707 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
708 }
709
710 arm_compute::Iterator in(&tensor, window);
711
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100712 execute_window_loop(window, [&](const Coordinates &)
steniu01bee466b2017-06-21 16:45:41 +0100713 {
714 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
715 },
716 in);
717
Anthony Barbier7068f992017-10-26 15:23:08 +0100718 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100719 unmap(tensor);
steniu01bee466b2017-06-21 16:45:41 +0100720 }
721 catch(const std::ofstream::failure &e)
722 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100723 ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", filename.c_str(), e.what());
steniu01bee466b2017-06-21 16:45:41 +0100724 }
725}
726
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100727template <typename T>
728void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100729{
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100730 std::random_device rd;
731 std::mt19937 gen(rd());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100732
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100733 Window window;
Michalis Spyrou5e69bb42018-03-09 16:36:00 +0000734 window.use_tensor_dimensions(tensor.info()->tensor_shape());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100735
736 map(tensor, true);
737
738 Iterator it(&tensor, window);
739
Gian Marcobfa3b522017-12-12 10:08:38 +0000740 switch(tensor.info()->data_type())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100741 {
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000742 case arm_compute::DataType::F16:
743 {
744 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
745
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100746 execute_window_loop(window, [&](const Coordinates &)
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000747 {
748 *reinterpret_cast<half *>(it.ptr()) = (half)dist(gen);
749 },
750 it);
751
752 break;
753 }
Gian Marcobfa3b522017-12-12 10:08:38 +0000754 case arm_compute::DataType::F32:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100755 {
756 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
757
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100758 execute_window_loop(window, [&](const Coordinates &)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100759 {
760 *reinterpret_cast<float *>(it.ptr()) = dist(gen);
761 },
762 it);
763
764 break;
765 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100766 default:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100767 {
768 ARM_COMPUTE_ERROR("Unsupported format");
769 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100770 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100771
772 unmap(tensor);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100773}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100774
775template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000776void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100777{
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000778 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 +0100779}
Gian Marco5ca74092018-02-08 16:21:54 +0000780/** This function returns the amount of memory free reading from /proc/meminfo
781 *
782 * @return The free memory in kB
783 */
784uint64_t get_mem_free_from_meminfo();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100785
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000786/** Compare two tensors
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100787 *
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000788 * @param[in] tensor1 First tensor to be compared.
789 * @param[in] tensor2 Second tensor to be compared.
790 * @param[in] tolerance Tolerance used for the comparison.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100791 *
792 * @return The number of mismatches
793 */
794template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000795int compare_tensor(ITensor &tensor1, ITensor &tensor2, T tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100796{
797 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2);
798 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
799
800 int num_mismatches = 0;
801 Window window;
802 window.use_tensor_dimensions(tensor1.info()->tensor_shape());
803
804 map(tensor1, true);
805 map(tensor2, true);
Pablo Tello32521432018-11-15 14:43:10 +0000806
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100807 Iterator itensor1(&tensor1, window);
808 Iterator itensor2(&tensor2, window);
809
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100810 execute_window_loop(window, [&](const Coordinates &)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100811 {
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000812 if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100813 {
814 ++num_mismatches;
815 }
816 },
817 itensor1, itensor2);
818
819 unmap(itensor1);
820 unmap(itensor2);
821
822 return num_mismatches;
823}
Pablo Tellodb9116f2019-07-11 16:50:37 +0100824
825/** This function saves opencl kernels library to a file
826 *
827 * @param[in] filename Name of the file to be used to save the library
828 */
829void save_program_cache_to_file(const std::string &filename = "cache.bin");
830
831/** This function loads prebuilt opencl kernels from a file
832 *
833 * @param[in] filename Name of the file to be used to load the kernels
834 */
835void restore_program_cache_from_file(const std::string &filename = "cache.bin");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100836} // namespace utils
837} // namespace arm_compute
838#endif /* __UTILS_UTILS_H__*/