Read one tensor by name.
108{
110 fail(
"no file is open");
111 return std::nullopt;
112 }
113 if (!name.empty() && name !=
m_spec.name) {
114 fail(
"no tensor named '" + std::string(name) +
"'");
115 return std::nullopt;
116 }
117
119 if (!tensor) {
120 fail(
"cannot allocate tensor '" +
m_spec.name +
"'");
121 return std::nullopt;
122 }
123
124 const auto storage = tensor->storage();
125 if (!storage.empty()) {
128 if (!
m_file.read(
reinterpret_cast<char*
>(storage.data()),
static_cast<std::streamsize
>(storage.size()))) {
129 fail(
"numpy data is truncated");
130 return std::nullopt;
131 }
134 }
135 }
136 return tensor;
137}
Kakshya::TensorSpec m_spec
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.