CANN/cann-recipes-train:Qwen3-30B-A3B医学SFT训练示例

发布时间:2026/8/28 9:31:15

CANN/cann-recipes-train:Qwen3-30B-A3B医学SFT训练示例
Qwen3-30B-A3B Medical SFT Training Example【免费下载链接】cann-recipes-train本项目针对LLM与多模态模型训练业务中的典型模型、加速算法提供基于CANN平台的优化样例项目地址: https://gitcode.com/cann/cann-recipes-trainThis example uses the torchtitan-npu framework to fine-tuneQwen3-30B-A3Bon a medical domain SFT task. Training effectiveness is measured via Keyword Recall on medical QA samples.The Medical R1 dataset (question/think/answer three-field format) is used for training. The MoE parallelism config (EP8) enables full-parameter fine-tuning on a single node with 16 cards. Evaluation uses vLLM vLLM-Ascend to compare the base model, CPT checkpoint, and SFT model under the same conditions.Supported ProductsItemSpecProductAtlas A3 seriesRecommended cards16 (EP8)CANN version9.0.0Python3.11Training frameworktorchtitan-npuInference frameworkvLLM vLLM-AscendFilesFileDescriptionREADME_EN.mdThis documentREADME.mdChinese documentationconfig_registry_medical.pytorchtitan-npu Qwen3-30B-A3B medical SFT configrun_medical_sft.shTraining launch script (copy to torchtitan-npu dir before running)prepare_medical_r1_dataset.pyMedical R1 dataset split toolfigures/training_loss.pngTraining loss curve (Epoch 1-5, optimal at step 156)Environment Setup1. Docker ContainerUse an Ascend training image with CANN 9.0.0 and Python 3.11 pre-installed. Example for single-node 16-card setup:docker run -itd \ --device/dev/davinci0 --device/dev/davinci1 \ --device/dev/davinci2 --device/dev/davinci3 \ --device/dev/davinci4 --device/dev/davinci5 \ --device/dev/davinci6 --device/dev/davinci7 \ --device/dev/davinci_manager --device/dev/devmm_svm \ --device/dev/hisi_hdc \ -v /usr/local/dcmi:/usr/local/dcmi \ -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \ -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \ -v /home:/home \ -v /data:/data \ --nethost \ --shm-size128g \ --privileged \ --name qwen3_30b_medical_sft \ cann:9.0.0-a3-openeuler24.03-py3.11 \ /bin/bashInitialize CANN after entering the container. CANN paths vary by deployment method — adjust according to your environment:# Docker image default path source /usr/local/Ascend/ascend-toolkit/set_env.sh # Conda installation path (example for CANN 9.0.0) source /home/developer/Ascend/cann-9.0.0/set_env.sh source /home/developer/Ascend/nnal/atb/set_env.sh # If the system libstdc is too old export LD_PRELOAD/path/to/conda/envs/torchtitan/lib/libstdc.so.62. Install torchtitan-npugit clone https://link.gitcode.com/i/a16fe6012169aa86df6ff4c2d4faa8cd.git cd torchtitan-npu pip install -r requirements.txt pip install -e .DatasetDownloadr1_data_example.jsonlfrom ModelScope and place it in theassetsdirectory of torchtitan-npu:cd /path/to/torchtitan-npu mkdir -p assets # Manually download from # https://modelscope.cn/datasets/krisfu/delicate_medical_r1_data/files # to assets/ ls assets/r1_data_example.jsonlThen split using the provided script:python /path/to/recipe/prepare_medical_r1_dataset.py \ --input ./assets/r1_data_example.jsonl \ --output ./assets/medical_r1Split result:DatasetSamplesUsetrain.jsonl~2,166SFT trainingtest.jsonl~241Keyword Recall evaluationModel WeightsDownloadQwen3-30B-A3Bweights (~60 GB) from ModelScope and create a symlink in the torchtitan-npu source directory:pip install modelscope mkdir -p /data/models/Qwen3-30B-A3B modelscope download \ --model Qwen/Qwen3-30B-A3B \ --local_dir /data/models/Qwen3-30B-A3B cd /path/to/torchtitan-npu mkdir -p assets/hf ln -sf /data/models/Qwen3-30B-A3B assets/hf/Qwen3-30B-A3BTraining ConfigurationConfig RegistrationCopyconfig_registry_medical.pyto the torchtitan-npu source:cp /path/to/recipe/config_registry_medical.py \ /path/to/torchtitan-npu/torchtitan_npu/models/qwen3/config_registry_medical.pyThen append the following totorchtitan_npu/models/qwen3/config_registry.py:from torchtitan_npu.models.qwen3.config_registry_medical import ( sft_qwen3_30ba3b_medical, sft_qwen3_30ba3b_medical_tnd, )Parallelism StrategySingle-node 16-card MoE parallelism (CP2, EP8, TP2):ParameterValueDescriptionNGPU16Total cardscontext_parallel_degree2Context parallelismtensor_parallel_degree2Tensor parallelismexpert_parallel_degree8128 experts sharded along EPpipeline_parallel_degree1PP disableddata_parallel_shard_degree-1FSDP full shard (mesh size 4)HyperparametersConfigRecommended ValueDescriptionsteps156Training steps (5 epochs, ~31 steps/epoch)lr2e-5Learning ratewarmup_steps5Warmup stepslocal_batch_size1Per-device batch sizeseq_len4096Sequence lengthactivation_checkpointselectiveSelective recomputationTRAIN_DATASplit training setTraining data path, set viaTRAIN_DATAenv varMODEL_DIRassets/hf/Qwen3-30B-A3BHF weights pathSample FormatThis example uses the R1 think template format, wrapping the datasetsthinkfield inthinktags:def _process_sample(sample): output fthink\n{sample[think]}\n/think\n\n{sample[answer]} return [ {role: user, content: sample[question]}, {role: assistant, content: output}, ]Attention VariantsConfig functionAttention typeDescriptionsft_qwen3_30ba3b_medicalBSND (SDPA)Reference onlysft_qwen3_30ba3b_medical_tndTND (NPUVarlenAttention)Recommended, validatedUseCONFIGsft_qwen3_30ba3b_medical_tndfor TND variant.TrainingLaunchCopy the launch script to the torchtitan-npu directory and execute:cp /path/to/recipe/run_medical_sft.sh /path/to/torchtitan-npu/ cd /path/to/torchtitan-npu bash run_medical_sft.shThe script uses environment variablesNGPU16andCONFIGsft_qwen3_30ba3b_medical_tndfor the TND variant. Example log output (EP8):step: 1 loss: 1.45426 memory: 37.73GiB(61.58%) tps: 59 69.018s (compilation) step: 2 loss: 1.39178 memory: 52.27GiB(85.31%) tps: 798 5.135s step: 3 loss: 1.26931 memory: 52.31GiB(85.37%) tps: 1215 3.370s step: 10 loss: 1.02183 memory: 52.44GiB(85.59%) tps: 993 4.126s step: 20 loss: 0.95751 memory: 52.44GiB(85.59%) tps: 1199 3.416s step: 31 loss: 0.70617 memory: 52.44GiB(85.59%) tps: 1345 3.046s ← epoch 1 end step: 32 loss: 0.67716 memory: 52.44GiB(85.59%) tps: 701 5.842s step: 50 loss: 0.58786 memory: 52.50GiB(85.69%) tps: 1010 4.056s step: 62 loss: 0.34057 memory: 52.56GiB(85.79%) tps: 1177 3.479s ← epoch 2 end step: 63 loss: 0.33076 memory: 52.56GiB(85.79%) tps: 803 5.102s step: 90 loss: 0.19230 memory: 52.56GiB(85.79%) tps: 733 5.590s step: 93 loss: 0.16940 memory: 52.56GiB(85.79%) tps: 1014 4.040s ← epoch 3 end step: 94 loss: 0.16507 memory: 52.56GiB(85.79%) tps: 1286 3.185s step: 120 loss: 0.08754 memory: 52.62GiB(85.88%) tps: 942 4.349s step: 124 loss: 0.08219 memory: 52.62GiB(85.88%) tps: 1257 3.260s ← epoch 4 end step: 125 loss: 0.08480 memory: 52.62GiB(85.88%) tps: 1274 3.215s step: 150 loss: 0.04411 memory: 52.62GiB(85.88%) tps: 918 4.462s step: 155 loss: 0.04376 memory: 52.62GiB(85.88%) tps: 1199 3.416s step: 156 loss: 0.04450 memory: 52.62GiB(85.88%) tps: 1244 3.292s ← end (epoch 5)Training Loss CurveBased on loss curve analysis,Epoch 5 (step 156) is the optimal stop: loss decline flattens after step 150, and training beyond 186 steps (epoch 6) enters the overfitting regime with no meaningful loss improvement.Model ExportWithlast_save_in_hfTrue, the final checkpoint is exported in HuggingFace format:mkdir -p /data/models/Qwen3-30B-A3B-SFT cp /data/models/Qwen3-30B-A3B/*.json /data/models/Qwen3-30B-A3B-SFT/ cp /data/models/Qwen3-30B-A3B/tokenizer* /data/models/Qwen3-30B-A3B-SFT/ cp checkpoint_medical/step-156/*.safetensors* /data/models/Qwen3-30B-A3B-SFT/Evaluation ResultsEvaluation MethodThis experiment uses a jieba-based keyword extraction method with POS tagging (n, v, a, i, j, l) to extract keywords from both reference answers and model outputs, then computes:Recall matched reference keywords / total reference keywordsPrecision matched reference keywords / total model keywordsF1 harmonic mean of Recall and PrecisionThink Rate proportion of outputs containingthinkreasoningEvaluation data: 241 medical QA samples. Base model, CPT intermediate checkpoint, and SFT model are compared under the same conditions.Keyword Recall ComparisonModelRecallPrecisionF1Base (Qwen3-30B-A3B)53.83%25.16%33.30%CPT Checkpoint (step 156)62.45%28.06%37.82%Improvement8.62pp2.90pp4.52ppOutput Format ComparisonMetricBaseCPTAvg output length1,061 chars831 chars(-21.7%)Format errors (repeated/think)199/2419/241Sample: What are the two components of consciousness?ItemBase ModelCPT CheckpointAnswer/think The components... arousal... content...(Markdown list 3x/think)Consciousness consists of two parts: the content and the switch system...(conversational)Recall52.4%95.2%Length392 chars287 charsTraining MetricsMetricValueStable step time~3.2-3.5sStable memory~52.6 GiB/card (85.9%)Loss start (step 1)1.45Loss end (step 156)0.045Total time (156 steps)~8-9 minutesNote: With CP2, TP2 the memory usage per card is ~52.6 GiB (85.9%).FAQ1. Loss starts abnormally highIf initial loss is significantly higher than expected (e.g., ~12), check whether HF pretrained weights were loaded correctly. Delete the checkpoint directory before re-running:rm -rf checkpoint_medical2. NPU out of memoryCheck for residual processes occupying NPU memory and ensurePYTORCH_NPU_ALLOC_CONFexpandable_segments:Trueis set. If necessary, addtorch.npu.set_per_process_memory_fraction(1.0)at the entry.py entry point.3. HCCL communication timeoutMulti-card training may trigger HCCL watchdog timeout. If intermittent, restarting training usually resolves it. If frequent, check HCCL network configuration and inter-node communication.【免费下载链接】cann-recipes-train本项目针对LLM与多模态模型训练业务中的典型模型、加速算法提供基于CANN平台的优化样例项目地址: https://gitcode.com/cann/cann-recipes-train创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

相关新闻

jqjq核心架构揭秘:词法分析器与解析器设计原理

jqjq核心架构揭秘:词法分析器与解析器设计原理

2026/8/27 20:41:03

jqjq核心架构揭秘:词法分析器与解析器设计原理 【免费下载链接】jqjq jq implementation of jq 项目地址: https://gitcode.com/gh_mirrors/jq/jqjq jqjq是一个用jq语言实现的jq处理器,这是一个极具创意和教育意义的项目。作为一个jq实现&#xf…

CANN/hccl自定义通信算子AllGather

CANN/hccl自定义通信算子AllGather

2026/8/28 13:36:12

Custom Communication Operator - AllGather 【免费下载链接】hccl 集合通信库(Huawei Collective Communication Library,简称HCCL)是基于昇腾AI处理器的高性能集合通信库,为计算集群提供高性能、高可靠的通信方案 项目地址: h…

TestNG入门指南:从零搭建Java自动化测试框架

TestNG入门指南:从零搭建Java自动化测试框架

2026/8/27 11:27:01

1. 项目概述:为什么选择TestNG作为你的第一个测试框架?如果你刚开始接触Java自动化测试,面对JUnit、TestNG这些名词可能有点懵。我刚开始做测试开发那会儿,也纠结过到底该从哪个入手。后来在多个实际项目中摸爬滚打,发…

AI×低代码医疗新范式:信通院推荐背后有何深意?

AI×低代码医疗新范式:信通院推荐背后有何深意?

2026/8/29 1:49:44

在数字化浪潮席卷各行各业的今天,医疗行业的软件系统建设正面临前所未有的挑战与机遇。一边是临床业务对信息化、智能化的迫切需求,另一边是传统软件开发周期长、成本高、迭代慢的固有痛点。当AI遇见低代码,一种全新的开发范式正在医疗领域悄…

LLM API推理轨迹泄露风险与防护:从接口透传到日志权限的全面自查指南

LLM API推理轨迹泄露风险与防护:从接口透传到日志权限的全面自查指南

2026/8/29 1:49:44

Reasoning Traces 现在的讨论热度很高,但大部分人只关心“模型推理能力变强了多少”,很少有人认真想过:如果你把一个商业 LLM API 的响应原样写进日志、原样透传给前端、或者原样喂给下游系统,那内部推理过程很可能就一起泄露出去…

Parallels Desktop 27性能解析:OpenGL提升160%与AI矩阵计算7倍加速

Parallels Desktop 27性能解析:OpenGL提升160%与AI矩阵计算7倍加速

2026/8/29 1:49:44

Parallels Desktop 是 macOS 平台上使用频率很高的一款虚拟机软件,很多开发者、设计师和工程师靠它在 Mac 上运行 Windows 应用。27 版本发布后,公开信息里最引人关注的两个数字是:OpenGL 性能最高提升 160%,AI 矩阵计算最高提升 …

LLM推理轨迹泄露风险与流式API安全防护指南

LLM推理轨迹泄露风险与流式API安全防护指南

2026/8/29 1:49:44

专有 LLM API 的推理轨迹(Reasoning Traces)正在成为一类新的安全研究对象。服务端明明希望隐藏模型的思考过程,客户端却可能通过流式响应、token 用量、完成原因、响应差异等公开信息,把中间推理内容拼凑出来。这个问题不是某个模…

外呼合规系统设计:从同意管理到实时判定

外呼合规系统设计:从同意管理到实时判定

2026/8/29 1:49:44

电话营销,可以说是全球互联网和通讯行业都绕不开的治理难题。最近有一条消息值得所有做外呼系统、用户运营和隐私合规的团队关注:法国正在推进全面禁止未经用户同意的主动电话营销。翻译成开发者能听懂的话就是,过去很多企业习惯的“先打过去…

macOS虚拟机内用llama.cpp跑LLM推理:GPU加速与API服务实战

macOS虚拟机内用llama.cpp跑LLM推理:GPU加速与API服务实战

2026/8/29 1:39:43

这次我们来看一个偏工程向的话题:在 Apple Silicon 的 macOS 虚拟机上,用 llama.cpp 跑 LLM 推理,到底值不值得折腾。重点不是概念解释,而是三个实际问题的答案:虚拟机里跑 llama.cpp 能不能用上 GPU 加速?…

[光学原理与应用-521]:对光的错误理解与纠偏

[光学原理与应用-521]:对光的错误理解与纠偏

2026/8/27 11:10:02

首先光是一种能量的载体和形态,宏观上观察到的光是由无数个微观的光量子组成的,每个光子在产生的瞬间,其在真空的空间中以确定不变的速度沿着一个初始的方向一直向前,在微观层面,每个光量子的运动轨迹是以波函数所展现…

SIP通话转接原理与REFER方法实战解析

SIP通话转接原理与REFER方法实战解析

2026/8/27 7:25:23

1. 通话转接不是“挂断再拨号”,而是SIP会话的动态重定向你有没有遇到过这样的场景:客服坐席A正在和客户通电话,突然需要把这通对话无缝转给专家坐席B,客户完全感知不到中间的断连——既没听到忙音,也没被要求重新拨号…

Kolla-ansible单节点OpenStack部署实战:从环境准备到排坑指南

Kolla-ansible单节点OpenStack部署实战:从环境准备到排坑指南

2026/8/28 7:34:42

1. 为什么选择Kolla-ansible来部署单节点OpenStack?如果你正在寻找一种能把OpenStack从“概念”快速变成“可用的实验环境”的方法,那么Kolla-ansible几乎是当前最主流、最省心的选择。我见过太多人卡在手动编译依赖、配置服务、处理版本冲突的泥潭里&am…

四款热门降AI工具测评:研究生和本科生怎么选?

四款热门降AI工具测评:研究生和本科生怎么选?

2026/8/29 0:09:39

马上要交论文了,最近真的被论文ai率折磨的够呛。 明明查重都没问题了,但是ai率就是居高不下,崩溃了,明明都是我自己写的,天杀的,明明都是我亲生的啊 改来改去,终于给我搞出一套完美的降ai方案…

论文降AI率免费攻略:自查、提示词与工具推荐

论文降AI率免费攻略:自查、提示词与工具推荐

2026/8/29 0:09:39

马上要交论文了,最近真的被论文ai率折磨的够呛。 明明查重都没问题了,但是ai率就是居高不下,崩溃了,明明都是我自己写的,天杀的,明明都是我亲生的啊 改来改去,终于给我搞出一套完美的降ai方案…

北京GEO优化服务商推荐:预算型企业如何选北京GEO优化服务商?

北京GEO优化服务商推荐:预算型企业如何选北京GEO优化服务商?

2026/8/29 0:09:39

前言:预算有限的企业更关心投入能否形成可持续的品牌资产。评估北京GEO优化服务商时,不能只比较单篇内容或单月报价,还要看是否能够把问题词、官网、信源和监测串成完整链路。本期重点放在预算配置、试点范围和交付边界,帮助企业先…

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

2026/8/28 7:35:26

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…

导师推荐!2026最新AI论文工具测评与实用推荐

导师推荐!2026最新AI论文工具测评与实用推荐

2026/8/28 7:34:51

2026年真正好用的AI论文工具,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

告别游戏崩溃:XCOM 2模组管理器的智能革命

告别游戏崩溃:XCOM 2模组管理器的智能革命

2026/8/28 7:34:35

告别游戏崩溃:XCOM 2模组管理器的智能革命 【免费下载链接】xcom2-launcher The Alternative Mod Launcher (AML) is a replacement for the default game launchers from XCOM 2 and XCOM Chimera Squad. 项目地址: https://gitcode.com/gh_mirrors/xc/xcom2-lau…