Hub

Toro.Hub provides a Hugging Face Hub client for downloading pre-trained model weights. SafeTensors I/O is in the core Toro package.

open Toro
open Toro.NN
open Toro.Hub

SafeTensors

Read and write the SafeTensors binary format, the standard for storing model weights securely. The SafeTensors module is in the Toro namespace (core package).

Save

let tensors = Map [ "weight", weightTensor; "bias", biasTensor ]
SafeTensors.save tensors "model.safetensors"

Load

let loaded = SafeTensors.load "model.safetensors"
// loaded: Map<string, Tensor>

Supported data types: F16, BF16, F32, F64, I32, I64, U8, Bool.

Hugging Face Hub

Download model files from the Hugging Face Hub with automatic caching to ~/.cache/toro/hub/.

Download a file

let path =
    Hub.download "openai-community/gpt2" "model.safetensors"
    |> Async.RunSynchronously
// path: local file path in the cache directory

Download and load weights

let weights =
    Hub.loadSafeTensors "openai-community/gpt2" "model.safetensors"
    |> Async.RunSynchronously
// weights: Map<string, Tensor>

Authentication

For gated models, set the HF_TOKEN environment variable:

export HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxx

The client adds a Bearer token automatically when HF_TOKEN is set.

Loading Weights into a Model

Use Model.loadFromDict (from Toro.NN) to copy downloaded weights into a model:

let weights =
    Hub.loadSafeTensors "openai-community/gpt2" "model.safetensors"
    |> Async.RunSynchronously

let report = Model.loadFromDict model weights None Strict

Pass a name map when the parameter names in the file do not match the model:

let nameMap = Map [
    "transformer.wte.weight", "Embedding.Embeddings"
    // ...
]
let report = Model.loadFromDict model weights (Some nameMap) Lenient
printfn "Loaded %d params, %d missing" report.Loaded.Length report.Missing.Length

loadFromDict returns a LoadReport listing Loaded, Missing, Unexpected keys, and any ShapeMismatches or DTypeMismatches. Use Strict to error on any mismatch, or Lenient to allow partial loading.