pulsatrix 1.1.0
Loading...
Searching...
No Matches
pulsatrix::SafetensorsFile Class Reference

A parsed, fully validated safetensors file, held in memory or memory-mapped. Copies share the same bytes. More...

#include <safetensors.hpp>

Public Member Functions

const std::vector< std::string > & names () const
 Tensor names in storage order (by data offset).
 
bool contains (const std::string &name) const
 
const SafetensorsTensorInfo & info (const std::string &name) const
 The header entry for name.
 
const std::map< std::string, std::string > & metadata () const
 The __metadata__ string map (empty if the file has none).
 
std::pair< const uint8_t *, size_t > bytes (const std::string &name) const
 The raw little-endian bytes of name, as {pointer, size}, valid while this object lives. For dtypes that don't convert to a Tensor yet.
 
Tensor tensor (const std::string &name, DeviceBackend *backend) const
 Copies tensor name into a new float32 Tensor on backend's device, converting its dtype (IO-6): BF16, F16, F8_E4M3 and F8_E5M2 widen exactly (subnormals, infinities and NaN included); F64 rounds to nearest, with out-of-range values becoming infinities.
 

Static Public Member Functions

static SafetensorsFile Read (const std::string &path)
 Reads and validates a file.
 
static SafetensorsFile Map (const std::string &path)
 Memory-maps a file and validates it (IO-5): the operating system loads pages as tensors are read, so a multi-gigabyte checkpoint isn't copied into memory whole. The file must not change while any copy of this object lives.
 
static SafetensorsFile Parse (std::vector< uint8_t > bytes)
 Validates a file's bytes.
 

Detailed Description

A parsed, fully validated safetensors file, held in memory or memory-mapped. Copies share the same bytes.

Member Function Documentation

◆ bytes()

std::pair< const uint8_t *, size_t > pulsatrix::SafetensorsFile::bytes ( const std::string &  name) const

The raw little-endian bytes of name, as {pointer, size}, valid while this object lives. For dtypes that don't convert to a Tensor yet.

Exceptions
std::invalid_argumentif there is no tensor called name.

◆ contains()

bool pulsatrix::SafetensorsFile::contains ( const std::string &  name) const
inline

◆ info()

const SafetensorsTensorInfo & pulsatrix::SafetensorsFile::info ( const std::string &  name) const

The header entry for name.

Exceptions
std::invalid_argumentif there is none.

◆ Map()

static SafetensorsFile pulsatrix::SafetensorsFile::Map ( const std::string &  path)
static

Memory-maps a file and validates it (IO-5): the operating system loads pages as tensors are read, so a multi-gigabyte checkpoint isn't copied into memory whole. The file must not change while any copy of this object lives.

Exceptions
std::runtime_errorif the file can't be opened or mapped.
std::invalid_argumentif its contents are not a valid safetensors file.

◆ metadata()

const std::map< std::string, std::string > & pulsatrix::SafetensorsFile::metadata ( ) const
inline

The __metadata__ string map (empty if the file has none).

◆ names()

const std::vector< std::string > & pulsatrix::SafetensorsFile::names ( ) const
inline

Tensor names in storage order (by data offset).

◆ Parse()

static SafetensorsFile pulsatrix::SafetensorsFile::Parse ( std::vector< uint8_t >  bytes)
static

Validates a file's bytes.

Exceptions
std::invalid_argumentif they are not a valid safetensors file.

◆ Read()

static SafetensorsFile pulsatrix::SafetensorsFile::Read ( const std::string &  path)
static

Reads and validates a file.

Exceptions
std::runtime_errorif the file can't be opened or read.
std::invalid_argumentif its contents are not a valid safetensors file.

◆ tensor()

Tensor pulsatrix::SafetensorsFile::tensor ( const std::string &  name,
DeviceBackend *  backend 
) const

Copies tensor name into a new float32 Tensor on backend's device, converting its dtype (IO-6): BF16, F16, F8_E4M3 and F8_E5M2 widen exactly (subnormals, infinities and NaN included); F64 rounds to nearest, with out-of-range values becoming infinities.

Exceptions
std::invalid_argumentif there is no tensor called name, or it holds integers or booleans.

The documentation for this class was generated from the following file: