Read one tensor by name.
179{
181 fail(
"no file is open");
182 return std::nullopt;
183 }
184
185 const auto entry = std::ranges::find_if(
m_entries,
186 [name](
const Entry& candidate) {
return candidate.spec.name == name; });
188 fail(
"no tensor named '" + std::string(name) +
"'");
189 return std::nullopt;
190 }
191
193 if (!tensor) {
194 fail(
"cannot allocate tensor '" + std::string(name) +
"'");
195 return std::nullopt;
196 }
197
198 const auto storage = tensor->storage();
199 if (!storage.empty()) {
202 if (!
m_file.read(
reinterpret_cast<char*
>(storage.data()),
static_cast<std::streamsize
>(storage.size()))) {
203 fail(
"tensor '" + std::string(name) +
"' is truncated");
204 return std::nullopt;
205 }
208 }
209 return tensor;
210}
std::vector< Entry > m_entries
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.