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