2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
25 #include "utils/GraphUtils.h"
26 #include "utils/Utils.h"
29 #include "arm_compute/core/CL/OpenCL.h"
30 #include "arm_compute/runtime/CL/CLTensor.h"
31 #endif /* ARM_COMPUTE_CL */
33 #include "arm_compute/core/Error.h"
34 #include "arm_compute/core/PixelValue.h"
35 #include "libnpy/npy.hpp"
40 using namespace arm_compute::graph_utils;
42 PPMWriter::PPMWriter(std::string name, unsigned int maximum)
43 : _name(std::move(name)), _iterator(0), _maximum(maximum)
47 bool PPMWriter::access_tensor(ITensor &tensor)
50 ss << _name << _iterator << ".ppm";
51 if(dynamic_cast<Tensor *>(&tensor) != nullptr)
53 arm_compute::utils::save_to_ppm(dynamic_cast<Tensor &>(tensor), ss.str());
56 else if(dynamic_cast<CLTensor *>(&tensor) != nullptr)
58 arm_compute::utils::save_to_ppm(dynamic_cast<CLTensor &>(tensor), ss.str());
60 #endif /* ARM_COMPUTE_CL */
67 return _iterator < _maximum;
70 DummyAccessor::DummyAccessor(unsigned int maximum)
71 : _iterator(0), _maximum(maximum)
75 bool DummyAccessor::access_tensor(ITensor &tensor)
77 ARM_COMPUTE_UNUSED(tensor);
78 bool ret = _maximum == 0 || _iterator < _maximum;
79 if(_iterator == _maximum)
90 RandomAccessor::RandomAccessor(PixelValue lower, PixelValue upper, std::random_device::result_type seed)
91 : _lower(lower), _upper(upper), _seed(seed)
95 template <typename T, typename D>
96 void RandomAccessor::fill(ITensor &tensor, D &&distribution)
98 std::mt19937 gen(_seed);
100 if(tensor.info()->padding().empty())
102 for(size_t offset = 0; offset < tensor.info()->total_size(); offset += tensor.info()->element_size())
104 const T value = distribution(gen);
105 *reinterpret_cast<T *>(tensor.buffer() + offset) = value;
110 // If tensor has padding accessing tensor elements through execution window.
112 window.use_tensor_dimensions(tensor.info()->tensor_shape());
114 execute_window_loop(window, [&](const Coordinates & id)
116 const T value = distribution(gen);
117 *reinterpret_cast<T *>(tensor.ptr_to_element(id)) = value;
122 bool RandomAccessor::access_tensor(ITensor &tensor)
124 switch(tensor.info()->data_type())
128 std::uniform_int_distribution<uint8_t> distribution_u8(_lower.get<uint8_t>(), _upper.get<uint8_t>());
129 fill<uint8_t>(tensor, distribution_u8);
135 std::uniform_int_distribution<int8_t> distribution_s8(_lower.get<int8_t>(), _upper.get<int8_t>());
136 fill<int8_t>(tensor, distribution_s8);
141 std::uniform_int_distribution<uint16_t> distribution_u16(_lower.get<uint16_t>(), _upper.get<uint16_t>());
142 fill<uint16_t>(tensor, distribution_u16);
148 std::uniform_int_distribution<int16_t> distribution_s16(_lower.get<int16_t>(), _upper.get<int16_t>());
149 fill<int16_t>(tensor, distribution_s16);
154 std::uniform_int_distribution<uint32_t> distribution_u32(_lower.get<uint32_t>(), _upper.get<uint32_t>());
155 fill<uint32_t>(tensor, distribution_u32);
160 std::uniform_int_distribution<int32_t> distribution_s32(_lower.get<int32_t>(), _upper.get<int32_t>());
161 fill<int32_t>(tensor, distribution_s32);
166 std::uniform_int_distribution<uint64_t> distribution_u64(_lower.get<uint64_t>(), _upper.get<uint64_t>());
167 fill<uint64_t>(tensor, distribution_u64);
172 std::uniform_int_distribution<int64_t> distribution_s64(_lower.get<int64_t>(), _upper.get<int64_t>());
173 fill<int64_t>(tensor, distribution_s64);
178 std::uniform_real_distribution<float> distribution_f16(_lower.get<float>(), _upper.get<float>());
179 fill<float>(tensor, distribution_f16);
184 std::uniform_real_distribution<float> distribution_f32(_lower.get<float>(), _upper.get<float>());
185 fill<float>(tensor, distribution_f32);
190 std::uniform_real_distribution<double> distribution_f64(_lower.get<double>(), _upper.get<double>());
191 fill<double>(tensor, distribution_f64);
195 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
200 NumPyBinLoader::NumPyBinLoader(std::string filename)
201 : _filename(std::move(filename))
205 bool NumPyBinLoader::access_tensor(ITensor &tensor)
207 const TensorShape tensor_shape = tensor.info()->tensor_shape();
208 std::vector<unsigned long> shape;
211 std::ifstream stream(_filename, std::ios::in | std::ios::binary);
212 ARM_COMPUTE_ERROR_ON_MSG(!stream.good(), "Failed to load binary data");
213 // Check magic bytes and version number
214 unsigned char v_major = 0;
215 unsigned char v_minor = 0;
216 npy::read_magic(stream, &v_major, &v_minor);
220 if(v_major == 1 && v_minor == 0)
222 header = npy::read_header_1_0(stream);
224 else if(v_major == 2 && v_minor == 0)
226 header = npy::read_header_2_0(stream);
230 ARM_COMPUTE_ERROR("Unsupported file format version");
234 bool fortran_order = false;
236 npy::ParseHeader(header, typestr, &fortran_order, shape);
238 // Check if the typestring matches the given one
239 std::string expect_typestr = arm_compute::utils::get_typestring(tensor.info()->data_type());
240 ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
242 // Validate tensor shape
243 ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor_shape.num_dimensions(), "Tensor ranks mismatch");
246 for(size_t i = 0; i < shape.size(); ++i)
248 ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[i], "Tensor dimensions mismatch");
253 for(size_t i = 0; i < shape.size(); ++i)
255 ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
260 if(tensor.info()->padding().empty())
262 // If tensor has no padding read directly from stream.
263 stream.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
267 // If tensor has padding accessing tensor elements through execution window.
269 window.use_tensor_dimensions(tensor_shape);
271 execute_window_loop(window, [&](const Coordinates & id)
273 stream.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size());