RK3588 Linux平台部署DeepSeek模型教程

news/2025/2/18 22:55:49/

请添加图片描述

更多内容可以加入Linux系统知识库套餐(教程+视频+答疑)

文章目录

  • 一、下载rknn-llm 和 deepseek模型
  • 二、RKLLM-Toolkit 安装
    • 2.1 安装 miniforge3 工具
    • 2.2 下载 miniforge3 安装包
    • 2.3 安装 miniforge3
  • 三、创建 RKLLM-Toolkit Conda 环境
    • 3.1 进入 Conda base 环境
    • 3.2 创建一个 Python3.8 版本(建议版本)名为 RKLLM-Toolkit 的 Conda 环境
    • 3.3 进入 RKLLM-Toolkit Conda 环境
  • 四、安装 RKLLM-Toolkit
  • 五、DeepSeek-R1-1.5B HunggingFace转换成RKLLM模型
  • 六、RK3588端运行demo
  • 七、推荐开发板

沉淀、分享、成长,让自己和他人都能有所收获!😄

• ubuntu20.04
• python3.8
RK3588开发板

先上效果:
请添加图片描述

deepseek_22">一、下载rknn-llm 和 deepseek模型


git clone https://github.com/airockchip/rknn-llm.git 
git clone https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B

将rknn-llm的文件放入以下目录
在这里插入图片描述
deepseek模型放入examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo目录
在这里插入图片描述

二、RKLLM-Toolkit 安装


2.1 安装 miniforge3 工具

检查是否安装 miniforge3 和 conda 版本信息,若已安装则可省略此小节步骤

conda -V
# 提示 conda: command not found 则表示未安装 conda
# 提示 例如版本 conda 23.9.0

2.2 下载 miniforge3 安装包

wget -c https://github.com/condaforge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh

2.3 安装 miniforge3

chmod 777 Miniforge3-Linux-x86_64.sh
./Miniforge3-Linux-x86_64.sh

三、创建 RKLLM-Toolkit Conda 环境

3.1 进入 Conda base 环境

source ~/miniforge3/bin/activate
# (base) xxx@xxx-pc:~$

3.2 创建一个 Python3.8 版本(建议版本)名为 RKLLM-Toolkit 的 Conda 环境

conda create -n RKLLM-Toolkit python=3.8

3.3 进入 RKLLM-Toolkit Conda 环境

conda activate RKLLM-Toolkit
# (RKLLM-Toolkit) xxx@xxx-pc:~$

四、安装 RKLLM-Toolkit


在 RKLLM-Toolkit Conda 环境下使用 pip 工具直接安装所提供的工具链 whl 包,在安装过程中,安装工具会自动下载 RKLLM-Toolkit 工具所需要的相关依赖包。,

whl文件指定的是前面下载rknn-llm中的文件路径

在这里插入图片描述

pip3 install 1.1.4/rkllm-1.1.4/rkllm-toolkit/packages/rkllm_toolkit-1.1.4-cp38-
cp38-linux_x86_64.whl

若执行以下命令没有报错,则安装成功

(RKLLM-Toolkit) xxx@sys2206:~/temp/SDK$ python
Python 3.8.20 | packaged by conda-forge | (default, Sep 30 2024, 17:52:49)
[GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rkllm.api import RKLLM
INFO: Note: NumExpr detected 64 cores but "NUMEXPR_MAX_THREADS" not set, so
enforcing safe limit of 8.
INFO: NumExpr defaulting to 8 threads.
>>>

在这里插入图片描述

五、DeepSeek-R1-1.5B HunggingFace转换成RKLLM模型


  • 编写转换脚本 transform.py 保存到DeepSeek-R1-Distill-Qwen-1.5B目录下
from rkllm.api import RKLLM
from datasets import load_dataset
from transformers import AutoTokenizer
from tqdm import tqdm
import torch
from torch import nn
import os
# os.environ['CUDA_VISIBLE_DEVICES']='1'modelpath = '.'
llm = RKLLM()# Load model
# Use 'export CUDA_VISIBLE_DEVICES=2' to specify GPU device
# options ['cpu', 'cuda']
ret = llm.load_huggingface(model=modelpath, model_lora = None, device='cpu')
# ret = llm.load_gguf(model = modelpath)
if ret != 0:print('Load model failed!')exit(ret)# Build model
dataset = "./data_quant.json"
# Json file format, please note to add prompt in the input,like this:
# [{"input":"Human: 你好!\nAssistant: ", "target": "你好!我是人工智能助手KK!"},...]qparams = None
# qparams = 'gdq.qparams' # Use extra_qparams
ret = llm.build(do_quantization=True, optimization_level=1, quantized_dtype='w8a8',quantized_algorithm='normal', target_platform='rk3588', num_npu_core=3, extra_qparams=qparams, dataset=dataset)#ret = llm.build(do_quantization=True, optimization_level=1, quantized_dtype='w8a8',
#                quantized_algorithm='normal', target_platform='rk3576', num_npu_core=2, extra_qparams=qparams, dataset=dataset)if ret != 0:print('Build model failed!')exit(ret)# Evaluate Accuracy
def eval_wikitext(llm):seqlen = 512tokenizer = AutoTokenizer.from_pretrained(modelpath, trust_remote_code=True)# Dataset download link:# https://huggingface.co/datasets/Salesforce/wikitext/tree/main/wikitext-2-raw-v1testenc = load_dataset("parquet", data_files='./wikitext/wikitext-2-raw-1/test-00000-of-00001.parquet', split='train')testenc = tokenizer("\n\n".join(testenc['text']), return_tensors="pt").input_idsnsamples = testenc.numel() // seqlennlls = []for i in tqdm(range(nsamples), desc="eval_wikitext: "):batch = testenc[:, (i * seqlen): ((i + 1) * seqlen)]inputs = {"input_ids": batch}lm_logits = llm.get_logits(inputs)if lm_logits is None:print("get logits failed!")returnshift_logits = lm_logits[:, :-1, :]shift_labels = batch[:, 1:].to(lm_logits.device)loss_fct = nn.CrossEntropyLoss().to(lm_logits.device)loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))neg_log_likelihood = loss.float() * seqlennlls.append(neg_log_likelihood)ppl = torch.exp(torch.stack(nlls).sum() / (nsamples * seqlen))print(f'wikitext-2-raw-1-test ppl: {round(ppl.item(), 2)}')# eval_wikitext(llm)# Chat with model
messages = "<|im_start|>system You are a helpful assistant.<|im_end|><|im_start|>user你好!\n<|im_end|><|im_start|>assistant"
kwargs = {"max_length": 128, "top_k": 1, "top_p": 0.8,"temperature": 0.8, "do_sample": True, "repetition_penalty": 1.1}
# print(llm.chat_model(messages, kwargs))# Export rkllm model
ret = llm.export_rkllm("./deepseek-r1.rkllm")
if ret != 0:print('Export model failed!')exit(ret)
  • 编写量化校正数据集data_quant.json 保存到DeepSeek-R1-Distill-Qwen-1.5B目录下
[{"input":"Human: 你好!\nAssistant: ", "target": "你好!我是人工智能助手!"}]
  • 运行转接脚本transform.py
(RKLLM-Toolkit) chris@bestom-Precision-Tower-7910:~/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo$ python transform.py 
INFO: rkllm-toolkit version: 1.1.4 
The argument `trust_remote_code` is to be used with Auto classes. It has no effect here and is ignored. 
Downloading data files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 7157.52it/s] 
Extracting data files: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 57.08it/s] 
Generating train split: 1 examples [00:00,  2.34 examples/s] 
Optimizing model: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 28/28 [00:40<00:00,  1.44s/it] 
Building model: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 399/399 [00:13<00:00, 30.41it/s] 
WARNING: The bos token has two ids: 151646 and 151643, please ensure that the bos token ids in config.json and tokenizer_config.json are consistent! 
INFO: The token_id of bos is set to 151646 
INFO: The token_id of eos is set to 151643 
INFO: The token_id of pad is set to 151643 
Converting model: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 339/339 [00:00<00:00, 584169.70it/s] 
INFO: Exporting the model, please wait .... 
[=================================================>] 597/597 (100%) 
INFO: Model has been saved to ./deepseek-r1.rkllm! 
(RKLLM-Toolkit) chris@bestom-Precision-Tower-7910:~/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo$

在这里插入图片描述

RK3588demo_242">六、RK3588端运行demo


使用DeepSeek-R1-Distill-Qwen-1.5B_Demo进行测试验证
• DeepSeek-R1-Distill-Qwen-1.5B_Demo代码路径
cd examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/deploy
• 译DeepSeek-R1-Distill-Qwen-1.5B_Demo

这里以编译Linux版本为例,下载安装编译需要的交叉编译工具:gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu

https://developer.arm.com/downloads/-/gnu-a/10-2-2020-11

在这里插入图片描述

tar -xf gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu.tar.xz

修改编译脚本指定交叉编译工具路径

vi build-linux.sh

在这里插入图片描述
执行build-linux.sh开始编译

(RKLLM-Toolkit) chris@bestom-Precision-Tower-7910:~/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/deploy$ ./build-linux.sh 
-- The C compiler identification is GNU 10.2.1 
-- The CXX compiler identification is GNU 10.2.1 
-- Check for working C compiler: /home/chris/opts/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc 
-- Check for working C compiler: /home/chris/opts/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc -- works 
-- Detecting C compiler ABI info 
-- Detecting C compiler ABI info - done 
-- Detecting C compile features 
-- Detecting C compile features - done 
-- Check for working CXX compiler: /home/chris/opts/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ 
-- Check for working CXX compiler: /home/chris/opts/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ -- works 
-- Detecting CXX compiler ABI info 
-- Detecting CXX compiler ABI info - done 
-- Detecting CXX compile features 
-- Detecting CXX compile features - done 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /home/chris/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/deploy/build/build_linux_aarch64_Release 
Scanning dependencies of target llm_demo 
[ 50%] Building CXX object CMakeFiles/llm_demo.dir/src/llm_demo.cpp.o 
[100%] Linking CXX executable llm_demo 
[100%] Built target llm_demo 
[100%] Built target llm_demo 
Install the project... 
-- Install configuration: "Release" 
-- Installing: /home/chris/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/deploy/install/demo_Linux_aarch64/./llm_demo 
-- Set runtime path of "/home/chris/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/deploy/install/demo_Linux_aarch64/./llm_demo" to "" 
-- Installing: /home/chris/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/deploy/install/demo_Linux_aarch64/lib/librkllmrt.so

打包编译生成的文件,方便push到设备中

(RKLLM-Toolkit) chris@bestom-Precision-Tower-7910:~/Projects/DeepSeekDemo/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/deploy$ tar -zcvf install/demo_Linux_aarch64.tar.gz install/demo_Linux_aarch64/

• 运行llm_demo

# push deepseek-r1.rkllm to device 
C:\Users\king>adb push E:\lhj_files\deepSeekDemo\deepseek-r1.rkllm data/ 
# push install dir to device 
C:\Users\king>adb push E:\lhj_files\deepSeekDemo\demo_Linux_aarch64.tar.gz data/ 
# Unzip the demo 
C:\Users\king>adb shell 
root@linaro-alip:/# cd data 
root@linaro-alip:/data# tar -zxvf demo_Linux_aarch64.tar.gz 
root@linaro-alip:/data# cd install/demo_Linux_aarch64/ 
# Run Demo 
root@linaro-alip:/data/install/demo_Linux_aarch64# export LD_LIBRARY_PATH=./lib 
root@linaro-alip:/data/install/demo_Linux_aarch64# taskset f0 ./llm_demo /data/deepseek-r1.rkllm  2048 4096 # Running result                                                           
rkllm init start 
rkllm init success

开始使用DeepSeek
在这里插入图片描述

七、推荐开发板


开发板官网:http://www.bestom.net/

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/news/1572212.html

相关文章

【xdoj离散数学上机】T283

递归函数易错&#xff1a; 防止出现递归死循环&#xff01; 题目 题目&#xff1a;求诱导出的等价关系的关系矩阵 问题描述 给定有限集合上二元关系的关系矩阵&#xff0c;求由其诱导出的等价关系的关系矩阵。 输入格式 第一行输入n&#xff0c;表示矩阵为n阶方阵&#xff0c…

100N03-ASEMI豆浆机专用MOS管100N03

编辑&#xff1a;ll 100N03-ASEMI豆浆机专用MOS管100N03 型号&#xff1a;100N03 品牌&#xff1a;ASEMI 封装&#xff1a;TO-252 最大漏源电流&#xff1a;100A 漏源击穿电压&#xff1a;30V 批号&#xff1a;最新 RDS&#xff08;ON&#xff09;Max&#xff1a;5.0mΩ…

Lean 工具链教程 | Lake elan

前边安装 Lean4 提到了 Lean 项目开发的三件套&#xff1a;版本管理器 elan 包管理器和构建工具 lake 语言本身的核心组件 lean。本篇分别介绍这三个工具的基本用法。 elan 常用功能 elan 是 Lean 版本管理器&#xff0c;用于安装、管理和切换不同版本的 Lean。 版本管理&…

初学总结SpringBoot项目在mac上环境搭建和运行

mac一定要安装上homebrew&#xff0c;这个玩意在mac上搭建环境贼拉好用&#xff0c;打开终端安装国内镜像的 /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"1. brew安装maven brew install maven2.修改maven国内镜像 ma…

【React】react-redux+redux-toolkit实现状态管理

安装 npm install reduxjs/toolkit react-reduxRedux Toolkit 是官方推荐编写Redux的逻辑方式&#xff0c;用于简化书写方式React-redux 用来链接Redux和React组件之间的中间件 使用 定义数据 创建要管理的数据模块 store/module/counter.ts import { createSlice, Payloa…

基于SSM+uniapp的数学辅导小程序+LW示例参考

1.项目介绍 系统角色&#xff1a;管理员、普通用户功能模块&#xff1a;用户管理、学习中心、知识分类管理、学习周报管理、口算练习管理、试题管理、考试管理、错题本等技术选型&#xff1a;SSM&#xff0c;Vue&#xff08;后端管理web&#xff09;&#xff0c;uniapp等测试环…

海思3559a_使用2.0.4.0版SDK编译固件下载后i2c_write和i2c_read不支持怎么办

问题如下&#xff1a; 烧录完固件后想读取i2c寄存器内容发现不支持&#xff0c;如下&#xff1a; 解决方法如下&#xff1a; 进入Hi3559AV100_SDK_V2.0.4.0/osdrv/tools/board/reg-tools-1.0.0 在该目录下make 该目录下的bin文件内容如下&#xff1a; 将bin目录下的内容…

使用pocketpal-ai在手机上搭建本地AI聊天环境

1、下载安装pocketpal-ai 安装github的release APK 2、安装大模型 搜索并下载模型&#xff0c;没找到deepseek官方的&#xff0c;因为海外的开发者上传了一堆乱七八糟的deepseek qwen模型&#xff0c;导致根本找不到官方上传的……deepseek一开源他们觉得自己又行了。 点击之…