|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "numpy_support.h" |
| 10 | +#include "zip_impl.h" |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <cstring> |
| 14 | +#include <sstream> |
| 15 | +#include <stdexcept> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +namespace spdl::archive { |
| 19 | + |
| 20 | +////////////////////////////////////////////////////////////////////////////// |
| 21 | +// load_npy |
| 22 | +////////////////////////////////////////////////////////////////////////////// |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +void check_magic(const char** data, size_t* size) { |
| 27 | + const static char* prefix = "\x93NUMPY"; |
| 28 | + const static size_t len = std::strlen(prefix); |
| 29 | + if (*size < len) { |
| 30 | + throw std::runtime_error( |
| 31 | + "Failed to parse the magic prefix. (data too short)"); |
| 32 | + } |
| 33 | + if (std::strncmp(*data, prefix, len) != 0) { |
| 34 | + throw std::runtime_error( |
| 35 | + "The data must start with the prefix '\\x93NUMPY'"); |
| 36 | + } |
| 37 | + *data = (*data) + len; |
| 38 | + *size = (*size) - len; |
| 39 | +} |
| 40 | + |
| 41 | +std::string_view extract_header(const char** data, size_t* size) { |
| 42 | + auto s = (*size); |
| 43 | + auto* d = (*data); |
| 44 | + if (s < 2) { |
| 45 | + throw std::runtime_error("Failed to parse version number."); |
| 46 | + } |
| 47 | + int major = static_cast<int>(d[0]); |
| 48 | + // int minor = static_cast<int>(data[1]); |
| 49 | + s -= 2; |
| 50 | + d += 2; |
| 51 | + switch (major) { |
| 52 | + case 1: { |
| 53 | + // The next two bytes are header length in little endien. |
| 54 | + if (s < 2) { |
| 55 | + throw std::runtime_error("Failed to parse header length."); |
| 56 | + } |
| 57 | + unsigned short len = (*d); |
| 58 | + len += (unsigned short)(*(d + 1)) << 8; |
| 59 | + s -= 2; |
| 60 | + d += 2; |
| 61 | + if (s < len) { |
| 62 | + throw std::runtime_error("Failed to parse header"); |
| 63 | + } |
| 64 | + std::string_view header{d, len}; |
| 65 | + *data = d + len; |
| 66 | + *size = s - len; |
| 67 | + return header; |
| 68 | + } |
| 69 | + case 2: |
| 70 | + [[fallthrough]]; |
| 71 | + case 3: { |
| 72 | + // The next four bytes are header length. |
| 73 | + if (s < 4) { |
| 74 | + throw std::runtime_error("Failed to parse header length."); |
| 75 | + } |
| 76 | + size_t len; |
| 77 | + { |
| 78 | + int l = (int)*d; |
| 79 | + l += (int)(*(d + 1) << 8); |
| 80 | + l += (int)(*(d + 2) << 16); |
| 81 | + l += (int)(*(d + 3) << 24); |
| 82 | + if (l <= 0) { |
| 83 | + throw std::runtime_error( |
| 84 | + "Invalid data. The header length must be greater than 0."); |
| 85 | + } |
| 86 | + len = l; |
| 87 | + } |
| 88 | + s -= 4; |
| 89 | + d += 4; |
| 90 | + if (s < len) { |
| 91 | + throw std::runtime_error("Failed to parse header"); |
| 92 | + } |
| 93 | + std::string_view header{d, len}; |
| 94 | + *data = d + len; |
| 95 | + *size = s - len; |
| 96 | + return header; |
| 97 | + } |
| 98 | + default: |
| 99 | + throw std::runtime_error( |
| 100 | + "Unexpected format version. Only 1, 2 and 3 are supported."); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +NPYArray parse_header(const std::string_view header) { |
| 105 | + // NPY header is a string expression of Python dictionary with the following |
| 106 | + // keys See: |
| 107 | + // https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html |
| 108 | + // |
| 109 | + // - "descr": Format description. e.g. "'<i8'", "'<f4'" |
| 110 | + // - "fortran_order": "True" or "False". |
| 111 | + // - "shape": Tuple of int. e.g. "()", "(3, 4, 5)" |
| 112 | + NPYArray ret; |
| 113 | + { |
| 114 | + size_t pos = header.find("'descr':"); |
| 115 | + if (pos == std::string::npos) { |
| 116 | + throw std::runtime_error("Failed to parse header `'descr'`."); |
| 117 | + } |
| 118 | + pos = header.find('\'', pos + 7); |
| 119 | + if (pos == std::string::npos) { |
| 120 | + throw std::runtime_error("Failed to parse header `'descr'`."); |
| 121 | + } |
| 122 | + size_t end_pos = header.find('\'', pos + 1); |
| 123 | + if (end_pos == std::string::npos) { |
| 124 | + throw std::runtime_error("Failed to parse header `'descr'`."); |
| 125 | + } |
| 126 | + ret.descr = header.substr(pos + 1, end_pos - pos - 1); |
| 127 | + } |
| 128 | + { |
| 129 | + size_t pos = header.find("'shape':"); |
| 130 | + if (pos == std::string::npos) { |
| 131 | + throw std::runtime_error("Failed to parse header `'shape'`."); |
| 132 | + } |
| 133 | + pos = header.find('(', pos); |
| 134 | + if (pos == std::string::npos) { |
| 135 | + throw std::runtime_error("Failed to parse header `'shape'`."); |
| 136 | + } |
| 137 | + size_t end_pos = header.find(')', pos); |
| 138 | + if (end_pos == std::string::npos) { |
| 139 | + throw std::runtime_error("Failed to parse header `'shape'`."); |
| 140 | + } |
| 141 | + std::string shape_str(header.substr(pos + 1, end_pos - pos - 1)); |
| 142 | + std::istringstream shape_stream(shape_str); |
| 143 | + std::string number; |
| 144 | + while (std::getline(shape_stream, number, ',')) { |
| 145 | + number.erase( |
| 146 | + std::remove_if(number.begin(), number.end(), ::isspace), |
| 147 | + number.end()); |
| 148 | + if (!number.empty()) { |
| 149 | + ret.shape.push_back(std::stoi(number)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + { |
| 154 | + const std::string key = "'fortran_order':"; |
| 155 | + size_t pos = header.find(key); |
| 156 | + if (pos != std::string::npos) { |
| 157 | + pos += key.length(); |
| 158 | + while (pos < header.size() && std::isspace(header[pos])) { |
| 159 | + ++pos; |
| 160 | + } |
| 161 | + if (pos < header.size() && header[pos] == 'T') { |
| 162 | + ret.fortran_order = true; |
| 163 | + } else if (pos < header.size() && header[pos] == 'F') { |
| 164 | + ret.fortran_order = false; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + return ret; |
| 169 | +} |
| 170 | +} // namespace |
| 171 | + |
| 172 | +NPYArray load_npy(const char* data, size_t size) { |
| 173 | + check_magic(&data, &size); |
| 174 | + auto header = extract_header(&data, &size); |
| 175 | + auto array = parse_header(header); |
| 176 | + array.data = (void*)data; |
| 177 | + return array; |
| 178 | +} |
| 179 | + |
| 180 | +NPYArray load_npy_compressed( |
| 181 | + const char* data, |
| 182 | + uint32_t compressed_size, |
| 183 | + uint32_t uncompressed_size) { |
| 184 | + auto buffer = std::make_unique<char[]>(uncompressed_size); |
| 185 | + zip::inflate(data, compressed_size, buffer.get(), uncompressed_size); |
| 186 | + auto ret = load_npy(buffer.get(), uncompressed_size); |
| 187 | + ret.buffer = std::move(buffer); |
| 188 | + return ret; |
| 189 | +} |
| 190 | + |
| 191 | +} // namespace spdl::archive |
0 commit comments