1、 将hf模型转换为GGUF
1.1 需要用llama.cpp仓库的convert_hf_to_gguf.py脚本来转换,安装过程中会自动卸载pytorch,因为只能在另一台机器上进行.

点击查看代码
git clone https://github.com/ggerganov/llama.cpp.git
 pip install -r llama.cpp/requirements.txt

1.2 执行转换

点击查看代码
# 如果不量化,保留模型的效果
python llama.cpp/convert_hf_to_gguf.py ./Meta-Llama-3-8B-Instruct  --outtype f16 --verbose --outfile Meta-Llama-3-8B-Instruct-gguf.gguf
 # 如果需要量化(加速并有损效果),直接执行下面脚本就可以
python llama.cpp/convert_hf_to_gguf.py ./Meta-Llama-3-8B-Instruct  --outtype 
q8_0 --verbose --outfile Meta-Llama-3-8B-Instruct-gguf_q8_0.gguf
点击查看代码
这里--outtype是输出类型,代表含义:
q2_k:特定张量(Tensor)采用较高的精度设置,而其他的则保持基础级别。
q3_k_l、q3_k_m、q3_k_s:这些变体在不同张量上使用不同级别的精度,从而达到性能和效率的平衡。
q4_0:这是最初的量化方案,使用 4 位精度。
q4_1 和 q4_k_m、q4_k_s:这些提供了不同程度的准确性和推理速度,适合需要平衡资源使用的场景。
q5_0、q5_1、q5_k_m、q5_k_s:这些版本在保证更高准确度的同时,会使用更多的资源并且推理速度较
慢。
q6_k 和 q8_0:这些提供了最高的精度,但是因为高资源消耗和慢速度,可能不适合所有用户。
fp16 和 f32: 不量化,保留原始精度。

1.3 转换后的模型

  1. 使用ollama运行gguf
    2.1 安装ollama
    curl -fsSL https://ollama.com/install.sh | sh

2.2 启动ollama服务
ollama serve

2.3 创建ModelFile
复制模型路径,创建名为“ModelFile”的meta文件,内容如下,

点击查看代码
FROM ./deepseek-merged_q8_0.gguf


TEMPLATE """<|begin▁of▁sentence|>{{ if .System }}{{ .System }}{{ end }}{{ range .Messages }}{{ if eq .Role "user" }}<|User|>{{ .Content }}<|Assistant|>{{ else if eq .Role "assistant" }}{{ .Content }}<|end▁of▁sentence|>{{ end }}{{ end }}"""

PARAMETER stop "<|end▁of▁sentence|>"
PARAMETER num_ctx 4096

2.4 创建自定义模型
使用ollama create命令创建自定义模型
ollama create llama-3-8B-Instruct --file ./ModeFile

2.5 运行模型:
ollama run llama-3-8B-Instruct