Candle
大约 3 分钟
Candle
概述
Candle 是 Rust 的极简 ML 框架,专注于性能(包括 GPU 支持)和易用性
Candle 的核心目标是实现无服务器推理。像 PyTorch 这样的全机器学习框架非常庞大,这使得在集群上创建实例变得很慢。Candle 允许部署轻量级二进制文件。
其次,Candle 可让您从生产工作负载中移除 Python。Python开销会严重影响性能,而GIL则是出了名的麻烦根源。
最后,Rust 很酷!很多 HF 生态系统已经拥有 Rust 包,例如safetensor和tokenizer。
网站
部署
源码部署
需要先安装Rust环境
部署Candle
# 下载项目
git clone https://github.com/huggingface/candle.git
export HF_HOME=/data/hf_cache
下载模型
# 进入目录安装python虚拟环境下载 huggingface_hub
## 由于默认是从huggingface下载,使用这个工具可以改镜像地址
cd /data/candle
python3 -m venv .venv
source .venv/bin/activate
pip3 install huggingface_hub
## 写下载脚本
cat <<EOL > download.py
import os
import sys
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
# 设置hf 路径:
os.environ['HF_HOME'] = '/data/hf_cache'
if len(sys.argv) <= 1:
print("参数错误!请执行:python3 download.py Qwen/Qwen1.5-0.5B")
sys.exit(0)
print('argv: ', str(sys.argv))
repo_id=sys.argv[1]
from huggingface_hub import snapshot_download
#snapshot_download(repo_id="Qwen/Qwen1.5-0.5B")
# https://hf-mirror.com/Qwen/Qwen1.5-0.5B
snapshot_download(repo_id=repo_id)
EOL
## 下载模型
python3 download.py Qwen/Qwen1.5-0.5B
运行
## 下载依赖,编译打包
export HF_HOME=/data/hf_cache
export HF_HOME=/root/autodl-tmp/hf_cache
## 官方提供的示例 包含llama2
cargo run --example quantized
## 使用千问大模型
cargo run --example qwen -- --model-id Qwen/Qwen1.5-0.5B --prompt 北京景点推荐
## GPU
cargo run --example qwen --features cuda -- --model-id Qwen/Qwen1.5-0.5B --prompt 北京景点推荐
修改源码
前面我们下载模型用到了Python脚本,如果只用Rust,我们可以修改项目的代码
全局搜索 huggingface.co没有找到相关代码,那就在依赖的包里
Cargo.toml文件中是项目的依赖包,其中 hf-hub就是用来下载大模型的
代码在cargo安装目录中
如$CARGO_HOME/registry/src/index.crates.io-6f17d22bba15001f/hf-hub-0.3.2/src/api/sync.rs
pub fn from_cache(cache: Cache) -> Self {
let token = cache.token();
let progress = true;
Self {
// endpoint: "https://huggingface.co".to_string(),
endpoint: "https://hf-mirror.com".to_string(),
url_template: "{endpoint}/{repo_id}/resolve/{revision}/{filename}".to_string(),
cache,
token,
progress,
}
}
删除target中的包重新打包即可
模型示例
qwen-0.5b-Chat
模型地址:https://hf-mirror.com/Qwen/Qwen1.5-0.5B-Chat
python3 download.py Qwen/Qwen1.5-0.5B-Chat
cargo run --example qwen --features cuda -- --model-id Qwen/Qwen1.5-0.5B-Chat --prompt 北京景点推荐
cargo run --example qwen -- --model-id Qwen/Qwen1.5-0.5B-Chat --prompt 北京景点推荐
qwen-4b-Chat
模型地址:https://hf-mirror.com/Qwen/Qwen1.5-4B-Chat
python3 download.py Qwen/Qwen1.5-4B
cargo run --example qwen --features cuda -- --model-id Qwen/Qwen1.5-4B-Chat --prompt 北京景点推荐
Yi-6B-Chat-4bits
模型地址:https://hf-mirror.com/01-ai/Yi-6B-Chat-4bits
python3 download.py 01-ai/Yi-6B-Chat-4bits
cargo run --example yi --features cuda -- --model-id 01-ai/Yi-6B --prompt 北京景点推荐
报错
openssl相关
error: failed to run custom build command for `openssl-sys v0.9.103`
Caused by:
process didn't exit successfully: `/data/candle/target/debug/build/openssl-sys-6c3d630eed03711e/build-script-main` (exit status: 101)
--- stdout
cargo:rustc-check-cfg=cfg(osslconf, values("OPENSSL_NO_OCB", "OPENSSL_NO_SM4", "OPENSSL_NO_SEED", "OPENSSL_NO_CHACHA", "OPENSSL_NO_CAST", "OPENSSL_NO_IDEA", "OPENSSL_NO_CAMELLIA", "OPENSSL_NO_RC4", "OPENSSL_NO_BF", "OPENSSL_NO_PSK", "OPENSSL_NO_DEPRECATED_3_0", "OPENSSL_NO_SCRYPT", "OPENSSL_NO_SM3", "OPENSSL_NO_RMD160", "OPENSSL_NO_EC2M", "OPENSSL_NO_OCSP", "OPENSSL_NO_CMS", "OPENSSL_NO_COMP", "OPENSSL_NO_SOCK", "OPENSSL_NO_STDIO"))
cargo:rustc-check-cfg=cfg(openssl)
cargo:rustc-check-cfg=cfg(libressl)
cargo:rustc-check-cfg=cfg(boringssl)
cargo:rustc-check-cfg=cfg(libressl250)
安装openssl
Mac
# macOS (Homebrew)
brew install openssl@3
# macOS (MacPorts)
sudo port install openssl
# macOS (pkgsrc)
sudo pkgin install openssl
Linux
# Arch Linux
sudo pacman -S pkg-config openssl
# Debian and Ubuntu
sudo apt-get install pkg-config libssl-dev
# Fedora
sudo dnf install pkg-config perl-FindBin openssl-devel
# Alpine Linux
apk add pkgconfig openssl-dev
# openSUSE
sudo zypper in libopenssl-devel
Windows --- 如果是windows , 则使用 https://slproweb.com/products/Win32OpenSSL.html
还报错就需要配置一下 legacy
digital envelope routines:inner_evp_generic_fetch:unsupported:../crypto/evp/evp_fetch.c:349:Global default library context, Algorithm (RC2-40-CBC : 0), Properties ()
# 查看安装位置
openssl version -d ## OPENSSLDIR: “/usr/lib/ssl”
vim /usr/lib/ssl/openssl.cnf
# 修改完查看
openssl list -providers
## --------------------------------
Providers:
default
name: OpenSSL Default Provider
version: 3.0.7
status: active
legacy
name: OpenSSL Legacy Provider
version: 3.0.7
status: active
# List of providers to load
# 一定要在对应的位置修改和添加
[openssl_init]
providers = provider_sect
# 一定要在对应的位置修改和添加
[provider_sect]
default = default_sect
legacy = legacy_sect
# 一定要在对应的位置修改和添加
[default_sect]
activate = 1
[legacy_sect]
activate = 1
