SafeTensors

Read and write the SafeTensors binary format.

FunctionDescription
indexShardFiles indexPathReturn the distinct shard filenames referenced by an index, sorted ordinally.
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.
openFile filePathOpen and validate one SafeTensors file for name-based tensor reads.
openIndex indexPathOpen and validate a sharded SafeTensors index and every referenced shard.
save tensors filePathSave tensors to a .safetensors file after staging them as contiguous CPU tensors.

indexShardFiles

indexShardFiles indexPath

Return the distinct shard filenames referenced by an index, sorted ordinally.

Parameters

  • indexPath : string

Returns string list


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>


openFile

openFile filePath

Open and validate one SafeTensors file for name-based tensor reads.

Parameters

  • filePath : string

Returns SafeTensorReader


openIndex

openIndex indexPath

Open and validate a sharded SafeTensors index and every referenced shard.

Parameters

  • indexPath : string

Returns SafeTensorReader


save

save tensors filePath

Save tensors to a .safetensors file after staging them as contiguous CPU tensors.

Parameters

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