13 struct SafetensorsEntry {
14 std::string_view name;
19 constexpr std::array<SafetensorsEntry, 22> k_safetensors { {
20 {
"BOOL", Kind::BOOL, 8 },
21 {
"F4", Kind::FLOAT4_E2M1FN, 4 },
22 {
"F6_E2M3", Kind::FLOAT6_E2M3FN, 6 },
23 {
"F6_E3M2", Kind::FLOAT6_E3M2FN, 6 },
24 {
"U8", Kind::UINT, 8 },
25 {
"I8", Kind::INT, 8 },
26 {
"F8_E5M2", Kind::FLOAT8_E5M2, 8 },
27 {
"F8_E4M3", Kind::FLOAT8_E4M3FN, 8 },
28 {
"F8_E8M0", Kind::FLOAT8_E8M0FNU, 8 },
29 {
"F8_E4M3FNUZ", Kind::FLOAT8_E4M3FNUZ, 8 },
30 {
"F8_E5M2FNUZ", Kind::FLOAT8_E5M2FNUZ, 8 },
31 {
"I16", Kind::INT, 16 },
32 {
"U16", Kind::UINT, 16 },
33 {
"F16", Kind::FLOAT, 16 },
34 {
"BF16", Kind::BFLOAT, 16 },
35 {
"I32", Kind::INT, 32 },
36 {
"U32", Kind::UINT, 32 },
37 {
"F32", Kind::FLOAT, 32 },
38 {
"C64", Kind::COMPLEX, 64 },
39 {
"F64", Kind::FLOAT, 64 },
40 {
"I64", Kind::INT, 64 },
41 {
"U64", Kind::UINT, 64 },
48 return std::endian::native == std::endian::little;
53 const size_t bits = (dtype.kind == Kind::COMPLEX || dtype.kind == Kind::BCOMPLEX)
56 return bits % 8 == 0 ? bits / 8 : 0;
64 std::reverse(data.begin() +
static_cast<std::ptrdiff_t
>(offset),
70 for (
const auto& entry : k_safetensors) {
71 if (entry.name == name)
81 for (
const auto& entry : k_safetensors) {
82 if (entry.kind == dtype.kind && entry.bits == dtype.bits)
88std::optional<NpyTypestr>
npy_dtype(std::string_view typestr, std::string& why)
91 bool big_endian =
false;
92 if (!typestr.empty()) {
110 if (pos >= typestr.size()) {
111 why =
"malformed numpy type string '" + std::string(typestr) +
"'";
115 const char code = typestr[pos++];
117 const char* end = typestr.data() + typestr.size();
118 const auto [parsed, error] = std::from_chars(typestr.data() + pos, end, size);
119 if (error != std::errc {} || parsed != end || size == 0 || size > 16) {
120 why =
"malformed numpy type string '" + std::string(typestr) +
"'";
124 const auto bits =
static_cast<uint8_t
>(size * 8);
125 const auto make = [&](Kind kind) -> std::optional<NpyTypestr> {
126 return NpyTypestr { .dtype = { .kind = kind, .bits = bits, .lanes = 1 }, .big_endian = big_endian && size > 1 };
131 if (size == 2 || size == 4 || size == 8)
132 return make(Kind::FLOAT);
135 if (size == 1 || size == 2 || size == 4 || size == 8)
136 return make(Kind::INT);
139 if (size == 1 || size == 2 || size == 4 || size == 8)
140 return make(Kind::UINT);
144 return make(Kind::BOOL);
147 if (size == 8 || size == 16)
148 return make(Kind::COMPLEX);
154 why =
"numpy type '" + std::string(typestr) +
"' is not supported";
160 if (dtype.
lanes != 1)
163 const auto sized = [&](
char code) -> std::optional<std::string> {
164 const unsigned size = dtype.
bits / 8U;
165 return std::string(size == 1 ?
"|" :
"<") + code + std::to_string(size);
168 switch (dtype.
kind) {
170 return (dtype.
bits == 16 || dtype.
bits == 32 || dtype.
bits == 64) ? sized(
'f') : std::nullopt;
172 return (dtype.
bits == 8 || dtype.
bits == 16 || dtype.
bits == 32 || dtype.
bits == 64) ? sized(
'i') : std::nullopt;
174 return (dtype.
bits == 8 || dtype.
bits == 16 || dtype.
bits == 32 || dtype.
bits == 64) ? sized(
'u') : std::nullopt;
176 return dtype.
bits == 8 ? std::optional<std::string> {
"|b1" } : std::nullopt;
178 return (dtype.
bits == 64 || dtype.
bits == 128) ? sized(
'c') : std::nullopt;
186 std::ifstream file(path, std::ios::binary);
190 file.seekg(0, std::ios::end);
192 head.
file_size =
static_cast<uint64_t
>(file.tellg());
195 head.
bytes.resize(
static_cast<size_t>(std::min<uint64_t>(limit, head.
file_size)));
196 file.read(
reinterpret_cast<char*
>(head.
bytes.data()),
static_cast<std::streamsize
>(head.
bytes.size()));
197 head.
bytes.resize(
static_cast<size_t>(file.gcount()));
203 auto ext = std::filesystem::path(path).extension().string();
204 if (!ext.empty() && ext[0] ==
'.')
206 std::ranges::transform(ext, ext.begin(), [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
213 const auto view = tensor.
access();
217 const auto raw = view->bytes();
218 if (!raw.empty() || view->element_count() == 0)
224 const auto gathered = dense->access();
227 return gathered->bytes();
static std::optional< TensorData > copy_of(const TensorAccess &view)
Dense C-ordered copy of any tensor view.
std::optional< TensorAccess > access() const
Borrow the tensor, or std::nullopt when it holds nothing valid.
Owning host tensor: a TensorSpec and the bytes it describes.
std::string lower_extension(const std::string &path)
Lowercase extension of a path without its dot.
size_t component_bytes(Kakshya::TensorDType dtype) noexcept
Bytes of one independently ordered component of an element.
std::optional< std::string > npy_typestr(Kakshya::TensorDType dtype)
Little-endian numpy type string for an element type, when numpy has one.
std::optional< Kakshya::TensorDType > safetensors_dtype(std::string_view name) noexcept
Element type for a safetensors dtype name such as F32 or BF16.
std::optional< std::span< const std::byte > > dense_bytes(const Kakshya::TensorData &tensor, std::optional< Kakshya::TensorData > &dense)
Elements of a tensor as dense C-order bytes.
void swap_components(std::span< std::byte > data, size_t component_bytes) noexcept
Reverse the byte order of every component in place.
std::optional< std::string_view > safetensors_name(Kakshya::TensorDType dtype) noexcept
Safetensors dtype name for an element type, when the format has one.
bool host_is_little_endian() noexcept
Whether this machine stores multi-byte values least significant byte first.
std::optional< NpyTypestr > npy_dtype(std::string_view typestr, std::string &why)
Parse a numpy type string such as <f4, >i8 or |b1.
std::optional< FileHead > read_file_head(const std::string &path, size_t limit)
Read up to limit leading bytes of a file, or std::nullopt if it cannot be opened.
TensorDTypeKind
Element family of a tensor.
std::vector< std::byte > bytes
First bytes of a file together with its total size.
Element type and byte order read from a numpy type string.
Element type as kind, bit width and lane count.