SafeTensors

Read and write the SafeTensors binary format.

FunctionDescription
load filePathLoad all tensors from a .safetensors file.
loadMeta filePathLoad only the header metadata from a .safetensors file.
loadMetaFromStream streamParse the SafeTensors header from an open stream.
Returns (headerSize, tensorMetadata).
loadSelected filePath namesLoad only the tensors whose names are in the given set.
save tensors filePathSave tensors to a .safetensors file.

load

load filePath

Load all tensors from a .safetensors file.

Parameters

  • filePath : string

Returns Map<string, Tensor>


loadMeta

loadMeta filePath

Load only the header metadata from a .safetensors file.

Parameters

  • filePath : string

Returns Map<string, TensorMeta>


loadMetaFromStream

loadMetaFromStream stream

Parse the SafeTensors header from an open stream. Returns (headerSize, tensorMetadata).

Parameters

  • stream : Stream

Returns int64 * Map<string, TensorMeta>


loadSelected

loadSelected filePath names

Load only the tensors whose names are in the given set.

Parameters

  • filePath : string
  • names : Set<string>

Returns Map<string, TensorMeta> * Map<string, Tensor>


save

save tensors filePath

Save tensors to a .safetensors file.

Parameters

  • tensors : Map<string, Tensor>
  • filePath : string