Skip to content

Hugging Face

Jacobi integrates directly with the official Rust tokenizers, safetensors, and hf-hub crates. Downloaded files are cached in ~/.cache/huggingface/hub/ and automatically use HF_TOKEN when set.

Tokenizers

Load any Hugging Face tokenizer (BPE, Unigram, SentencePiece, or WordPiece) from the Hub or from a local tokenizer.json file.

use jacobi::prelude::*;

let tok = Tokenizer::from_pretrained("gpt2")?;
let ids = tok.encode_tensor("Hello from Jacobi!")?;
let text = tok.decode_tensor(&ids)?;

To load from disk instead of the Hub, call Tokenizer::from_file("tokenizer.json")?.

Scoped Weight Loader and Candle Export (hub::weights, LoraLinear, HfExporter)

For post-training (SFT, LoRA, DPO, or GRPO) and direct handoff to Candle, vLLM, SGLang, or Hugging Face Transformers, open checkpoints lazily with hub::weights (memory-mapped on demand with .pp(...) prefix scoping). Wrap frozen linear layers in LoraLinear so AdamW and Muon only optimize the low-rank adapters, then merge and write a ready-to-serve Hugging Face and Candle model folder with HfExporter.

// 1. Open single or sharded HF safetensors lazily on demand
let w = hub::weights("Qwen/Qwen2.5-0.5B")?.dtype(Dtype::BF16);
let embed = w.embedding("model.embed_tokens")?;
let blk0 = w.pp("model.layers").pp(0);
let norm = blk0.rms_norm("input_layernorm", 1e-6)?;
let q_base = blk0.linear("self_attn.q_proj")?; // transposes [out, in] -> [in, out]

// 2. Post-train with LoRA (base_w stays frozen with zero optimizer state)
let q_lora = LoraLinear::from_linear(q_base, 16, 32.0);
// ... run jit + value_and_grad + AdamW ...

// 3. Merge LoRA and export directly to a Candle / HF directory
let mut out = HfExporter::new().dtype(Dtype::BF16);
out.add_embedding("model.embed_tokens", &embed);
out.add_rms_norm("model.layers.0.input_layernorm", &norm);
out.add_lora_merged("model.layers.0.self_attn.q_proj", &q_lora, true);
out.save_dir("out/qwen-posttrained", Some("Qwen/Qwen2.5-0.5B"))?;

save_dir writes model.safetensors with PyTorch [out, in] linear weight orientation and format = "pt" metadata, and copies config.json, tokenizer.json, and related config files from the base repo cache so Candle loads "out/qwen-posttrained" out of the box.

Pretrained Weights and Configs

Download single or sharded .safetensors checkpoints directly from a Hub repository.

// Load all tensors into a HashMap<String, Tensor>
let weights = hub::load_safetensors("Qwen/Qwen2.5-0.5B")?;

// Or load matching weights directly into a #[module] struct
let model = hub::load_into(&model, "my-org/my-model")?;

// Parse config.json as serde_json::Value
let cfg = hub::load_config("Qwen/Qwen2.5-0.5B")?;

You can fetch any individual repository or dataset file with hub::download(repo, filename) and hub::dataset_file(repo, filename).

For a complete runnable walkthrough that downloads Qwen/Qwen2.5-0.5B, maps all 24 layers into a #[module], and runs compiled KvCache decoding, see the Pretrained Qwen2.5 example.

Hugging Face Datasets

Stream rows or tokenize text columns from any public or gated Hugging Face dataset without Python.

let ds = hub::load_dataset("roneneldan/TinyStories", "default", "train");

// Read JSON rows by offset and length
let rows = ds.rows(0, 10)?;

// Or tokenize a text column directly into a flat i32 token Tensor
let tokens = ds.tokenize_column(&tok, "text", 0, 100)?;

Minimal Core Builds

The Hugging Face integration is enabled by default under the hf feature. If you want a smaller dependency tree without tokenizers or hf-hub, disable default features in Cargo.toml.

[dependencies]
jacobi = { version = "0.1", default-features = false }