在纯 C/C++ 中推理 Meta 的 LLaMA 模型(以及其他模型)
llama-server
中的通用工具调用支持:#9639llama.cpp
的主要目标是在各种硬件上以最少的设置和最先进的性能实现 LLM 推理 - 本地和云端。
llama.cpp
项目是为 ggml 库开发新功能的主要场所。
通常也支持以下基础模型的微调。
添加对新模型支持的说明:HOWTO-add-model.md
(要将项目列在此处,它应该明确声明它依赖于 llama.cpp
)
后端 | 目标设备 |
---|---|
Metal | Apple Silicon |
BLAS | 全部 |
BLIS | 全部 |
SYCL | Intel 和 Nvidia GPU |
MUSA | 摩尔线程 MTT GPU |
CUDA | Nvidia GPU |
HIP | AMD GPU |
Vulkan | GPU |
CANN | 昇腾 NPU |
OpenCL | Adreno GPU |
该项目的主要产品是 llama
库。它的 C 风格接口可以在 include/llama.h 中找到。该项目还包括许多使用 llama
库的示例程序和工具。这些示例从简单的、最小的代码片段到复杂的子项目,例如与 OpenAI 兼容的 HTTP 服务器。 获取二进制文件的可能方法
llama.cpp
Hugging Face 平台托管着许多与 llama.cpp
兼容的 LLM
您可以手动下载 GGUF 文件,或者直接使用来自 Hugging Face 的任何 llama.cpp
兼容模型,方法是使用此 CLI 参数:-hf <user>/<model>[:quant]
下载模型后,使用 CLI 工具在本地运行它 - 见下文。
llama.cpp
要求模型以 GGUF 文件格式存储。其他数据格式的模型可以使用此 repo 中的 convert_*.py
Python 脚本转换为 GGUF。
Hugging Face 平台提供了各种在线工具,用于转换、量化和托管带有 llama.cpp
的模型
llama.cpp
(更多信息: #9669)要了解有关模型量化的更多信息,请阅读本文档
具有内置聊天模板的模型将自动激活对话模式。如果未发生这种情况,您可以通过添加 -cnv
并使用 --chat-template NAME
指定合适的聊天模板来手动启用它
llama-cli -m model.gguf
# > hi, who are you?
# Hi there! I'm your helpful assistant! I'm an AI-powered chatbot designed to assist and provide information to users like you. I'm here to help answer your questions, provide guidance, and offer support on a wide range of topics. I'm a friendly and knowledgeable AI, and I'm always happy to help with anything you need. What's on your mind, and how can I assist you today?
#
# > what is 1+1?
# Easy peasy! The answer to 1+1 is... 2!
# use the "chatml" template (use -h to see the list of supported templates)
llama-cli -m model.gguf -cnv --chat-template chatml
# use a custom template
llama-cli -m model.gguf -cnv --in-prefix 'User: ' --reverse-prompt 'User:'
要显式禁用对话模式,请使用 -no-cnv
llama-cli -m model.gguf -p "I believe the meaning of life is" -n 128 -no-cnv
# I believe the meaning of life is to find your own truth and to live in accordance with it. For me, this means being true to myself and following my passions, even if they don't align with societal expectations. I think that's what I love about yoga – it's not just a physical practice, but a spiritual one too. It's about connecting with yourself, listening to your inner voice, and honoring your own unique journey.
llama-cli -m model.gguf -n 256 --grammar-file grammars/json.gbnf -p 'Request: schedule a call at 8pm; Command:'
# {"appointmentTime": "8pm", "appointmentDetails": "schedule a a call"}
grammars/ 文件夹包含一些示例语法。要编写自己的语法,请查看 GBNF 指南。
对于编写更复杂的 JSON 语法,请查看 https://grammar.intrinsiclabs.ai/
llama-server -m model.gguf --port 8080
# Basic web UI can be accessed via browser: https://:8080
# Chat completion endpoint: https://:8080/v1/chat/completions
# up to 4 concurrent requests, each with 4096 max context
llama-server -m model.gguf -c 16384 -np 4
# the draft.gguf model should be a small variant of the target model.gguf
llama-server -m model.gguf -md draft.gguf
# use the /embedding endpoint
llama-server -m model.gguf --embedding --pooling cls -ub 8192
# use the /reranking endpoint
llama-server -m model.gguf --reranking
# custom grammar
llama-server -m model.gguf --grammar-file grammar.gbnf
# JSON
llama-server -m model.gguf --grammar-file grammars/json.gbnf
llama-perplexity -m model.gguf -f file.txt
# [1]15.2701,[2]5.4007,[3]5.3073,[4]6.2965,[5]5.8940,[6]5.6096,[7]5.7942,[8]4.9297, ...
# Final estimate: PPL = 5.4007 +/- 0.67339
# TODO
llama-bench -m model.gguf
# Output:
# | model | size | params | backend | threads | test | t/s |
# | ------------------- | ---------: | ---------: | ---------- | ------: | ------------: | -------------------: |
# | qwen2 1.5B Q4_0 | 885.97 MiB | 1.54 B | Metal,BLAS | 16 | pp512 | 5765.41 ± 20.55 |
# | qwen2 1.5B Q4_0 | 885.97 MiB | 1.54 B | Metal,BLAS | 16 | tg128 | 197.71 ± 0.81 |
#
# build: 3e0ba0e60 (4229)
llama.cpp
模型的综合示例。 用于推理。 与 RamaLama3 一起使用。llama-run granite-code
llama-simple -m model.gguf
# Hello my name is Kaitlyn and I am a 16 year old girl. I am a junior in high school and I am currently taking a class called "The Art of
llama.cpp
存储库中的分支,并将 PR 合并到 master
分支中如果你的问题与模型生成质量有关,那么请至少浏览以下链接和论文,以了解 LLaMA 模型的局限性。这在选择合适的模型大小并理解 LLaMA 模型和 ChatGPT 之间重大而细微的差异时尤为重要
命令行补全适用于某些环境。
$ build/bin/llama-cli --completion-bash > ~/.llama-completion.bash
$ source ~/.llama-completion.bash
可以选择将其添加到你的 .bashrc
或 .bash_profile
以自动加载。 例如
$ echo "source ~/.llama-completion.bash" >> ~/.bashrc