Transformers documentation

Candle

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.24.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Candle

Candle 是一个机器学习框架,提供了 Transformers 模型的 Rust 原生实现。它原生支持 safetensors,可直接加载 Transformers 模型。

/// load model config
let config: Config = 
    serde_json::from_reader(std::fs::File::open(config_filename)?)?;

/// load safetensors and memory-maps them
let vb = unsafe {
    VarBuilder::from_mmaped_safetensors(&filenames, dtype, &device)?
};

/// materialize tensors from VarBuilder into model class
let model = Model::new(args.use_flash_attn, &config, vb)?;

Transformers 集成

  1. hf-hub crate 会在本地 Hugging Face 缓存 中查找模型。如果未找到,它会从 Hub 下载模型权重和配置文件。
  2. VarBuilder 会延迟加载 safetensor 文件。它将 state-dict 的键名映射到代表模型层的 Rust 结构体。这与 Transformers 组织其权重的方式一致。
  3. Candle 会解析 config.json 以提取模型元数据,并使用来自 VarBuilder 的权重实例化对应的 Rust 模型类。

资源

Update on GitHub