Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +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 | |
| 25 | #include "utils/GraphUtils.h" |
| 26 | #include "utils/Utils.h" |
| 27 | |
| 28 | #ifdef ARM_COMPUTE_CL |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 31 | #endif /* ARM_COMPUTE_CL */ |
| 32 | |
| 33 | #include "arm_compute/core/Error.h" |
| 34 | #include "libnpy/npy.hpp" |
| 35 | |
| 36 | #include <sstream> |
| 37 | |
| 38 | using namespace arm_compute::graph_utils; |
| 39 | |
| 40 | PPMWriter::PPMWriter(std::string name, unsigned int maximum) |
| 41 | : _name(std::move(name)), _iterator(0), _maximum(maximum) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | bool PPMWriter::access_tensor(ITensor &tensor) |
| 46 | { |
| 47 | std::stringstream ss; |
| 48 | ss << _name << _iterator << ".ppm"; |
| 49 | if(dynamic_cast<Tensor *>(&tensor) != nullptr) |
| 50 | { |
| 51 | arm_compute::utils::save_to_ppm(dynamic_cast<Tensor &>(tensor), ss.str()); |
| 52 | } |
| 53 | #ifdef ARM_COMPUTE_CL |
| 54 | else if(dynamic_cast<CLTensor *>(&tensor) != nullptr) |
| 55 | { |
| 56 | arm_compute::utils::save_to_ppm(dynamic_cast<CLTensor &>(tensor), ss.str()); |
| 57 | } |
| 58 | #endif /* ARM_COMPUTE_CL */ |
| 59 | |
| 60 | _iterator++; |
| 61 | if(_maximum == 0) |
| 62 | { |
| 63 | return true; |
| 64 | } |
| 65 | return _iterator < _maximum; |
| 66 | } |
| 67 | |
| 68 | DummyAccessor::DummyAccessor(unsigned int maximum) |
| 69 | : _iterator(0), _maximum(maximum) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | bool DummyAccessor::access_tensor(ITensor &tensor) |
| 74 | { |
| 75 | ARM_COMPUTE_UNUSED(tensor); |
| 76 | bool ret = _maximum == 0 || _iterator < _maximum; |
| 77 | if(_iterator == _maximum) |
| 78 | { |
| 79 | _iterator = 0; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | _iterator++; |
| 84 | } |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | NumPyBinLoader::NumPyBinLoader(std::string filename) |
| 89 | : _filename(std::move(filename)) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | bool NumPyBinLoader::access_tensor(ITensor &tensor) |
| 94 | { |
| 95 | const TensorShape tensor_shape = tensor.info()->tensor_shape(); |
| 96 | std::vector<unsigned long> shape; |
| 97 | |
| 98 | // Open file |
| 99 | std::ifstream stream(_filename, std::ios::in | std::ios::binary); |
| 100 | ARM_COMPUTE_ERROR_ON_MSG(!stream.good(), "Failed to load binary data"); |
| 101 | // Check magic bytes and version number |
| 102 | unsigned char v_major = 0; |
| 103 | unsigned char v_minor = 0; |
| 104 | npy::read_magic(stream, &v_major, &v_minor); |
| 105 | |
| 106 | // Read header |
| 107 | std::string header; |
| 108 | if(v_major == 1 && v_minor == 0) |
| 109 | { |
| 110 | header = npy::read_header_1_0(stream); |
| 111 | } |
| 112 | else if(v_major == 2 && v_minor == 0) |
| 113 | { |
| 114 | header = npy::read_header_2_0(stream); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | ARM_COMPUTE_ERROR("Unsupported file format version"); |
| 119 | } |
| 120 | |
| 121 | // Parse header |
| 122 | bool fortran_order = false; |
| 123 | std::string typestr; |
| 124 | npy::ParseHeader(header, typestr, &fortran_order, shape); |
| 125 | |
| 126 | // Check if the typestring matches the given one |
| 127 | std::string expect_typestr = arm_compute::utils::get_typestring(tensor.info()->data_type()); |
| 128 | ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch"); |
| 129 | |
| 130 | // Validate tensor shape |
| 131 | ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor_shape.num_dimensions(), "Tensor ranks mismatch"); |
| 132 | if(fortran_order) |
| 133 | { |
| 134 | for(size_t i = 0; i < shape.size(); ++i) |
| 135 | { |
| 136 | ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[i], "Tensor dimensions mismatch"); |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | for(size_t i = 0; i < shape.size(); ++i) |
| 142 | { |
| 143 | ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch"); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Read data |
| 148 | if(tensor.info()->padding().empty()) |
| 149 | { |
| 150 | // If tensor has no padding read directly from stream. |
| 151 | stream.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size()); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | // If tensor has padding accessing tensor elements through execution window. |
| 156 | Window window; |
| 157 | window.use_tensor_dimensions(tensor_shape); |
| 158 | |
| 159 | execute_window_loop(window, [&](const Coordinates & id) |
| 160 | { |
| 161 | stream.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size()); |
| 162 | }); |
| 163 | } |
| 164 | return true; |
| 165 | } |