| 91 | } |
| 92 | |
| 93 | int Tensor::parseNpyHeader(FILE*& f_ptr, uint32_t header_len, DataType& type, std::vector<size_t>& shape) |
| 94 | { |
| 95 | char* header_c = (char*)malloc(header_len * sizeof(char)); |
| 96 | size_t n_elems = fread((void*)header_c, sizeof(char), header_len, f_ptr); |
| 97 | if (n_elems != header_len) { |
| 98 | free(header_c); |
| 99 | return -1; |
| 100 | } |
| 101 | std::string header(header_c, header_len); |
| 102 | free(header_c); |
| 103 | |
| 104 | size_t start, end; |
| 105 | start = header.find("'descr'") + 7; |
| 106 | start = header.find("'", start); |
| 107 | end = header.find("'", start + 1); |
| 108 | type = typeFromNumpyDesc(header.substr(start + 1, end - start - 1)); |
| 109 | |
| 110 | start = header.find("'fortran_order'") + 15; |
| 111 | start = header.find(":", start); |
| 112 | end = header.find(",", start + 1); |
| 113 | if (header.substr(start + 1, end - start - 1).find("False") == std::string::npos) { |
| 114 | throw std::runtime_error("Unsupported value for fortran_order while reading npy file"); |
| 115 | } |
| 116 | |
| 117 | start = header.find("'shape'") + 7; |
| 118 | start = header.find("(", start); |
| 119 | end = header.find(")", start + 1); |
| 120 | |
| 121 | std::istringstream shape_stream(header.substr(start + 1, end - start - 1)); |
| 122 | std::string token; |
| 123 | |
| 124 | shape.clear(); |
| 125 | while (std::getline(shape_stream, token, ',')) { |
| 126 | if (token.find_first_not_of(' ') == std::string::npos) { |
| 127 | break; |
| 128 | } |
| 129 | shape.push_back(std::stoul(token)); |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | Tensor Tensor::loadNpy(const std::string& npy_file, const MemoryType where) |
| 136 | { |