pulsatrix
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 <string>
26#include <utility>
27#include <vector>
28
29#include "pulsatrix/tensor.hpp"
30
31namespace pulsatrix {
32
35
39 std::vector<int64_t> shape;
40 uint64_t data_begin;
41 uint64_t data_end;
42};
43
46public:
52 [[nodiscard]] static SafetensorsFile Read(const std::string& path);
53
58 [[nodiscard]] static SafetensorsFile Parse(std::vector<uint8_t> bytes);
59
61 [[nodiscard]] const std::vector<std::string>& names() const { return names_; }
62
63 [[nodiscard]] bool contains(const std::string& name) const { return infos_.count(name) != 0; }
64
66 [[nodiscard]] const SafetensorsTensorInfo& info(const std::string& name) const;
67
69 [[nodiscard]] const std::map<std::string, std::string>& metadata() const { return metadata_; }
70
76 [[nodiscard]] std::pair<const uint8_t*, size_t> bytes(const std::string& name) const;
77
83 [[nodiscard]] Tensor tensor(const std::string& name, DeviceBackend* backend) const;
84
85private:
86 SafetensorsFile() = default;
87
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_;
93};
94
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 = {});
106
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 = {});
113
114} // namespace pulsatrix
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).