MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ read()

std::optional< Kakshya::TensorData > MayaFlux::IO::SafetensorsReader::read ( std::string_view name)
nodiscardoverridevirtual

Read one tensor by name.

Returns
The tensor, or std::nullopt with get_last_error() set.

Implements MayaFlux::IO::TensorReader.

Definition at line 178 of file SafetensorsFormat.cpp.

179{
180 if (!m_open) {
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; });
187 if (entry == m_entries.end()) {
188 fail("no tensor named '" + std::string(name) + "'");
189 return std::nullopt;
190 }
191
192 auto tensor = Kakshya::TensorData::allocate(entry->spec);
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()) {
200 m_file.clear();
201 m_file.seekg(static_cast<std::streamoff>(m_data_start + entry->begin));
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 }
207 Detail::swap_components(storage, Detail::component_bytes(entry->spec.dtype));
208 }
209 return tensor;
210}
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.
Definition Tensor.cpp:72
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.

References MayaFlux::Kakshya::TensorData::allocate(), MayaFlux::IO::Detail::component_bytes(), MayaFlux::IO::TensorReader::fail(), MayaFlux::IO::Detail::host_is_little_endian(), m_data_start, m_entries, m_file, m_open, MayaFlux::Kakshya::TensorSpec::name, MayaFlux::IO::SafetensorsReader::Entry::spec, and MayaFlux::IO::Detail::swap_components().

Here is the call graph for this function: