Merged
feat: DeepSeek V4.1 main KV 的C4复用 #899
wanghui0Add on Sep 22
feat: DeepSeek V4.1 main KV 的C4复用 #899
Merged
Pull Request has been successfully merged by CANN-robot.
(Thank you for wanghui0's contribution.)atomgit-bot
Sep 22 Comments:
Sep 22 Comments:
变更摘要
本 PR 为 DeepSeek V4.1 引入主 KV 复用能力:将主 KV 的 MXFP4 伪量化从稀疏注意力内部迁移到源 Compressor,并由其产生的共享主 KV 供所有消费层复用,避免重复量化/反量化。为此新增了通用 KV 缓存伪量化算子 fake_quantize_mx_bf16、模块级配置 QuantCompressorConfig 与模块 QuantCompressor,并重构 v41_sparse_attention.py 使其只负责 SWA 的 FP8 伪量化、主 KV 直接透传,最后在 torchao_converter.py 中把源 Compressor 纳入量化转换链路。
主要改动
- 新增 KV 缓存伪量化算子: 新增
torchao_npu/ops/kv_cache_ops.py,提供fake_quantize_mx_bf16,通过_MXBF16FakeQuantize自定义 autograd 函数调用kv_compress_epilog_v2完成打包并用dequantize_mx_bf16反量化,支持mxfp4_bf16/mxfp8_bf16两种模式,反向为恒等 STE。 - 新增主 KV 复用模块与配置: 新增
QuantCompressor(quantized_modules/compressor.py)与QuantCompressorConfig(configs/module_swap_configs/quant_compressor.py,group_size限定 16/32),源 compressor 对主 KV 做 FP4 伪量化,复用层(is_source为 False)直接返回同一共享张量而不再次量化。 - 重构 V4.1 稀疏注意力量化逻辑:
v41_sparse_attention.py删除_fake_quantize_kv、_quantized_cache_width等内部实现,改为仅对 SWA 调用fake_quantize_mx_bf16,主 KV(cmp_k)直接传入内核并在前向、反向保存与复用同一份量化结果,ori_kv由swa_k改为量化后的qdq_swa_k。 - 转换器接入源 Compressor:
interfaces/torchao_converter.py新增V41Compressor.Config目标类型、_is_v41_kv_source过滤函数与_DSV41_CONFIG_FILTERS["compressor"],并在enable_sparse_attention_quantization时追加QuantCompressorConfig转换器。 - 对外接口与配置导出:
torchao_npu/__init__.py、configs/__init__.py、configs/module_swap_configs/__init__.py导出QuantCompressorConfig;torchtitan_npu/config/configs.py为enable_sparse_attention_quantization补充说明文档字符串。


Incorrect?
atomgit-bot
Sep 22 Comments:
Sep 22 Comments:
Sep 22 Added label: cann-cla/yes
CANN-robot
Sep 22 Comments:
Sep 22 Comments:
48 messages are collapsed here。 See more
Sep 22 Added label: lgtm
Sep 22 Merged pull request
CANN-robot
Sep 22 Comments:
Sep 22 Comments:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


26 days ago Associated issue: [RFC] DeepSeek V4.1 支持 compress KV C4 和 swa KV C8
CANN-robot
26 days ago Comments:
26 days ago Comments:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
将cmp_k的伪量化行为从消费位置(smla)移到生产位置(compressor),compressor 模块会根据是否是 kv_source_layers 来创建新的 cmp_k,非 kv_source_layers 层会进行复用之前的cmp_k。 结果上,cmp_k的伪量化发生位置从全部40层减至第2、8、14、20层。
类型
Checklist:
如何测试
采用与之前相同的测试方式:DeepSeek V4.1模型 开启--extension.quantization.enable-sparse-attention-quantization 后进行cmp_k的C4和swa_k的C8伪量化。功能与改动前一致,性能收益如下(seq_len=512):

其他信息
在这里可以添加任何与这个 Pull Request 相关的其他说明。