Encode a tensor as a complete .npy array in dense C order.
202{
203 const auto& spec = tensor.
spec();
205 if (!typestr) {
206 why = "numpy has no type for element type " + spec.dtype.name();
207 return std::nullopt;
208 }
209
210 std::optional<Kakshya::TensorData> gathered;
211 const auto payload =
dense_bytes(tensor, gathered);
212 if (!payload) {
213 why = "tensor '" + spec.name + "' could not be read";
214 return std::nullopt;
215 }
216
217 const std::string body = "{'descr': '" + *typestr + "', 'fortran_order': False, 'shape': "
218 + shape_text(spec.shape) + ", }";
219 auto bytes = build_header(body, 1);
220 if (bytes.size() - 10 > 0xFFFF) {
221 bytes = build_header(body, 2);
222 }
223
224 const size_t data_start = bytes.size();
225 bytes.insert(bytes.end(), payload->begin(), payload->end());
228 }
229 return bytes;
230}
const TensorSpec & spec() const noexcept
Layout of the stored bytes.
size_t component_bytes(Kakshya::TensorDType dtype) noexcept
Bytes of one independently ordered component of an element.
std::optional< std::string > npy_typestr(Kakshya::TensorDType dtype)
Little-endian numpy type string for an element type, when numpy has one.
std::optional< std::span< const std::byte > > dense_bytes(const Kakshya::TensorData &tensor, std::optional< Kakshya::TensorData > &dense)
Elements of a tensor as dense C-order bytes.
void swap_components(std::span< std::byte > data, size_t component_bytes) noexcept
Reverse the byte order of every component in place.
bool host_is_little_endian() noexcept
Whether this machine stores multi-byte values least significant byte first.