34enum class SafetensorsDtype {
Bool,
U8,
I8,
I16,
U16,
I32,
U32,
I64,
U64,
F8_E4M3,
F8_E5M2,
F16,
BF16,
F32,
F64 };
61 [[nodiscard]]
const std::vector<std::string>&
names()
const {
return names_; }
63 [[nodiscard]]
bool contains(
const std::string& name)
const {
return infos_.count(name) != 0; }
69 [[nodiscard]]
const std::map<std::string, std::string>&
metadata()
const {
return metadata_; }
76 [[nodiscard]] std::pair<const uint8_t*, size_t>
bytes(
const std::string& name)
const;
88 std::vector<uint8_t> bytes_;
89 size_t data_start_ = 0;
90 std::vector<std::string> names_;
91 std::map<std::string, SafetensorsTensorInfo> infos_;
92 std::map<std::string, std::string> metadata_;
104[[nodiscard]] std::vector<uint8_t>
SerializeSafetensors(
const std::vector<std::pair<std::string, const Tensor*>>& tensors,
105 const std::map<std::string, std::string>& metadata = {});
111void WriteSafetensors(
const std::string& path,
const std::vector<std::pair<std::string, const Tensor*>>& tensors,
112 const std::map<std::string, std::string>& metadata = {});
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:219
A parsed, fully validated safetensors file held in memory.
Definition safetensors.hpp:45
bool contains(const std::string &name) const
Definition safetensors.hpp:63
static SafetensorsFile Parse(std::vector< uint8_t > bytes)
Validates a file's bytes.
static SafetensorsFile Read(const std::string &path)
Reads and validates a file.
const std::map< std::string, std::string > & metadata() const
The __metadata__ string map (empty if the file has none).
Definition safetensors.hpp:69
Tensor tensor(const std::string &name, DeviceBackend *backend) const
Copies tensor name into a new Tensor on backend's device.
const SafetensorsTensorInfo & info(const std::string &name) const
The header entry for name.
const std::vector< std::string > & names() const
Tensor names in storage order (by data offset).
Definition safetensors.hpp:61
std::pair< const uint8_t *, size_t > bytes(const std::string &name) const
The raw little-endian bytes of name, as {pointer, size}, valid while this object lives....
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Definition acquisition_functions.hpp:16
std::vector< uint8_t > SerializeSafetensors(const std::vector< std::pair< std::string, const Tensor * > > &tensors, const std::map< std::string, std::string > &metadata={})
Serializes tensors (as F32) and string metadata into safetensors bytes.
SafetensorsDtype
Element types a safetensors file can declare. Only F32 converts to a Tensor so far.
Definition safetensors.hpp:34
void WriteSafetensors(const std::string &path, const std::vector< std::pair< std::string, const Tensor * > > &tensors, const std::map< std::string, std::string > &metadata={})
SerializeSafetensors() written to path.
One tensor's header entry. Offsets are relative to the start of the data section.
Definition safetensors.hpp:37
uint64_t data_begin
Definition safetensors.hpp:40
std::vector< int64_t > shape
Definition safetensors.hpp:39
uint64_t data_end
Definition safetensors.hpp:41
SafetensorsDtype dtype
Definition safetensors.hpp:38
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).