Decode a .npy prefix into a tensor spec.
Fortran-ordered arrays become strided specs, so no data is rearranged.
139{
141 if (!total || *total != prefix.size()) {
142 if (total) {
143 why = "numpy header is truncated";
144 }
145 return std::nullopt;
146 }
147
148 const size_t header_start = std::to_integer<uint8_t>(prefix[6]) == 1 ? 10 : 12;
149 const std::string_view text(reinterpret_cast<const char*>(prefix.data()) + header_start,
150 prefix.size() - header_start);
151
152 const auto descr = value_after_key(text, "descr");
153 if (descr.empty() || (descr[0] != '\'' && descr[0] != '"')) {
154 why = descr.empty() ? "numpy header has no descr" : "structured numpy dtypes are not supported";
155 return std::nullopt;
156 }
157 const auto descr_end = descr.find(descr[0], 1);
158 if (descr_end == std::string_view::npos) {
159 why = "numpy descr is malformed";
160 return std::nullopt;
161 }
162 const auto typestr =
npy_dtype(descr.substr(1, descr_end - 1), why);
163 if (!typestr) {
164 return std::nullopt;
165 }
166
167 const auto order = value_after_key(text, "fortran_order");
168 bool fortran = false;
169 if (order.starts_with("True")) {
170 fortran = true;
171 } else if (!order.starts_with("False")) {
172 why = "numpy header has no fortran_order";
173 return std::nullopt;
174 }
175
176 const auto shape = parse_shape(value_after_key(text, "shape"));
177 if (!shape) {
178 why = "numpy header has no valid shape";
179 return std::nullopt;
180 }
181
184 header.data_offset = *total;
186
187 if (fortran && shape->size() > 1) {
188 header.spec.strides.assign(shape->size(), 1);
189 for (size_t i = 1; i < shape->size(); ++i) {
190 header.spec.strides[i] = header.spec.strides[i - 1] * static_cast<int64_t>(std::max<uint64_t>((*shape)[i - 1], 1));
191 }
192 }
193
194 if (const auto reason = header.spec.diagnose(); !reason.empty()) {
195 why = reason;
196 return std::nullopt;
197 }
198 return header;
199}
std::optional< size_t > npy_prefix_size(std::span< const std::byte > leading, std::string &why)
Total prefix size (magic, version, length field and header) of a .npy array.
std::optional< NpyTypestr > npy_dtype(std::string_view typestr, std::string &why)
Parse a numpy type string such as <f4, >i8 or |b1.
static TensorSpec contiguous(TensorDType dtype, std::vector< uint64_t > shape, std::string name={})
C-ordered spec with no offset, axes or quantization.