ollama把huggingface下载下来的模型转换为gguf

说明

ollama导入从huggingface下载下来的模型在ollama的说明文档中有详细的介绍,大家可以按照文档说明进行操作。importing-pytorch–safetensors 。既然官方已经给出了明确的操作步骤,那么我写这篇博客的意义又是什么呢?主要有两个目的:1.我的操作可能更适合中国宝宝体质 2.方便后期自己查看

要求

  • 建议使用conda管理python环境
  • 建议使用linux或mac环境,如果只有windows环境建议安装wsl
  • 也可以在docker容器中运行,方便其他人共用(这篇文章采用docker的方式运行,有需要的同学可以找我要docker镜像)

操作步骤

拉取基础镜像

docker pull  continuumio/anaconda3:2024.02-1

启动容器
docker run -itd --name gguf-convert  continuumio/anaconda3:2024.02-1
# 创建工作目录
mkdir work
cd work
进入容器
docker exec -it gguf-convert bash
下载ollama/ollama代码
git clone https://github.com/ollama/ollama.git ollama
cd ollama
获取其llama.cpp子模块
git submodule init
git submodule update llm/llama.cpp
创建python环境
conda create -n llama-env python=3.11.0

conda activate llama-env

pip install -r llm/llama.cpp/requirements.txt

pip install tiktoken
安装make
# 查看是否安装make
make --version
# 如果没有安装,则继续安装
apt-get update
apt-get install build-essential
构建quantize工具
make -C llm/llama.cpp quantize
下载HuggingFace中的模型 (可选择)

从官网或者镜像网站上下载需要的模型,说明文档中采用git lfs的方式进行下载,使用过程中发现会报错,建议手动下载(用迅雷会快些),下载完成后把数据放在/root/work目录下

转换模型

一些模型需要特殊的转换脚本,比如千问的模型可能就需要convert-hf-to-gguf.py脚本才可以正常转换

cd llm/llama.cpp
python convert-hf-to-gguf.py /root/work/MindChat/

生成的文件是:/root/work/MindChat/ggml-model-f16.gguf

番外篇

X-D-Lab/MindChat-Qwen-7B-v2模型转换为gguf
下载模型

模型下载完毕后(需要检查文件是否下载完整,下载不完整转换会报错),移动到 /root/work 目录下

切换git版本

截至到2024年05月22日ollama的开源代码是有问题的,需要回退到指定版本(如果已经切换到指定版本不需要重复切换

cd /root/work/ollama/llm/llama.cp
git reset --hard 46e12c4692a37bdd31a0432fc5153d7d22bc7f72
转换
python convert-hf-to-gguf.py /root/work/MindChat/

其他

llama.cp提供了更加详细的向量化教程,参考:git-instruction。如果有同学需要使用docker镜像版本的可以自取链接:https://pan.baidu.com/s/1oNgYPjZFWs6a1ezK_1hMyQ
提取码:jrfh

### 如何从 HuggingFace 下载 GGUF 格式的模型 HuggingFace 提供了丰富的预训练模型,但并非所有模型都直接以 GGUF 格式提供。如果目标是从 HuggingFace 下载 GGUF 格式的模型文件,通常需要检查目标模型页面是否已提供 GGUF 文件的下载链接。如果没有直接提供的 GGUF 文件,则需要通过其他方式转换生成。 #### 1. 检查 HuggingFace 模型页面是否有 GGUF 文件 在 HuggingFace模型页面上,查看“Files and versions”部分,确认是否存在 `.gguf` 文件。如果存在,可以直接使用 `wget` 或 `curl` 命令下载[^4]。例如: ```bash wget https://huggingface.co/gorilla-llm/gorilla-openfunctions-v2-gguf/resolve/main/gorilla-openfunctions-v2-q2_K.gguf ``` #### 2. 如果没有直接提供的 GGUF 文件 如果 HuggingFace 模型页面未提供 GGUF 文件,则需要将 HuggingFace 格式的模型文件转换GGUF 格式。以下是具体步骤: ##### (1) 安装依赖工具 确保安装了必要的工具和库,例如 `llama.cpp` 和相关脚本。可以通过以下命令安装: ```bash git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp pip install -r requirements.txt ``` ##### (2) 使用转换脚本 利用 `convert-hf-to-gguf.py` 脚本将 HuggingFace 格式的模型转换GGUF 格式。例如: ```bash python convert-hf-to-gguf.py /path/to/huggingface/model /output/path/qwen2.5-3b-chat-1022-r2.q4_K_M.gguf ``` 此脚本会读取 HuggingFace 模型文件并生成对应的 GGUF 文件[^2]。 ##### (3) 验证 GGUF 文件 转换完成后,可以使用以下命令验证 GGUF 文件是否正确生成: ```bash ls -lh /output/path/qwen2.5-3b-chat-1022-r2.q4_K_M.gguf ``` #### 3. 在 Ollama 中加载 GGUF 文件 一旦获得了 GGUF 文件,可以将其放置到 Ollama模型目录中,并创建 `Model File` 来完成加载[^3]。例如: ```bash mkdir -p ~/.ollama/models/qwen2.5-instruct cp /path/to/qwen2.5-3b-chat-1022-r2.q4_K_M.gguf ~/.ollama/models/qwen2.5-instruct/ ``` ### 示例代码 以下是一个完整的脚本示例,用于从 HuggingFace 下载模型转换GGUF 格式: ```bash # 克隆 llama.cpp 仓库 git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp # 安装依赖 pip install -r requirements.txt # 下载 HuggingFace 模型(如果未提供 GGUF 文件) # 替换为实际的 HuggingFace 模型路径 wget https://huggingface.co/{model-repo}/resolve/main/pytorch_model.bin -O /tmp/hf-model.bin # 转换GGUF 格式 python convert-hf-to-gguf.py /tmp/hf-model.bin /tmp/model.gguf # 验证 GGUF 文件 ls -lh /tmp/model.gguf ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值