Read one tensor by name.
153{
155 fail(
"no file is open");
156 return std::nullopt;
157 }
158
159 const auto entry = std::ranges::find_if(
m_entries,
160 [name](
const Entry& candidate) {
return candidate.spec.name == name; });
162 fail(
"no tensor named '" + std::string(name) +
"'");
163 return std::nullopt;
164 }
165
166 size_t size = 0;
167 void* heap = mz_zip_reader_extract_to_heap(&
m_archive->zip, entry->index, &size, 0);
168 if (!heap) {
169 fail(
"cannot extract '" + std::string(name) +
"'");
170 return std::nullopt;
171 }
172
174 const auto storage = tensor ? tensor->storage() : std::span<std::byte> {};
175 if (!tensor || entry->data_offset + storage.size() > size) {
176 mz_free(heap);
177 fail(
"tensor '" + std::string(name) +
"' is truncated");
178 return std::nullopt;
179 }
180
181 if (!storage.empty()) {
182 std::memcpy(storage.data(), static_cast<const std::byte*>(heap) + entry->data_offset, storage.size());
185 }
186 }
187 mz_free(heap);
188 return tensor;
189}
std::vector< Entry > m_entries
std::unique_ptr< Detail::NpzArchive > m_archive
bool fail(std::string reason)
Record a failure reason and return false.
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
size_t component_bytes(Kakshya::TensorDType dtype) noexcept
Bytes of one independently ordered component of an element.
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.