说明
- deepseek r1 是开源的大模型
- llama.cpp 使用的是 C 语言写的机器学习张量库 ggml。可以使用GPU或者CPU计算资源
- llama.cpp 提供了模型量化的工具。可以对模型进行量化,降低内存或者显存的使用量
部署的步骤
- 安装GPU驱动
- 安装CUDA
- 下载模型参数
- 下载并编译 llama.cpp 项目
- 模型格式转换
- 启动大模型
1. 安装GPU驱动 (可选)
如果你不想使用GPU,这一步骤可以跳过。
进入NVIDIA官网,查询你显卡驱动清单,选择合适的版本
下面是我服务器显卡的情况。
下载完毕执行安装命令。
sudo sh ./NVIDIA-Linux-x86_64-510.54.run
安装完毕后,可以重启服务器。
重启后,执行nvidia-smi可以看到输出GPU信息证明安装成功。
2. 安装CUDA
显卡和驱动有对应关系的,请查阅下面的链接进行确认。
显卡驱动与CUDA版本对应关系docs.nvidia.com/cuda/cuda-t…
下载之后,执行安装命令
sudo chmod a+x cuda_11.6.1_510.47.03_linux.run
sudo sh ./cuda_11.6.1_510.47.03_linux.run
等一会,出现界面后,输入accept接受许可。
取消cuda自带驱动。键盘上下箭头进行移动,回车键进行选择和取消。然后移动到Install上面,回车,即可进入安装。
安装完毕后,配置环境变量。
vim ~/.bashrc
增加如下内容:
你的路径可能和我有区别,请自行确认。
执行下面命令使得环境变量生效,或者重新开一个终端。
source ~/.bashrc
执行nvcc --version 确保配置正确。
3. 下载模型参数
可以根据需要选择从哪个网站进行下载
从huggingface下载
可以从网站搜索你要下载的模型。
deepseek对应的网址是 huggingface.co/deepseek-ai
我们选择1.5B这个最小的模型来进行测试,网址。你可以根据需要选择你要的模型大小。
点击files and versions,然后点击文件后面的下载按钮,下载模型文件。注意所有的文件都要下载。下载完毕后,上传到你的服务器上即可。
需要注意的是huggingface上模型的存储格式和llama.cpp需要的gguf格式不同,后面会进行模型转换。
从modelscope下载
魔塔是阿里搭建的类似huggingface的模型共享社区,也可以找到deepseek模型。 魔塔网址。
可以搜索deepseek模型。可以看到modelscope上是有转换后到gguf格式的模型参数文件的。我们可以直接下载这个,后面就需要转换格式了。
点击模型文件,点击文件后面的下载按钮进行下载。注意所有的文件都要下载。
huggingface 和 modelscope 都提供了命令行的方式进行一键下载,这里不再赘述。
4. 下载并编译 llama.cpp
下载
使用git下载llama.cpp项目
$ git clone https://github.com/ggerganov/llama.cpp
$ cd llama.cpp
如果你没有,或者不想使用git,可以直接从网页上下载压缩包,然后到服务器上zip解压。
安装编译环境
我的服务器是ubuntu环境,安装操作如下。
$ sudo apt-get install make cmake gcc g++ locate
CPU环境编译
如果你没有GPU,可以为CPU环境编译。--config Release -j4 是指定使用4个线程进行编译,编译速度会更快一些。
$ mkdir build
$ cmake -B build
$ cmake --build build --config Release -j 4
$ cd build
GPU环境编译
如果你有GPU,可以使用如下的编译方式进行编译。-DGGML_CUDA=ON 指定使用CUDA。如果你不是英伟达的GPU,可以根据需要,依据文档选择编译的参数。
$ mkdir build
$ cmake -B build -DGGML_CUDA=ON
$ cmake --build build --config Release -j 4
$ cd build
编译后的样子
编译后build文件夹下会出现这些文件。其中 bin 目录下是编译好的可执行文件。
5. 模型格式转换(可选)
如果你的模型格式是gguf的,就可以跳过这一步骤。如果是huggingface 的格式,可以进行参数格式转换。
进入llama.cpp 目录下,找到convert_hf_to_gguf.py
# 如果不量化,保留模型的效果
python convert_hf_to_gguf.py /your/path/DeepSeek-R1-Distill-Qwen-1.5B --outtype f16 --verbose --outfile QWen2.5-7B-lora.gguf
# 如果需要量化(加速并有损效果),直接执行下面脚本就可以
python convert_hf_to_gguf.py /your/path/DeepSeek-R1-Distill-Qwen-1.5B --outtype q8_0 --verbose --outfile DeepSeek-R1-Distill-Qwen-1.5B.gguf
6. 启动大模型
这里以我从modelscope上下载的gguf格式的DeepSeek-R1-Distill-Qwen-1.5B-GGUF模型为例。文件夹中包含了多个gguf模型,是不同量化精度下的结果。根据你的需要选择量化的精度。我们这里是测试,就随便选择一个了。
cli形式启动
进入到llama.cpp/build 目录下。
./bin/llama-cli -m /your/path/DeepSeek-R1-Distill-Qwen-1.5B-Q4_K_M.gguf
# > 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!
server形式启动
./bin/llama-server -m /your/path/DeepSeek-R1-Distill-Qwen-1.5B-Q4_K_M.gguf --port 8080
# Basic web UI can be accessed via browser: http://localhost:8080
# Chat completion endpoint: http://localhost:8080/v1/chat/completions
也可以配置一些参数。
llama-server -t 48 -m ./DeepSeek-R1-Q8_0/DeepSeek-R1.Q8_0-00001-of-00015.gguf \
-c 4096 --no-mmap \
--host 0.0.0.0 --port 9090
--no-mmap的话会直接把模型拉到内存里-t 48使用 48 个线程-c 4096context 设置为 4k
server端启动后,可以通过http协议访问。
curl --request POST \
--url http://localhost:9090/completion \
--header "Content-Type: application/json" \
--data '{"prompt": "What color is the sun?","n_predict": 512}'