[{"data":1,"prerenderedAt":437},["ShallowReactive",2],{"content-query-K5uD8ZW0dF":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"body":13,"_type":431,"_id":432,"_source":433,"_file":434,"_stem":435,"_extension":436},"/technology-blogs/zh/3867","zh",false,"","昇思+昇腾开发板：基于昇思MindSpore使用 LoRA 微调模型模仿甄嬛语气对话","微调示例代码可参考 deepseek-r1-distill-qwen-1.5b-lora.py","2025-10-17","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/10/25/89a13258c71b41e69aebb6650cc6d689.png","technology-blogs",{"type":14,"children":15,"toc":423},"root",[16,24,30,35,52,57,72,77,95,100,110,115,123,131,139,144,152,167,175,180,188,195,203,208,216,224,229,237,242,247,255,262,270,285,297,302,310,315,323,328,336,343,351,362,367,375,380,385,393,401,409],{"type":17,"tag":18,"props":19,"children":21},"element","h1",{"id":20},"昇思昇腾开发板基于昇思mindspore使用-lora-微调模型模仿甄嬛语气对话",[22],{"type":23,"value":8},"text",{"type":17,"tag":25,"props":26,"children":27},"p",{},[28],{"type":23,"value":29},"作者：qianduanjidi",{"type":17,"tag":25,"props":31,"children":32},{},[33],{"type":23,"value":34},"来源：昇思论坛",{"type":17,"tag":18,"props":36,"children":38},{"id":37},"_01-微调背景与目标",[39,45,47],{"type":17,"tag":40,"props":41,"children":42},"strong",{},[43],{"type":23,"value":44},"# 01",{"type":23,"value":46}," ",{"type":17,"tag":40,"props":48,"children":49},{},[50],{"type":23,"value":51},"微调背景与目标",{"type":17,"tag":25,"props":53,"children":54},{},[55],{"type":23,"value":56},"当前，MindSpore NLP 仓库 0.4 分支已支持适配昇腾开发板的 Qwen 模型。本内容将介绍如何在昇思大模型平台的单卡环境下，采用 LoRA 方法对 DeepSeek-R1-Distill-Qwen-1.5B 模型进行微调，使模型能模仿《甄嬛传》中甄嬛的语气风格进行对话。微调示例代码可参考 deepseek-r1-distill-qwen-1.5b-lora.py，昇腾开发板上的实际操作以该示例代码为准。",{"type":17,"tag":18,"props":58,"children":60},{"id":59},"_02-lora-方法简介",[61,66,67],{"type":17,"tag":40,"props":62,"children":63},{},[64],{"type":23,"value":65},"# 02",{"type":23,"value":46},{"type":17,"tag":40,"props":68,"children":69},{},[70],{"type":23,"value":71},"LoRA 方法简介",{"type":17,"tag":25,"props":73,"children":74},{},[75],{"type":23,"value":76},"LoRA（Low-Rank Adaptation）是一种高效的参数微调方法，属于参数高效微调（PEFT）范式。其核心思路是：冻结原始模型参数，在 Attention 层的 Query、Key、Value（QKV）等模块引入由两个可训练的低维矩阵 A 和 B 组成的低秩旁路结构。微调时仅更新这两个矩阵，大幅减少训练参数量，降低计算和内存开销，同时性能接近全参数微调。",{"type":17,"tag":18,"props":78,"children":80},{"id":79},"_03-环境准备",[81,86,87],{"type":17,"tag":40,"props":82,"children":83},{},[84],{"type":23,"value":85},"# 03",{"type":23,"value":46},{"type":17,"tag":40,"props":88,"children":89},{},[90],{"type":17,"tag":40,"props":91,"children":92},{},[93],{"type":23,"value":94},"环境准备",{"type":17,"tag":25,"props":96,"children":97},{},[98],{"type":23,"value":99},"卸载现有 MindSpore 并安装 2.6.0 版本：",{"type":17,"tag":101,"props":102,"children":104},"pre",{"code":103},"%%capture captured_output\n# 实验环境已经预装了mindspore==2.6.0，如需更换mindspore版本，可更改下面 MINDSPORE_VERSION 变量\n!pip uninstall mindspore -y\n%env MINDSPORE_VERSION=2.6.0\n!pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MINDSPORE_VERSION}/MindSpore/un\n",[105],{"type":17,"tag":106,"props":107,"children":108},"code",{"__ignoreMap":7},[109],{"type":23,"value":103},{"type":17,"tag":25,"props":111,"children":112},{},[113],{"type":23,"value":114},"查看当前 MindSpore 版本：",{"type":17,"tag":101,"props":116,"children":118},{"code":117},"# 查看当前 mindspore 版本\n!pip show mindspore\n",[119],{"type":17,"tag":106,"props":120,"children":121},{"__ignoreMap":7},[122],{"type":23,"value":117},{"type":17,"tag":25,"props":124,"children":125},{},[126],{"type":17,"tag":40,"props":127,"children":128},{},[129],{"type":23,"value":130},"运行结果：",{"type":17,"tag":101,"props":132,"children":134},{"code":133},"Name: mindspore\nVersion: 2.6.0\nSummary: MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.\nHome-page: https://www.mindspore.cn\nAuthor: The MindSpore Authors\nAuthor-email: contact@mindspore.cn\nLicense: Apache 2.0\nLocation: /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages\nRequires: asttokens, astunparse, dill, numpy, packaging, pillow, protobuf, psutil, safetensors, scipy\nRequired-by: mindnlp\n",[135],{"type":17,"tag":106,"props":136,"children":137},{"__ignoreMap":7},[138],{"type":23,"value":133},{"type":17,"tag":25,"props":140,"children":141},{},[142],{"type":23,"value":143},"安装 MindSpore NLP 0.4.1 版本及 openMind Hub Client：",{"type":17,"tag":101,"props":145,"children":147},{"code":146},"%%capture captured_output\n",[148],{"type":17,"tag":106,"props":149,"children":150},{"__ignoreMap":7},[151],{"type":23,"value":146},{"type":17,"tag":18,"props":153,"children":155},{"id":154},"_04-数据集下载与处理",[156,161,162],{"type":17,"tag":40,"props":157,"children":158},{},[159],{"type":23,"value":160},"# 04",{"type":23,"value":46},{"type":17,"tag":40,"props":163,"children":164},{},[165],{"type":23,"value":166},"数据集下载与处理",{"type":17,"tag":25,"props":168,"children":169},{},[170],{"type":17,"tag":40,"props":171,"children":172},{},[173],{"type":23,"value":174},"1、下载数据集",{"type":17,"tag":25,"props":176,"children":177},{},[178],{"type":23,"value":179},"通过 openmind_hub 接口从魔乐社区下载 huanhuan.json 数据集。",{"type":17,"tag":101,"props":181,"children":183},{"code":182},"import os\n\nfrom mindnlp.transformers import AutoModelForCausalLM, AutoTokenizer\nfrom mindnlp.engine import TrainingArguments, Trainer\nfrom mindnlp.dataset import load_dataset\nfrom mindnlp.transformers import GenerationConfig\nfrom mindnlp.peft import LoraConfig, TaskType, get_peft_model\n\nfrom mindnlp.engine.utils import PREFIX_CHECKPOINT_DIR\nfrom mindnlp.configs import SAFE_WEIGHTS_NAME\nfrom mindnlp.engine.callbacks import TrainerCallback, TrainerState, TrainerControl\nfrom mindspore._c_expression import disable_multi_thread\n\ndisable_multi_thread()  # 禁用多线程，提升微调性能\n\n# 开启同步，在出现报错，定位问题时开启\n# mindspore.set_context(pynative_synchronize=True)\n\nfrom openmind_hub import om_hub_download\n\n# 从魔乐社区下载数据集\nom_hub_download(\n    repo_id=\"MindSpore-Lab/huanhuan\",\n    repo_type=\"dataset\",\n    filename=\"huanhuan.json\",\n    local_dir=\"./\",\n)\n",[184],{"type":17,"tag":106,"props":185,"children":186},{"__ignoreMap":7},[187],{"type":23,"value":182},{"type":17,"tag":25,"props":189,"children":190},{},[191],{"type":17,"tag":40,"props":192,"children":193},{},[194],{"type":23,"value":130},{"type":17,"tag":101,"props":196,"children":198},{"code":197},"'./huanhuan.json'\n",[199],{"type":17,"tag":106,"props":200,"children":201},{"__ignoreMap":7},[202],{"type":23,"value":197},{"type":17,"tag":25,"props":204,"children":205},{},[206],{"type":23,"value":207},"加载数据集： 使用 mindnlp 的 load_dataset 函数加载下载好的数据集。",{"type":17,"tag":101,"props":209,"children":211},{"code":210},"# 加载数据集\n",[212],{"type":17,"tag":106,"props":213,"children":214},{"__ignoreMap":7},[215],{"type":23,"value":210},{"type":17,"tag":25,"props":217,"children":218},{},[219],{"type":17,"tag":40,"props":220,"children":221},{},[222],{"type":23,"value":223},"2、数据处理",{"type":17,"tag":25,"props":225,"children":226},{},[227],{"type":23,"value":228},"实例化 tokenizer，设置 pad_token 为 eos_token，padding_side 为 right。",{"type":17,"tag":101,"props":230,"children":232},{"code":231},"# 实例化tokenizer\n",[233],{"type":17,"tag":106,"props":234,"children":235},{"__ignoreMap":7},[236],{"type":23,"value":231},{"type":17,"tag":25,"props":238,"children":239},{},[240],{"type":23,"value":241},"定义 process_func 函数，将数据处理成模型所需格式，包括生成对话模板、处理输入_ids、attention_mask 和 labels，进行截断和填充等操作。",{"type":17,"tag":25,"props":243,"children":244},{},[245],{"type":23,"value":246},"对数据集应用处理函数，生成格式化数据集，并为演示裁剪数据集。",{"type":17,"tag":101,"props":248,"children":250},{"code":249},"# 定义数据处理逻辑\n",[251],{"type":17,"tag":106,"props":252,"children":253},{"__ignoreMap":7},[254],{"type":23,"value":249},{"type":17,"tag":25,"props":256,"children":257},{},[258],{"type":17,"tag":40,"props":259,"children":260},{},[261],{"type":23,"value":130},{"type":17,"tag":101,"props":263,"children":265},{"code":264},"User: 小姐，别的秀女都在求中选，唯有咱们小姐想被撂牌子，菩萨一定记得真真儿的——\n",[266],{"type":17,"tag":106,"props":267,"children":268},{"__ignoreMap":7},[269],{"type":23,"value":264},{"type":17,"tag":18,"props":271,"children":273},{"id":272},"_05-执行微调",[274,279,280],{"type":17,"tag":40,"props":275,"children":276},{},[277],{"type":23,"value":278},"# 05",{"type":23,"value":46},{"type":17,"tag":40,"props":281,"children":282},{},[283],{"type":23,"value":284},"执行微调",{"type":17,"tag":286,"props":287,"children":289},"h3",{"id":288},"_1实例化基础模型",[290,292],{"type":23,"value":291},"**1、**",{"type":17,"tag":40,"props":293,"children":294},{},[295],{"type":23,"value":296},"实例化基础模型：",{"type":17,"tag":25,"props":298,"children":299},{},[300],{"type":23,"value":301},"加载 MindSpore-Lab/DeepSeek-R1-Distill-Qwen-1.5B-FP16 模型，并配置生成参数。",{"type":17,"tag":101,"props":303,"children":305},{"code":304},"# 实例化base model\n",[306],{"type":17,"tag":106,"props":307,"children":308},{"__ignoreMap":7},[309],{"type":23,"value":304},{"type":17,"tag":25,"props":311,"children":312},{},[313],{"type":23,"value":314},"配置 LoRA： 设置任务类型、目标模块、秩等参数，实例化 LoRA 模型。",{"type":17,"tag":101,"props":316,"children":318},{"code":317},"# LoRA配置\n",[319],{"type":17,"tag":106,"props":320,"children":321},{"__ignoreMap":7},[322],{"type":23,"value":317},{"type":17,"tag":25,"props":324,"children":325},{},[326],{"type":23,"value":327},"获取模型参与训练的参数，发现仅占总参数量的0.5%。",{"type":17,"tag":101,"props":329,"children":331},{"code":330},"# 获取模型参与训练的参数，发现仅占总参数量的0.5%\n",[332],{"type":17,"tag":106,"props":333,"children":334},{"__ignoreMap":7},[335],{"type":23,"value":330},{"type":17,"tag":25,"props":337,"children":338},{},[339],{"type":17,"tag":40,"props":340,"children":341},{},[342],{"type":23,"value":130},{"type":17,"tag":101,"props":344,"children":346},{"code":345},"trainable params: 9,232,384 || all params: 1,786,320,384 || trainable%: 0.5168380813819342\n",[347],{"type":17,"tag":106,"props":348,"children":349},{"__ignoreMap":7},[350],{"type":23,"value":345},{"type":17,"tag":286,"props":352,"children":354},{"id":353},"_2定义回调函数",[355,357],{"type":23,"value":356},"**2、**",{"type":17,"tag":40,"props":358,"children":359},{},[360],{"type":23,"value":361},"定义回调函数：",{"type":17,"tag":25,"props":363,"children":364},{},[365],{"type":23,"value":366},"用于保存 LoRA 适配器权重。",{"type":17,"tag":101,"props":368,"children":370},{"code":369},"# Callback函数，随save_steps定义的步数保存LoRA adapter权重\n",[371],{"type":17,"tag":106,"props":372,"children":373},{"__ignoreMap":7},[374],{"type":23,"value":369},{"type":17,"tag":25,"props":376,"children":377},{},[378],{"type":23,"value":379},"配置训练参数：",{"type":17,"tag":25,"props":381,"children":382},{},[383],{"type":23,"value":384},"设置输出路径、批次大小、日志步数、训练轮数等。",{"type":17,"tag":101,"props":386,"children":388},{"code":387},"# 训练超参\n",[389],{"type":17,"tag":106,"props":390,"children":391},{"__ignoreMap":7},[392],{"type":23,"value":387},{"type":17,"tag":25,"props":394,"children":395},{},[396],{"type":17,"tag":40,"props":397,"children":398},{},[399],{"type":23,"value":400},"3、实例化 Trainer 并启动微调",{"type":17,"tag":101,"props":402,"children":404},{"code":403},"# 定义Trainer\n",[405],{"type":17,"tag":106,"props":406,"children":407},{"__ignoreMap":7},[408],{"type":23,"value":403},{"type":17,"tag":25,"props":410,"children":411},{},[412,414],{"type":23,"value":413},"代码仓仓库：",{"type":17,"tag":415,"props":416,"children":420},"a",{"href":417,"rel":418},"https://xihe.mindspore.cn/models/qianduanjidi/MindSpore%5C_Demo/tree",[419],"nofollow",[421],{"type":23,"value":422},"https://xihe.mindspore.cn/models/qianduanjidi/MindSpore\\_Demo/tree",{"title":7,"searchDepth":424,"depth":424,"links":425},4,[426,429],{"id":288,"depth":427,"text":428},3,"**1、**实例化基础模型：",{"id":353,"depth":427,"text":430},"**2、**定义回调函数：","markdown","content:technology-blogs:zh:3867.md","content","technology-blogs/zh/3867.md","technology-blogs/zh/3867","md",1776506136363]