Expand description
Reading and writing arrays: safetensors (MLX’s checkpoint format) and NumPy’s .npy,
any tree of arrays by path (save, load_into),
the IDX format (MNIST) and a minimal json parser for model files. Files whose
tensors are aligned (files nuvec writes are) are memory-mapped without a copy; others,
such as mlx-community checkpoints with unpadded headers, are read once at load, in
parallel, without being mapped.
Stability: no promise when this crate is depended on directly; depend on nuvec,
whose re-exports are semver-tracked.
Status: pre-alpha.
Modules§
- json
- A minimal JSON parser (RFC 8259) for safetensors headers and model files (configs, tokenizers), so that no serialization framework is needed.
Structs§
- Footprint
- What loading safetensors checkpoints holds in the GPU allocator, in bytes (see
safetensors_footprint). Compare withnuvec_gpu::MemoryStats’smapped,activeandpeak_with_mapped. - Safe
Tensors - The tensors and metadata of a safetensors file.
- Save
Handle - A background safetensors write (
save_safetensors_async).SaveHandle::waitreturns its result; dropping the handle waits for the write to finish.
Constants§
- VERSION
- The version of this crate.
Functions§
- load_
into - Reads a file written by
saveintotreeby path, strictly: every leaf must be in the file with its shape, and nothing else (loaded dtypes are kept). On any problem the error lists every one andtreeis unchanged. - load_
npy - Loads a
.npyfile as a view of the memory-mapped file. - load_
safetensors - Loads a safetensors file. The tensors are views of the memory-mapped file or, if they are not aligned to their element sizes (MLX writes headers without padding), of one buffer they are read into with parallel positioned reads (the file is then not mapped).
- load_
safetensors_ filtered load_safetensors, returning only the tensors whose nameskeepaccepts. In files that are read rather than mapped (misaligned ones, as MLX writes them), the others are not read at all: loading a language model from a multimodal checkpoint, say, skips the vision weights.- parse_
idx - Parses an IDX file’s bytes. Element types map to
uint8,int8,int16,int32,float32andfloat64. - read_
idx - Reads an (uncompressed) IDX file.
- safetensors_
footprint - What loading a safetensors file, or every
*.safetensorsfile of a directory, will hold, from their headers only (ordinary reads; nothing is mapped, copied or wired): the bytes kept mapped, the bytes copied, and the transient peak of the load. - save
- Writes the leaves of
treeas a safetensors file, each named by its path (MLX’stree_flattennames), evaluating them. - save_
npy - Saves
a(evaluating it) as a C-order.npyfile (version 1). bfloat16 has no NumPy type and is rejected. - save_
safetensors - Saves
tensors(evaluating them) andmetadataas a safetensors file, written topath.tmpand renamed into place. - save_
safetensors_ async save_safetensorsin the background, for snapshots taken while work continues (a KV cache or recurrent state copied withcopy, say): the arrays are submitted for evaluation (nuvec_core::async_eval) and returned at once with a handle; a background thread waits for them and writespath.tmp, renamed into place on success. The file is byte-identical tosave_safetensors’s. The handle holds the arrays until the write is done, so an in-place update of one of them copies instead of writing into memory being saved.