尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

AMCT Qwen3 MoE 大模型压缩适配实战:Qwen3-30B-A3B 与 Qwen3-235B-A22B 的 W8A8 直转量化方案

AMCT Qwen3 MoE 大模型压缩适配实战:Qwen3-30B-A3B 与 Qwen3-235B-A22B 的 W8A8 直转量化方案 AMCT Qwen3 MoE 大模型压缩适配实战Qwen3-30B-A3B 与 Qwen3-235B-A22B 的 W8A8 直转量化方案【免费下载链接】amctAMCT是CANN提供的昇腾AI处理器亲和的模型压缩工具仓。项目地址: https://gitcode.com/cann/amct本文以 CANN AMCT 仓库中的 Qwen3 MoE 适配 casebookqwen3-moe.md为骨架结合仓库内模型 adapter 与量化模块源码qwen3_moe.py、moe_common.py以及单元测试系统梳理 Qwen3 系列 MoE 模型在 AMCT 上的压缩适配路径。读者可据此掌握MoE 大模型在 AMCT 中的 checkpoint 加载与 expert 重组原理、attn-linear moeW8A8-int8 直转量化的完整方案、以及 30B 与 235B 两档规模的精度结论与工程陷阱。一、案例背景与触发信号本案例对应 AMCT 的Qwen3Moe模型适配分支覆盖两个开源权重规格Qwen3-30B-A3B48 层 decoder、128 个 expert、top-8 路由、moe_intermediate768、32 个 attention head、kv_heads4Qwen3-235B-A22B94 层 decoder注意config 中谎报为 5 层。两者均属 MoE decoder 结构且 checkpoint 采用逐 expert 展开存储mlp.experts.i.{gate_proj,up_proj,down_proj}.weight而运行时 MoE 模块期望packed 张量因此适配的核心矛盾在于磁盘展开 → 运行时 packed的转换。触发信号判断是否落入本案例config 中num_experts 0checkpoint 中包含mlp.experts.i.*形态的 key。命中触发信号后应叠加阅读 AMCT 的两层通用经验库跨网络通用陷阱 cross-model-pitfalls.mdL1与按结构分类的 MoE 家族陷阱 structure-family-pitfalls.mdL2。二、模型结构与适配要点2.1 结构速览Qwen3Moe是一个标准的 Qwen3 MoE decoderattention 部分与 dense Qwen3 一致差异集中在 FFN 侧由mlp.gateroutermlp.experts128 个或 256 个 expert构成。适配层面的关键事实是checkpoint 中每个 expert 的gate_proj/up_proj/down_proj是独立张量运行时模块transformers 的Qwen3MoeSparseMoeBlock要求 expert 权重以packed形式存在gate_up_proj将 gate 与 up 拼接down_proj单独打包。2.2 参考路径与差异235B 为首条适配记录参考了 dense Qwen3 的适配思路 LongCat 系列的 packed-expert 处理思路30B 参考 235B同 MoE packed-expert 思路仅规模较小48 层 / 128 expert vs 94 层 / 256 expert 量级。两条记录共用同一个Qwen3Moeadapter 类无结构差异属规模不同、逻辑复用的关系。这一点在仓库的 Qwen 系列总览页.agents/docs/casebook/qwen/README.md中有明确归类qwen3_moe分支的既定特征是checkpoint 上是展开的 per-expert 权重运行时期待 packed experts。2.3 复用与新增模块复用BaseModel的 blockwise 前向框架、PtqUnitattn / moe 单元、QuantLinear新增QuantQwen3MoeAttn——attention 量化 wrapperQuantGatedExperts即 packed-expert 量化容器源码中亦有QuantPackedExperts称谓——对 packed experts 的量化包装PackedExpertView——惰性视图按 expert 索引切分 packed 张量pack_gated_expert_weights——checkpoint 加载时的 expert 重组函数。对应实现位于 amct_pytorch/common/models/llm/qwen/qwen3/qwen3_moe.py 与 amct_pytorch/common/models/llm/qwen/moe_common.py。2.4 checkpoint 加载时的 expert 重组在 qwen3_moe.py 中load_layer_weight在拿到原始 state_dict 后立即调用pack_gated_expert_weightsdef load_layer_weight(self, prefix): state_dict super().load_layer_weight(prefix) state_dict pack_gated_expert_weights(state_dict, expert_prefixmlp.experts) return state_dictpack_gated_expert_weightsmoe_common.py的核心逻辑是用正则mlp\.experts\.(\d)\.(gate_proj|up_proj|down_proj)\.weight$扫描 state_dict按 expert 索引归类三个投影权重将同一 expert 的gate_proj与up_proj沿 dim0 拼接为gate_up_projdown_proj单独保留所有 expert 沿 dim0 stack 成 packed 张量mlp.experts.gate_up_proj与mlp.experts.down_proj若三个投影集合不一致缺失某个投影直接抛KeyError: Inconsistent expert weights while packing做 fail-fast 而非静默产出错误权重。这也解释了为何 L2 经验库强调先确认 transformers 版本expert 布局随 transformers 版本可能变化packed 重组逻辑必须与运行时模块的期望对齐。三、适配验证结论三步闭环两条记录均按 AMCT 标准三步闭环完成验证BF16 blockwise baseline → 关闭量化浮点等价验证 → 最小 PTQ smoke。3.1 Qwen3-30B-A3B三步闭环全部通过关闭量化后attnmoe与 BF16 对齐差异约-0.005在可接受浮点漂移范围最小 PTQ smoke 在block0/moe上打通覆盖 128 个 expert 的枚举、并选中实际命中的 expert进行校准对应 L2 中用真实 hidden states 过 gate、按实际命中 expert 做 capture的经验。3.2 Qwen3-235B-A22B三步闭环通过关闭量化后attn取 0/47/93 层抽样与 BF16精确对齐moe仅存在 BF16 级微漂最小 PTQ smoke 在layer0/expert_0上打通。注意attention 单元的量化验证覆盖了通用 PTQ provider 的局限——L1 经验库 cross-model-pitfalls.md 明确记录通用 PTQ provider 是 tensor-only不覆盖 attention unit还需position_embeddings/attention_mask因此本案例的最小 PTQ smoke 先以 expert/mlp 单元闭环attention 单元的 PTQ 需泛化 provider 支持额外上下文。四、关键陷阱与规避通用坑的典型案例本系列遇到的均为可迁移的通用坑已上抽到 L1/L2 经验库无Qwen3Moe专属、不可迁移的 L3 坑。以下五条全部有仓库证据可查config 层数 ≠ checkpoint 真实层数235B 的 config 报 5 层、真实 94 层。根因是 config 字段不可信处理方式是先拉model.safetensors.index.json用事实定层数与 load 路径见 cross-model-pitfalls.mdL1 · checkpoint 是唯一事实源。expert 磁盘展开 vs 运行时 packedstate_dict key / expert 权重形状与运行时模块不一致处理方式即上文pack_gated_expert_weights的重组见 structure-family-pitfalls.mdL2 · MoE 类。MoE activation capture 误命中mlp.gate30B 上宽泛hook_namemlp同时匹配mlp.gate而 gate 返回 tuplecapture 时报AttributeError: tuple object has no attribute detach。处理用真实 hidden states 过 gate、只对实际命中的 expert 单元做 capture/PTQ不按名宽抓见 structure-family-pitfalls.mdL2 · MoE 类。初始 BF16 PPL 离谱235B 初始 PPL466根因是 blockwise 路径漏传attention_mask修正后 chunk0 loss 从 5.47 回落到 1.23。处理BF16 PPL 一旦离谱第一件事查 blockwise mask/pos 传递链见 cross-model-pitfalls.mdL1 · BF16 PPL 异常。通用 PTQ provider tensor-only不覆盖 attention unit见上节说明出自 cross-model-pitfalls.mdL1 · 量化通用。4.1 量化目标合法性校验源码佐证adapter 在parse_quant_mode中做了量化目标合法性校验qwen3_moe.pydef parse_quant_mode(self): if mlp in self.quant_target: raise ValueError( Qwen3-MoE is a moe model and does not support quant_targetmlp. )即 MoE 模型的 FFN 侧量化目标必须写作moe误开quant_targetmlp会直接报错。对应的单元测试见 tests/unit_test/common/models/llm/qwen/test_qwen3_adapters.pytest_qwen3_moe_rejects_mlp_target断言抛ValueErrortest_qwen3_moe_accepts_moe_and_attn_targets验证moe/attn-linear/attn-cache均可接受。此外该测试文件还覆盖了get_layer_weight_prefix返回model.layers.i.前缀的断言印证权重索引路径。4.2build_quant_block的分支装配源码佐证build_quant_block 展示了量化的装配逻辑def build_quant_block(self, layer_idx): decoder_layer self.block(layer_idx) if attn-linear in self.quant_target or attn-cache in self.quant_target: apply_quant_to_attn(self.args, decoder_layer, QuantQwen3Attn) if moe in self.quant_target: mlp getattr(decoder_layer, mlp, None) if mlp is not None and hasattr(mlp, experts): mlp.experts QuantGatedExperts(self.args, mlp.experts) elif mlp is not None: decoder_layer.mlp QuantQwen3MLP(self.args, mlp) return decoder_layerattention 目标attn-linear/attn-cache由apply_quant_to_attnQuantQwen3Attn完成MoE 目标则优先用QuantGatedExperts包装mlp.expertspacked-expert 路径仅当模块形态不满足时回退到QuantQwen3MLP。在 moe_common.py 中QuantGatedExperts通过GatedExpertView(..., materializeFalse)以惰性视图方式从 packed 张量中切出每个 expert 的权重再逐个包装为QuantGatedMLP同时提供build_ptq_expert_module(expert_idx)materializeTrue实体化为真Parameter的 PTQ 路径与iter_ptq_expert_modules()前向时按 top-k 命中情况index_add_聚合专家输出。部署绑定iter_deploy_bindings则将mlp.experts.expert_modules.i.proj重新映射回mlp.experts.i.proj.weight的原始 checkpoint key。五、量化结论与性能注意5.1 首推方案attnmoeW8A8-int8 直转Qwen3-30B-A3Bdelta -0.016量化 PPL 8.0280 vs BF16 8.0444Qwen3-235B-A22Bdelta 0.0363量化 PPL 5.1427 vs BF16 5.09。两者均满足 AMCT 默认接受阈值delta ≤ 0.2无需 PTQ 即可直转部署。已落地量化粒度量化对象位宽配置量化粒度Attention Linearq/k/v/o 投影A8W8INT Per-TokenMoE Expert不含 gate/routerA8W8INT Per-Channel5.2 性能注意MoE per-expert 动态量化收益存疑这是本案例最重要的工程提醒MoE per-expert 动态量化在 prefill 场景下平均每个 expert 的有效 token 数M_eff ≈ seq × topk / num_experts偏小——30B 上约为4096 × 8 / 128 ≈ 256。单 expert 的dynamic_quant 头开销可能超过 BF16 matmul 本身再乘以层数 × expert 数会放大为负收益而 attention 投影 / MLP 路径通常是正收益。因此MoE 是否纳入量化首要看精度 delta 下游 infer 路径覆盖性能必须由 infer 侧 packedMoEGMM实测确认decode 阶段M1为 weight-bound收益可能翻正。完整分析见 structure-family-pitfalls.mdL2 · MoE per-expert 动态量化的 prefill 性能。六、适配建议下一条同系列 / 同结构模型6.1 起步复用清单从以下三个文件起步首轮直转目标为attnmoe的 a8w8-int8amct_pytorch/common/models/llm/qwen/qwen3/qwen3_moe.pyadapteramct_pytorch/common/models/llm/qwen/moe_common.pyQuantGatedExpertspack_gated_expert_weights复用抽象BaseModelblockwise、PtqUnitattn/moe、QuantLinear。6.2 先做什么先验 checkpoint 真实深度 expert 布局先拉model.safetensors.index.json列顶层 key 代表层 key 模式用事实定层数与 load 路径不靠 config 深度或命名推断BF16 baseline 离谱先查 blockwiseattention_mask先看 chunk0 loss 是否回到合理量级235B 案例为 5.47 → 1.23再全量 PPL首轮attnmoea8w8-int8 直转关闭量化bits16先验 wrapper 与 BF16 等价再合并直转delta 0.2或做过粗粒度定位后再升级 PTQ。6.3 不建议做的事盲信config.num_hidden_layers——235B config5 层、真实 94 层写 expert loader 前不比对磁盘 key——先确认 checkpoint 是展开还是 packed 存储以及当前 transformers 版本的布局按名宽抓 capture——hook_namemlp会撞上mlp.gate返回 tuple 导致 detach 报错须按实际命中 expert 选单元未确认 inferMoEGMM前给 MoE 性能结论——per-expert 动态量化在 prefill 下可能为负收益性能结论必须以 packed MoE GMM 实测为准。七、精度速查表ppl 口径seq_len4096Wikitext。MXFP 双值为两次评测口径 a/b。7.1 Qwen3-30B-A3BBF16 PPL 8.0444数据类型量化配置量化算法pplBF16无无8.0444INTA8w8: moe, attn-linear无8.02807.2 Qwen3-235B-A22BBF16 PPL 5.09数据类型量化配置量化算法pplBF16无无5.09INTA4w4: moe, attn-linear无46.3021INTA8w4: moe, attn-linear无5.71INTA8w8: moe, attn-linear无5.1427MXFPMoE Expert A8W8无5.115MXFPAttn A8W8无5.12MXFPMoE Expert A4W4无5.52MXFPAttn A4W4无5.40MXFPA4w4: moe, attn-linear无5.82/5.656MXFPA8w4: moe, attn-linear无5.417/5.368MXFPA8w8: moe, attn-linear无5.1267/5.135从速查表可读出的关键规律INT 侧A4w4 在 235B 上完全不可用PPL 46.3A8w4 已有明显改善5.71A8w8 是 INT 侧最稳选择5.1427delta0.0363MXFP 侧MoE Expert 与 Attn 各自 A8W8 均在 5.11~5.12 区间整体略优于 INT A8W85.1427A4W4 类配置5.4~5.82仍可用但精度劣于 8 位双值口径a/b的波动在 0.01 量级说明评测稳定性良好。八、延伸阅读Qwen 系列总览含 dense / MoE / 混合 attention 全分支归并.agents/docs/casebook/qwen/README.mddense 对照案例attn-linear mlpW8A8 直转4B/8Bqwen3-dense.md跨网络通用陷阱L1.agents/docs/casebook/cross-model-pitfalls.md结构家族陷阱 · MoE 类L2.agents/docs/casebook/structure-family-pitfalls.md模型 adapter 实现qwen3_moe.py、moe_common.py量化模块实现quant_module.pyQuantQwen3Attn单元测试test_qwen3_adapters.py【免费下载链接】amctAMCT是CANN提供的昇腾AI处理器亲和的模型压缩工具仓。项目地址: https://gitcode.com/cann/amct创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表