RKLLM 模型转换从0开始

RKLLM 模型转换从0开始

本指南详细介绍了在虚拟机上配置和转换 RKLLM 模型的步骤。假设您使用的是 Ubuntu 20.04 LTS (focal),虚拟机内存建议至少 32GB(小模型可使用 16GB)。本流程包括配置 Python 环境(使用 Anaconda)、安装依赖、下载模型,以及在内存不足时配置交换分区。


前置条件

  • 操作系统:Ubuntu 20.04 LTS (focal)
  • 内存:至少 16GB RAM(建议 32GB 用于大型模型)
  • 所需工具aptvimgitgit-lfs 和网络连接
  • 模型来源:建议从 ModelScope 下载模型(Hugging Face 下载速度较慢)

RKLLM RKNN MODEL


步骤 1:更新 APT 并配置镜像源

为确保软件包安装快速可靠,需更新 APT 包管理器并配置为使用阿里云镜像。

  1. 编辑 APT 源列表
    使用以下命令打开源文件进行编辑:

    sudo vi /etc/apt/sources.list
    
  2. 替换为阿里云镜像源:(其他版本看这
    对于 Ubuntu 20.04 LTS (focal),使用以下配置:

    deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    
    deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    
    deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    
    deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    
  3. 更新 APT
    运行以下命令刷新包索引并检查可升级的包:

    sudo apt update
    apt list --upgradable
    

    确认更新是否正常。


步骤 2:安装 Anaconda 并配置环境

安装 Anaconda 以创建 Python 虚拟环境,用于模型转换和运行。

  1. 下载并安装 Anaconda
    下载 Anaconda 安装包(假设文件名为 Anaconda3-2025.06-0-Linux-x86_64.sh),然后运行:

    ./Anaconda3-2025.06-0-Linux-x86_64.sh
    
  2. 配置环境变量
    安装完成后,Anaconda 通常会自动添加环境变量。如果未自动添加,可手动配置:

    • 安装

      vim
      

      (如未安装):

      sudo apt install vim
      
    • 编辑环境变量文件(选择其一):

      • 系统级别:sudo vim /etc/profile
      • 用户级别:vim ~/.bashrc
    • 在文件末尾添加:

      export PATH="/root/anaconda3/bin:$PATH"
      
    • 使配置生效:

      source /etc/profile  # 或 source ~/.bashrc
      
  3. 初始化 Conda

    conda init
    
  4. 创建虚拟环境
    根据需要创建单独或统一的虚拟环境,Python 版本建议为 3.10:

    conda create -n rkllm python=3.10
    conda create -n rknn python=3.10
    conda create -n runAi python=3.10
    

    或创建统一环境:

    conda create -n All python=3.10
    
  5. 激活虚拟环境
    激活目标环境,例如:

    conda activate rkllm
    
  6. 退出虚拟环境

    conda deactivate
    

步骤 3:更新 pip 并配置镜像源

确保虚拟环境中 pip 版本最新,并配置快速的镜像源。

  1. 检查 Python 和 pip 版本

    python3 -V
    pip -V
    
  2. 安装 pip(如果缺失)

    sudo apt install python3-pip
    

    或:

    python -m pip install --upgrade pip
    
  3. 更新 pip

    pip install --upgrade pip
    
  4. 配置 pip 镜像源(阿里镜像源有时候没有需要的工具):
    使用阿里云镜像源加速下载:

    pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/
    

    使用清华镜像源加速下载(这次推荐):

    pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    

步骤 4:安装 RKLLM 和 RKNN 工具

安装 RKLLM 模型转换工具及其依赖。

  1. 安装 RKLLM 工具使用清华镜像源:
    使用以下命令安装(假设已下载 rkllm_toolkit-1.2.1-cp310-cp310-linux_x86_64.whl):

    pip install rkllm_toolkit-1.2.1-cp310-cp310-linux_x86_64.whl -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    
  2. 解决依赖问题
    如果安装失败,可能需要安装特定版本的依赖。:

    pip install numpy==1.26.4 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    pip install transformers==4.51.3 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    pip install torch==2.3.0 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    pip install datasets==2.14.6 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    pip install sentencepiece==0.2.0 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    pip install pyarrow==12.0.1 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    
  3. 再次安装 RKLLM 工具
    确保依赖安装完成后重新运行:

    pip install rkllm_toolkit-1.2.1-cp310-cp310-linux_x86_64.whl
    

步骤 5:下载模型

从 ModelScope 下载模型(例如 Qwen3-4B),速度比 Hugging Face 快。

  1. 克隆模型仓库
    使用以下命令下载模型元数据(避免直接下载大文件):

    GIT_LFS_SKIP_SMUDGE=1 git clone https://www.modelscope.cn/Qwen/Qwen3-4B.git
    
  2. 下载模型文件
    进入克隆的目录并拉取完整模型:

    cd Qwen3-4B
    git lfs pull
    
  3. 等待下载完成


步骤 6:处理依赖版本问题

运行 rknn-llm/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/export 下的 Python 脚本可能遇到 datasetspyarrow 的版本兼容问题(新版 pyarrow 废弃了 PyExtensionType)。

  • 安装兼容版本

    pip install pyarrow==12.0.1 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
    
  • 确认安装后即可运行脚本。


步骤 7:配置交换分区(内存不足时)

如果内存不足,可创建 8GB 交换分区(swap)。

  1. 关闭现有交换分区(如果有):

    sudo swapoff -a
    
  2. 删除旧交换文件(如果存在)

    sudo rm /swapfile
    
  3. 创建 8GB 交换文件

    sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
    
  4. 设置权限

    sudo chmod 600 /swapfile
    
  5. 格式化为交换分区

    sudo mkswap /swapfile
    
  6. 启用交换分区

    sudo swapon /swapfile
    
  7. 验证交换分区

    free -h
    swapon --show
    
  8. 永久启用交换分区
    编辑 /etc/fstab

    sudo vim /etc/fstab
    

    在末尾添加:

    /swapfile none swap sw 0 0
    

步骤 8:运行模型转换

  1. 确保所有依赖和模型文件准备就绪。
  2. 进入虚拟环境(例如 conda activate rkllm)。
  3. 运行 rknn-llm/examples/DeepSeek-R1-Distill-Qwen-1.5B_Demo/export 下的 Python 脚本进行模型转换。
    模型路径
    记得是模型的整个路径,而不是模型文件本身!!!!

注意事项

  • 内存不足:如果本地机器内存不足,可尝试使用 Google Colab。
  • 镜像源:如果阿里云镜像源缺少某些包,切换到清华镜像源(https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple)。
  • 依赖冲突:根据错误提示搜索解决方案,必要时手动安装特定版本的依赖。
  • 模型下载:优先使用 ModelScope 下载模型以提高速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iczq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值