Header-only safetensors loader and saver in C++
MIT License
Secure, dependency-free safetensors loader/writer in portable C/C++. Code is tested with fuzzer.
Little-endian.
See fuzz directory.
// define only in one *.cc
#define SAFETENSORS_CPP_IMPLEMENTATION
#include "safetensors.hh"
std::string warn, err;
bool ret = safetensors::load_from_file(filename, &st, &warn, &err);
if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}
if (!ret) {
std::cerr << "Failed to load: " << filename << "\n";
std::cerr << " ERR: " << err << "\n";
return false;
}
// Check if data_offsets are valid.
if (!safetensors::validate_data_offsets(st, err)) {
std::cerr << "Invalid data_offsets\n";
std::cerr << err << "\n";
return false;
}
for (size_t i = 0; i < st.tensors.size(); i++) {
// do something with tensor
}
for (size_t i = 0; i < st.metadata.size(); i++) {
// do something with __metadata__
}
Please see example.cc for more details.
> vcsetup.bat
Then open solution file in build
folder.
Run makefile
$ make
or
$ ./bootstrap-cmake-linux.sh
$ cd build
$ make
W.I.P.
C API will be provided in safetensors-c.h
for other language bindings.
shape
size check.internal::from_chars
(parse number(floating point value) from string)
MIT license
safetensors.hh
) : MIT license