llama.cpp使用

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)
    

可参考魔搭社区GGUF模型怎么玩!看这篇就够了_魔搭ModelScope社区-ModelScope魔搭社区

  • 22
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值