马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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模型为例。
运行完swift和infer脚本,微调、验证并归并模型后(该过程详见魔搭社区的模型训练相关文档),举行模型转化,将其转化为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-cli或llama-server运行模型。
- 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)
- 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魔搭社区
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |