llama.cpp使用教程

AI基础 llama.cpp  收藏
0 / 188

llama.cpp 的github库地址为 ggerganov/llama.cpp: LLM inference in C/C++ (github.com),具体使用以官方说明为准。

简介

llama.cpp 目标是在本地和云端的各种硬件上以最小的设置和最先进性能实现LLM推理。

具体而言,可以用 llama.cpp 将训练好的模型转化为通用格式如 gguf 等,进行量化,以 server 或命令行方式运行模型。

安装

git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp/
mkdir build
cd build
apt install make cmake gcc
cmake ..
cmake --build . --config Release
make install

基本使用(以微调好的模型为例)

注意,由于该库在不断更新,请注意以官方库的说明为准。目前互联网上很多教程是基于之前的版本,而 2024 年 6 月 12 日后库更新了,修改了可执行文件名,导致网上很多教程使用的 quantize、main、server 等指令无法找到,在当前版本(截至 2024 年 7 月 20 日)这些指令分别被重命名为 llama-quantize、llama-cli、llama-server。

安装完之后可以在 /usr/bin/usr/local/bin 目录下建立命令链接,以便在任意目录下使用 llama.cpp

例如:在 /usr/bin 目录下

ln -s your/path/to/llama.cpp/build/bin/llama-quantize llama-quantize
ln -s your/path/to/llama.cpp/build/bin/llama-server llama-server
ln -s your/path/to/llama.cpp/build/bin/llama-cli llama-cli

模型转化与量化

该库支持的模型详见官方文档,以下以 qwen1.5-14b-chat 模型为例。

运行完 swiftinfer 脚本,微调、验证并合并模型后(该过程详见魔搭社区的模型训练相关文档),进行模型转化,将其转化为 gguf 格式并进行量化,在 llama.cpp 路径下:

# 1. qwen必须先使用convert-hf-to-gguf.py转换再降低精度
# issue: KeyError: 'transformer.h.0.attn.c_attn.bias' #4304
# 14b转f16大概28G
# 我训练好的模型位于/root/autodl-tmp/swift/examples/pytorch/llm/output/qwen-14b-chat/v2-20240514-122025/checkpoint-93-merged路径下,请参考并替换为自己的对应路径
python convert-hf-to-gguf.py /root/autodl-tmp/swift/examples/pytorch/llm/output/qwen-14b-chat/v2-20240514-122025/checkpoint-93-merged --outfile my-qwen-14b.gguf --outtype f16

# 2. 使用llama-quantize 转换精度
# llama-quantize支持的精度以及更多的使用方法可通过llama-quantize --help查看
llama-quantize ./my-qwen-14b.gguf my-qwen-14b-q8_0.gguf q8_0

模型运行

可通过 llama-clillama-server 运行模型。

  1. llama-cli

    llama-cli -m my-qwen-14-q8_0.gguf -p "you are a helpful assistant" -cnv -ngl 24
    
    # Output:
    # > 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!
    

    其中:

    • -m 参数后跟要运行的模型
    • -cnv 表示以对话模式运行模型
    • -ngl:当编译支持 GPU 时,该选项允许将某些层卸载到 GPU 上进行计算。一般情况下,性能会有所提高。

    其他参数详见官方文档 llama.cpp/examples/main/README.md at master · ggerganov/llama.cpp (github.com)

  2. llama-server:

    ./llama-server -m /mnt/workspace/qwen2-7b-instruct-q8_0.gguf -ngl 28
    

    会启动一个类似 Web 服务器的进程,默认端口号为 8080,可通过 Web 页面或者 OpenAI API 等进行访问。

    使用 OpenAI API 访问:

    import openai
    
    client = openai.OpenAI(
        base_url="http://127.0.0.1:8080/v1",
        api_key = "sk-no-key-required"
    )
    
    completion = client.chat.completions.create(
        model="qwen", # model name can be chosen arbitrarily
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "tell me something about michael jordan"}
        ]
    )
    print(completion.choices[0].message.content)