SafetensorsSourceSafetensors - Simple, safe way to store and distribute tensors
This is an OCaml port of the safetensors format, providing efficient serialization and deserialization of tensor data with metadata support.
type safetensor_error = | Invalid_header of stringInvalid UTF-8 in header
*)| Invalid_header_startInvalid start character in header
*)| Invalid_header_deserialization of stringJSON parse error
*)| Header_too_largeHeader exceeds maximum size
*)| Header_too_smallHeader is too small
*)| Invalid_header_lengthInvalid header length
*)| Tensor_not_found of stringTensor with given name not found
*)| Tensor_invalid_infoInvalid shape, dtype, or offset for tensor
*)| Invalid_offset of stringInvalid offset for tensor
*)| Io_error of stringI/O error during file operations
*)| Json_error of stringJSON processing error
*)| Invalid_tensor_view of string * int list * intInvalid tensor view creation
*)| Metadata_incomplete_bufferIncomplete metadata, file not fully covered
*)| Validation_overflowOverflow computing buffer size
*)| Misaligned_sliceSlice not aligned to byte boundary for sub-byte dtypes
*)Convert an error to a human-readable string
type dtype = | BOOL| F44-bit float
*)| F6_E2M36-bit float with 2 exponent bits, 3 mantissa bits
*)| F6_E3M26-bit float with 3 exponent bits, 2 mantissa bits
*)| U8Unsigned 8-bit integer
*)| I8Signed 8-bit integer
*)| F8_E5M28-bit float with 5 exponent bits, 2 mantissa bits
*)| F8_E4M38-bit float with 4 exponent bits, 3 mantissa bits
*)| F8_E8M08-bit float with 8 exponent bits, no mantissa
*)| I16Signed 16-bit integer
*)| U16Unsigned 16-bit integer
*)| F16Half-precision float
*)| BF16Brain float 16
*)| I32Signed 32-bit integer
*)| U32Unsigned 32-bit integer
*)| F32Single-precision float
*)| F64Double-precision float
*)| I64Signed 64-bit integer
*)| U64Unsigned 64-bit integer
*)type tensor_view = {dtype : dtype;shape : int list;data : string;Backing buffer
*)offset : int;Byte offset into data
*)length : int;Number of bytes
*)}A view into tensor data without copying
val tensor_view_new :
dtype:dtype ->
shape:int list ->
data:string ->
(tensor_view, safetensor_error) resultCreate a new tensor view. The data must exactly match the expected size based on dtype and shape.
type metadata = {metadata_kv : (string * string) list option;tensors : tensor_info array;index_map : (string, int) Hashtbl.t;}Metadata structure containing tensor information and optional key-value pairs
The main container holding tensor metadata and data
Deserialize a safetensors buffer into a container. The buffer should contain the complete safetensors file content.
val serialize :
(string * tensor_view) list ->
(string * string) list option ->
(string, safetensor_error) resultSerialize a list of named tensors with optional metadata key-value pairs into a safetensors buffer.
val serialize_to_file :
(string * tensor_view) list ->
(string * string) list option ->
string ->
(unit, safetensor_error) resultSerialize tensors directly to a file. This is more memory-efficient than serialize for large tensors.
Get a specific tensor by name from the container
Get all tensors from the container as a list of name-view pairs
Iterate over tensors in offset order (the order they appear in the file)
Get the names of all tensors in the container
Get the number of tensors in the container
Check if the container has no tensors
type slice_iterator = {view : tensor_view;mutable indices : (int * int) list;newshape : int list;}Read a little-endian 64-bit integer from a string at the given offset