Skip to content

[DSV4] Use the shared memory budget for dense prefill - #40967

Closed
harmya wants to merge 2 commits into
sgl-project:mainfrom
modal-labs:harmya/dsv41-adaptive-prefill-budget-main
Closed

harmya wants to merge 2 commits into
sgl-project:mainfrom
modal-labs:harmya/dsv41-adaptive-prefill-budget-main

Conversation

@harmya

@harmya harmya commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Continued in #47363 with the remaining fixes adapted to current upstream. GitHub rejected reopening this PR.

Motivation

DSV4 dense prefill still uses a fixed 2 GiB score budget. This switches it to the existing free-memory-aware helper.

While checking this, I also found that the sparse publisher keeps the previous score tile alive while allocating the next one. A 2 GiB score budget used 4.38 GiB of peak additional memory in the B200 test below.

Modifications

  • Use mqa_logits_budget_bytes() once per forward. Pass that budget to each indexer layer and the replay tail.
  • Count block keys, padding, masks and top-k buffers when choosing tile sizes with mqa_logits_rows_per_chunk().
  • Release scores and block keys before allocating the next tile.

Small inputs do not query free memory. Capture uses the helper's static budget.

score_bytes_per_row = 4 * align_up(context_entries, 256)
row_bytes = score_bytes_per_row + temporary_bytes_per_row
row_alignment = 128 // heads

rows_per_tile = max(
    row_alignment,
    align_down((budget - fixed_bytes) // row_bytes, row_alignment),
)

Scoring precision is unchanged. Every row still sees its full context.

The budget covers temporary indexer memory. It is not a limit on total model memory. Small inputs and the minimum kernel row count keep their existing behavior. Free memory can change later in the forward.

Accuracy Tests

49 CPU tests, 9 B200 GPU tests and all-file pre-commit passed. These include the existing nonpaged-indexer and replay-metadata tests. The new lifetime test fails when the explicit tile release is removed. GPU tests check selection correctness and peak allocation at aligned and unaligned context lengths.

The existing replay-tail test sometimes misses its 98% overlap threshold. After seeing 501/512 on the patch, I repeated it eight times per version on the same GPU. Upstream had the same miss once, the patch had none. The threshold is unchanged.

Speed Tests and Profiling

Candidate selection on one B200, against upstream 954458567. 16K query rows, 32 heads, blocks of 8, 2048 candidate blocks and 512 token picks per row. Median of 10 warm calls across two runs in reverse order. Each run used a fresh process. No model loaded.

Version 64K context, ms Peak extra GiB 256K context, ms Peak extra GiB
Upstream, 2 GiB scores 19.69 4.38 42.47 4.38
Patch, 2 GiB budget 20.00 2.12 45.90 2.12
Patch, free-memory budget 19.66 4.63 39.89 7.26

Peak extra memory is the increase in PyTorch allocated memory during the call, including the returned candidate tensors. The free-memory run used mem_fraction_static=0.8 and the default free-memory fraction of 0.2. That gave a 7.13 GiB budget on this mostly empty GPU.

At 2 GiB, the patch uses less memory and takes more tiles. With room available, it uses larger tiles. It does not always use less memory.


CI States

Latest PR Test (Base): ❌ Run #35924130383
Latest PR Test (Extra): ❌ Run #35924130100
Latest PR Test (AMD ROCm 10): ❌ Run #35924130110

@harmya
harmya marked this pull request as ready for review September 23, 2026 21:30
@harmya harmya changed the title [DSV4] Budget dense prefill workspace once per forward [DSV4] Use the shared memory budget for dense prefill Sep 23, 2026

def resolve(self, device: torch.device) -> int:
if self.bytes is None:
self.bytes = mqa_logits_budget_bytes(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

using existing helper here

@harmya

harmya commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

@Oasis-Git Hi, since #39095 got merged, here is follow up to use the free-memory-aware budget

@github-actions

Copy link
Copy Markdown
Contributor

Thanks @harmya. Closing this because you have 6 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/.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant