Skip to content

[Fix] dsv41: Size prefill indexer score tiles by memory in use to avoid OOM without shrinking them - #43376

Closed
ormandj wants to merge 3 commits into
sgl-project:mainfrom
ormandj:dsv41-indexer-prefill-tile-budget
Closed

ormandj wants to merge 3 commits into
sgl-project:mainfrom
ormandj:dsv41-indexer-prefill-tile-budget

Conversation

@ormandj

@ormandj ormandj commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The DeepSeek-V4.1 ratio-1/2 index layers score every prefill row against the whole compressed context with DeepGEMM fp8_fp4_mqa_logits, in row tiles (score_tiles / flat_index_logits_tiles). Each tile is rows x max compressed context fp32, allocated through the caching allocator outside every pool sized by --mem-fraction-static.

  1. OOM with a fixed budget. The tile budget was a fixed 2 GiB (_DEEP_GEMM_SCORE_BUDGET_BYTES), and it counted only the logits: the dense-blocks publisher also allocates a bool causal mask, select_candidate_block_ids padded an unaligned context (F.pad) into a full fp32 copy of the tile, and the sparse-table publisher kept the previous tile and its block keys alive while the next was allocated. At 485K a 2 GiB tile peaked at about 4.2 GiB. On 4x RTX PRO 6000 Blackwell (SM120, about 95 GiB each), TP4, --mem-fraction-static 0.85, chunked prefill 8192, a conversation with about 485K cached tokens growing by small chunks OOMed in _publish_prefill_blocks -> score_tiles -> fp8_fp4_mqa_logits (2.00 GiB requested, 1.98 GiB free).
  2. Free memory alone is the wrong budget. Sizing tiles from mem_get_info free memory (SGLANG_DSA_MQA_LOGITS_FREE_MEM_FRACTION of it) avoids the OOM but leaves out the caching allocator's unused blocks. A long-running server's allocator ends up holding nearly all free memory (measured on the setup above: 0.05 GiB free to the driver, 91.9 GiB reserved, 83.0 GiB allocated), so the budget falls to about 10 MiB and deep chunks run in one-row-group tiles (4 rows at 32 index heads), thousands of DeepGEMM calls per chunk. In production this showed as cold prefill decaying chunk by chunk to about 1K tok/s past 300K tokens of depth.

Related: #40967, #31704 (both closed), #39095.

Modifications

  • scoring.prefill_score_budget_bytes, read by score_tiles for its rows and width: the static budget (capped at 2 GiB) under model capture mode, stream capture or torch.compile piecewise graphs, without a device query; otherwise mqa_logits_budget_bytes (the free-memory fraction), capped at 2 GiB. When that first reading would leave fewer than 256 rows of the forward's widest score row per tile, it calls torch.cuda.empty_cache() and reads again, so the budget follows the memory in use rather than what the allocator happens to cache; an idle server's free memory already gives large tiles and skips the extra read. Forwards below MQA_LOGITS_STATIC_SKIP_ELEMS take the cap without a query. Breakable-graph replay runs these layers eagerly and takes the free-memory budget.
  • flat_index_logits_rows_per_tile / flat_index_logits_tiles / score_tiles take the caller's scratch as bytes a row and bytes a tile. A tile takes all rows when their aligned allocation (rows aligned to the kernel's row group 128 // heads, row stride aligned to 256 fp32) plus that scratch fits the budget; otherwise row-group multiples, with one row group as the floor.
  • Dense-blocks publisher: tiles are FLAT_LOGITS_WIDTH_ALIGN (64) columns aligned, which block_size must divide; it passes logits[rows, :ceil(lc / block_size) * block_size] to select_candidate_block_ids and masks padding columns to -inf in place, so it no longer pads a copy. It declares the mask, block maxima and block top-k as row scratch and one request's int64 index vector as tile scratch. The dense-blocks consumer declares topk_among_blocks's candidate gather, masks and picks as row scratch.
  • Sparse-table publisher: drops each tile and its block keys before the next tile is scored, and declares the block keys as row scratch.

A fixed score-tile pool held for the server's lifetime was implemented and measured on the setup above, then dropped: held from the activation headroom it left the FlashInfer MoE workspace too little memory (warmup OOM, 942 MiB requested with 808 MiB free), and held from the KV pool it costs about 500 cached tokens per MiB at DeepSeek-V4.1's KV size. The tiles are live only while their layer scores, so they share memory with the rest of the forward.

Accuracy Tests

CPU test test/registered/unit/layers/test_dsv41_prefill_score_tiles.py replaces DeepGEMM and the ragged top-k with exact references and covers allocation alignment and scratch, tiled selection parity at every tile size down to one row group, tile lifetimes, the budget (no reclaim with ample memory or enough rows per tile, one empty_cache and a second read when the first is low, the static budget without a query under each capture state, the query under breakable-graph replay, no query for small forwards) and that score_tiles sizes tiles by the budget for its own rows and width.

GPU tests in test/registered/kernels/ops/attention/test_dense_prefill_indexer.py (SM100 and SM120), at a 485,123-entry compressed context:

  • tiles under a 256 MiB budget return bitwise the same scores as one untiled call within each row's valid window;
  • publish, consume and the full top-k select positions with the same scores (exact ties may resolve to different positions) and the same blocks at a 256 MiB budget as in one tile, and the published blocks match the pad-based reference;
  • with 12 query rows at one-row-group tiles, the measured requested peak bytes past each path's outputs stay within the logits allocation plus declared scratch and a 1 KiB allowance;
  • memory parked in the allocator's cache leaves the tile count and the scores unchanged, and memory in use shrinks the tiles with bitwise the same scores.

On this head, rebased onto main 974831643f: test_dsv41_prefill_score_tiles.py passes (12 tests), and the neighboring DSv4/V4.1 CPU suites (test_dsa_mqa_logits_chunking.py, test_dsv41_candidate_blocks.py, test_dsv41_late_layer_tail_readers.py, test_dsv4_nonpaged_indexer.py, test_dsv4_compressed_pools.py, the DSv4.1 draft/verify metadata glue tests, test_encoder_swa_replay.py) pass, 113 passed and 1 skipped in total. The GPU test file has not yet been run on this head.

The OOM scenario: on the setup above, with this change, one cold 491,520-token prompt followed by 12 turns that each resend the history plus 2,000 new tokens (prefix-cached, up to 515,520 tokens), with 8 other requests decoding and the allocator holding all but 50 MiB, ran without an OOM or restart (0.43 to 0.45 s per turn).

Speed Tests and Profiling

DeepSeek-V4.1-Flash on 4x RTX PRO 6000 Blackwell Max-Q (250 W), TP4/EP2, FP8 KV, chunked prefill 8192, --mem-fraction-static 0.85, DSpark. The same image and server process configuration with only the budget changed (these two files applied to the SGLang tree of that image, which carries the first version of this PR), one cold prompt of whole 8K chunks per cell, prompt tokens per second over the whole request. "This change" was measured at the previous revision, which emptied the cache whenever the first reading was below the 2 GiB cap; that cost about 25 ms per idle 8K chunk (the idle cells), which the 256-row threshold of the current head removes:

Cell Free memory only This change
256K, idle 14,938 14,299
512K, idle 13,697 13,238
256K, allocator holding all but 50 MiB 8,432 14,324
512K, allocator holding all but 50 MiB 3,209 13,399
256K, 8 requests decoding 14,572 14,210

At about 460K tokens of depth with the allocator holding all but 50 MiB, an 8K chunk took 5.4 s with a 9 MiB budget (8-row tiles, 1,024 per scorer) and 0.69 s with this change (1.48 GiB budget, about 1,660 rows per tile); idle it took 0.68 s. The idle cells are single runs; their 3 to 4% difference is within this setup's run-to-run spread. The "all but 50 MiB" cells park the rest of the free memory in the caching allocator's cache, the state of the production server where the slowdown was found (26 to 136 MiB free to the driver per GPU).

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

Developed with AI assistance.


CI States

Latest PR Test (Base): ❌ Run #40972559104
Latest PR Test (Extra): ❌ Run #40972559177
Latest PR Test (AMD ROCm 10): ❌ Run #40972559088

@ormandj ormandj mentioned this pull request Oct 9, 2026
17 of 56 tasks
@ormandj ormandj changed the title [DSv4.1] Bound prefill indexer score tiles by a memory budget [Fix] dsv41: Bound prefill indexer score tiles by free memory to avoid OOM Oct 19, 2026
@ormandj
ormandj force-pushed the dsv41-indexer-prefill-tile-budget branch from c653738 to ca267bd Compare October 19, 2026 17:33
detain added a commit to detain/sglang that referenced this pull request Oct 20, 2026
…indexer) + stack_want decisions

- stack.txt: entries 1-2 (local) + Group A boot-critical indexer chain sgl-project#42371/sgl-project#42861/sgl-project#45261/sgl-project#42294/sgl-project#43008 (fe7be85..0dbd8c5).
- stack_want.txt: consolidated decision set from 8 parallel evaluators + dsv41(422)/SM120(254) sweeps + sybil/qwen harvests.
  Records E verdict (merge only sgl-project#43376) and D verdict (merge only sgl-project#42368; sgl-project#33407/sgl-project#34717 dead on flashinfer 0.7.2).
detain added a commit to detain/sglang that referenced this pull request Oct 20, 2026
… correctness)

- core batch [10-15]: sgl-project#43376 long-ctx indexer OOM bound, sgl-project#42368 FlashInfer-0.7 decode dispatch,
  sgl-project#39914 extra-KV 64-token page split + candidate fallback, sgl-project#42714 graph-safe page-split buffers,
  sgl-project#32464 SM120 sparse-prefill test gate, sgl-project#34027 int32->int64 page-split overflow guard.
- Group C [16-23]: sgl-project#42717 TP-rank verify-budget, sgl-project#43225/sgl-project#43374 compact-verify seq_lens + padding renorm,
  sgl-project#42718 packed-verify token->request map, sgl-project#42713 forced-uniform capture metadata, sgl-project#45301 padded-query
  graph lifetime, sgl-project#44141 bounded-replay guard, sgl-project#45185 V4.1-Flash-1017 FP8 QDQ (env-gated, default OFF).
  sgl-project#42710 skipped (superseded upstream: step_logits already fp32).
…count their scratch

The ratio-1/2 prefill scorers allocate each logits tile (rows x the widest
compressed context, fp32) outside every pool --mem-fraction-static sizes. A
fixed 2 GiB tile, plus selection scratch it did not count, OOMed a chunk at a
485K-token context; sizing tiles from free memory instead (the first version of
this change) avoided that but made the tiles depend on what the caching
allocator happens to hold. On a long-running server that is nearly all of the
device, so deep chunks fell to one-row-group tiles and prefill slowed by up to
an order of magnitude.

- The backend holds one pool per device for the tiles, sized at startup from the
  configured context, chunk size, index heads and the largest declared scratch,
  within the static budget (SGLANG_DSA_MQA_LOGITS_FREE_MEM_FRACTION of the share
  --mem-fraction-static leaves free) and the 2 GiB cap.
- MqaLogitsPool takes that capacity once per stream into a private allocator
  pool, which the caching allocator keeps from other allocations and from
  empty_cache; tiles and their scratch are allocated there and sized by the
  capacity, not by free memory. Graph capture keeps the default allocator at the
  same size.
- Tiles count their consumer's scratch: the dense-blocks publisher scores
  block-aligned and masks in place instead of padding a copy, and the
  sparse-table publisher frees each tile and its block keys before the next.
@ormandj ormandj changed the title [Fix] dsv41: Bound prefill indexer score tiles by free memory to avoid OOM [Fix] dsv41: Hold a fixed pool for prefill indexer score tiles to avoid OOM Oct 29, 2026
@ormandj
ormandj force-pushed the dsv41-indexer-prefill-tile-budget branch from ca267bd to c28c12f Compare October 29, 2026 21:51
A score-tile pool held for the life of the server either comes out of the
activation headroom, where it starves the MoE workspace (a 2 GiB pool OOMed
warmup on 4x RTX PRO 6000 at --mem-fraction-static 0.85), or out of the KV
pool, where at DeepSeek-V4.1's ~2 KB per cached token it costs about 500
tokens per MiB. The tiles are live only while their layer scores, so they
should share memory with the rest of the forward.

- score_tiles takes a budget for its rows and width: the static budget under
  graph capture, else SGLANG_DSA_MQA_LOGITS_FREE_MEM_FRACTION of free memory,
  at most 2 GiB. When that reads below the cap, the allocator's unused blocks
  are returned (empty_cache) and free memory is read again, so the budget
  follows memory in use, not memory the caching allocator happens to hold.
- MqaLogitsPool and the backend's pool sizing are removed.
@ormandj ormandj changed the title [Fix] dsv41: Hold a fixed pool for prefill indexer score tiles to avoid OOM [Fix] dsv41: Size prefill indexer score tiles by memory in use to avoid OOM without shrinking them Oct 29, 2026
Emptying the cache before every deep prefill forward cost about 25 ms per 8K
chunk on an idle server, whose free memory already gives large tiles. Read
free memory again only when the first reading leaves fewer than 256 rows of
the forward's widest score row per tile.
ormandj added a commit to ormandj/sglang-deepseek-v41-flash-sm120 that referenced this pull request Oct 30, 2026
…l fix

SGLang main 974831643f with 29 patches (15, 28 and 32 dropped: now upstream
or superseded), FlashInfer main 453a7e2450 with fi-01, sgl-project DeepGEMM
v0.2.3.post2 and Triton release/3.8.x with tr-01, on the lmsysorg/sglang CUDA
13 nightly of 2026-10-29. Every carried pull request is refreshed on main.

Patch 24 (sgl-project/sglang#43376) sizes the prefill indexer's score tiles by
memory in use: free memory alone left out the caching allocator's unused
blocks, so on a running server deep chunks fell to tiles of a few rows and
cold prefill slowed to about 1K tok/s at 300K tokens of depth.
ormandj added a commit to ormandj/sglang-deepseek-v41-flash-sm120 that referenced this pull request Oct 30, 2026
ormandj added a commit to ormandj/sglang-deepseek-v41-flash-sm120 that referenced this pull request Oct 30, 2026
…small tiles

Patch 24 (sgl-project/sglang#43376 at b7b433967d) empties the caching
allocator's cache only when free memory alone would leave score tiles of
fewer than 256 rows; rc.1 did it on every deep prefill forward (about 25 ms
per idle 8K chunk). Same stack key.
@github-actions

Copy link
Copy Markdown
Contributor

Thanks @ormandj. Closing this because you have 25 PRs open and none updated in 7 days, over our soft cap of 5 for idle PRs.

Reopen the two or three you most want reviewed - a short active set moves faster than a long idle one.

Some directories moved recently, so an older branch may need retargeting:
sgl-kernel/ -> python/sglang/kernels/aot/, python/sglang/jit_kernel/
-> python/sglang/kernels/jit/, docs/ -> docs/docs/ (.mdx),
bench_serving.py -> benchmark/serving.py, test/srt/ -> test/registered/.

@github-actions github-actions Bot closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant