Class SafeTensorsLoader<T>
- Namespace
- AiDotNet.ModelLoading
- Assembly
- AiDotNet.dll
Loads model weights from SafeTensors format files.
public class SafeTensorsLoader<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
SafeTensorsLoader<T>
- Inherited Members
Remarks
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.
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]
Methods
GetTensorInfo(string)
Gets the list of tensor names in a SafeTensors file without loading data.
public List<TensorMetadata> GetTensorInfo(string path)
Parameters
pathstringPath to the .safetensors file.
Returns
- List<TensorMetadata>
List of tensor names and their metadata.
Load(string)
Loads all tensors from a SafeTensors file.
public Dictionary<string, Tensor<T>> Load(string path)
Parameters
pathstringPath to the .safetensors file.
Returns
- Dictionary<string, Tensor<T>>
Dictionary mapping tensor names to loaded tensors.
Remarks
For Beginners: This loads all the weights from a SafeTensors file.
Example usage:
var loader = new SafeTensorsLoader<float>();
var weights = loader.Load("model.safetensors");
var vaeWeight = weights["first_stage_model.encoder.conv_in.weight"];
Exceptions
- FileNotFoundException
Thrown when the file doesn't exist.
- InvalidDataException
Thrown when the file format is invalid.
Load(string, IEnumerable<string>)
Loads specific tensors from a SafeTensors file.
public Dictionary<string, Tensor<T>> Load(string path, IEnumerable<string> tensorNames)
Parameters
pathstringPath to the .safetensors file.
tensorNamesIEnumerable<string>Names of tensors to load.
Returns
- Dictionary<string, Tensor<T>>
Dictionary mapping tensor names to loaded tensors.