pulsatrix 1.1.0
Loading...
Searching...
No Matches
safetensors.hpp
Go to the documentation of this file.
1
20#pragma once
21
22#include <cstddef>
23#include <cstdint>
24#include <map>
25#include <memory>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include "pulsatrix/tensor.hpp"
31
32namespace pulsatrix {
33
36
40 std::vector<int64_t> shape;
41 uint64_t data_begin;
42 uint64_t data_end;
43};
44
48public:
54 [[nodiscard]] static SafetensorsFile Read(const std::string& path);
55
63 [[nodiscard]] static SafetensorsFile Map(const std::string& path);
64
69 [[nodiscard]] static SafetensorsFile Parse(std::vector<uint8_t> bytes);
70
72 [[nodiscard]] const std::vector<std::string>& names() const { return names_; }
73
74 [[nodiscard]] bool contains(const std::string& name) const { return infos_.count(name) != 0; }
75
77 [[nodiscard]] const SafetensorsTensorInfo& info(const std::string& name) const;
78
80 [[nodiscard]] const std::map<std::string, std::string>& metadata() const { return metadata_; }
81
87 [[nodiscard]] std::pair<const uint8_t*, size_t> bytes(const std::string& name) const;
88
96 [[nodiscard]] Tensor tensor(const std::string& name, DeviceBackend* backend) const;
97
98private:
99 SafetensorsFile() = default;
100
102 struct Storage {
103 virtual ~Storage() = default;
104 const uint8_t* data = nullptr;
105 size_t size = 0;
106 };
108 [[nodiscard]] static SafetensorsFile FromStorage(std::shared_ptr<const Storage> storage);
109
110 std::shared_ptr<const Storage> storage_;
111 size_t data_start_ = 0;
112 std::vector<std::string> names_;
113 std::map<std::string, SafetensorsTensorInfo> infos_;
114 std::map<std::string, std::string> metadata_;
115};
116
126[[nodiscard]] std::vector<uint8_t> SerializeSafetensors(const std::vector<std::pair<std::string, const Tensor*>>& tensors,
127 const std::map<std::string, std::string>& metadata = {});
128
133void WriteSafetensors(const std::string& path, const std::vector<std::pair<std::string, const Tensor*>>& tensors,
134 const std::map<std::string, std::string>& metadata = {});
135
136} // namespace pulsatrix
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
A parsed, fully validated safetensors file, held in memory or memory-mapped. Copies share the same by...
Definition safetensors.hpp:47
bool contains(const std::string &name) const
Definition safetensors.hpp:74
static SafetensorsFile Map(const std::string &path)
Memory-maps a file and validates it (IO-5): the operating system loads pages as tensors are read,...
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:80
Tensor tensor(const std::string &name, DeviceBackend *backend) const
Copies tensor name into a new float32 Tensor on backend's device, converting its dtype (IO-6): BF16,...
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:72
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. The floating-point ones convert to a Tensor.
Definition safetensors.hpp:35
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:38
uint64_t data_begin
Definition safetensors.hpp:41
std::vector< int64_t > shape
Definition safetensors.hpp:40
uint64_t data_end
Definition safetensors.hpp:42
SafetensorsDtype dtype
Definition safetensors.hpp:39
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).