Skip to content

feat(csa2): support low-precision KV/Indexer and allowlisted Linear schemes in CSA2 family - #1641

Open
yooooo00 wants to merge 1 commit into
mindspore-ai:masterfrom
yooooo00:feat/csa2-quant-primitives
Open

yooooo00 wants to merge 1 commit into
mindspore-ai:masterfrom
yooooo00:feat/csa2-quant-primitives

Conversation

@yooooo00

@yooooo00 yooooo00 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Paired: GitHub #1641 ↔ GitCode !2010

Summary

Following the merge of PR #1606 (CompressedSparseAttention2 Module Family), this PR unifies the CSA2 quantization primitives and the CompressedSparseAttention2 Module Family low-precision integration into a single clean feature commit for single-card (TP=CP=PP=1, EP>=1) training:

  1. Dedicated Low-Precision CSA2 Family Member & Dual-Layer Allowlist:
    • Introduces LowPrecisionCompressedSparseAttention2Module(CompressedSparseAttention2Module) and registers "precision": SelectorSpec(str, "inherit") with explicit low_precision_reference / low_precision_fused rules in CompressedSparseAttention2's ModuleFamily allowlist (variant_rules), keeping baseline CompressedSparseAttention2Module pure floating-point math and leaving CPCompressedSparseAttention2 untouched for follow-up CP expansion.
    • Defines DEFAULT_LINEAR_ALLOWLIST (q_a_proj, q_b_proj, kv_proj, o_b_proj, compressor.wkv, compressor.wgate, indexer.q_b_proj, indexer.wk) and delegates internal 2D projection replacement to the Linear Module Family (MXFP8 and HiFloat8 via linear_dtype_scheme), while explicitly preserving o_a_proj (3D grouped torch.bmm) and indexer.weights_proj (small head-merge gate) in BF16.
  2. CSA2 Physical Low-Precision Cache & Indexer QAT Primitives:
    • Adds CSA2IndexerQKFormat, CSA2MainKVFormat, and CSA2SWAKVFormat to LowPrecisionConfig (indexer_qk, main_kv, swa_kv).
    • Implements CSA2CacheTensor (QuantizedTensor physical uint8 packed storage without floating shadow copies) with full-bank dequantize(), sparse row-selected dequantize_selected(indices) for reference Indexer KL teacher reads, and straight-through estimator (STE) gradients.
    • Implements MainKVCacheQATStrategy (FP4 E2M1 + group-16 E4M3/E8M7 scales), SWAKVQATStrategy (FP8 E4M3 + group-32 E8M0/E8M7 scales), and MXFP4FakeQuantPolicy (OCP MXFP4 query QDQ + single-encode Full-layer Indexer K packed cache).
  3. Supporting Linear & Native SMLA Fixes:
    • Registers "hif8" (replace_hif8_linear) in Linear Module Family and adds 0-Token empty-slice short-circuit protection in LinearStrategy forward/backward.
    • Completes sparse_flash_mla_grad_metadata and pure-SWA cmp_mask_mode=0 handling in sparse_flash_mla.py.

Verification

  • CPU Unit Tests & Static Gates:
    • pytest across tests/ut/components/quantization/, test_shared_compressed_dsa_attention.py, test_csa2_recompute_contract.py, test_sparse_lightning_indexer_kl_loss_grad.py, test_linear_family.py, test_family_import_compatibility.py, test_module_build.py, test_replacement_initialization.py, and test_import_boundaries.py: all passed.
    • scripts/pre-commit/run_pylint.py, incremental scripts/pre-commit/run_lizard.py, and .agent/scripts/check_agents_catalog.py: 0 findings.
  • Ascend 950PR NPU Verification:
    • tests/ut/components/quantization/test_width_matrix.py (TestWidthMatrixNPU): 6 passed (13 subtests passed).
    • tests/torch/expert_parallel/test_linear_family.py: 1 passed.
    • 4-layer DeepseekV41ForCausalLM (SWA-only, Full, Reindex, Reuse roles) end-to-end training smoke across MXFP8 / HiFloat8 × use_fused_ops=False/True + e8m7 scales: all 5 configurations passed with finite decreasing losses and verified allowlisted LowPrecisionLinear replacements.

@yooooo00
yooooo00 force-pushed the feat/csa2-quant-primitives branch from b61d6f7 to 2bac04f Compare November 25, 2026 08:59
@yooooo00 yooooo00 changed the title feat(quantization): add CSA2 KV cache and Indexer Q/K quantization primitives feat(csa2): support low-precision KV/Indexer and allowlisted Linear schemes in CSA2 family Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant