Skip to content

Commit

Permalink
Merge pull request #675 from zifeng-radxa/main
Browse files Browse the repository at this point in the history
add deepseek-r1 RKLLM docs
  • Loading branch information
peterwang2050 authored Feb 7, 2025
2 parents efd34dd + 6a9954d commit 95c8e9f
Show file tree
Hide file tree
Showing 24 changed files with 599 additions and 261 deletions.
100 changes: 100 additions & 0 deletions docs/common/dev/_rkllm-deepseek-r1.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[DeepSeek-R1](https://api-docs.deepseek.com/news/news250120) 是由杭州[深度求索](https://www.deepseek.com/)公司开发,
该模型完全开源了所有训练技术和模型权重,性能对齐闭源的 OpenAI-o1,
deepseek 通过 DeepSeek-R1 的输出,蒸馏了 6 个小模型给开源社区,包括 Qwen2.5 和 Llama3.1。
本文档将讲述如何使用 RKLLM 将 DeepSeek-R1 蒸馏模型 [DeepSeek-R1-Distill-Qwen-1.5B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B) 大语言模型部署到 RK3588 上利用 NPU 进行硬件加速推理。

![rkllm_2.webp](/img/general-tutorial/rknn/rkllm_ds_1.webp)

### 模型文件下载

:::tip
瑞莎已经提供编译好的 rkllm 模型和执行文件,用户可直接下载使用,如要参考编译过程可继续参考可选部分
:::

- 使用 [git LFS](https://git-lfs.com/)[ModelScope](https://modelscope.cn/models/radxa/DeepSeek-R1-Distill-Qwen-1.5B_RKLLM) 下载预编译好的 rkllm

```bash
git clone https://www.modelscope.cn/radxa/DeepSeek-R1-Distill-Qwen-1.5B_RKLLM.git
```

### (可选)模型编译

:::tip
请用户根据 [RKLLM安装](./rkllm_install) 完成 PC 端和开发板端 RKLLM 工作环境的准备
:::

- x86 PC 工作站中下载 [DeepSeek-R1-Distill-Qwen-1.5B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B) 权重文件, 如没安装 [git-lfs](https://git-lfs.com/),请自行安装
```bash
git clone https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
```
- 激活 rkllm conda 环境, 可参考[RKLLM conda 安装](rkllm_install#x86-pc-工作站)
```bash
conda activate rkllm
```
- 更改 `rknn-llm/rkllm-toolkit/examples/test.py` 中 modelpath 模型路径, dataset路径, rkllm 导出路径
```python
15 modelpath = 'Your DeepSeek-R1-Distill-Qwen-1.5B Folder Path'
29 datasert = None # 默认是 "./data_quant.json", 如无可以填写 None
83 ret = llm.export_rkllm("./DeepSeek-R1-Distill-Qwen-1.5B.rkllm")
```
- 运行模型转换脚本
```bash
cd rknn-llm/rkllm-toolkit/examples/
python3 test.py
```
转换成功后可得到 DeepSeek-R1-Distill-Qwen-1.5B.rkllm 模型

### (可选)编译可执行文件

- 下载交叉编译工具链 [gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu](https://developer.arm.com/downloads/-/gnu-a/10-2-2020-11)
- 修改主程序 `rknn-llm/examples/rkllm_api_demo/src/llm_demo.cpp` 代码, 这里修改 PROMPT 格式的设置,和 PROMPT 的构造

```cpp
24 #define PROMPT_TEXT_PREFIX "<|im_start|>system\nYou are a helpful assistant.\n<|im_end|>\n<|im_start|>user\n"
25 #define PROMPT_TEXT_POSTFIX "\n<|im_end|>\n<|im_start|>assistant\n<think>"
184 text = PROMPT_TEXT_PREFIX + input_str + PROMPT_TEXT_POSTFIX;
185 // text = input_str;
```

:::tip
为什么要修改 PROMPT_TEXT_PREFIX 和 PROMPT_TEXT_POSTFIX? 这里需要参考 [DeepSeek-R1 论文](https://github.com/deepseek-ai/DeepSeek-R1/blob/main/DeepSeek_R1.pdf)中 Table1 的说明,需按照其格式对 DeepSeek-R1 模型提问
:::
![rkllm_2.webp](/img/general-tutorial/rknn/rkllm_ds_2.webp)

- 修改 `rknn-llm/examples/rkllm_api_demo/build-linux.sh` 编译脚本中 `GCC_COMPILER_PATH` 路径
```bash
GCC_COMPILER_PATH=gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu
```
- 运行模型转换脚本
```bash
cd rknn-llm/examples/rkllm_api_demo/
bash build-linux.sh
```
生成的可执行文件在 `build/build_linux_aarch64_Release/llm_demo`

### 板端部署

#### 终端模式

- 将转换成功后的 DeepSeek-R1-Distill-Qwen-1.5B.rkllm 模型与编译后的二进制文件 llm_demo 复制到板端
- 导入环境变量
```bash
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:rknn-llm/rkllm-runtime/Linux/librkllm_api/aarch64
```
:::tip
使用 ModelScope 下载的用户可直接 export 下载仓库里的 librkllmrt.so
:::
- 运行 llm_demo,输入 `exit` 退出
```bash
export RKLLM_LOG_LEVEL=1
./llm_demo DeepSeek-R1-Distill-Qwen-1.5B.rkllm 10000 10000
```
![rkllm_2.webp](/img/general-tutorial/rknn/rkllm_ds_3.webp)

### 性能分析

对于数学问题: `解方程 x+y=12, 2x+4y=34, 求x,y的值`, 在 RK3588 上达 14.93 token/s
| Stage | Total Time (ms) | Tokens | Time per Token (ms) | Tokens per Second |
|----------|-----------------|--------|---------------------|-------------------|
| Prefill | 429.63 | 81 | 5.30 | 188.53 |
| Generate | 56103.71 | 851 | 66.99 | 14.93 |
41 changes: 23 additions & 18 deletions docs/common/dev/_rkllm-install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ RKLLM 可以帮助用户快速将 LLM 模型部署到 Rockchip 芯片中,目

#### 目前支持模型

- [TinyLLAMA 1.1B](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0/tree/fe8a4ea1ffedaf415f4da2f062534de366a451e6)
- [Qwen 1.8B](https://huggingface.co/Qwen/Qwen-1_8B-Chat/tree/1d0f68de57b88cfde81f3c3e537f24464d889081)
- [Qwen2 0.5B](https://huggingface.co/Qwen/Qwen1.5-0.5B/tree/8f445e3628f3500ee69f24e1303c9f10f5342a39)
- [Phi-2 2.7B](https://hf-mirror.com/microsoft/phi-2/tree/834565c23f9b28b96ccbeabe614dd906b6db551a)
- [Phi-3 3.8B](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct/tree/291e9e30e38030c23497afa30f3af1f104837aa6)
- [ChatGLM3 6B](https://huggingface.co/THUDM/chatglm3-6b/tree/103caa40027ebfd8450289ca2f278eac4ff26405)
- [Gemma 2B](https://huggingface.co/google/gemma-2b-it/tree/de144fb2268dee1066f515465df532c05e699d48)
- [InternLM2 1.8B](https://huggingface.co/internlm/internlm2-chat-1_8b/tree/ecccbb5c87079ad84e5788baa55dd6e21a9c614d)
- [MiniCPM 2B](https://huggingface.co/openbmb/MiniCPM-2B-sft-bf16/tree/79fbb1db171e6d8bf77cdb0a94076a43003abd9e)
- [LLAMA models](https://huggingface.co/meta-llama)
- [TinyLLAMA models](https://huggingface.co/TinyLlama)
- [Qwen models](https://huggingface.co/models?search=Qwen/Qwen)
- [Phi models](https://huggingface.co/models?search=microsoft/phi)
- [ChatGLM3-6B](https://huggingface.co/THUDM/chatglm3-6b/tree/103caa40027ebfd8450289ca2f278eac4ff26405)
- [Gemma models](https://huggingface.co/collections/google/gemma-2-release-667d6600fd5220e7b967f315)
- [InternLM2 models](https://huggingface.co/collections/internlm/internlm2-65b0ce04970888799707893c)
- [MiniCPM models](https://huggingface.co/collections/openbmb/minicpm-65d48bf958302b9fd25b698f)
- [TeleChat models](https://huggingface.co/Tele-AI)
- [Qwen2-VL](https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct)
- [MiniCPM-V](https://huggingface.co/openbmb/MiniCPM-V-2_6)

## RKLLM 安装

Expand All @@ -39,23 +41,23 @@ RKLLM 可以帮助用户快速将 LLM 模型部署到 Rockchip 芯片中,目

- 创建 conda 环境
```bash
conda create -n rkllm python=3.8
conda create -n rkllm python=3.8.2
```
- 进入 rkllm conda 环境

```bash
conda activate rkllm
```

- 退出环境
- _如要退出环境_
```bash
conda deactivate
```

- RKLLM-Toolkit是一套软件开发包,供用户在 PC 上进行 Huggingface 格式的 LLM 模型转换和量化
```bash
git clone -b release-v1.0.1 https://github.com/airockchip/rknn-llm.git
pip3 install ./rknn-llm/rkllm-toolkit/packages/rkllm_toolkit-1.0.1-cp38-cp38-linux_x86_64.whl
git clone -b release-v1.1.4 https://github.com/airockchip/rknn-llm.git
pip3 install ./rknn-llm/rkllm-toolkit/packages/rkllm_toolkit-1.1.4-cp38-cp38-linux_x86_64.whl
```
若执行以下命令没有报错,则安装成功
```bash
Expand All @@ -65,18 +67,21 @@ RKLLM 可以帮助用户快速将 LLM 模型部署到 Rockchip 芯片中,目

### 开发板

- 检查 NPU 驱动版本是否大于等于 0.9.6,如小于此版本请下载并烧录最新 radxa 6.1 固件
- 检查 NPU 驱动版本是否大于等于 0.9.8,如小于此版本请下载并烧录最新 radxa 6.1 固件
:::tip
radxa 6.1 固件默认 NPU 驱动版本为 0.9.6,请通过: `sudo rsetup -> System -> System Update` 升级系统以更新至 0.9.8 驱动。
:::

```bash
$ sudo cat /sys/kernel/debug/rknpu/version
RKNPU driver: v0.9.6
RKNPU driver: v0.9.8
```

(可选)手动编译 NPU 内核
- (可选)手动编译 NPU 内核

若用户所使用的为非官方固件,需要对内核进行更新;其中,RKNPU 驱动包支持两个主要内核版本:[kernel-5.10](https://github.com/radxa/kernel/tree/stable-5.10-rock5)[kernel-6.1](https://github.com/radxa/kernel/tree/linux-6.1-stan-rkr1);用户可在内核根目录下的 Makefile 中确认具体版本号。内核的具体的更新步骤如下:

1) 下载压缩包 [rknpu_driver_0.9.6_20240322.tar.bz2](https://github.com/airockchip/rknn-llm/tree/main/rknpu-driver)
1) 下载压缩包 [rknpu_driver_0.9.8_20241009.tar.bz2](https://github.com/airockchip/rknn-llm/tree/release-v1.1.4/rknpu-driver)

2) 解压该压缩包,将其中的 rknpu 驱动代码覆盖到当前内核代码目录

Expand All @@ -86,5 +91,5 @@ RKLLM 可以帮助用户快速将 LLM 模型部署到 Rockchip 芯片中,目

- RKLLM Runtime 为 Rockchip NPU 平台提供 C/C++ 编程接口,帮助用户部署 RKLLM 模型,加速 LLM 应用的实现
```bash
git clone https://github.com/airockchip/rknn-llm.git
git clone -b release-v1.1.4 https://github.com/airockchip/rknn-llm.git
```
Loading

0 comments on commit 95c8e9f

Please sign in to comment.