Skip to content

tenzr:🇮🇴:SafetensorsFile

Read-only memory-mapped view of a safetensors file exposing its tensors, descriptors, and metadata.

#include <safetensors.h>

Public Functions

Name
SafetensorsFile(const std::string & filepath)
~SafetensorsFile()
Unmap the file and close its descriptor.
SafetensorsFile(const SafetensorsFile & ) =delete
SafetensorsFile & operator=(const SafetensorsFile & ) =delete
SafetensorsFile(SafetensorsFile && other)
SafetensorsFile & operator=(SafetensorsFile && other)
std::vector< std::string > TensorNames() const
Names of all tensors in the file.
bool HasTensor(const std::string & name) const
const TensorDescriptor & Descriptor(const std::string & name) const
const std::unordered_map< std::string, TensorDescriptor > & Descriptors() const
All tensor descriptors keyed by name.
const std::unordered_map< std::string, std::string > & Metadata() const
File metadata key/value pairs.
Tensor LoadTensor(const std::string & name) const
std::unordered_map< std::string, Tensor > LoadAll() const
Load every tensor in the file, keyed by name.

Public Functions Documentation

function SafetensorsFile

explicit SafetensorsFile(
    const std::string & filepath
)

Parameters:

  • filepath Path to the file.

Open and memory-map a safetensors file, parsing its header eagerly.

function ~SafetensorsFile

~SafetensorsFile()

Unmap the file and close its descriptor.

function SafetensorsFile

SafetensorsFile(
    const SafetensorsFile & 
) =delete

function operator=

SafetensorsFile & operator=(
    const SafetensorsFile & 
) =delete

function SafetensorsFile

SafetensorsFile(
    SafetensorsFile && other
)

Parameters:

  • other File to move from.

Move-construct a file mapping, leaving other closed.

function operator=

SafetensorsFile & operator=(
    SafetensorsFile && other
)

function TensorNames

std::vector< std::string > TensorNames() const

Names of all tensors in the file.

Return: Tensor name list.

Names of all tensors in the file.

function HasTensor

bool HasTensor(
    const std::string & name
) const

Parameters:

  • name Tensor name.
  • name Tensor name.

Return:

  • True when the tensor is present.
  • true when present.

Whether a named tensor exists. Test whether the file contains a tensor named name.

function Descriptor

const TensorDescriptor & Descriptor(
    const std::string & name
) const

Parameters:

  • name Tensor name.
  • name Tensor name.

Return:

  • The tensor's descriptor.
  • Reference to the tensor descriptor.

Descriptor for a named tensor. Descriptor for a named tensor, throwing when absent.

function Descriptors

inline const std::unordered_map< std::string, TensorDescriptor > & Descriptors() const

All tensor descriptors keyed by name.

function Metadata

inline const std::unordered_map< std::string, std::string > & Metadata() const

File metadata key/value pairs.

function LoadTensor

Tensor LoadTensor(
    const std::string & name
) const

Parameters:

  • name Tensor name.
  • name Tensor name.

Return:

  • The loaded tensor.
  • The loaded tensor.

Load a named tensor. Copy a named tensor's payload out of the mapping into a pooled buffer.

function LoadAll

std::unordered_map< std::string, Tensor > LoadAll() const

Load every tensor in the file, keyed by name.

Return: Named tensors, keyed by tensor name.

Load every tensor in the file.