6 个版本

0.3.2 2024 年 4 月 29 日
0.3.1 2024 年 2 月 28 日
0.2.2 2023 年 11 月 8 日
0.2.0 2023 年 10 月 25 日

315图形 API 中排名

Download history 14/week @ 2024-04-15 27/week @ 2024-04-22 210/week @ 2024-04-29 51/week @ 2024-05-06 95/week @ 2024-05-13 57/week @ 2024-05-20 84/week @ 2024-05-27 31/week @ 2024-06-03 62/week @ 2024-06-10 68/week @ 2024-06-17 15/week @ 2024-06-24 7/week @ 2024-07-08 13/week @ 2024-07-15 23/week @ 2024-07-22 36/week @ 2024-07-29

79 每月下载次数
2 个软件包中使用 (通过 llama_cpp)

MIT/Apache

9.5MB
102K SLoC

C++ 47K SLoC // 0.1% comments C 30K SLoC // 0.1% comments CUDA 7.5K SLoC // 0.0% comments Python 7K SLoC // 0.4% comments Metal Shading Language 6K SLoC // 0.0% comments Objective-C 2.5K SLoC // 0.0% comments GLSL 1.5K SLoC // 0.0% comments Rust 885 SLoC // 0.0% comments Shell 469 SLoC // 0.1% comments Zig 125 SLoC // 0.0% comments Swift 54 SLoC // 0.1% comments INI 7 SLoC AsciiDoc 6 SLoC // 0.2% comments

llama_cpp-rs

Documentation Crate

安全的、高级的 Rust 绑定到同名 C++ 项目 llama.cpp,旨在尽可能用户友好。只需 15 行代码即可在 CPU 上运行基于 GGUF 的大型语言模型,无需机器学习经验!

// Create a model from anything that implements `AsRef<Path>`:
let model = LlamaModel::load_from_file("path_to_model.gguf", LlamaParams::default()).expect("Could not load model");

// A `LlamaModel` holds the weights shared across many _sessions_; while your model may be
// several gigabytes large, a session is typically a few dozen to a hundred megabytes!
let mut ctx = model.create_session(SessionParams::default()).expect("Failed to create session");

// You can feed anything that implements `AsRef<[u8]>` into the model's context.
ctx.advance_context("This is the story of a man named Stanley.").unwrap();

// LLMs are typically used to predict the next word in a sequence. Let's generate some tokens!
let max_tokens = 1024;
let mut decoded_tokens = 0;

// `ctx.start_completing_with` creates a worker thread that generates tokens. When the completion
// handle is dropped, tokens stop generating!
let mut completions = ctx.start_completing_with(StandardSampler::default(), 1024).into_strings();

for completion in completions {
    print!("{completion}");
    let _ = io::stdout().flush();
    
    decoded_tokens += 1;
    
    if decoded_tokens > max_tokens {
        break;
    }
}

此仓库托管高级绑定 (crates/llama_cpp) 以及自动生成的 llama.cpp 的低级 C API 绑定 (crates/llama_cpp_sys)。欢迎贡献——只需保持用户体验清晰即可!

构建

请注意,llama.cpp 计算量非常大,这意味着标准的调试构建(仅运行 cargo build/cargo run)将严重受到优化不足的影响。因此,除非真的需要调试,否则强烈建议使用 Cargo 的 --release 标志进行构建和运行。

Cargo 特性

llama.cpp 的几个后端通过特性得到支持

  • cuda - 启用 CUDA 后端,如果启用此功能,则编译需要 CUDA 工具包。
  • vulkan - 启用 Vulkan 后端,如果启用此功能,则编译需要 Vulkan SDK。
  • metal - 启用 Metal 后端,仅限 macOS。
  • hipblas - 启用 hipBLAS/ROCm 后端,如果启用此功能,则编译需要 ROCm。

实验性

这些绑定提供的能力是预测内存中的上下文大小,但需要注意的是,这是一个高度实验性的功能,因为这并不是 llama.cpp 本身提供的。返回的值可能非常不准确,但会尝试始终返回不低于实际大小的值。

许可证

MIT 或 Apache-2.0 许可证,任选其一("Rust" 许可证)。请参阅 LICENSE-MITLICENSE-APACHE

依赖项