9 for (
size_t i = 0; i < entries.size(); ++i) {
10 const auto& spec = entries[i].spec;
11 if (spec.name.empty()) {
12 return fail(
"every raw tensor needs a name");
14 if (
const auto why = spec.diagnose(); !why.empty()) {
17 if (!spec.device.is_host_accessible()) {
18 return fail(
"tensor '" + spec.name +
"' is not host memory");
20 for (
size_t j = i + 1; j < entries.size(); ++j) {
21 if (entries[j].spec.name == spec.name) {
22 return fail(
"duplicate tensor name '" + spec.name +
"'");
32 std::error_code error;
33 return std::filesystem::is_regular_file(
resolve_path(filepath), error);
49 return fail(
"set_layout() must describe the tensors before open()");
57 m_file.seekg(0, std::ios::end);
58 const auto file_size =
static_cast<uint64_t
>(
m_file.tellg());
62 const uint64_t begin = entry.file_offset.value_or(cursor);
63 const auto range = entry.spec.byte_range();
64 if (!range || range->second > file_size || begin > file_size - range->second) {
66 return fail(
"tensor '" + entry.spec.name +
"' extends past the end of the file");
69 cursor = begin + range->second;
79 std::vector<Kakshya::TensorSpec> specs;
82 specs.push_back(entry.spec);
90 fail(
"no file is open");
94 const auto entry = std::ranges::find_if(
m_layout,
97 fail(
"no tensor named '" + std::string(name) +
"'");
103 fail(
"cannot allocate tensor '" + std::string(name) +
"'");
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");
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
std::vector< Kakshya::TensorSpec > describe() const override
Specs of every tensor in the open file, in file order, without reading data.
std::vector< RawTensorEntry > m_layout
std::vector< uint64_t > m_offsets
bool can_read(const std::string &filepath) const override
Check if a file can be read by this reader.
void close() override
Close the currently open file.
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Open a file for reading.
std::optional< Kakshya::TensorData > read(std::string_view name) override
Read one tensor by name.
bool set_layout(std::vector< RawTensorEntry > entries)
Declare the tensors the file holds.
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.
FileReadOptions
Generic options for file reading behavior.
Kakshya::TensorSpec spec
Named layout of the tensor: dtype, shape, strides and offset.
Where one tensor sits in a headerless file, and what it is.