Candle 是 Rust 的極簡 ML 框架,專注於效能(包括 GPU 支援)和易用性。嘗試我們的線上演示:whisper、LLaMA2、T5、yolo、Segment Anything。
確保您已按照安裝中的說明正確安裝了candle-core
。
讓我們看看如何運行簡單的矩陣乘法。將以下內容寫入您的myapp/src/main.rs
檔案:
use candle_core :: { Device , Tensor } ;
fn main ( ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
let device = Device :: Cpu ;
let a = Tensor :: randn ( 0f32 , 1. , ( 2 , 3 ) , & device ) ? ;
let b = Tensor :: randn ( 0f32 , 1. , ( 3 , 4 ) , & device ) ? ;
let c = a . matmul ( & b ) ? ;
println ! ( "{c}" ) ;
Ok ( ( ) )
}
cargo run
應顯示形狀為Tensor[[2, 4], f32]
的張量。
安裝帶有 Cuda 支援的candle
後,只需將device
定義在 GPU 上即可:
- let device = Device::Cpu;
+ let device = Device::new_cuda(0)?;
有關更高級的範例,請查看以下部分。
這些線上演示完全在您的瀏覽器中運行:
我們也提供了一些使用最先進模型的基於命令列的範例:
使用以下命令運行它們:
cargo run --example quantized --release
為了使用CUDA,請將--features cuda
加入到範例命令列。如果您安裝了 cuDNN,請使用--features cudnn
來獲得更多加速。
還有一些針對 Whisper 和 llama2.c 的 wasm 範例。您可以使用trunk
構建它們,也可以在線嘗試它們:whisper、llama2、T5、Phi-1.5 和 Phi-2、Segment Anything Model。
對於 LLaMA2,執行以下命令來檢索權重檔案並啟動測試伺服器:
cd candle-wasm-examples/llama2-c
wget https://huggingface.co/spaces/lmz/candle-llama2/resolve/main/model.bin
wget https://huggingface.co/spaces/lmz/candle-llama2/resolve/main/tokenizer.json
trunk serve --release --port 8081
然後造訪 http://localhost:8081/。
candle-tutorial
:非常詳細的教學課程,展示如何將 PyTorch 模型轉換為 Candle。candle-lora
:針對 Candle 的高效且符合人體工學的 LoRA 實現。 candle-lora
有optimisers
:優化器的集合,包括具有動量的 SGD、AdaGrad、AdaDelta、AdaMax、NAdam、RAdam 和 RMSprop。candle-vllm
:用於推理和服務本地 LLM 的高效平台,包括 OpenAI 相容的 API 伺服器。candle-ext
:Candle 的擴充庫,提供 Candle 目前無法使用的 PyTorch 功能。candle-coursera-ml
:Coursera 機器學習專業課程中的 ML 演算法的實作。kalosm
:Rust 中的多模式元框架,用於與本地預訓練模型交互,支援受控生成、自訂採樣器、記憶體向量資料庫、音訊轉錄等。candle-sampling
:蠟燭的採樣技術。gpt-from-scratch-rs
:YouTube 上 Andrej Karpathy 的Let's build GPT教學的移植版,展示了針對玩具問題的 Candle API。candle-einops
:Python einops 函式庫的純 Rust 實作。atoma-infer
:一個用於大規模快速推理的 Rust 函式庫,利用 FlashAttention2 進行高效能的注意力計算,利用 PagedAttention 進行高效的 KV 快取記憶體管理,以及多 GPU 支援。它與 OpenAI api 相容。如果您對此清單有任何補充,請提交拉取請求。
備忘錄:
使用 PyTorch | 使用蠟燭 | |
---|---|---|
創建 | torch.Tensor([[1, 2], [3, 4]]) | Tensor::new(&[[1f32, 2.], [3., 4.]], &Device::Cpu)? |
創建 | torch.zeros((2, 2)) | Tensor::zeros((2, 2), DType::F32, &Device::Cpu)? |
索引 | tensor[:, :4] | tensor.i((.., ..4))? |
營運 | tensor.view((2, 2)) | tensor.reshape((2, 2))? |
營運 | a.matmul(b) | a.matmul(&b)? |
算術 | a + b | &a + &b |
裝置 | tensor.to(device="cuda") | tensor.to_device(&Device::new_cuda(0)?)? |
資料類型 | tensor.to(dtype=torch.float16) | tensor.to_dtype(&DType::F16)? |
儲存 | torch.save({"A": A}, "model.bin") | candle::safetensors::save(&HashMap::from([("A", A)]), "model.safetensors")? |
載入中 | weights = torch.load("model.bin") | candle::safetensors::load("model.safetensors", &device) |
Tensor
結構定義Candle 的核心目標是讓無伺服器推理成為可能。像 PyTorch 這樣的完整機器學習框架非常大,這使得在叢集上建立實例的速度很慢。 Candle 允許部署輕量級二進位。
其次,Candle 可以讓您從生產工作負載中刪除 Python 。 Python 開銷會嚴重影響效能,而 GIL 是眾所周知的令人頭痛的問題。
最後,Rust 很酷!許多 HF 生態系統已經擁有 Rust 箱,例如安全張量和標記器。
dfdx 是一個強大的板條箱,其形狀包含在類型中。透過讓編譯器立即抱怨形狀不匹配,可以避免很多令人頭痛的問題。然而,我們發現某些功能仍然需要 nightly,而編寫程式碼對於非 Rust 專家來說可能有點令人畏懼。
我們在運行時利用其他核心 crate 並為其做出貢獻,因此希望這兩個 crate 能夠相互受益。
burn 是一個通用包,可以利用多個後端,因此您可以為您的工作負載選擇最佳引擎。
tch-rs 綁定到 Rust 中的 torch 函式庫。非常通用,但它們將整個火炬庫引入運行時。 tch-rs
的主要貢獻者也參與了candle
的開發。
如果您在使用 mkl 或加速功能編譯二進位檔案/測試時遇到一些遺失的符號,例如對於 mkl 您會得到:
= note: /usr/bin/ld: (....o): in function `blas::sgemm':
.../blas-0.22.0/src/lib.rs:1944: undefined reference to `sgemm_' collect2: error: ld returned 1 exit status
= note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo
或加速:
Undefined symbols for architecture arm64:
"_dgemm_", referenced from:
candle_core::accelerate::dgemm::h1b71a038552bcabe in libcandle_core...
"_sgemm_", referenced from:
candle_core::accelerate::sgemm::h2cf21c592cba3c47 in libcandle_core...
ld: symbol(s) not found for architecture arm64
這可能是由於缺少啟用 mkl 庫所需的連結器標誌。您可以嘗試在二進位檔案的頂部添加 mkl 的以下內容:
extern crate intel_mkl_src ;
或加速:
extern crate accelerate_src ;
Error: request error: https://huggingface.co/meta-llama/Llama-2-7b-hf/resolve/main/tokenizer.json: status code 401
這可能是因為您無權使用 LLaMA-v2 模型。要解決此問題,您必須在 Huggingface-hub 上註冊,接受 LLaMA-v2 模型條件,並設定您的身份驗證令牌。有關更多詳細信息,請參閱問題#350。
In file included from kernels/flash_fwd_launch_template.h:11:0,
from kernels/flash_fwd_hdim224_fp16_sm80.cu:5:
kernels/flash_fwd_kernel.h:8:10: fatal error: cute/algorithm/copy.hpp: No such file or directory
#include <cute/algorithm/copy.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Error: nvcc error while compiling:
cutlass 作為 git 子模組提供,因此您可能需要執行以下命令來正確簽入它。
git submodule update --init
/usr/include/c++/11/bits/std_function.h:530:146: error: parameter packs not expanded with ‘...’:
這是由 Cuda 編譯器觸發的 gcc-11 中的錯誤。若要解決此問題,請安裝不同的支援的 gcc 版本 - 例如 gcc-10,並在 NVCC_CCBIN 環境變數中指定編譯器的路徑。
env NVCC_CCBIN=/usr/lib/gcc/x86_64-linux-gnu/10 cargo ...
Couldn't compile the test.
---- .candle-booksrcinferencehub.md - Using_the_hub::Using_in_a_real_model_ (line 50) stdout ----
error: linking with `link.exe` failed: exit code: 1181
//very long chain of linking
= note: LINK : fatal error LNK1181: cannot open input file 'windows.0.48.5.lib'
確保連結可能位於專案目標外部的所有本機庫,例如,要執行 mdbook 測試,您應該執行:
mdbook test candle-book -L .targetdebugdeps `
-L native=$env:USERPROFILE.cargoregistrysrcindex.crates.io-6f17d22bba15001fwindows_x86_64_msvc-0.42.2lib `
-L native=$env:USERPROFILE.cargoregistrysrcindex.crates.io-6f17d22bba15001fwindows_x86_64_msvc-0.48.5lib
這可能是由於從/mnt/c
載入模型引起的,有關 stackoverflow 的更多詳細資訊。
您可以設定RUST_BACKTRACE=1
以便在產生蠟燭錯誤時提供回溯。
如果您on an
遇到called
Result::unwrap() 的錯誤value: LoadLibraryExW { source: Os { code: 126, kind: Uncategorized, message: "The specified module could not be found." } }
在value: LoadLibraryExW { source: Os { code: 126, kind: Uncategorized, message: "The specified module could not be found." } }
上。若要修復複製並重新命名這 3 個檔案(確保它們位於路徑中)。路徑取決於您的 cuda 版本。 c:WindowsSystem32nvcuda.dll
- > cuda.dll
c:Program FilesNVIDIA GPU Computing ToolkitCUDAv12.4bincublas64_12.dll
- > cublas.dll
c:Program FilesNVIDIA GPU Computing ToolkitCUDAv12.4bincurand64_10.dll
-> curand.dll