Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__ |
| 25 | #define __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__ |
| 26 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/TensorShape.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Window.h" |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 34 | #include "libnpy/npy.hpp" |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 35 | #include "tests/RawTensor.h" |
| 36 | #include "tests/TensorCache.h" |
| 37 | #include "tests/Utils.h" |
Anthony Barbier | f6705ec | 2017-09-28 12:01:10 +0100 | [diff] [blame] | 38 | #include "tests/framework/Exceptions.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | #include <algorithm> |
| 41 | #include <cstddef> |
| 42 | #include <fstream> |
| 43 | #include <random> |
| 44 | #include <string> |
| 45 | #include <type_traits> |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 46 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | |
| 48 | namespace arm_compute |
| 49 | { |
| 50 | namespace test |
| 51 | { |
| 52 | /** Factory class to create and fill tensors. |
| 53 | * |
| 54 | * Allows to initialise tensors from loaded images or by specifying the shape |
| 55 | * explicitly. Furthermore, provides methods to fill tensors with the content of |
| 56 | * loaded images or with random values. |
| 57 | */ |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 58 | class AssetsLibrary final |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | { |
| 60 | public: |
John Richardson | 70f946b | 2017-10-02 16:52:16 +0100 | [diff] [blame] | 61 | /** Initialises the library with a @p path to the assets directory. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | * Furthermore, sets the seed for the random generator to @p seed. |
| 63 | * |
John Richardson | 70f946b | 2017-10-02 16:52:16 +0100 | [diff] [blame] | 64 | * @param[in] path Path to load assets from. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | * @param[in] seed Seed used to initialise the random number generator. |
| 66 | */ |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 67 | AssetsLibrary(std::string path, std::random_device::result_type seed); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | |
John Richardson | 70f946b | 2017-10-02 16:52:16 +0100 | [diff] [blame] | 69 | /** Path to assets directory used to initialise library. */ |
| 70 | std::string path() const; |
| 71 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | /** Seed that is used to fill tensors with random values. */ |
| 73 | std::random_device::result_type seed() const; |
| 74 | |
Giorgio Arena | fda4618 | 2017-06-16 13:57:33 +0100 | [diff] [blame] | 75 | /** Provides a tensor shape for the specified image. |
| 76 | * |
| 77 | * @param[in] name Image file used to look up the raw tensor. |
| 78 | */ |
| 79 | TensorShape get_image_shape(const std::string &name); |
| 80 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | /** Provides a contant raw tensor for the specified image. |
| 82 | * |
| 83 | * @param[in] name Image file used to look up the raw tensor. |
| 84 | */ |
| 85 | const RawTensor &get(const std::string &name) const; |
| 86 | |
| 87 | /** Provides a raw tensor for the specified image. |
| 88 | * |
| 89 | * @param[in] name Image file used to look up the raw tensor. |
| 90 | */ |
| 91 | RawTensor get(const std::string &name); |
| 92 | |
| 93 | /** Creates an uninitialised raw tensor with the given @p data_type and @p |
| 94 | * num_channels. The shape is derived from the specified image. |
| 95 | * |
| 96 | * @param[in] name Image file used to initialise the tensor. |
| 97 | * @param[in] data_type Data type used to initialise the tensor. |
| 98 | * @param[in] num_channels Number of channels used to initialise the tensor. |
| 99 | */ |
| 100 | RawTensor get(const std::string &name, DataType data_type, int num_channels = 1) const; |
| 101 | |
| 102 | /** Provides a contant raw tensor for the specified image after it has been |
| 103 | * converted to @p format. |
| 104 | * |
| 105 | * @param[in] name Image file used to look up the raw tensor. |
| 106 | * @param[in] format Format used to look up the raw tensor. |
| 107 | */ |
| 108 | const RawTensor &get(const std::string &name, Format format) const; |
| 109 | |
| 110 | /** Provides a raw tensor for the specified image after it has been |
| 111 | * converted to @p format. |
| 112 | * |
| 113 | * @param[in] name Image file used to look up the raw tensor. |
| 114 | * @param[in] format Format used to look up the raw tensor. |
| 115 | */ |
| 116 | RawTensor get(const std::string &name, Format format); |
| 117 | |
| 118 | /** Provides a contant raw tensor for the specified channel after it has |
| 119 | * been extracted form the given image. |
| 120 | * |
| 121 | * @param[in] name Image file used to look up the raw tensor. |
| 122 | * @param[in] channel Channel used to look up the raw tensor. |
| 123 | * |
| 124 | * @note The channel has to be unambiguous so that the format can be |
| 125 | * inferred automatically. |
| 126 | */ |
| 127 | const RawTensor &get(const std::string &name, Channel channel) const; |
| 128 | |
| 129 | /** Provides a raw tensor for the specified channel after it has been |
| 130 | * extracted form the given image. |
| 131 | * |
| 132 | * @param[in] name Image file used to look up the raw tensor. |
| 133 | * @param[in] channel Channel used to look up the raw tensor. |
| 134 | * |
| 135 | * @note The channel has to be unambiguous so that the format can be |
| 136 | * inferred automatically. |
| 137 | */ |
| 138 | RawTensor get(const std::string &name, Channel channel); |
| 139 | |
| 140 | /** Provides a constant raw tensor for the specified channel after it has |
| 141 | * been extracted form the given image formatted to @p format. |
| 142 | * |
| 143 | * @param[in] name Image file used to look up the raw tensor. |
| 144 | * @param[in] format Format used to look up the raw tensor. |
| 145 | * @param[in] channel Channel used to look up the raw tensor. |
| 146 | */ |
| 147 | const RawTensor &get(const std::string &name, Format format, Channel channel) const; |
| 148 | |
| 149 | /** Provides a raw tensor for the specified channel after it has been |
| 150 | * extracted form the given image formatted to @p format. |
| 151 | * |
| 152 | * @param[in] name Image file used to look up the raw tensor. |
| 153 | * @param[in] format Format used to look up the raw tensor. |
| 154 | * @param[in] channel Channel used to look up the raw tensor. |
| 155 | */ |
| 156 | RawTensor get(const std::string &name, Format format, Channel channel); |
| 157 | |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 158 | /** Puts garbage values all around the tensor for testing purposes |
| 159 | * |
| 160 | * @param[in, out] tensor To be filled tensor. |
| 161 | * @param[in] distribution Distribution used to fill the tensor's surroundings. |
| 162 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 163 | */ |
| 164 | template <typename T, typename D> |
| 165 | void fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const; |
| 166 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | /** Fills the specified @p tensor with random values drawn from @p |
| 168 | * distribution. |
| 169 | * |
| 170 | * @param[in, out] tensor To be filled tensor. |
| 171 | * @param[in] distribution Distribution used to fill the tensor. |
| 172 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 173 | * |
| 174 | * @note The @p distribution has to provide operator(Generator &) which |
| 175 | * will be used to draw samples. |
| 176 | */ |
| 177 | template <typename T, typename D> |
| 178 | void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const; |
| 179 | |
| 180 | /** Fills the specified @p raw tensor with random values drawn from @p |
| 181 | * distribution. |
| 182 | * |
| 183 | * @param[in, out] raw To be filled raw. |
| 184 | * @param[in] distribution Distribution used to fill the tensor. |
| 185 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 186 | * |
| 187 | * @note The @p distribution has to provide operator(Generator &) which |
| 188 | * will be used to draw samples. |
| 189 | */ |
| 190 | template <typename D> |
| 191 | void fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const; |
| 192 | |
| 193 | /** Fills the specified @p tensor with the content of the specified image |
| 194 | * converted to the given format. |
| 195 | * |
| 196 | * @param[in, out] tensor To be filled tensor. |
| 197 | * @param[in] name Image file used to fill the tensor. |
| 198 | * @param[in] format Format of the image used to fill the tensor. |
| 199 | * |
| 200 | * @warning No check is performed that the specified format actually |
| 201 | * matches the format of the tensor. |
| 202 | */ |
| 203 | template <typename T> |
| 204 | void fill(T &&tensor, const std::string &name, Format format) const; |
| 205 | |
| 206 | /** Fills the raw tensor with the content of the specified image |
| 207 | * converted to the given format. |
| 208 | * |
| 209 | * @param[in, out] raw To be filled raw tensor. |
| 210 | * @param[in] name Image file used to fill the tensor. |
| 211 | * @param[in] format Format of the image used to fill the tensor. |
| 212 | * |
| 213 | * @warning No check is performed that the specified format actually |
| 214 | * matches the format of the tensor. |
| 215 | */ |
| 216 | void fill(RawTensor &raw, const std::string &name, Format format) const; |
| 217 | |
| 218 | /** Fills the specified @p tensor with the content of the specified channel |
| 219 | * extracted from the given image. |
| 220 | * |
| 221 | * @param[in, out] tensor To be filled tensor. |
| 222 | * @param[in] name Image file used to fill the tensor. |
| 223 | * @param[in] channel Channel of the image used to fill the tensor. |
| 224 | * |
| 225 | * @note The channel has to be unambiguous so that the format can be |
| 226 | * inferred automatically. |
| 227 | * |
| 228 | * @warning No check is performed that the specified format actually |
| 229 | * matches the format of the tensor. |
| 230 | */ |
| 231 | template <typename T> |
| 232 | void fill(T &&tensor, const std::string &name, Channel channel) const; |
| 233 | |
| 234 | /** Fills the raw tensor with the content of the specified channel |
| 235 | * extracted from the given image. |
| 236 | * |
| 237 | * @param[in, out] raw To be filled raw tensor. |
| 238 | * @param[in] name Image file used to fill the tensor. |
| 239 | * @param[in] channel Channel of the image used to fill the tensor. |
| 240 | * |
| 241 | * @note The channel has to be unambiguous so that the format can be |
| 242 | * inferred automatically. |
| 243 | * |
| 244 | * @warning No check is performed that the specified format actually |
| 245 | * matches the format of the tensor. |
| 246 | */ |
| 247 | void fill(RawTensor &raw, const std::string &name, Channel channel) const; |
| 248 | |
| 249 | /** Fills the specified @p tensor with the content of the specified channel |
| 250 | * extracted from the given image after it has been converted to the given |
| 251 | * format. |
| 252 | * |
| 253 | * @param[in, out] tensor To be filled tensor. |
| 254 | * @param[in] name Image file used to fill the tensor. |
| 255 | * @param[in] format Format of the image used to fill the tensor. |
| 256 | * @param[in] channel Channel of the image used to fill the tensor. |
| 257 | * |
| 258 | * @warning No check is performed that the specified format actually |
| 259 | * matches the format of the tensor. |
| 260 | */ |
| 261 | template <typename T> |
| 262 | void fill(T &&tensor, const std::string &name, Format format, Channel channel) const; |
| 263 | |
| 264 | /** Fills the raw tensor with the content of the specified channel |
| 265 | * extracted from the given image after it has been converted to the given |
| 266 | * format. |
| 267 | * |
| 268 | * @param[in, out] raw To be filled raw tensor. |
| 269 | * @param[in] name Image file used to fill the tensor. |
| 270 | * @param[in] format Format of the image used to fill the tensor. |
| 271 | * @param[in] channel Channel of the image used to fill the tensor. |
| 272 | * |
| 273 | * @warning No check is performed that the specified format actually |
| 274 | * matches the format of the tensor. |
| 275 | */ |
| 276 | void fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const; |
| 277 | |
| 278 | /** Fill a tensor with uniform distribution across the range of its type |
| 279 | * |
| 280 | * @param[in, out] tensor To be filled tensor. |
| 281 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 282 | */ |
| 283 | template <typename T> |
| 284 | void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const; |
| 285 | |
| 286 | /** Fill a tensor with uniform distribution across the a specified range |
| 287 | * |
| 288 | * @param[in, out] tensor To be filled tensor. |
| 289 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 290 | * @param[in] low lowest value in the range (inclusive) |
| 291 | * @param[in] high highest value in the range (inclusive) |
| 292 | * |
| 293 | * @note @p low and @p high must be of the same type as the data type of @p tensor |
| 294 | */ |
| 295 | template <typename T, typename D> |
| 296 | void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const; |
| 297 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 298 | /** Fills the specified @p tensor with data loaded from .npy (numpy binary) in specified path. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 299 | * |
| 300 | * @param[in, out] tensor To be filled tensor. |
| 301 | * @param[in] name Data file. |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 302 | * |
| 303 | * @note The numpy array stored in the binary .npy file must be row-major in the sense that it |
| 304 | * must store elements within a row consecutively in the memory, then rows within a 2D slice, |
| 305 | * then 2D slices within a 3D slice and so on. Note that it imposes no restrictions on what |
| 306 | * indexing convention is used in the numpy array. That is, the numpy array can be either fortran |
| 307 | * style or C style as long as it adheres to the rule above. |
| 308 | * |
| 309 | * More concretely, the orders of dimensions for each style are as follows: |
| 310 | * C-style (numpy default): |
| 311 | * array[HigherDims..., Z, Y, X] |
| 312 | * Fortran style: |
| 313 | * array[X, Y, Z, HigherDims...] |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 314 | */ |
| 315 | template <typename T> |
| 316 | void fill_layer_data(T &&tensor, std::string name) const; |
| 317 | |
| 318 | private: |
| 319 | // Function prototype to convert between image formats. |
| 320 | using Converter = void (*)(const RawTensor &src, RawTensor &dst); |
| 321 | // Function prototype to extract a channel from an image. |
| 322 | using Extractor = void (*)(const RawTensor &src, RawTensor &dst); |
| 323 | // Function prototype to load an image file. |
| 324 | using Loader = RawTensor (*)(const std::string &path); |
| 325 | |
| 326 | const Converter &get_converter(Format src, Format dst) const; |
| 327 | const Converter &get_converter(DataType src, Format dst) const; |
| 328 | const Converter &get_converter(Format src, DataType dst) const; |
| 329 | const Converter &get_converter(DataType src, DataType dst) const; |
| 330 | const Extractor &get_extractor(Format format, Channel) const; |
| 331 | const Loader &get_loader(const std::string &extension) const; |
| 332 | |
| 333 | /** Creates a raw tensor from the specified image. |
| 334 | * |
| 335 | * @param[in] name To be loaded image file. |
| 336 | * |
| 337 | * @note If use_single_image is true @p name is ignored and the user image |
| 338 | * is loaded instead. |
| 339 | */ |
| 340 | RawTensor load_image(const std::string &name) const; |
| 341 | |
| 342 | /** Provides a raw tensor for the specified image and format. |
| 343 | * |
| 344 | * @param[in] name Image file used to look up the raw tensor. |
| 345 | * @param[in] format Format used to look up the raw tensor. |
| 346 | * |
| 347 | * If the tensor has already been requested before the cached version will |
| 348 | * be returned. Otherwise the tensor will be added to the cache. |
| 349 | * |
| 350 | * @note If use_single_image is true @p name is ignored and the user image |
| 351 | * is loaded instead. |
| 352 | */ |
| 353 | const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format) const; |
| 354 | |
| 355 | /** Provides a raw tensor for the specified image, format and channel. |
| 356 | * |
| 357 | * @param[in] name Image file used to look up the raw tensor. |
| 358 | * @param[in] format Format used to look up the raw tensor. |
| 359 | * @param[in] channel Channel used to look up the raw tensor. |
| 360 | * |
| 361 | * If the tensor has already been requested before the cached version will |
| 362 | * be returned. Otherwise the tensor will be added to the cache. |
| 363 | * |
| 364 | * @note If use_single_image is true @p name is ignored and the user image |
| 365 | * is loaded instead. |
| 366 | */ |
| 367 | const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const; |
| 368 | |
| 369 | mutable TensorCache _cache{}; |
| 370 | mutable std::mutex _format_lock{}; |
| 371 | mutable std::mutex _channel_lock{}; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 372 | const std::string _library_path; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 373 | std::random_device::result_type _seed; |
| 374 | }; |
| 375 | |
| 376 | template <typename T, typename D> |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 377 | void AssetsLibrary::fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const |
| 378 | { |
| 379 | const PaddingSize padding_size = tensor.padding(); |
| 380 | |
| 381 | Window window; |
| 382 | window.set(0, Window::Dimension(-padding_size.left, tensor.shape()[0] + padding_size.right, 1)); |
| 383 | window.set(1, Window::Dimension(-padding_size.top, tensor.shape()[1] + padding_size.bottom, 1)); |
| 384 | |
| 385 | std::mt19937 gen(_seed); |
| 386 | |
| 387 | execute_window_loop(window, [&](const Coordinates & id) |
| 388 | { |
| 389 | TensorShape shape = tensor.shape(); |
| 390 | |
| 391 | // If outside of valid region |
| 392 | if(id.x() < 0 || id.x() >= static_cast<int>(shape.x()) || id.y() < 0 || id.y() >= static_cast<int>(shape.y())) |
| 393 | { |
| 394 | using ResultType = typename std::remove_reference<D>::type::result_type; |
| 395 | const ResultType value = distribution(gen); |
| 396 | void *const out_ptr = tensor(id); |
| 397 | store_value_with_data_type(out_ptr, value, tensor.data_type()); |
| 398 | } |
| 399 | }); |
| 400 | } |
| 401 | |
| 402 | template <typename T, typename D> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 403 | void AssetsLibrary::fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 404 | { |
| 405 | Window window; |
| 406 | for(unsigned int d = 0; d < tensor.shape().num_dimensions(); ++d) |
| 407 | { |
| 408 | window.set(d, Window::Dimension(0, tensor.shape()[d], 1)); |
| 409 | } |
| 410 | |
| 411 | std::mt19937 gen(_seed + seed_offset); |
| 412 | |
| 413 | //FIXME: Replace with normal loop |
| 414 | execute_window_loop(window, [&](const Coordinates & id) |
| 415 | { |
| 416 | using ResultType = typename std::remove_reference<D>::type::result_type; |
| 417 | const ResultType value = distribution(gen); |
| 418 | void *const out_ptr = tensor(id); |
| 419 | store_value_with_data_type(out_ptr, value, tensor.data_type()); |
| 420 | }); |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 421 | |
| 422 | fill_borders_with_garbage(tensor, distribution, seed_offset); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | template <typename D> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 426 | void AssetsLibrary::fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 427 | { |
| 428 | std::mt19937 gen(_seed + seed_offset); |
| 429 | |
| 430 | for(size_t offset = 0; offset < raw.size(); offset += raw.element_size()) |
| 431 | { |
| 432 | using ResultType = typename std::remove_reference<D>::type::result_type; |
| 433 | const ResultType value = distribution(gen); |
| 434 | store_value_with_data_type(raw.data() + offset, value, raw.data_type()); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 439 | void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 440 | { |
| 441 | const RawTensor &raw = get(name, format); |
| 442 | |
| 443 | for(size_t offset = 0; offset < raw.size(); offset += raw.element_size()) |
| 444 | { |
| 445 | const Coordinates id = index2coord(raw.shape(), offset / raw.element_size()); |
| 446 | |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 447 | const RawTensor::value_type *const raw_ptr = raw.data() + offset; |
| 448 | const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 449 | std::copy_n(raw_ptr, raw.element_size(), out_ptr); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 454 | void AssetsLibrary::fill(T &&tensor, const std::string &name, Channel channel) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 455 | { |
| 456 | fill(std::forward<T>(tensor), name, get_format_for_channel(channel), channel); |
| 457 | } |
| 458 | |
| 459 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 460 | void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format, Channel channel) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 461 | { |
| 462 | const RawTensor &raw = get(name, format, channel); |
| 463 | |
| 464 | for(size_t offset = 0; offset < raw.size(); offset += raw.element_size()) |
| 465 | { |
| 466 | const Coordinates id = index2coord(raw.shape(), offset / raw.element_size()); |
| 467 | |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 468 | const RawTensor::value_type *const raw_ptr = raw.data() + offset; |
| 469 | const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 470 | std::copy_n(raw_ptr, raw.element_size(), out_ptr); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 475 | void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 476 | { |
| 477 | switch(tensor.data_type()) |
| 478 | { |
| 479 | case DataType::U8: |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 480 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 481 | { |
| 482 | std::uniform_int_distribution<uint8_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(), std::numeric_limits<uint8_t>::max()); |
| 483 | fill(tensor, distribution_u8, seed_offset); |
| 484 | break; |
| 485 | } |
| 486 | case DataType::S8: |
| 487 | case DataType::QS8: |
| 488 | { |
| 489 | std::uniform_int_distribution<int8_t> distribution_s8(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max()); |
| 490 | fill(tensor, distribution_s8, seed_offset); |
| 491 | break; |
| 492 | } |
| 493 | case DataType::U16: |
| 494 | { |
| 495 | std::uniform_int_distribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), std::numeric_limits<uint16_t>::max()); |
| 496 | fill(tensor, distribution_u16, seed_offset); |
| 497 | break; |
| 498 | } |
| 499 | case DataType::S16: |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 500 | case DataType::QS16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 501 | { |
| 502 | std::uniform_int_distribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), std::numeric_limits<int16_t>::max()); |
| 503 | fill(tensor, distribution_s16, seed_offset); |
| 504 | break; |
| 505 | } |
| 506 | case DataType::U32: |
| 507 | { |
| 508 | std::uniform_int_distribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max()); |
| 509 | fill(tensor, distribution_u32, seed_offset); |
| 510 | break; |
| 511 | } |
| 512 | case DataType::S32: |
| 513 | { |
| 514 | std::uniform_int_distribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max()); |
| 515 | fill(tensor, distribution_s32, seed_offset); |
| 516 | break; |
| 517 | } |
| 518 | case DataType::U64: |
| 519 | { |
| 520 | std::uniform_int_distribution<uint64_t> distribution_u64(std::numeric_limits<uint64_t>::lowest(), std::numeric_limits<uint64_t>::max()); |
| 521 | fill(tensor, distribution_u64, seed_offset); |
| 522 | break; |
| 523 | } |
| 524 | case DataType::S64: |
| 525 | { |
| 526 | std::uniform_int_distribution<int64_t> distribution_s64(std::numeric_limits<int64_t>::lowest(), std::numeric_limits<int64_t>::max()); |
| 527 | fill(tensor, distribution_s64, seed_offset); |
| 528 | break; |
| 529 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 530 | case DataType::F16: |
SiCong Li | 02dfb2c | 2017-07-27 17:59:20 +0100 | [diff] [blame] | 531 | { |
| 532 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 533 | std::uniform_real_distribution<float> distribution_f16(-100.f, 100.f); |
| 534 | fill(tensor, distribution_f16, seed_offset); |
| 535 | break; |
| 536 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 537 | case DataType::F32: |
| 538 | { |
| 539 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 540 | std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f); |
| 541 | fill(tensor, distribution_f32, seed_offset); |
| 542 | break; |
| 543 | } |
| 544 | case DataType::F64: |
| 545 | { |
| 546 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 547 | std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f); |
| 548 | fill(tensor, distribution_f64, seed_offset); |
| 549 | break; |
| 550 | } |
| 551 | case DataType::SIZET: |
| 552 | { |
| 553 | std::uniform_int_distribution<size_t> distribution_sizet(std::numeric_limits<size_t>::lowest(), std::numeric_limits<size_t>::max()); |
| 554 | fill(tensor, distribution_sizet, seed_offset); |
| 555 | break; |
| 556 | } |
| 557 | default: |
| 558 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | template <typename T, typename D> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 563 | void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 564 | { |
| 565 | switch(tensor.data_type()) |
| 566 | { |
| 567 | case DataType::U8: |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 568 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 569 | { |
| 570 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint8_t, D>::value)); |
| 571 | std::uniform_int_distribution<uint8_t> distribution_u8(low, high); |
| 572 | fill(tensor, distribution_u8, seed_offset); |
| 573 | break; |
| 574 | } |
| 575 | case DataType::S8: |
| 576 | case DataType::QS8: |
| 577 | { |
| 578 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int8_t, D>::value)); |
| 579 | std::uniform_int_distribution<int8_t> distribution_s8(low, high); |
| 580 | fill(tensor, distribution_s8, seed_offset); |
| 581 | break; |
| 582 | } |
| 583 | case DataType::U16: |
| 584 | { |
| 585 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint16_t, D>::value)); |
| 586 | std::uniform_int_distribution<uint16_t> distribution_u16(low, high); |
| 587 | fill(tensor, distribution_u16, seed_offset); |
| 588 | break; |
| 589 | } |
| 590 | case DataType::S16: |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 591 | case DataType::QS16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 592 | { |
| 593 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int16_t, D>::value)); |
| 594 | std::uniform_int_distribution<int16_t> distribution_s16(low, high); |
| 595 | fill(tensor, distribution_s16, seed_offset); |
| 596 | break; |
| 597 | } |
| 598 | case DataType::U32: |
| 599 | { |
| 600 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint32_t, D>::value)); |
| 601 | std::uniform_int_distribution<uint32_t> distribution_u32(low, high); |
| 602 | fill(tensor, distribution_u32, seed_offset); |
| 603 | break; |
| 604 | } |
| 605 | case DataType::S32: |
| 606 | { |
| 607 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int32_t, D>::value)); |
| 608 | std::uniform_int_distribution<int32_t> distribution_s32(low, high); |
| 609 | fill(tensor, distribution_s32, seed_offset); |
| 610 | break; |
| 611 | } |
| 612 | case DataType::U64: |
| 613 | { |
| 614 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint64_t, D>::value)); |
| 615 | std::uniform_int_distribution<uint64_t> distribution_u64(low, high); |
| 616 | fill(tensor, distribution_u64, seed_offset); |
| 617 | break; |
| 618 | } |
| 619 | case DataType::S64: |
| 620 | { |
| 621 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int64_t, D>::value)); |
| 622 | std::uniform_int_distribution<int64_t> distribution_s64(low, high); |
| 623 | fill(tensor, distribution_s64, seed_offset); |
| 624 | break; |
| 625 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 626 | case DataType::F16: |
| 627 | { |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 628 | std::uniform_real_distribution<float> distribution_f16(low, high); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 629 | fill(tensor, distribution_f16, seed_offset); |
| 630 | break; |
| 631 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 632 | case DataType::F32: |
| 633 | { |
| 634 | ARM_COMPUTE_ERROR_ON(!(std::is_same<float, D>::value)); |
| 635 | std::uniform_real_distribution<float> distribution_f32(low, high); |
| 636 | fill(tensor, distribution_f32, seed_offset); |
| 637 | break; |
| 638 | } |
| 639 | case DataType::F64: |
| 640 | { |
| 641 | ARM_COMPUTE_ERROR_ON(!(std::is_same<double, D>::value)); |
| 642 | std::uniform_real_distribution<double> distribution_f64(low, high); |
| 643 | fill(tensor, distribution_f64, seed_offset); |
| 644 | break; |
| 645 | } |
| 646 | case DataType::SIZET: |
| 647 | { |
| 648 | ARM_COMPUTE_ERROR_ON(!(std::is_same<size_t, D>::value)); |
| 649 | std::uniform_int_distribution<size_t> distribution_sizet(low, high); |
| 650 | fill(tensor, distribution_sizet, seed_offset); |
| 651 | break; |
| 652 | } |
| 653 | default: |
| 654 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 659 | void AssetsLibrary::fill_layer_data(T &&tensor, std::string name) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 660 | { |
| 661 | #ifdef _WIN32 |
| 662 | const std::string path_separator("\\"); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 663 | #else /* _WIN32 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 664 | const std::string path_separator("/"); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 665 | #endif /* _WIN32 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 666 | const std::string path = _library_path + path_separator + name; |
| 667 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 668 | std::vector<unsigned long> shape; |
| 669 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 670 | // Open file |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 671 | std::ifstream stream(path, std::ios::in | std::ios::binary); |
Anthony Barbier | f6705ec | 2017-09-28 12:01:10 +0100 | [diff] [blame] | 672 | if(!stream.good()) |
| 673 | { |
| 674 | throw framework::FileNotFound("Could not load npy file: " + path); |
| 675 | } |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 676 | std::string header = npy::read_header(stream); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 677 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 678 | // Parse header |
| 679 | bool fortran_order = false; |
| 680 | std::string typestr; |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 681 | npy::parse_header(header, typestr, fortran_order, shape); |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 682 | |
| 683 | // Check if the typestring matches the given one |
| 684 | std::string expect_typestr = get_typestring(tensor.data_type()); |
| 685 | ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch"); |
| 686 | |
| 687 | // Validate tensor shape |
| 688 | ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch"); |
| 689 | if(fortran_order) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 690 | { |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 691 | for(size_t i = 0; i < shape.size(); ++i) |
| 692 | { |
| 693 | ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[i], "Tensor dimensions mismatch"); |
| 694 | } |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | for(size_t i = 0; i < shape.size(); ++i) |
| 699 | { |
| 700 | ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch"); |
| 701 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 702 | } |
| 703 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 704 | // Read data |
| 705 | if(tensor.padding().empty()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 706 | { |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 707 | // If tensor has no padding read directly from stream. |
| 708 | stream.read(reinterpret_cast<char *>(tensor.data()), tensor.size()); |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | // If tensor has padding accessing tensor elements through execution window. |
| 713 | Window window; |
| 714 | window.use_tensor_dimensions(tensor.shape()); |
| 715 | |
| 716 | //FIXME : Replace with normal loop |
| 717 | execute_window_loop(window, [&](const Coordinates & id) |
| 718 | { |
| 719 | stream.read(reinterpret_cast<char *>(tensor(id)), tensor.element_size()); |
| 720 | }); |
| 721 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 722 | } |
| 723 | } // namespace test |
| 724 | } // namespace arm_compute |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 725 | #endif /* __ARM_COMPUTE_TEST_TENSOR_LIBRARY_H__ */ |