Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 1 | |
Eric Kunze | cc426df | 2024-01-03 00:27:59 +0000 | [diff] [blame] | 2 | // Copyright (c) 2020-2024, ARM Limited. |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include "numpy_utils.h" |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 17 | #include "half.hpp" |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Eric Kunze | cc426df | 2024-01-03 00:27:59 +0000 | [diff] [blame] | 19 | #include <memory> |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 20 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 21 | // Magic NUMPY header |
| 22 | static const char NUMPY_HEADER_STR[] = "\x93NUMPY\x1\x0\x76\x0{"; |
| 23 | static const int NUMPY_HEADER_SZ = 128; |
Jeremy Johnson | 82dbb32 | 2021-07-08 11:53:04 +0100 | [diff] [blame] | 24 | // Maximum shape dimensions supported |
| 25 | static const int NUMPY_MAX_DIMS_SUPPORTED = 10; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 26 | |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 27 | // This is an entry function for reading 8-/16-/32-bit npy file. |
| 28 | template <> |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 29 | NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf) |
| 30 | { |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 31 | FILE* infile = nullptr; |
| 32 | NPError rc = HEADER_PARSE_ERROR; |
| 33 | assert(filename); |
| 34 | assert(databuf); |
| 35 | |
| 36 | infile = fopen(filename, "rb"); |
| 37 | if (!infile) |
| 38 | { |
| 39 | return FILE_NOT_FOUND; |
| 40 | } |
| 41 | |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 42 | bool is_signed = false; |
| 43 | int length_per_byte = 0; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 44 | char byte_order; |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 45 | rc = getHeader(infile, is_signed, length_per_byte, byte_order); |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 46 | if (rc != NO_ERROR) |
| 47 | return rc; |
| 48 | |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 49 | switch (length_per_byte) |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 50 | { |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 51 | case 1: |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 52 | if (is_signed) |
| 53 | { |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 54 | int8_t* tmp_buf = new int8_t[elems]; |
| 55 | rc = readFromNpyFile<int8_t>(filename, elems, tmp_buf); |
| 56 | copyBufferByElement(databuf, tmp_buf, elems); |
Eric Kunze | cc426df | 2024-01-03 00:27:59 +0000 | [diff] [blame] | 57 | delete[] tmp_buf; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 58 | } |
| 59 | else |
| 60 | { |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 61 | uint8_t* tmp_buf = new uint8_t[elems]; |
| 62 | rc = readFromNpyFile<uint8_t>(filename, elems, tmp_buf); |
| 63 | copyBufferByElement(databuf, tmp_buf, elems); |
Eric Kunze | cc426df | 2024-01-03 00:27:59 +0000 | [diff] [blame] | 64 | delete[] tmp_buf; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 65 | } |
| 66 | break; |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 67 | case 2: |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 68 | if (is_signed) |
| 69 | { |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 70 | int16_t* tmp_buf = new int16_t[elems]; |
| 71 | rc = readFromNpyFile<int16_t>(filename, elems, tmp_buf); |
| 72 | copyBufferByElement(databuf, tmp_buf, elems); |
Eric Kunze | cc426df | 2024-01-03 00:27:59 +0000 | [diff] [blame] | 73 | delete[] tmp_buf; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 74 | } |
| 75 | else |
| 76 | { |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 77 | uint16_t* tmp_buf = new uint16_t[elems]; |
| 78 | rc = readFromNpyFile<uint16_t>(filename, elems, tmp_buf); |
| 79 | copyBufferByElement(databuf, tmp_buf, elems); |
Eric Kunze | cc426df | 2024-01-03 00:27:59 +0000 | [diff] [blame] | 80 | delete[] tmp_buf; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 81 | } |
| 82 | break; |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 83 | case 4: |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 84 | if (is_signed) |
| 85 | { |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 86 | bool is_bool; |
| 87 | const char* dtype_str = getDTypeString<int32_t>(is_bool); |
| 88 | rc = readFromNpyFileCommon(filename, dtype_str, sizeof(int32_t), elems, databuf, is_bool); |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | { |
| 92 | // uint32, not supported |
TatWai Chong | 679bdad | 2023-07-31 15:15:12 -0700 | [diff] [blame] | 93 | rc = DATA_TYPE_NOT_SUPPORTED; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 94 | } |
| 95 | break; |
| 96 | default: |
| 97 | return DATA_TYPE_NOT_SUPPORTED; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | return rc; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 104 | NumpyUtilities::NPError NumpyUtilities::readFromNpyFileCommon(const char* filename, |
| 105 | const char* dtype_str, |
| 106 | const size_t elementsize, |
| 107 | const uint32_t elems, |
| 108 | void* databuf, |
| 109 | bool bool_translate) |
| 110 | { |
| 111 | FILE* infile = nullptr; |
| 112 | NPError rc = NO_ERROR; |
| 113 | |
| 114 | assert(filename); |
| 115 | assert(databuf); |
| 116 | |
| 117 | infile = fopen(filename, "rb"); |
| 118 | if (!infile) |
| 119 | { |
| 120 | return FILE_NOT_FOUND; |
| 121 | } |
| 122 | |
| 123 | rc = checkNpyHeader(infile, elems, dtype_str); |
| 124 | if (rc == NO_ERROR) |
| 125 | { |
| 126 | if (bool_translate) |
| 127 | { |
| 128 | // Read in the data from numpy byte array to native bool |
| 129 | // array format |
| 130 | bool* buf = reinterpret_cast<bool*>(databuf); |
| 131 | for (uint32_t i = 0; i < elems; i++) |
| 132 | { |
| 133 | int val = fgetc(infile); |
| 134 | |
| 135 | if (val == EOF) |
| 136 | { |
| 137 | rc = FILE_IO_ERROR; |
| 138 | } |
| 139 | |
| 140 | buf[i] = val; |
| 141 | } |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | // Now we are at the beginning of the data |
| 146 | // Parse based on the datatype and number of dimensions |
| 147 | if (fread(databuf, elementsize, elems, infile) != elems) |
| 148 | { |
| 149 | rc = FILE_IO_ERROR; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (infile) |
| 155 | fclose(infile); |
| 156 | |
| 157 | return rc; |
| 158 | } |
| 159 | |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 160 | NumpyUtilities::NPError NumpyUtilities::getHeader(FILE* infile, bool& is_signed, int& bit_length, char& byte_order) |
| 161 | { |
| 162 | char buf[NUMPY_HEADER_SZ + 1]; |
| 163 | NPError rc = NO_ERROR; |
| 164 | assert(infile); |
| 165 | |
| 166 | if (fread(buf, NUMPY_HEADER_SZ, 1, infile) != 1) |
| 167 | { |
| 168 | return HEADER_PARSE_ERROR; |
| 169 | } |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 170 | |
Jerry Ge | 0bc46e4 | 2023-07-07 21:52:26 +0000 | [diff] [blame] | 171 | // Validate the numpy magic number |
| 172 | if (memcmp(buf, NUMPY_HEADER_STR, sizeof(NUMPY_HEADER_STR) - 1)) |
| 173 | { |
| 174 | return HEADER_PARSE_ERROR; |
| 175 | } |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 176 | |
Jerry Ge | 0bc46e4 | 2023-07-07 21:52:26 +0000 | [diff] [blame] | 177 | std::string dic_string(buf, NUMPY_HEADER_SZ); |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 178 | |
Jerry Ge | 0bc46e4 | 2023-07-07 21:52:26 +0000 | [diff] [blame] | 179 | std::string desc_str("descr':"); |
| 180 | size_t offset = dic_string.find(desc_str); |
| 181 | if (offset == std::string::npos) |
| 182 | return HEADER_PARSE_ERROR; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 183 | |
Jerry Ge | 0bc46e4 | 2023-07-07 21:52:26 +0000 | [diff] [blame] | 184 | offset += desc_str.size() + 1; |
| 185 | // Skip whitespace and the opening ' |
| 186 | while (offset < dic_string.size() && (std::isspace(dic_string[offset]) || dic_string[offset] == '\'')) |
| 187 | offset++; |
| 188 | // Check for overflow |
| 189 | if (offset + 2 > dic_string.size()) |
| 190 | return HEADER_PARSE_ERROR; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 191 | |
Jerry Ge | 0bc46e4 | 2023-07-07 21:52:26 +0000 | [diff] [blame] | 192 | byte_order = dic_string[offset]; |
| 193 | is_signed = dic_string[offset + 1] == 'u' ? false : true; |
| 194 | bit_length = (int)dic_string[offset + 2] - '0'; |
Jerry Ge | 13a3291 | 2023-07-03 16:36:41 +0000 | [diff] [blame] | 195 | |
| 196 | rewind(infile); |
| 197 | return rc; |
| 198 | } |
| 199 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 200 | NumpyUtilities::NPError NumpyUtilities::checkNpyHeader(FILE* infile, const uint32_t elems, const char* dtype_str) |
| 201 | { |
| 202 | char buf[NUMPY_HEADER_SZ + 1]; |
| 203 | char* ptr = nullptr; |
| 204 | NPError rc = NO_ERROR; |
| 205 | bool foundFormat = false; |
| 206 | bool foundOrder = false; |
| 207 | bool foundShape = false; |
| 208 | bool fortranOrder = false; |
| 209 | std::vector<int> shape; |
| 210 | uint32_t totalElems = 1; |
| 211 | char* outer_end = NULL; |
| 212 | |
| 213 | assert(infile); |
| 214 | assert(elems > 0); |
| 215 | |
| 216 | if (fread(buf, NUMPY_HEADER_SZ, 1, infile) != 1) |
| 217 | { |
| 218 | return HEADER_PARSE_ERROR; |
| 219 | } |
| 220 | |
| 221 | if (memcmp(buf, NUMPY_HEADER_STR, sizeof(NUMPY_HEADER_STR) - 1)) |
| 222 | { |
| 223 | return HEADER_PARSE_ERROR; |
| 224 | } |
| 225 | |
| 226 | ptr = strtok_r(buf + sizeof(NUMPY_HEADER_STR) - 1, ":", &outer_end); |
| 227 | |
| 228 | // Read in the data type, order, and shape |
| 229 | while (ptr && (!foundFormat || !foundOrder || !foundShape)) |
| 230 | { |
| 231 | |
| 232 | // End of string? |
| 233 | if (!ptr) |
| 234 | break; |
| 235 | |
| 236 | // Skip whitespace |
| 237 | while (isspace(*ptr)) |
| 238 | ptr++; |
| 239 | |
| 240 | // Parse the dictionary field name |
| 241 | if (!strcmp(ptr, "'descr'")) |
| 242 | { |
| 243 | ptr = strtok_r(NULL, ",", &outer_end); |
| 244 | if (!ptr) |
| 245 | break; |
| 246 | |
| 247 | while (isspace(*ptr)) |
| 248 | ptr++; |
| 249 | |
Won Jeon | a814152 | 2024-04-29 23:57:27 +0000 | [diff] [blame] | 250 | // ml_dtypes writes '<f1' for 'numpy.dtype' in the header for float8_e5m2, but |
| 251 | // default NumPy does not understand this notation, which causes trouble |
| 252 | // when other code tries to open this file. |
| 253 | // To avoid this, '|u1' notation is used when the file is written, and the uint8 |
| 254 | // data is viewed as float8_e5m2 later when the file is read. |
| 255 | if (!strcmp(dtype_str, "'<f1'")) |
| 256 | dtype_str = "'|u1'"; |
| 257 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 258 | if (strcmp(ptr, dtype_str)) |
| 259 | { |
| 260 | return FILE_TYPE_MISMATCH; |
| 261 | } |
| 262 | |
| 263 | foundFormat = true; |
| 264 | } |
| 265 | else if (!strcmp(ptr, "'fortran_order'")) |
| 266 | { |
| 267 | ptr = strtok_r(NULL, ",", &outer_end); |
| 268 | if (!ptr) |
| 269 | break; |
| 270 | |
| 271 | while (isspace(*ptr)) |
| 272 | ptr++; |
| 273 | |
| 274 | if (!strcmp(ptr, "False")) |
| 275 | { |
| 276 | fortranOrder = false; |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | return FILE_TYPE_MISMATCH; |
| 281 | } |
| 282 | |
| 283 | foundOrder = true; |
| 284 | } |
| 285 | else if (!strcmp(ptr, "'shape'")) |
| 286 | { |
| 287 | |
| 288 | ptr = strtok_r(NULL, "(", &outer_end); |
| 289 | if (!ptr) |
| 290 | break; |
| 291 | ptr = strtok_r(NULL, ")", &outer_end); |
| 292 | if (!ptr) |
| 293 | break; |
| 294 | |
| 295 | while (isspace(*ptr)) |
| 296 | ptr++; |
| 297 | |
Jeremy Johnson | 82dbb32 | 2021-07-08 11:53:04 +0100 | [diff] [blame] | 298 | // The shape contains N comma-separated integers. Read up to MAX_DIMS. |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 299 | char* end = NULL; |
| 300 | |
| 301 | ptr = strtok_r(ptr, ",", &end); |
Jeremy Johnson | 82dbb32 | 2021-07-08 11:53:04 +0100 | [diff] [blame] | 302 | for (int i = 0; i < NUMPY_MAX_DIMS_SUPPORTED; i++) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 303 | { |
| 304 | // Out of dimensions |
| 305 | if (!ptr) |
| 306 | break; |
| 307 | |
| 308 | int dim = atoi(ptr); |
| 309 | |
| 310 | // Dimension is 0 |
| 311 | if (dim == 0) |
| 312 | break; |
| 313 | |
| 314 | shape.push_back(dim); |
| 315 | totalElems *= dim; |
| 316 | ptr = strtok_r(NULL, ",", &end); |
| 317 | } |
| 318 | |
| 319 | foundShape = true; |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | return HEADER_PARSE_ERROR; |
| 324 | } |
| 325 | |
| 326 | if (!ptr) |
| 327 | break; |
| 328 | |
| 329 | ptr = strtok_r(NULL, ":", &outer_end); |
| 330 | } |
| 331 | |
| 332 | if (!foundShape || !foundFormat || !foundOrder) |
| 333 | { |
| 334 | return HEADER_PARSE_ERROR; |
| 335 | } |
| 336 | |
| 337 | // Validate header |
| 338 | if (fortranOrder) |
| 339 | { |
| 340 | return FILE_TYPE_MISMATCH; |
| 341 | } |
| 342 | |
| 343 | if (totalElems != elems) |
| 344 | { |
| 345 | return BUFFER_SIZE_MISMATCH; |
| 346 | } |
| 347 | |
| 348 | // Go back to the begininng and read until the end of the header dictionary |
| 349 | rewind(infile); |
| 350 | int val; |
| 351 | |
| 352 | do |
| 353 | { |
| 354 | val = fgetc(infile); |
| 355 | } while (val != EOF && val != '\n'); |
| 356 | |
| 357 | return rc; |
| 358 | } |
| 359 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 360 | NumpyUtilities::NPError NumpyUtilities::writeToNpyFileCommon(const char* filename, |
| 361 | const char* dtype_str, |
| 362 | const size_t elementsize, |
| 363 | const std::vector<int32_t>& shape, |
| 364 | const void* databuf, |
| 365 | bool bool_translate) |
| 366 | { |
| 367 | FILE* outfile = nullptr; |
| 368 | NPError rc = NO_ERROR; |
| 369 | uint32_t totalElems = 1; |
| 370 | |
| 371 | assert(filename); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 372 | assert(databuf); |
| 373 | |
| 374 | outfile = fopen(filename, "wb"); |
| 375 | |
| 376 | if (!outfile) |
| 377 | { |
| 378 | return FILE_NOT_FOUND; |
| 379 | } |
| 380 | |
| 381 | for (uint32_t i = 0; i < shape.size(); i++) |
| 382 | { |
| 383 | totalElems *= shape[i]; |
| 384 | } |
| 385 | |
| 386 | rc = writeNpyHeader(outfile, shape, dtype_str); |
| 387 | |
| 388 | if (rc == NO_ERROR) |
| 389 | { |
| 390 | if (bool_translate) |
| 391 | { |
| 392 | // Numpy save format stores booleans as a byte array |
| 393 | // with one byte per boolean. This somewhat inefficiently |
| 394 | // remaps from system bool[] to this format. |
| 395 | const bool* buf = reinterpret_cast<const bool*>(databuf); |
| 396 | for (uint32_t i = 0; i < totalElems; i++) |
| 397 | { |
| 398 | int val = buf[i] ? 1 : 0; |
| 399 | if (fputc(val, outfile) == EOF) |
| 400 | { |
| 401 | rc = FILE_IO_ERROR; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | if (fwrite(databuf, elementsize, totalElems, outfile) != totalElems) |
| 408 | { |
| 409 | rc = FILE_IO_ERROR; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (outfile) |
| 415 | fclose(outfile); |
| 416 | |
| 417 | return rc; |
| 418 | } |
| 419 | |
| 420 | NumpyUtilities::NPError |
| 421 | NumpyUtilities::writeNpyHeader(FILE* outfile, const std::vector<int32_t>& shape, const char* dtype_str) |
| 422 | { |
| 423 | NPError rc = NO_ERROR; |
| 424 | uint32_t i; |
| 425 | char header[NUMPY_HEADER_SZ + 1]; |
| 426 | int headerPos = 0; |
| 427 | |
| 428 | assert(outfile); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 429 | |
| 430 | // Space-fill the header and end with a newline to start per numpy spec |
| 431 | memset(header, 0x20, NUMPY_HEADER_SZ); |
| 432 | header[NUMPY_HEADER_SZ - 1] = '\n'; |
| 433 | header[NUMPY_HEADER_SZ] = 0; |
| 434 | |
| 435 | // Write out the hard-coded header. We only support a 128-byte 1.0 header |
| 436 | // for now, which should be sufficient for simple tensor types of any |
| 437 | // reasonable rank. |
| 438 | memcpy(header, NUMPY_HEADER_STR, sizeof(NUMPY_HEADER_STR) - 1); |
| 439 | headerPos += sizeof(NUMPY_HEADER_STR) - 1; |
| 440 | |
Won Jeon | a814152 | 2024-04-29 23:57:27 +0000 | [diff] [blame] | 441 | // NumPy does not understand float8_e5m2, so change it to uint8 type, so that |
| 442 | // Python can read .npy files. |
| 443 | if (!strcmp(dtype_str, "'<f1'")) |
| 444 | { |
| 445 | dtype_str = "'|u1'"; |
| 446 | } |
| 447 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 448 | // Output the format dictionary |
| 449 | // Hard-coded for I32 for now |
Jeremy Johnson | 8f9e284 | 2024-04-04 11:14:06 +0100 | [diff] [blame] | 450 | headerPos += snprintf(header + headerPos, NUMPY_HEADER_SZ - headerPos, |
| 451 | "'descr': %s, 'fortran_order': False, 'shape': (", dtype_str); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 452 | |
Jeremy Johnson | 8f9e284 | 2024-04-04 11:14:06 +0100 | [diff] [blame] | 453 | // Add shape contents (if any - as this will be empty for rank 0) |
| 454 | for (i = 0; i < shape.size(); i++) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 455 | { |
Won Jeon | a814152 | 2024-04-29 23:57:27 +0000 | [diff] [blame] | 456 | // Output NumPy file from tosa_refmodel_sut_run generates the shape information |
| 457 | // without a trailing comma when the rank is greater than 1. |
| 458 | if (i == 0) |
| 459 | { |
| 460 | if (shape.size() == 1) |
| 461 | headerPos += snprintf(header + headerPos, NUMPY_HEADER_SZ - headerPos, "%d,", shape[i]); |
| 462 | else |
| 463 | headerPos += snprintf(header + headerPos, NUMPY_HEADER_SZ - headerPos, "%d", shape[i]); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | headerPos += snprintf(header + headerPos, NUMPY_HEADER_SZ - headerPos, ", %d", shape[i]); |
| 468 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // Close off the dictionary |
| 472 | headerPos += snprintf(header + headerPos, NUMPY_HEADER_SZ - headerPos, "), }"); |
| 473 | |
| 474 | // snprintf leaves a NULL at the end. Replace with a space |
| 475 | header[headerPos] = 0x20; |
| 476 | |
| 477 | if (fwrite(header, NUMPY_HEADER_SZ, 1, outfile) != 1) |
| 478 | { |
| 479 | rc = FILE_IO_ERROR; |
| 480 | } |
| 481 | |
| 482 | return rc; |
| 483 | } |