Skip to content

Implemented MXFP8 quant lightning indexer for DSA - #40591

Open
mvbakulin wants to merge 3 commits into
sgl-project:mainfrom
mvbakulin:b00920894_quant_li
Open

mvbakulin wants to merge 3 commits into
sgl-project:mainfrom
mvbakulin:b00920894_quant_li

Conversation

@mvbakulin

@mvbakulin mvbakulin commented Sep 21, 2026 •

Copy link
Copy Markdown

Motivation

On Ascend NPU, the DSA Lightning Indexer (LI) already runs quantized when the KV cache is FP8, but it quantizes per token (one FP32 scale per token) and calls torch_npu.npu_quant_lightning_indexer (v1).

This PR moves the quantized indexer to MXFP8 (microscaling FP8: one E8M0 scale per 32-element block) and to the v2 kernel from CANN ops-transformer. Block scales lose less accuracy than one scale per token, so the quantized indexer gets faster without the accuracy drop (see results below).

The v2 kernel is two ops:

  1. torch.ops.cann_ops_transformer.quant_lightning_indexer_metadata plans the load balance (a task list) from the batch shape only. That plan is the same for every indexer layer, so it runs once per forward, not once per layer.
  2. torch.ops.cann_ops_transformer.quant_lightning_indexer does the indexer computation using that plan.

MXFP8 (quant_mode=3) in the v2 kernel is supported on Ascend 950 only.

Modifications

hardware_backend/npu/attention/ascend_backend.py

  • New _plan_quant_indexer_metadata, called from init_forward_metadata. It builds cu_seqlens_q (int32, 0-prefixed, (b+1,)), seqused_k (int32, (b,)) and the metadata tensor, and stores all three on ForwardMetadata for every indexer layer to reuse.
  • Planning is skipped when the quant indexer is off, and for context-parallel extend, which takes a different indexer path and never calls the v2 kernel.

layers/attention/dsa/dsa_npu_indexer.py

  • _quantize_npu_indexer_activation now uses npu_dynamic_mx_quant and returns E8M0 block scales shaped (..., d/64, 2), the descale layout the v2 kernel expects.
  • The v1 npu_quant_lightning_indexer call is replaced by the v2 quant_lightning_indexer call (layout_k PA_BSND → PA_BBND, cu_seqlens_q / seqused_k / metadata arguments, weights cast to fp32).
  • If the backend did not pre-plan the metadata (for example under graph capture), the indexer computes it inline.
  • New _check_quant_lightning_indexer_constraints: PA_BBND requires a page size in [16, 1024] that is a multiple of 16. A wrong page size now fails with a clear message (use --page-size 64) instead of an error inside the kernel.
  • Imports cann_ops_transformer on NPU so the torch.ops.cann_ops_transformer namespace is registered.

hardware_backend/npu/memory_pool_npu.py

  • The index-K scale cache changes from one FP32 value per token to MX scales: uint8 storage, per-token shape (k_n, d/64, 2). Each token still takes 4 bytes per indexer layer, so KV pool capacity does not change.
  • get_index_k_scale_buffer returns a float8_e8m0fnu view of that storage. set_index_k_scale_buffer writes whole per-token scale rows and asserts that the scale is 1 byte per element.

mem_cache/pool_host/mla.py (HiCache)

  • The host mirror of the scale cache takes its shape and dtype from the device pool, so the two match byte for byte. The size per token is still 4 bytes, so host pool sizing and page pointer arithmetic do not change. Only the comments are updated.

Tests

  • test_npu_ascend_backend.py: per-forward metadata planning for plain extend and decode, int64 → int32 cast, and the CP-extend and disabled cases are skipped.
  • test_npu_quant_lightning_indexer.py (new): page-size constraint check, MX quantization shapes, dtypes, empty input and dequant round-trip, and smoke tests for the metadata op and the full kernel.
  • test_npu_dsa_index_k_scale_pool.py (new): scale-cache layout, E8M0 view, set/get round trip across pages, per-layer slot isolation, and that the HiCache host mirror matches the device pool.

Accuracy Tests

GSM-8K, FP (baseline)
image

GSM-8K, KV/LI MXFP8
image

Speed Tests and Profiling

Len=256K, FP16
fp16

Len=256K, KV/LI MXFP8
mxfp8

@github-actions github-actions Bot added hicache Hierarchical Caching for SGLang npu quant LLM Quantization labels Sep 21, 2026
TamirBaydasov added a commit to TamirBaydasov/sglang that referenced this pull request Sep 24, 2026
PR sgl-project#40591's v2 operator takes a pre-planned task list instead of per-call
lengths, and AscendAttnBackend plans it once per forward from the whole
batch. The DSA indexer query shard hands each attention-TP rank 1/tp of the
prefill rows. Each is correct alone; together the operator was told to walk
sum(extend_lens) rows through a tensor holding ceil(total / tp_size) of them.

0c64af9 fixed that -- plan from the shard's own lengths, cached on the
shard -- but shipped it as a merge resolution with no test. Neither feature's
CI can reach the bug: the query shard needs no quant pool and the quant
indexer needs no shard, so both suites stay green while the combination reads
out of bounds. The kernel reports it as an AICore trap with no Python frame
naming either feature.

The plan is integer arithmetic, so the invariant tests on CPU:
cu_seqlens_q[-1] is how many rows of query the kernel reads, and it must
never exceed the rows the caller passed. Asserted over all 180 rank/shape
combinations, against the real plan_indexer_query_shard.

test_the_batch_wide_plan_would_read_past_the_query_tensor is the control: it
builds the plan the pre-fix code built and asserts the overrun, so the file
still means something if the two ever agree again.

This test belongs with the fix. Both move down to dcp_vllm_port once sgl-project#40591
is in main, since that branch merges second and carries the integration.

Co-Authored-By: Claude Opus 5 <[email protected]>
TamirBaydasov added a commit to TamirBaydasov/sglang that referenced this pull request Sep 25, 2026
…words it

No code change: the comment only.

The same eight lines are being upstreamed on their own, off origin/main, as
"[NPU] Elide index-K for non-indexer DSA layers on every die, not only 950".
The code there is byte-identical to the code here, but the comment was not --
this branch carried a NOTE(dcp-port) aimed at readers who already know what the
port is, and the standalone one explains the arch gate to a reviewer who does
not.

Left alone, that difference would have stopped the merge. When the standalone
PR lands and this branch next merges main, git would meet two versions of one
comment block over identical code and raise a conflict for a change that is
semantically nothing -- at a moment when nobody is looking for one. Same shape
as the PR sgl-project#40591 trap: a merge that ought to be a no-op and is not.

Matched exactly, so the merge resolves to nothing at all.

The ~222 GiB-on-A3 figure the old comment carried is not lost. It is in the
standalone commit's message with the measurement behind it (a live pool build
at 32,768 tokens: 3.365 GiB dense against 2.918 GiB compact, which is exactly
57 layers x 257 pages x 32,768 B), which is where an upstream reviewer will
look for it.

Note this branch keeps the elision rather than dropping it: A3 needs it to run,
and it lives inside merge commit fe37540, so removing it would mean
rewriting merge history for eight lines.

Co-Authored-By: Claude Opus 5 <[email protected]>
OrangeRedeng pushed a commit to OrangeRedeng/sglang that referenced this pull request Sep 29, 2026
Merge PR sgl-project#40591 (MXFP8 quant_lightning_indexer v2) onto the DCP branch and
fix the one place where the two features disagree.

The v2 operator takes cu_seqlens_q / seqused_k / a pre-planned metadata task
list, and AscendAttnBackend plans them once per forward from the whole batch.
Stage A hands each attention-TP rank 1/attn_tp_size of the prefill query rows.
The quant branch read the batch-wide plan and dropped the shard's own lengths,
so the operator was told to walk the full batch's token count through a query
tensor holding attn_tp_size times fewer rows.

Nothing catches that on the host: the lengths are device tensors, so
quant_lightning_indexer_tiling.cpp cannot check them against the query shape,
and the kernel surfaces the out-of-bounds read as an AICore trap
("timeout or trap error, subErrType 0x4") from inside the operator.

- plan cu_seqlens_q / seqused_k / metadata from the shard's lengths when a
  shard is active, cached on the shard so the other 77 layers reuse it
- skip the backend's per-batch pre-plan for sharded extend batches, so a plan
  that cannot be used is not built
- keep the shard's top-k all-gather on the quant path; the PR's structure
  returned before it
- drop the per-layer torch.npu.synchronize() debug leftover

Co-Authored-By: Claude Opus 5 <[email protected]>
TamirBaydasov added a commit to TamirBaydasov/sglang that referenced this pull request Oct 3, 2026
No textual conflicts. Checked first: sgl-project#37787 came in with the previous
merge, and sgl-project#40591 is still not in main (dsa_npu_indexer.py calls the v1
npu_quant_lightning_indexer), so the indexer-shard trap is not live.

One semantic break, fixed here: sgl-project#41818 replaced --enable-dp-attention
with --attn-dp-size. handle_deprecated_dp_attention now runs first and
rewrites the old spelling to attn_dp_size with dp_size=1, so our DCP
nesting check in handle_decode_context_parallelism, which still read
dp_size and enable_dp_attention, saw attn_dp_size=1 and let a DCP group
straddle two DP replicas. It now reads attn_dp_size. Its tests use the
new flag.

Also renamed cache_unfinished_req to checkpoint in two comments; the
partial-page behaviour they describe is unchanged.

Local CPU tests: the same failures before and after the merge (11 in
test/registered/dcp + test_npu_dcp, 5 in test_server_args); none are new.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TamirBaydasov added a commit to TamirBaydasov/sglang that referenced this pull request Oct 5, 2026
No textual conflicts. sgl-project#40591 is still not in main (dsa_npu_indexer.py
calls the v1 npu_quant_lightning_indexer), so the indexer-shard trap is
not live.

Checked the eight files both sides touched. sgl-project#42481 drops make_stages
and the _stage_* fields from DeepseekV2DecoderLayer; nothing on this
branch uses them. sgl-project#42348's handle_deprecated_dp_attention change still
leaves attn_dp_size set before our DCP nesting check reads it. sgl-project#42041's
ForwardBatch change is gated on upstream CP metadata, not DCP.

Local CPU tests (test/registered/dcp, test_npu_dcp, test_server_args):
the same 5 failures before and after the merge; none are new.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@Makcum888e Makcum888e left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Bakulin Mikhail added 3 commits October 26, 2026 11:43
Replaced torch_npu.npu_quant_lightning_indexer() kernel with
torch.ops.cann_ops_transformer.quant_lightning_indexer ((version 2 kernel)
preceded by torch.ops.cann_ops_transformer.quant_lightninf_indexer_metadata()
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Required checks failed on 446a997; this PR cannot be merged until they pass.

PR title (run)

PR title must use "[Type] scope: title" or "[Type] [Hardware] scope: title". Type must be one of: Feat, Fix, Refactor, Perf, Doc, Test, CI, Chore. Scope is required and may be any module or model name, with unrestricted letter case (letters, numbers, dots, underscores, slashes, or hyphens). Hardware is optional; use a single tag without spaces. Use uppercase hardware tags (AMD, NVIDIA, NPU, MI300X), except for exactly these four spellings: Intel, Apple, Huawei, Ascend. Reverts must use GitHub's default format: Revert "original title". Use one space between fields and after the colon, with a non-empty title and no leading or trailing whitespace. Examples: "[Fix] attention: Fix KV cache indexing", "[Feat] qwen3: Add model support", "[Perf] [AMD] flash-attn: Optimize decode performance".
Edit the title; the check reruns on its own.

Lint (run)
Run pre-commit run --all-files locally and push the result. The run log shows any other failing step.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hicache Hierarchical Caching for SGLang npu quant LLM Quantization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants