微调前期准备
下载模型与框架
首先确保已安装 git-lfs,然后克隆 Qwen3.5-4B 模型仓库:
git lfs install
git clone https://modelers.cn/Qwen-AI/Qwen3.5-4B.git
接着拉取 Llama-Factory 源码,这里使用浅克隆以节省时间:
git clone --depth 1 https://github.com/hiyouga/LlamaFactory.git
搭建微调环境
我们推荐使用 Miniconda 来隔离依赖。在终端中执行以下操作:
# 清除当前 shell 会话中的 PYTHONPATH 环境变量
unset PYTHONPATH
# 安装 miniconda (以 aarch64 为例)
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh
bash Miniconda3-latest-Linux-aarch64.sh
# 配置 conda
conda config --set auto_activate_base false
source ~/.bashrc
# 接受 main 和 r 通道的条款
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r
# 创建 python3.11 环境并激活
conda create --name LlamaFactory python=3.11 -y
conda activate LlamaFactory
安装 Llama-Factory 依赖
进入项目目录后安装核心依赖。如果你使用的是昇腾 NPU 算力,需要额外安装 torch-npu 和相关库以获得加速效果:
cd LlamaFactory
pip install -e .
# 可选:安装 flash-linear-attention 获得训练推理加速效果
pip uninstall fla-core flash-linear-attention -y && pip install -U git+https://github.com/fla-org/flash-linear-attention
# 昇腾 NPU 环境依赖
pip install torch-npu==2.10.0rc2
pip install decorator
安装完成后,运行 llamafactory-cli version 验证版本信息。
完成环境搭建后,记得加载昇腾的环境启动命令:
source /usr/local/Ascend/ascend-toolkit/set_env.sh
source /usr/local/Ascend/nnal/asdsip/set_env.sh
source /usr/local/Ascend/nnal/atb/set_env.sh
准备数据集
根据 Llama-Factory 的要求,数据集需放置在 LlamaFactory/data 目录下。我们可以先下载官方测试数据集(或自行制作):


