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