Open a file for reading.
50{
56 }
57
58 m_file.seekg(0, std::ios::end);
59 const auto file_size =
static_cast<uint64_t
>(
m_file.tellg());
61
62 std::array<std::byte, 12> leading {};
63 m_file.read(
reinterpret_cast<char*
>(leading.data()),
static_cast<std::streamsize
>(leading.size()));
64 const auto got =
static_cast<size_t>(
m_file.gcount());
66
67 std::string why;
69 if (!prefix_size || *prefix_size > file_size) {
71 return fail(prefix_size ?
"numpy header is truncated" : why);
72 }
73
74 std::vector<std::byte> prefix(*prefix_size);
76 if (!
m_file.read(
reinterpret_cast<char*
>(prefix.data()),
static_cast<std::streamsize
>(prefix.size()))) {
78 return fail(
"numpy header is truncated");
79 }
80
81 const auto stem = std::filesystem::path(
m_path).stem().string();
83 if (!header) {
86 }
87
88 const auto bytes = header->spec.dtype.storage_bytes(header->spec.element_count().value_or(0));
89 if (!bytes || header->data_offset + *bytes > file_size) {
91 return fail(
"numpy data is shorter than its header declares");
92 }
93
99 return true;
100}
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
Kakshya::TensorSpec m_spec
void close() override
Close the currently open file.
bool fail(std::string reason)
Record a failure reason and return false.
std::optional< NpyHeader > parse_npy_header(std::span< const std::byte > prefix, std::string_view name, std::string &why)
Decode a .npy prefix into a tensor spec.
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.