| 58 | } |
| 59 | |
| 60 | void Tensor::parseNpyIntro(FILE*& f_ptr, uint32_t& header_len, uint32_t& start_data) |
| 61 | { |
| 62 | const char magic[] = "\x93" |
| 63 | "NUMPY"; |
| 64 | char magic_test[sizeof(magic)] = "\0"; |
| 65 | |
| 66 | size_t n_elems = fread((void*)magic_test, sizeof(char), sizeof(magic) - 1, f_ptr); |
| 67 | if (n_elems != sizeof(magic) - 1 || std::string(magic) != std::string(magic_test)) { |
| 68 | throw std::runtime_error("Could read magic token in NPY file"); |
| 69 | } |
| 70 | |
| 71 | uint8_t npy_major = 0; |
| 72 | uint8_t npy_minor = 0; |
| 73 | n_elems = fread((void*)&npy_major, sizeof(uint8_t), 1, f_ptr); |
| 74 | n_elems += fread((void*)&npy_minor, sizeof(uint8_t), 1, f_ptr); |
| 75 | |
| 76 | if (npy_major == 1) { |
| 77 | uint16_t header_len_u16 = 0; |
| 78 | n_elems = fread((void*)&header_len_u16, sizeof(uint16_t), 1, f_ptr); |
| 79 | header_len = header_len_u16; |
| 80 | } |
| 81 | else if (npy_major == 2) { |
| 82 | uint32_t header_len_u32 = 0; |
| 83 | n_elems = fread((void*)&header_len_u32, sizeof(uint32_t), 1, f_ptr); |
| 84 | header_len = header_len_u32; |
| 85 | } |
| 86 | else { |
| 87 | throw std::runtime_error("Unsupported npy version: " + std::to_string(npy_major)); |
| 88 | } |
| 89 | |
| 90 | start_data = 8 + 2 * npy_major + header_len; |
| 91 | } |
| 92 | |
| 93 | int Tensor::parseNpyHeader(FILE*& f_ptr, uint32_t header_len, DataType& type, std::vector<size_t>& shape) |
| 94 | { |
nothing calls this directly
no outgoing calls
no test coverage detected