Read one tensor by name.
88{
90 fail(
"no file is open");
91 return std::nullopt;
92 }
93
94 const auto entry = std::ranges::find_if(
m_layout,
95 [name](const RawTensorEntry& candidate) { return candidate.spec.name == name; });
97 fail(
"no tensor named '" + std::string(name) +
"'");
98 return std::nullopt;
99 }
100
102 if (!tensor) {
103 fail(
"cannot allocate tensor '" + std::string(name) +
"'");
104 return std::nullopt;
105 }
106
107 const auto storage = tensor->storage();
108 if (!storage.empty()) {
111 if (!
m_file.read(
reinterpret_cast<char*
>(storage.data()),
static_cast<std::streamsize
>(storage.size()))) {
112 fail(
"tensor '" + std::string(name) +
"' is truncated");
113 return std::nullopt;
114 }
117 }
118 }
119 return tensor;
120}
std::vector< RawTensorEntry > m_layout
std::vector< uint64_t > m_offsets
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.