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