MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TensorReader.cpp
Go to the documentation of this file.
1#include "TensorReader.hpp"
2
3namespace MayaFlux::IO {
4
5std::optional<Kakshya::TensorSet> TensorReader::read_tensors(const TensorReadOptions& options)
6{
7 if (!is_open()) {
8 fail("no file is open");
9 return std::nullopt;
10 }
11
12 std::vector<std::string> names = options.names;
13 if (names.empty()) {
14 for (const auto& spec : describe())
15 names.push_back(spec.name);
16 }
17
20 set.tensors.reserve(names.size());
21 for (const auto& name : names) {
22 auto tensor = read(name);
23 if (!tensor)
24 return std::nullopt;
25 set.tensors.push_back(std::move(*tensor));
26 }
27 return set;
28}
29
30std::optional<Kakshya::TensorSpec> TensorReader::find_spec(std::string_view name) const
31{
32 for (auto& spec : describe()) {
33 if (spec.name == name)
34 return std::move(spec);
35 }
36 return std::nullopt;
37}
38
39std::optional<FileMetadata> TensorReader::get_metadata() const
40{
41 if (!is_open())
42 return std::nullopt;
43
44 FileMetadata metadata;
45 metadata.format = format_name();
46 std::error_code error;
47 const auto size = std::filesystem::file_size(m_path, error);
48 metadata.file_size = error ? 0 : size;
49 return metadata;
50}
51
52std::vector<Kakshya::DataVariant> TensorReader::read_all()
53{
54 m_last_error = "Tensor data requires read() or read_tensors()";
55 return {};
56}
57
58std::vector<Kakshya::DataVariant> TensorReader::read_region(const FileRegion&)
59{
60 m_last_error = "Tensor data requires read() or read_tensors()";
61 return {};
62}
63
64std::shared_ptr<Kakshya::SignalSourceContainer> TensorReader::create_container()
65{
66 m_last_error = "Tensor data does not use a SignalSourceContainer";
67 return nullptr;
68}
69
70bool TensorReader::load_into_container(std::shared_ptr<Kakshya::SignalSourceContainer>)
71{
72 return fail("Tensor data cannot load into a SignalSourceContainer");
73}
74
75std::type_index TensorReader::get_data_type() const
76{
77 return typeid(Kakshya::TensorSet);
78}
79
80std::type_index TensorReader::get_container_type() const
81{
82 return typeid(Kakshya::TensorSet);
83}
84
85}
virtual bool is_open() const =0
Check if a file is currently open.
std::type_index get_data_type() const override
Get the data type this reader produces.
std::vector< Kakshya::DataVariant > read_all() override
Read all data from the file into memory.
virtual std::map< std::string, std::string > tensor_metadata() const
Free-form string metadata stored in the file, empty if the format has none.
std::optional< FileMetadata > get_metadata() const override
Get metadata from the open file.
virtual std::vector< Kakshya::TensorSpec > describe() const =0
Specs of every tensor in the open file, in file order, without reading data.
bool fail(std::string reason)
Record a failure reason and return false.
virtual std::optional< Kakshya::TensorData > read(std::string_view name)=0
Read one tensor by name.
bool load_into_container(std::shared_ptr< Kakshya::SignalSourceContainer >) override
Load file data into an existing container.
std::vector< Kakshya::DataVariant > read_region(const FileRegion &) override
Read a specific region of data.
std::type_index get_container_type() const override
Get the container type this reader creates.
std::optional< Kakshya::TensorSpec > find_spec(std::string_view name) const
Spec of one tensor by name, without reading data.
std::shared_ptr< Kakshya::SignalSourceContainer > create_container() override
Create and initialize a container from the file.
virtual std::string format_name() const =0
Format identifier such as safetensors or npy.
std::optional< Kakshya::TensorSet > read_tensors(const TensorReadOptions &options={})
Read a selection of tensors together with the file's metadata.
uint64_t file_size
Size in bytes.
std::string format
File format identifier (e.g., "wav", "mp3", "hdf5").
Generic metadata structure for any file type.
Generic region descriptor for any file type.
std::vector< std::string > names
Tensors to read, in this order.
Selection for TensorReader::read_tensors().
std::vector< TensorData > tensors
Definition Tensor.hpp:168
std::map< std::string, std::string > metadata
Definition Tensor.hpp:169
Named tensors that belong together, such as a model's weights or I/O.
Definition Tensor.hpp:167