SafeTensorsLoader<T>

Models & Types · AiDotNet.ModelLoading

Loads model weights from SafeTensors format files.

For Beginners

SafeTensors is like a special container for AI model weights.

Why SafeTensors instead of pickle files?

  • Safe: Cannot execute arbitrary code (unlike pickle)
  • Fast: Memory-mapped loading for quick access
  • Simple: Just tensors and their metadata

This loader reads SafeTensors files and converts them to our Tensor format so we can use pretrained weights from HuggingFace and other sources.

File structure:

[8 bytes: header length]
[JSON header: tensor metadata]
[tensor data: raw bytes]

How It Works

SafeTensors is a format developed by Hugging Face for storing model tensors safely. It’s the standard format for Stable Diffusion and other modern ML models.

Constructors

Constructor Summary
SafeTensorsLoader()

Methods

Method Summary
Dictionary<string, Tensor<T>> Load(string path) Loads all tensors from a SafeTensors file.
Dictionary<string, Tensor<T>> Load(string path, IEnumerable<string> tensorNames) Loads specific tensors from a SafeTensors file.
List<TensorMetadata> GetTensorInfo(string path) Gets the list of tensor names in a SafeTensors file without loading data.