[{"data":1,"prerenderedAt":1175},["ShallowReactive",2],{"content-query-Gjbg1Hh39u":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":1169,"_id":1170,"_source":1171,"_file":1172,"_stem":1173,"_extension":1174},"/technology-blogs/zh/3657","zh",false,"","MindSpore Transformers支持DeepSeek-R1蒸馏全流程","模型蒸馏（Model Distillation）是一种知识迁移技术，旨在将大型、复杂的教师模型的知识转移到较小、效率更高的学生模型中。","2025-03-24","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/03/26/642cee8d2668420fa8a651bc45024dba.png","technology-blogs","实践",{"type":15,"children":16,"toc":1166},"root",[17,25,31,36,41,49,58,66,71,79,87,92,100,105,115,120,128,136,141,146,151,159,173,178,186,191,199,206,211,216,224,229,237,242,250,255,263,271,276,281,286,291,296,301,306,311,316,321,326,331,336,341,346,351,356,361,366,371,376,384,389,397,402,410,418,426,431,436,441,445,450,455,460,465,470,475,480,484,489,494,499,504,512,517,525,530,538,543,551,559,567,575,580,588,596,608,616,621,676,681,689,697,704,708,716,723,728,736,741,749,757,762,769,777,782,787,795,799,807,814,819,824,829,837,844,852,857,865,872,880,892,900,908,913,918,923,931,935,943,951,956,961,966,971,976,981,989,997,1002,1010,1018,1026,1031,1039,1047,1055,1060,1068,1072,1079,1086,1090,1095,1100,1105,1110,1118,1126,1131,1136,1141,1146,1151,1156,1161],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore-transformers支持deepseek-r1蒸馏全流程",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":30},"模型蒸馏（Model Distillation）是一种知识迁移技术，旨在将大型、复杂的教师模型的知识转移到较小、效率更高的学生模型中。这种技术通常是通过让小模型（学生模型）学习大模型（教师模型）的预测结果，从而达到与大模型相近的思考和推理能力，以显著降低计算资源的需求、提高推理的性能。",{"type":18,"tag":26,"props":32,"children":33},{},[34],{"type":24,"value":35},"DeepSeek-R1具有生成长思维链（CoT）的能力，这种能力可以通过蒸馏技术迁移到小模型上。小模型学习了R1模型中的知识后，即使不经过强化学习（RL），也能得到出色的推理效果。DeepSeek团队通过使用R1模型生成60万条CoT推理数据，蒸馏了Qwen和Llama这样的开源模型，成功验证了这一迁移能力。",{"type":18,"tag":26,"props":37,"children":38},{},[39],{"type":24,"value":40},"MindSpore Transformers大模型使能套件基于昇腾AI算力，依托MindSpore AI框架提供的丰富的多维混合并行能力，亲和开源工具与通用数据格式，原生支持大模型蒸馏的端到端全流程开发，提供了高效、便捷的解决方案。",{"type":18,"tag":26,"props":42,"children":43},{},[44],{"type":18,"tag":45,"props":46,"children":48},"img",{"alt":7,"src":47},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/03/28/4ec7f929353148808ff408b3ec4fdd27.png",[],{"type":18,"tag":26,"props":50,"children":51},{},[52],{"type":18,"tag":53,"props":54,"children":55},"strong",{},[56],{"type":24,"value":57},"# 01",{"type":18,"tag":26,"props":59,"children":60},{},[61],{"type":18,"tag":53,"props":62,"children":63},{},[64],{"type":24,"value":65},"DeepSeek-R1蒸馏数据生产实践案例",{"type":18,"tag":26,"props":67,"children":68},{},[69],{"type":24,"value":70},"蒸馏数据的生产主要包含服务化推理和拒绝采样两个步骤。首先部署DeepSeek-R1的推理服务，调用服务接口，使用种子数据集中的问题生成思维链数据。然后对生成的数据进行拒绝采样的后处理，筛选出回答正确的高质量数据。MindSpore Transformers提供了一个蒸馏数据生产流程的案例，使用MindSpore Transformers+MindIE实现R1模型的服务化部署，并采用开源种子数据集NuminaMath-1.5和开源拒绝采样工具生成思维链数据集。",{"type":18,"tag":26,"props":72,"children":73},{},[74],{"type":18,"tag":53,"props":75,"children":76},{},[77],{"type":24,"value":78},"一",{"type":18,"tag":26,"props":80,"children":81},{},[82],{"type":18,"tag":53,"props":83,"children":84},{},[85],{"type":24,"value":86},"本地部署DeepSeek-R1",{"type":18,"tag":26,"props":88,"children":89},{},[90],{"type":24,"value":91},"DeepSeek-R1推理至少需要4台Atlas 800I A2（64G）服务器（基于BF16权重）。",{"type":18,"tag":26,"props":93,"children":94},{},[95],{"type":18,"tag":53,"props":96,"children":97},{},[98],{"type":24,"value":99},"1、准备环境",{"type":18,"tag":26,"props":101,"children":102},{},[103],{"type":24,"value":104},"执行以下 Shell 命令，拉取MindSpore DeepSeek推理容器镜像：",{"type":18,"tag":106,"props":107,"children":109},"pre",{"code":108},"\ndocker pull swr.cn-central-221.ovaijisuan.com/mindformers/deepseek_v3_mindspore2.5.0-infer:20250217\n",[110],{"type":18,"tag":111,"props":112,"children":113},"code",{"__ignoreMap":7},[114],{"type":24,"value":108},{"type":18,"tag":26,"props":116,"children":117},{},[118],{"type":24,"value":119},"执行以下命令启动容器：",{"type":18,"tag":106,"props":121,"children":123},{"code":122},"docker run -itd --privileged  --name=Deepseek-R1 --net=host \\\n   --shm-size 500g \\\n   --device=/dev/davinci0 \\\n   --device=/dev/davinci1 \\\n   --device=/dev/davinci2 \\\n   --device=/dev/davinci3 \\\n   --device=/dev/davinci4 \\\n   --device=/dev/davinci5 \\\n   --device=/dev/davinci6 \\\n   --device=/dev/davinci7 \\\n   --device=/dev/davinci_manager \\\n   --device=/dev/hisi_hdc \\\n   --device /dev/devmm_svm \\\n   -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \\\n   -v /usr/local/Ascend/firmware:/usr/local/Ascend/firmware \\\n   -v /usr/local/sbin/npu-smi:/usr/local/sbin/npu-smi \\\n   -v /usr/local/sbin:/usr/local/sbin \\\n   -v /etc/hccn.conf:/etc/hccn.conf \\\n   swr.cn-central-221.ovaijisuan.com/mindformers/deepseek_v3_mindspore2.5.0-infer:20250217 \\\n   bash\n",[124],{"type":18,"tag":111,"props":125,"children":126},{"__ignoreMap":7},[127],{"type":24,"value":122},{"type":18,"tag":26,"props":129,"children":130},{},[131],{"type":18,"tag":53,"props":132,"children":133},{},[134],{"type":24,"value":135},"注意：",{"type":18,"tag":26,"props":137,"children":138},{},[139],{"type":24,"value":140},"l 上述镜像中包含在研版本代码，仅供试用体验，请勿用作商业用途。",{"type":18,"tag":26,"props":142,"children":143},{},[144],{"type":24,"value":145},"l 起容器时，如果有部分宿主机的hostname是一致的，需要在起容器的时候修改容器的hostname，保证所有容器的hostname都不一致。",{"type":18,"tag":26,"props":147,"children":148},{},[149],{"type":24,"value":150},"l 后续所有操作均在容器内操作。",{"type":18,"tag":26,"props":152,"children":153},{},[154],{"type":18,"tag":53,"props":155,"children":156},{},[157],{"type":24,"value":158},"2、下载模型",{"type":18,"tag":26,"props":160,"children":161},{},[162,164],{"type":24,"value":163},"MindSpore版本的DeepSeek-R1已上传魔乐社区（",{"type":18,"tag":165,"props":166,"children":170},"a",{"href":167,"rel":168},"https://modelers.cn/models/MindSpore-Lab/DeepSeek-R1%EF%BC%89%EF%BC%8C%E6%8C%89%E7%85%A7%E5%A6%82%E4%B8%8B%E6%AD%A5%E9%AA%A4%E4%BB%8E%E9%AD%94%E4%B9%90%E7%A4%BE%E5%8C%BA%E4%B8%8B%E8%BD%BD%E6%A8%A1%E5%9E%8B%E3%80%82",[169],"nofollow",[171],{"type":24,"value":172},"https://modelers.cn/models/MindSpore-Lab/DeepSeek-R1），按照如下步骤从魔乐社区下载模型。",{"type":18,"tag":26,"props":174,"children":175},{},[176],{"type":24,"value":177},"创建模型目录，并添加至白名单：",{"type":18,"tag":106,"props":179,"children":181},{"code":180},"\nmkdir -p /home/work/DeepSeek-R1\nexport HUB_WHITE_LIST_PATHS=/home/work/DeepSeek-R1\n",[182],{"type":18,"tag":111,"props":183,"children":184},{"__ignoreMap":7},[185],{"type":24,"value":180},{"type":18,"tag":26,"props":187,"children":188},{},[189],{"type":24,"value":190},"执行以下 Python 脚本从魔乐社区下载昇思 MindSpore 版本的 DeepSeek-R1 文件至指定路径/home/work/DeepSeek-R1。下载的文件包含模型代码、权重、分词模型和示例代码，占用约 1.4TB 的磁盘空间：",{"type":18,"tag":106,"props":192,"children":194},{"code":193},"from openmind_hub import snapshot_download\n\nsnapshot_download(\n    repo_id=\"MindSpore-Lab/DeepSeek-R1\",\n    local_dir=\"/home/work/DeepSeek-R1\",\n    local_dir_use_symlink=False\n)\n",[195],{"type":18,"tag":111,"props":196,"children":197},{"__ignoreMap":7},[198],{"type":24,"value":193},{"type":18,"tag":26,"props":200,"children":201},{},[202],{"type":18,"tag":53,"props":203,"children":204},{},[205],{"type":24,"value":135},{"type":18,"tag":26,"props":207,"children":208},{},[209],{"type":24,"value":210},"l 模型路径可修改为自定义路径，确保该路径下有足够的磁盘空间（约 1.4TB）。",{"type":18,"tag":26,"props":212,"children":213},{},[214],{"type":24,"value":215},"l 下载时间可能因网络环境而异，建议在稳定的网络环境下操作。",{"type":18,"tag":26,"props":217,"children":218},{},[219],{"type":18,"tag":53,"props":220,"children":221},{},[222],{"type":24,"value":223},"3、修改模型配置",{"type":18,"tag":26,"props":225,"children":226},{},[227],{"type":24,"value":228},"修改模型配置文件/home/work/DeepSeek-R1/examples/predict_deepseek3_671B.yaml中的load_checkpoint参数为权重文件夹绝对路径，配置tokenizer_file参数和vocab_file参数为tokenizer.json文件绝对路径。",{"type":18,"tag":26,"props":230,"children":231},{},[232],{"type":18,"tag":53,"props":233,"children":234},{},[235],{"type":24,"value":236},"4、修改推理服务配置",{"type":18,"tag":26,"props":238,"children":239},{},[240],{"type":24,"value":241},"准备rank_table_file.json，按照如下进行配置，其中各配置项需要配置为实际的值。",{"type":18,"tag":106,"props":243,"children":245},{"code":244},"\n{\n   \"server_count\": \"4\",\n   \"server_list\": [\n      {\n         \"device\": [\n            {\n               \"device_id\": \"0\", \n               \"device_ip\": \"...\", \n               \"rank_id\": \"0\"\n            },\n            ...\n            {\n               \"device_id\": \"8\", \n               \"device_ip\": \"...\", \n               \"rank_id\": \"8\"\n            }\n         ],\n         \"server_ip\": \"...\", \n         \"container_ip\": \"...\" \n      },\n      ...\n      {\n         \"device\": [\n            {\n               \"device_id\": \"0\", \n               \"device_ip\": \"...\", \n               \"rank_id\": \"24\"\n            },\n            ...\n            {\n               \"device_id\": \"8\", \n               \"device_ip\": \"...\", \n               \"rank_id\": \"31\"\n            }\n         ],\n         \"server_ip\": \"...\", \n         \"container_ip\": \"...\" \n      }\n   ],\n   \"status\": \"completed\",\n   \"version\": \"1.0\"\n}\n",[246],{"type":18,"tag":111,"props":247,"children":248},{"__ignoreMap":7},[249],{"type":24,"value":244},{"type":18,"tag":26,"props":251,"children":252},{},[253],{"type":24,"value":254},"配置说明：",{"type":18,"tag":26,"props":256,"children":257},{},[258],{"type":18,"tag":53,"props":259,"children":260},{},[261],{"type":24,"value":262},"配置项",{"type":18,"tag":26,"props":264,"children":265},{},[266],{"type":18,"tag":53,"props":267,"children":268},{},[269],{"type":24,"value":270},"配置说明",{"type":18,"tag":26,"props":272,"children":273},{},[274],{"type":24,"value":275},"server_count",{"type":18,"tag":26,"props":277,"children":278},{},[279],{"type":24,"value":280},"总节点数",{"type":18,"tag":26,"props":282,"children":283},{},[284],{"type":24,"value":285},"server_list",{"type":18,"tag":26,"props":287,"children":288},{},[289],{"type":24,"value":290},"节点列表，列表中第一个server为主节点",{"type":18,"tag":26,"props":292,"children":293},{},[294],{"type":24,"value":295},"device",{"type":18,"tag":26,"props":297,"children":298},{},[299],{"type":24,"value":300},"NPU设备列表",{"type":18,"tag":26,"props":302,"children":303},{},[304],{"type":24,"value":305},"device_id",{"type":18,"tag":26,"props":307,"children":308},{},[309],{"type":24,"value":310},"NPU设备ID，取值范围[0,8)",{"type":18,"tag":26,"props":312,"children":313},{},[314],{"type":24,"value":315},"device_ip",{"type":18,"tag":26,"props":317,"children":318},{},[319],{"type":24,"value":320},"NPU设备IP，可通过hccn_tool命令获取",{"type":18,"tag":26,"props":322,"children":323},{},[324],{"type":24,"value":325},"rank_id",{"type":18,"tag":26,"props":327,"children":328},{},[329],{"type":24,"value":330},"NPU设备全局ID，取值范围[0,总卡数)",{"type":18,"tag":26,"props":332,"children":333},{},[334],{"type":24,"value":335},"server_id",{"type":18,"tag":26,"props":337,"children":338},{},[339],{"type":24,"value":340},"节点IP",{"type":18,"tag":26,"props":342,"children":343},{},[344],{"type":24,"value":345},"container_ip",{"type":18,"tag":26,"props":347,"children":348},{},[349],{"type":24,"value":350},"容器IP，服务化部署时需要，若无特殊配置，则与server_ip相同",{"type":18,"tag":26,"props":352,"children":353},{},[354],{"type":24,"value":355},"status",{"type":18,"tag":26,"props":357,"children":358},{},[359],{"type":24,"value":360},"配置状态，不用修改",{"type":18,"tag":26,"props":362,"children":363},{},[364],{"type":24,"value":365},"version",{"type":18,"tag":26,"props":367,"children":368},{},[369],{"type":24,"value":370},"配置版本，不用修改",{"type":18,"tag":26,"props":372,"children":373},{},[374],{"type":24,"value":375},"其中NPU设备IP通过如下命令获取：",{"type":18,"tag":106,"props":377,"children":379},{"code":378},"\nfor i in {0..7}; do hccn_tool -i $i -ip -g; done\n",[380],{"type":18,"tag":111,"props":381,"children":382},{"__ignoreMap":7},[383],{"type":24,"value":378},{"type":18,"tag":26,"props":385,"children":386},{},[387],{"type":24,"value":388},"执行以下命令配置服务化需要的环境变量，将MS_SCHED_HOST配置为实际的主节点IP、MIES_CONTAINER_IP配置为本节点IP、RANKTABLEFILE配置为rank_table_file.json的实际路径。",{"type":18,"tag":106,"props":390,"children":392},{"code":391},"export MS_SCHED_HOST=192.168.1.1\nexport MS_SCHED_PORT=8080\nexport MS_ENABLE_LCCL=off\nexport HCCL_OP_EXPANSION_MODE=AIV\nexport MIES_CONTAINER_IP=192.168.1.1\nexport RANKTABLEFILE=./rank_table_file.json\nexport PYTHONPATH=/root/mindformers/:$PYTHONPATH\n",[393],{"type":18,"tag":111,"props":394,"children":395},{"__ignoreMap":7},[396],{"type":24,"value":391},{"type":18,"tag":26,"props":398,"children":399},{},[400],{"type":24,"value":401},"执行以下命令打开服务化配置文件config.json：",{"type":18,"tag":106,"props":403,"children":405},{"code":404},"cd /usr/local/Ascend/mindie/latest/mindie-service/\nvim conf/config.json\n",[406],{"type":18,"tag":111,"props":407,"children":408},{"__ignoreMap":7},[409],{"type":24,"value":404},{"type":18,"tag":26,"props":411,"children":412},{},[413],{"type":18,"tag":53,"props":414,"children":415},{},[416],{"type":24,"value":417},"参数名",{"type":18,"tag":26,"props":419,"children":420},{},[421],{"type":18,"tag":53,"props":422,"children":423},{},[424],{"type":24,"value":425},"修改说明",{"type":18,"tag":26,"props":427,"children":428},{},[429],{"type":24,"value":430},"ipAddress",{"type":18,"tag":26,"props":432,"children":433},{},[434],{"type":24,"value":435},"修改为主节点IP",{"type":18,"tag":26,"props":437,"children":438},{},[439],{"type":24,"value":440},"managementIpAddress",{"type":18,"tag":26,"props":442,"children":443},{},[444],{"type":24,"value":435},{"type":18,"tag":26,"props":446,"children":447},{},[448],{"type":24,"value":449},"httpsEnabled",{"type":18,"tag":26,"props":451,"children":452},{},[453],{"type":24,"value":454},"配置为false",{"type":18,"tag":26,"props":456,"children":457},{},[458],{"type":24,"value":459},"multiNodesInferEnabled",{"type":18,"tag":26,"props":461,"children":462},{},[463],{"type":24,"value":464},"配置为true，开启多机推理",{"type":18,"tag":26,"props":466,"children":467},{},[468],{"type":24,"value":469},"interCommTLSEnabled",{"type":18,"tag":26,"props":471,"children":472},{},[473],{"type":24,"value":474},"若不需要安全认证，修改为false",{"type":18,"tag":26,"props":476,"children":477},{},[478],{"type":24,"value":479},"interNodeTLSEnabled",{"type":18,"tag":26,"props":481,"children":482},{},[483],{"type":24,"value":474},{"type":18,"tag":26,"props":485,"children":486},{},[487],{"type":24,"value":488},"modelName",{"type":18,"tag":26,"props":490,"children":491},{},[492],{"type":24,"value":493},"自定义模型名，不影响服务化拉起",{"type":18,"tag":26,"props":495,"children":496},{},[497],{"type":24,"value":498},"modelWeightPath",{"type":18,"tag":26,"props":500,"children":501},{},[502],{"type":24,"value":503},"修改为模型文件夹绝对路径/home/work/DeepSeek-R1/examples",{"type":18,"tag":26,"props":505,"children":506},{},[507],{"type":18,"tag":53,"props":508,"children":509},{},[510],{"type":24,"value":511},"5、拉起推理服务",{"type":18,"tag":26,"props":513,"children":514},{},[515],{"type":24,"value":516},"在所有机器上同时执行如下命令：",{"type":18,"tag":106,"props":518,"children":520},{"code":519},"\ncd /usr/local/Ascend/mindie/latest/mindie-service/\n./bin/mindieservice_daemon\n",[521],{"type":18,"tag":111,"props":522,"children":523},{"__ignoreMap":7},[524],{"type":24,"value":519},{"type":18,"tag":26,"props":526,"children":527},{},[528],{"type":24,"value":529},"执行命令后，首先会打印本次启动所用的所有参数，然后直到出现以下输出，则认为服务成功启动：",{"type":18,"tag":106,"props":531,"children":533},{"code":532},"Daemon start success!\n",[534],{"type":18,"tag":111,"props":535,"children":536},{"__ignoreMap":7},[537],{"type":24,"value":532},{"type":18,"tag":26,"props":539,"children":540},{},[541],{"type":24,"value":542},"执行以下命令发送流式推理请求进行测试（IP地址和端口号需修改为实际值）：",{"type":18,"tag":106,"props":544,"children":546},{"code":545},"\ncurl -w \"\\ntime_total=%{time_total}\\n\" -H \"Accept: application/json\" -H \"Content-type: application/json\" -X POST -d '{\"inputs\": \"请介绍一个北京的景点\", \"parameters\": {\"do_sample\": false, \"max_new_tokens\": 128}, \"stream\": false}' http://127.0.0.1:9091/generate_stream &\n",[547],{"type":18,"tag":111,"props":548,"children":549},{"__ignoreMap":7},[550],{"type":24,"value":545},{"type":18,"tag":26,"props":552,"children":553},{},[554],{"type":18,"tag":53,"props":555,"children":556},{},[557],{"type":24,"value":558},"二",{"type":18,"tag":26,"props":560,"children":561},{},[562],{"type":18,"tag":53,"props":563,"children":564},{},[565],{"type":24,"value":566},"生成数据",{"type":18,"tag":26,"props":568,"children":569},{},[570],{"type":18,"tag":53,"props":571,"children":572},{},[573],{"type":24,"value":574},"1、安装依赖",{"type":18,"tag":26,"props":576,"children":577},{},[578],{"type":24,"value":579},"执行以下命令安装所需依赖：",{"type":18,"tag":106,"props":581,"children":583},{"code":582},"pip install datasets tqdm aiofiles aiohttp uvloop\n",[584],{"type":18,"tag":111,"props":585,"children":586},{"__ignoreMap":7},[587],{"type":24,"value":582},{"type":18,"tag":26,"props":589,"children":590},{},[591],{"type":18,"tag":53,"props":592,"children":593},{},[594],{"type":24,"value":595},"2、执行脚本",{"type":18,"tag":26,"props":597,"children":598},{},[599,601],{"type":24,"value":600},"从魔乐社区获取调用推理服务生成数据的脚本generate_reasoning.py（",{"type":18,"tag":165,"props":602,"children":605},{"href":603,"rel":604},"https://modelers.cn/models/MindSpore-Lab/OpenR1-Qwen-7B/blob/main/scripts/generate%5C_reasoning.py%EF%BC%89%EF%BC%8C%E6%89%A7%E8%A1%8C%E4%BB%A5%E4%B8%8B%E5%91%BD%E4%BB%A4%EF%BC%8C%E4%BD%BF%E7%94%A8%E7%A7%8D%E5%AD%90%E6%95%B0%E6%8D%AE%E9%9B%86%E4%B8%AD%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%8C%E7%94%9F%E6%88%90CoT%E6%95%B0%E6%8D%AE%EF%BC%9A",[169],[606],{"type":24,"value":607},"https://modelers.cn/models/MindSpore-Lab/OpenR1-Qwen-7B/blob/main/scripts/generate\\_reasoning.py），执行以下命令，使用种子数据集中的问题，生成CoT数据：",{"type":18,"tag":106,"props":609,"children":611},{"code":610},"python generate_reasoning.py \\\n    --model \"DeekSeek-R1\"\n    --dataset-name \"AI-MO/NuminaMath-1.5\" \\\n    --output-file \"numinamath_r1_generations.jsonl\" \\\n    --prompt-column \"problem\" \\\n    --uuid-column \"problem\" \\\n    --api-addr \"api.host.name\" \\\n    --num-generations 2 \\\n    --max-tokens 16384 \\\n    --max-concurrent 100\n",[612],{"type":18,"tag":111,"props":613,"children":614},{"__ignoreMap":7},[615],{"type":24,"value":610},{"type":18,"tag":26,"props":617,"children":618},{},[619],{"type":24,"value":620},"参数说明：",{"type":18,"tag":622,"props":623,"children":624},"ul",{},[625,636,641,646,651,656,661,666,671],{"type":18,"tag":626,"props":627,"children":628},"li",{},[629,634],{"type":18,"tag":53,"props":630,"children":631},{},[632],{"type":24,"value":633},"model:",{"type":24,"value":635}," 推理服务的模型名，需要和服务化配置文件 config.json 中的modelName 一致。",{"type":18,"tag":626,"props":637,"children":638},{},[639],{"type":24,"value":640},"**dataset-name：**种子数据集名称，配置为HuggingFace Datasets名称或本地的数据集路径。",{"type":18,"tag":626,"props":642,"children":643},{},[644],{"type":24,"value":645},"**output-file：**输出CoT数据文件的文件名。",{"type":18,"tag":626,"props":647,"children":648},{},[649],{"type":24,"value":650},"**prompt-column：**种子数据集中提示词的列名，使用此列的数据进行CoT数据生成。",{"type":18,"tag":626,"props":652,"children":653},{},[654],{"type":24,"value":655},"**uuid-column：**种子数据集中uuid的列名，使用此列计算哈希值去重数据。",{"type":18,"tag":626,"props":657,"children":658},{},[659],{"type":24,"value":660},"**api-addr：**推理服务api的地址，配置为 ip:port 。",{"type":18,"tag":626,"props":662,"children":663},{},[664],{"type":24,"value":665},"**num-generations：**对于种子数据集中每个问题生成CoT数据的数量。",{"type":18,"tag":626,"props":667,"children":668},{},[669],{"type":24,"value":670},"**max-tokens：**生成的CoT数据的最大Token数。",{"type":18,"tag":626,"props":672,"children":673},{},[674],{"type":24,"value":675},"**max-concurrent：**请求的最大并发数量。",{"type":18,"tag":26,"props":677,"children":678},{},[679],{"type":24,"value":680},"执行成功后，数据将保存在numinamath_r1_generations.jsonl中。",{"type":18,"tag":26,"props":682,"children":683},{},[684],{"type":18,"tag":53,"props":685,"children":686},{},[687],{"type":24,"value":688},"三",{"type":18,"tag":26,"props":690,"children":691},{},[692],{"type":18,"tag":53,"props":693,"children":694},{},[695],{"type":24,"value":696},"math-verify",{"type":18,"tag":26,"props":698,"children":699},{},[700],{"type":18,"tag":53,"props":701,"children":702},{},[703],{"type":24,"value":574},{"type":18,"tag":26,"props":705,"children":706},{},[707],{"type":24,"value":579},{"type":18,"tag":106,"props":709,"children":711},{"code":710},"pip install math_verify\n",[712],{"type":18,"tag":111,"props":713,"children":714},{"__ignoreMap":7},[715],{"type":24,"value":710},{"type":18,"tag":26,"props":717,"children":718},{},[719],{"type":18,"tag":53,"props":720,"children":721},{},[722],{"type":24,"value":595},{"type":18,"tag":26,"props":724,"children":725},{},[726],{"type":24,"value":727},"执行以下命令，利用math-verify库过滤错误数据，确保生成的cot数据中回答都是正确的：",{"type":18,"tag":106,"props":729,"children":731},{"code":730},"python reject_sampling.py --src numinamath_r1_generations.jsonl --dst numinamath_r1_generations_filtered.jsonl\n",[732],{"type":18,"tag":111,"props":733,"children":734},{"__ignoreMap":7},[735],{"type":24,"value":730},{"type":18,"tag":26,"props":737,"children":738},{},[739],{"type":24,"value":740},"最后生成的数据将保存在numinamath_r1_generations_filtered.jsonl中。",{"type":18,"tag":26,"props":742,"children":743},{},[744],{"type":18,"tag":53,"props":745,"children":746},{},[747],{"type":24,"value":748},"# 02",{"type":18,"tag":26,"props":750,"children":751},{},[752],{"type":18,"tag":53,"props":753,"children":754},{},[755],{"type":24,"value":756},"使用蒸馏数据微调模型实践案例",{"type":18,"tag":26,"props":758,"children":759},{},[760],{"type":24,"value":761},"本案例参考OpenR1-Qwen-7B，基于MindSpore Transformers大模型套件，利用OpenR1发布的DeepSeek-R1蒸馏数据OpenR1-Math-220K微调Qwen2.5-Math-7B-Instruct模型，并取得了和开源相当的评测分数。",{"type":18,"tag":26,"props":763,"children":764},{},[765],{"type":18,"tag":53,"props":766,"children":767},{},[768],{"type":24,"value":78},{"type":18,"tag":26,"props":770,"children":771},{},[772],{"type":18,"tag":53,"props":773,"children":774},{},[775],{"type":24,"value":776},"准备环境",{"type":18,"tag":26,"props":778,"children":779},{},[780],{"type":24,"value":781},"本案例复用DeepSeek-V3训练镜像。",{"type":18,"tag":26,"props":783,"children":784},{},[785],{"type":24,"value":786},"执行以下 Shell 命令，拉取MindSpore DeepSeek训练容器镜像：",{"type":18,"tag":106,"props":788,"children":790},{"code":789},"docker pull swr.cn-central-221.ovaijisuan.com/mindformers/deepseek_v3_mindspore2.4.10-train:20250209\n",[791],{"type":18,"tag":111,"props":792,"children":793},{"__ignoreMap":7},[794],{"type":24,"value":789},{"type":18,"tag":26,"props":796,"children":797},{},[798],{"type":24,"value":119},{"type":18,"tag":106,"props":800,"children":802},{"code":801},"\nimage_name=swr.cn-central-221.ovaijisuan.com/mindformers/deepseek_v3_mindspore2.4.10-train:20250209\n   docker_name=deepseek_v3\n   docker run -itd -u root \\\n   --ipc=host --net=host \\\n   --privileged \\\n   --device=/dev/davinci0 \\\n   --device=/dev/davinci1 \\\n   --device=/dev/davinci2 \\\n   --device=/dev/davinci3 \\\n   --device=/dev/davinci4 \\\n   --device=/dev/davinci5 \\\n   --device=/dev/davinci6 \\\n   --device=/dev/davinci7 \\\n   --device=/dev/davinci_manager \\\n   --device=/dev/devmm_svm \\\n   --device=/dev/hisi_hdc \\\n   -v /etc/localtime:/etc/localtime \\\n   -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \\\n   -v /usr/local/Ascend/driver/tools/hccn_tool:/usr/local/bin/hccn_tool \\\n   -v /etc/ascend_install.info:/etc/ascend_install.info \\\n   -v /var/log/npu:/usr/slog \\\n   -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \\\n   -v /etc/hccn.conf:/etc/hccn.conf \\\n   --name \"$docker_name\" \\\n   \"$image_name\" \\\n   /bin/bash\n",[803],{"type":18,"tag":111,"props":804,"children":805},{"__ignoreMap":7},[806],{"type":24,"value":801},{"type":18,"tag":26,"props":808,"children":809},{},[810],{"type":18,"tag":53,"props":811,"children":812},{},[813],{"type":24,"value":135},{"type":18,"tag":26,"props":815,"children":816},{},[817],{"type":24,"value":818},"l上述镜像中包含在研版本代码，仅供试用体验，请勿用作商业用途。",{"type":18,"tag":26,"props":820,"children":821},{},[822],{"type":24,"value":823},"l后续所有操作均在容器内操作。",{"type":18,"tag":26,"props":825,"children":826},{},[827],{"type":24,"value":828},"执行如下命令拉取MindSpore Transformers代码，并编译安装：",{"type":18,"tag":106,"props":830,"children":832},{"code":831},"git clone -b dev https://gitee.com/mindspore/mindformers.git\ncd mindformers\ngit checkout b209ce38\nbash build.sh\n",[833],{"type":18,"tag":111,"props":834,"children":835},{"__ignoreMap":7},[836],{"type":24,"value":831},{"type":18,"tag":26,"props":838,"children":839},{},[840],{"type":18,"tag":53,"props":841,"children":842},{},[843],{"type":24,"value":558},{"type":18,"tag":26,"props":845,"children":846},{},[847],{"type":18,"tag":53,"props":848,"children":849},{},[850],{"type":24,"value":851},"拉取所需配置和代码",{"type":18,"tag":26,"props":853,"children":854},{},[855],{"type":24,"value":856},"案例所需的配置和代码已上传魔乐社区，使用如下命令拉取：",{"type":18,"tag":106,"props":858,"children":860},{"code":859},"git clone https://modelers.cn/MindSpore-Lab/OpenR1-Qwen-7B.git\n",[861],{"type":18,"tag":111,"props":862,"children":863},{"__ignoreMap":7},[864],{"type":24,"value":859},{"type":18,"tag":26,"props":866,"children":867},{},[868],{"type":18,"tag":53,"props":869,"children":870},{},[871],{"type":24,"value":688},{"type":18,"tag":26,"props":873,"children":874},{},[875],{"type":18,"tag":53,"props":876,"children":877},{},[878],{"type":24,"value":879},"准备模型",{"type":18,"tag":26,"props":881,"children":882},{},[883,885],{"type":24,"value":884},"本次微调使用的模型为Qwen2.5-Math-7B-Instruct，可以在HuggingFace（",{"type":18,"tag":165,"props":886,"children":889},{"href":887,"rel":888},"https://modelers.cn/link?target=https%3A%2F%2Fhuggingface.co%2FQwen%2FQwen2.5-Math-7B-Instruct%EF%BC%89%E4%B8%8B%E8%BD%BD%EF%BC%8C%E5%B9%B6%E5%8F%82%E8%80%83Qwen2.5%EF%BC%88https://gitee.com/mindspore/mindformers/blob/dev/research/qwen2%5C_5/qwen2%5C_5.md#%E6%A8%A1%E5%9E%8B%E6%9D%83%E9%87%8D%E8%BD%AC%E6%8D%A2%EF%BC%89%E6%9D%83%E9%87%8D%E8%BD%AC%E6%8D%A2%E3%80%82",[169],[890],{"type":24,"value":891},"https://modelers.cn/link?target=https%3A%2F%2Fhuggingface.co%2FQwen%2FQwen2.5-Math-7B-Instruct）下载，并参考Qwen2.5（https://gitee.com/mindspore/mindformers/blob/dev/research/qwen2\\_5/qwen2\\_5.md#%E6%A8%A1%E5%9E%8B%E6%9D%83%E9%87%8D%E8%BD%AC%E6%8D%A2）权重转换。",{"type":18,"tag":26,"props":893,"children":894},{},[895],{"type":18,"tag":53,"props":896,"children":897},{},[898],{"type":24,"value":899},"四",{"type":18,"tag":26,"props":901,"children":902},{},[903],{"type":18,"tag":53,"props":904,"children":905},{},[906],{"type":24,"value":907},"预处理数据",{"type":18,"tag":26,"props":909,"children":910},{},[911],{"type":24,"value":912},"由于数据集较大，我们采用离线tokenize的方式对数据集进行预处理。",{"type":18,"tag":26,"props":914,"children":915},{},[916],{"type":24,"value":917},"首先在HuggingFace上下载OpenR1-Math-220K原始数据集，本项目已提供数据集处理所需要的脚本，放在./modules目录下，数据集转换的配置文件放在./dataset/dyn，其中，需要修改配置项data_files的值为各个parquet格式数据文件的路径、修改配置项vocab_file为Qwen2.5-Math-7B-Instruct模型vocab.json文件的路径、修改配置项merges_file为Qwen2.5-Math-7B-Instruct模型merges.txt文件的路径。",{"type":18,"tag":26,"props":919,"children":920},{},[921],{"type":24,"value":922},"在MindSpore Transformers源码目录下执行以下命令：",{"type":18,"tag":106,"props":924,"children":926},{"code":925},"python toolkit/data_preprocess/huggingface/datasets_preprocess.py --config /path/to/data_process_dyn.yaml --save_path /path/to/save_data --register_path /path/to/modules\n",[927],{"type":18,"tag":111,"props":928,"children":929},{"__ignoreMap":7},[930],{"type":24,"value":925},{"type":18,"tag":26,"props":932,"children":933},{},[934],{"type":24,"value":620},{"type":18,"tag":26,"props":936,"children":937},{},[938],{"type":18,"tag":53,"props":939,"children":940},{},[941],{"type":24,"value":942},"参数",{"type":18,"tag":26,"props":944,"children":945},{},[946],{"type":18,"tag":53,"props":947,"children":948},{},[949],{"type":24,"value":950},"说明",{"type":18,"tag":26,"props":952,"children":953},{},[954],{"type":24,"value":955},"config",{"type":18,"tag":26,"props":957,"children":958},{},[959],{"type":24,"value":960},"数据集处理的yaml配置文件路径",{"type":18,"tag":26,"props":962,"children":963},{},[964],{"type":24,"value":965},"save_path",{"type":18,"tag":26,"props":967,"children":968},{},[969],{"type":24,"value":970},"处理后arrow格式文件的保存路径",{"type":18,"tag":26,"props":972,"children":973},{},[974],{"type":24,"value":975},"register_path",{"type":18,"tag":26,"props":977,"children":978},{},[979],{"type":24,"value":980},"外挂代码目录的路径，这里设置为./modules目录的路径",{"type":18,"tag":26,"props":982,"children":983},{},[984],{"type":18,"tag":53,"props":985,"children":986},{},[987],{"type":24,"value":988},"五",{"type":18,"tag":26,"props":990,"children":991},{},[992],{"type":18,"tag":53,"props":993,"children":994},{},[995],{"type":24,"value":996},"修改微调任务配置",{"type":18,"tag":26,"props":998,"children":999},{},[1000],{"type":24,"value":1001},"按照如下示例修改微调任务的配置文件configs/finetune_qwen_2_5_7b_dyn.yaml：",{"type":18,"tag":106,"props":1003,"children":1005},{"code":1004},"\n...\noutput_dir: './output'                  # 训练结果保存路径，根据实际情况修改\nload_checkpoint: '/path/to/checkpoint'  # 权重加载路径，根据实际情况修改\nauto_trans_ckpt: True                   # 打开权重自动转换\nresume_training: False                  # 断点续训时开启\n...\ntrain_dataset: &train_dataset\n  data_loader:\n    path: \"/path/to/save_data\"          # 落盘数据加载路径，根据实际情况修改\n...\n",[1006],{"type":18,"tag":111,"props":1007,"children":1008},{"__ignoreMap":7},[1009],{"type":24,"value":1004},{"type":18,"tag":26,"props":1011,"children":1012},{},[1013],{"type":18,"tag":53,"props":1014,"children":1015},{},[1016],{"type":24,"value":1017},"六",{"type":18,"tag":26,"props":1019,"children":1020},{},[1021],{"type":18,"tag":53,"props":1022,"children":1023},{},[1024],{"type":24,"value":1025},"设置所需环境变量",{"type":18,"tag":26,"props":1027,"children":1028},{},[1029],{"type":24,"value":1030},"执行以下命令设置环境变量：",{"type":18,"tag":106,"props":1032,"children":1034},{"code":1033},"\nexport ACLNN_CACHE_LIMIT=10\nexport MS_DEV_RUNTIME_CONF=\"aclnn_cache_queue_length:128\"\n",[1035],{"type":18,"tag":111,"props":1036,"children":1037},{"__ignoreMap":7},[1038],{"type":24,"value":1033},{"type":18,"tag":26,"props":1040,"children":1041},{},[1042],{"type":18,"tag":53,"props":1043,"children":1044},{},[1045],{"type":24,"value":1046},"七",{"type":18,"tag":26,"props":1048,"children":1049},{},[1050],{"type":18,"tag":53,"props":1051,"children":1052},{},[1053],{"type":24,"value":1054},"拉起微调任务",{"type":18,"tag":26,"props":1056,"children":1057},{},[1058],{"type":24,"value":1059},"执行以下命令拉起微调任务：",{"type":18,"tag":106,"props":1061,"children":1063},{"code":1062},"cd mindformers\nbash scripts/msrun_launcher.sh \"run_mindformer.py --config /path/to/finetune_qwen_2_5_7b_dyn.yaml\" 8\n",[1064],{"type":18,"tag":111,"props":1065,"children":1066},{"__ignoreMap":7},[1067],{"type":24,"value":1062},{"type":18,"tag":26,"props":1069,"children":1070},{},[1071],{"type":24,"value":620},{"type":18,"tag":26,"props":1073,"children":1074},{},[1075],{"type":18,"tag":53,"props":1076,"children":1077},{},[1078],{"type":24,"value":942},{"type":18,"tag":26,"props":1080,"children":1081},{},[1082],{"type":18,"tag":53,"props":1083,"children":1084},{},[1085],{"type":24,"value":950},{"type":18,"tag":26,"props":1087,"children":1088},{},[1089],{"type":24,"value":955},{"type":18,"tag":26,"props":1091,"children":1092},{},[1093],{"type":24,"value":1094},"微调任务的配置文件路径",{"type":18,"tag":26,"props":1096,"children":1097},{},[1098],{"type":24,"value":1099},"日志记录在output/msrun_log目录下，可以通过tail指令查看日志信息。",{"type":18,"tag":26,"props":1101,"children":1102},{},[1103],{"type":24,"value":1104},"微调完成后，输出的权重文件在output/checkpoint目录下。",{"type":18,"tag":26,"props":1106,"children":1107},{},[1108],{"type":24,"value":1109},"经实测，微调后模型的在MATH-500评测任务上与开源得分的对比如下表：",{"type":18,"tag":26,"props":1111,"children":1112},{},[1113],{"type":18,"tag":53,"props":1114,"children":1115},{},[1116],{"type":24,"value":1117},"Model",{"type":18,"tag":26,"props":1119,"children":1120},{},[1121],{"type":18,"tag":53,"props":1122,"children":1123},{},[1124],{"type":24,"value":1125},"MATH-500",{"type":18,"tag":26,"props":1127,"children":1128},{},[1129],{"type":24,"value":1130},"DeepSeek-Distill-Qwen-7B",{"type":18,"tag":26,"props":1132,"children":1133},{},[1134],{"type":24,"value":1135},"91.6",{"type":18,"tag":26,"props":1137,"children":1138},{},[1139],{"type":24,"value":1140},"OpenR1-Qwen-7B (HuggingFace)",{"type":18,"tag":26,"props":1142,"children":1143},{},[1144],{"type":24,"value":1145},"90.6",{"type":18,"tag":26,"props":1147,"children":1148},{},[1149],{"type":24,"value":1150},"OpenR1-Qwen-7B (MindSpore Transformers)",{"type":18,"tag":26,"props":1152,"children":1153},{},[1154],{"type":24,"value":1155},"89.8",{"type":18,"tag":26,"props":1157,"children":1158},{},[1159],{"type":24,"value":1160},"OpenThinker-7B",{"type":18,"tag":26,"props":1162,"children":1163},{},[1164],{"type":24,"value":1165},"89.6",{"title":7,"searchDepth":1167,"depth":1167,"links":1168},4,[],"markdown","content:technology-blogs:zh:3657.md","content","technology-blogs/zh/3657.md","technology-blogs/zh/3657","md",1776506132976]