MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RawTensorReader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "TensorReader.hpp"
4
5#include <fstream>
6
7namespace MayaFlux::IO {
8
9/**
10 * @struct RawTensorEntry
11 * @brief Where one tensor sits in a headerless file, and what it is.
12 */
14 /** @brief Named layout of the tensor: dtype, shape, strides and offset. */
16 /** @brief Byte position of the tensor's storage; defaults to just after the previous entry. */
17 std::optional<uint64_t> file_offset;
18 /** @brief Whether the file stores multi-byte values most significant byte first. */
19 bool big_endian {};
20};
21
22/**
23 * @class RawTensorReader
24 * @brief Reader for headerless binary files whose layout the caller supplies.
25 *
26 * Covers raw dumps, simple custom formats and any data whose layout lives in a
27 * sidecar the caller has already parsed. It is chosen explicitly, never by
28 * detection, because a raw file identifies nothing about itself.
29 *
30 * Usage:
31 * @code
32 * RawTensorReader reader;
33 * reader.set_layout({ { TensorSpec::contiguous(TensorDType::of<float>(), { 480, 640 }, "depth"), 0 } });
34 * reader.open("depth.bin");
35 * auto depth = reader.read("depth");
36 * @endcode
37 */
38class MAYAFLUX_API RawTensorReader : public TensorReader {
39public:
40 RawTensorReader() = default;
43 ~RawTensorReader() override { close(); }
44
45 /**
46 * @brief Declare the tensors the file holds.
47 * @return False, with get_last_error() set, for an invalid spec, a duplicate
48 * or empty name, or device memory.
49 */
50 bool set_layout(std::vector<RawTensorEntry> entries);
51
52 [[nodiscard]] bool can_read(const std::string& filepath) const override;
53 bool open(const std::string& filepath, FileReadOptions options = FileReadOptions::ALL) override;
54 void close() override;
55 [[nodiscard]] bool is_open() const override { return m_open; }
56 [[nodiscard]] std::vector<std::string> get_supported_extensions() const override { return {}; }
57
58 [[nodiscard]] std::string format_name() const override { return "raw"; }
59 [[nodiscard]] std::vector<Kakshya::TensorSpec> describe() const override;
60 [[nodiscard]] std::optional<Kakshya::TensorData> read(std::string_view name) override;
61
62private:
63 std::ifstream m_file;
64 std::vector<RawTensorEntry> m_layout;
65 std::vector<uint64_t> m_offsets;
66 bool m_open {};
67};
68
69}
bool is_open() const override
Check if a file is currently open.
std::vector< RawTensorEntry > m_layout
std::vector< uint64_t > m_offsets
RawTensorReader & operator=(const RawTensorReader &)=delete
void close() override
Close the currently open file.
std::vector< std::string > get_supported_extensions() const override
Get supported file extensions for this reader.
std::string format_name() const override
Format identifier such as safetensors or npy.
RawTensorReader(const RawTensorReader &)=delete
Typed FileReader boundary for formats that store named tensors.
FileReadOptions
Generic options for file reading behavior.
@ ALL
All options enabled.
Kakshya::TensorSpec spec
Named layout of the tensor: dtype, shape, strides and offset.
bool big_endian
Whether the file stores multi-byte values most significant byte first.
std::optional< uint64_t > file_offset
Byte position of the tensor's storage; defaults to just after the previous entry.
Where one tensor sits in a headerless file, and what it is.
Complete description of a tensor: type, shape, layout, meaning.