Skip to content

feat(rollout): align decode fronts and trim dead zones - #8497

Merged
delock merged 12 commits into
deepspeedai:masterfrom
nathon-lee:feat/cb-decode-align-trim
Oct 4, 2026
Merged

delock merged 12 commits into
deepspeedai:masterfrom
nathon-lee:feat/cb-decode-align-trim

Conversation

@nathon-lee

@nathon-lee nathon-lee commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds an opt-in decode-front alignment path for continuous-batching rollouts.

When align_decode_fronts=False, the existing equal-width padded layout remains the default. Requests may have different effective lengths in their attention masks, but their physical decode fronts are not right-aligned or compacted.

When align_decode_fronts=True, the rollout:

  • derives effective prompt lengths from attention masks;
  • orders requests from longest to shortest;
  • right-aligns prefill spans;
  • tracks logical positions and physical write positions separately;
  • trims dead cache columns with attention metadata and write positions;
  • restores the original request order in the returned batch.

The scheduler no longer exposes admit_if. Trimming preserves enough physical cache span for prompts admitted during the current refill.

Validation

Test Results

  • ✅ 14 tests passed
  • ✅ Changed-file pre-commit checks passed

Device-Backed Validation

  • GPU: NVIDIA RTX 4000 Ada, 20 GB
  • PyTorch: 2.9.1+cu128
  • CUDA: 12.8
  • Transformers: 4.57.1
  • Model: Qwen/Qwen2.5-3B-Instruct
  • Precision: FP32

The aligned and equal-width outputs matched the single-request eager oracle token by token.

Performance

Workload 1: 32 mixed left-padded requests

  • Prompt shape: [32, 20]
  • max_new_tokens: 16
Case Time Throughput
no-CB 1183.46 ms 432.63 tok/s
CB equal-width, batch 32 1418.99 ms 360.82 tok/s
aligned CB, batch 32 2600.53 ms 196.88 tok/s
dynamic CB, continuous_batch_size=8 4315.39 ms 118.65 tok/s

The optimization is workload-dependent. For uniform short prompts, aligned CB is slower because per-request prefill and cache-management overhead dominate. For long-tail prompt lengths, aligned CB can be much faster by avoiding padded prefill computation.

Workload 2: Long-tail workload

  • Composition: 1 × 512-token prompt and 31 × 16-token prompts (padded to [32, 512])
Case Mean time Mean throughput
no-CB 9539.69 ms 53.67 tok/s
CB equal-width, batch 32 9668.58 ms 52.96 tok/s
aligned CB, batch 32 3711.05 ms 137.97 tok/s

The aligned path was 2.57× faster than no-CB for this long-tail workload by avoiding repeated prefill computation on padded rows. These measurements are workload-dependent and are not presented as a general CB speedup claim.

The observed 2.57x improvement comes from avoiding padded prefill work in the long-tail prompt workload, not from cache trimming. The workload had trim_count=0, so it does not provide real-model trim overhead data. Trimming is covered by the synthetic regression test, while a real trim measurement would require a workload with staggered EOS or per-request token budgets.

Notes

The tested workloads did not trigger real-model trimming:

trim_count=0
trimmed_columns=0
trim_latency_ms=0.0
trim_bytes_moved=0

Applicability and Limitations

Workload Expected behavior Main reason
Short prompts with similar lengths May be slower Scheduler, cache-management, and per-request prefill overhead dominate
Highly variable prompt lengths Can improve throughput Reduces padded prefill computation
Dynamic request arrivals Can improve GPU utilization Released slots can be refilled with pending requests
Staggered request completion or EOS retirement Enables trimming benefits Dead cache columns can be reclaimed
Fixed batch with similar-length requests Standard static batching may be faster It avoids continuous-batching overhead
No early request completion No real trimming benefit The trimming path is not exercised

BF16 token-exactness is not claimed; separate BF16 runs showed numerical token divergence.

Related to #8462 and follow-up to #8368.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4c768545ab

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

assert (1, 3) in model.calls


def test_aligned_continuous_generation_supports_mixed_effective_prompt_lengths():

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add and run a device-backed integration test

When decode-front alignment runs with a real cache-class model, correctness depends on model-specific causal masking, positional encoding, and cache behavior, but the added test uses a synthetic model that only sums copied cache tensors. Add an integration generation test using a supported model on an actual device and report the hardware and result, as required for inference-level contract changes.

AGENTS.md reference: AGENTS.md:L35-L36

Useful? React with 👍 / 👎.

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.

Hi @delock,

I completed the device-backed validation on an NVIDIA RTX 4000 Ada with Qwen/Qwen2.5-3B-Instruct, and the CB outputs matched the single-request eager oracle token by token. The commands and results are included in the
latest PR comment.

The Codex review asks for an added device-backed integration test. I could not find an existing model-checkpoint-dependent integration test pattern in this area of the repository.

Would the external validation be sufficient for this PR? If a committed model-dependent integration test is needed, I can prepare it as a follow-up PR.

@delock
delock self-requested a review September 13, 2026 12:47
@nathon-lee
nathon-lee force-pushed the feat/cb-decode-align-trim branch from 4c76854 to 095c883 Compare September 14, 2026 05:50
use_graph_capture: bool = False
enable_profiling: bool = False
use_shared_prefill: bool = False
align_decode_fronts: bool = False

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it true that if align_decode_fronts is false, the prompt always have to have same length? Or we allow staggered decode fronts? Current name sound like we support staggered decode fronts.

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.

I agree the name could be slightly misleading since the alignment path is opt-in; I’ll consider clarifying the naming or documentation further.

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.

Thanks for pointing this out, @delock.

When align_decode_fronts=False, all requests must use the same padded prompt width. Their effective prompt lengths may still differ according to the attention masks, so the legacy path keeps staggered logical decode positions. It does not physically right-align or compact the decode fronts.

When align_decode_fronts=True, the rollout uses the effective prompt lengths to physically align decode fronts and trim dead cache columns.

I clarified this distinction in the configuration docstring and the inference documentation.

@delock

delock commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Hi @nathon-lee do you have performance of CB with this optimization vs no CB? Could be performance of 32 different prompts rollout, then measure the rollout end2end performance. This could show how much performance improvement CB can bring us, and could reveal real trim overhead with your optimization.

@nathon-lee

Copy link
Copy Markdown
Contributor Author

Hi @nathon-lee do you have performance of CB with this optimization vs no CB? Could be performance of 32 different prompts rollout, then measure the rollout end2end performance. This could show how much performance improvement CB can bring us, and could reveal real trim overhead with your optimization.

Hi @delock I did test this before, but unfortunately I didn’t save the results. I’ll run the 32-prompt E2E comparison again this weekend and share the numbers.

@nathon-lee

nathon-lee commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi, @delock

Modified

I removed the admission gate from the scheduler/rollout API and added a 32-request padded-prompt refill/trim regression with continuous_batch_size=8.

Cache trimming now preserves enough physical span for every prompt admitted in the current refill, so an admitted padded prompt cannot receive an empty cache slice. align_decode_fronts=False remains documented as the equal‑width
padded‑prompt baseline.

I also fixed the real Qwen prefill/decode mismatch for models whose cache‑class capability is unknown. Continuous prefill now obtains the first token and prompt KV through the model's standard greedy generation path, and subsequent continuous decode applies the model generation configuration's repetition penalty instead of using a bare argmax.

Validated: uniform mixed‑prompt workload

On an NVIDIA RTX 4000 Ada (20 GB), PyTorch 2.9.1+cu128, CUDA 12.8, Transformers 4.57.1, and Qwen/Qwen2.5‑3B‑Instruct in FP32, I ran 32 mixed left‑padded requests with prompt shape [32, 20] and max_new_tokens=16. All outputs were token‑exact against the single‑request eager oracle:

Case Time Throughput Exact match
no‑CB 1183.46 ms 432.63 tok/s Yes
CB equal‑width, batch 32 1418.99 ms 360.82 tok/s Yes
aligned CB, batch 32 2600.53 ms 196.88 tok/s Yes
dynamic CB, continuous_batch_size=8 4315.39 ms 118.65 tok/s Yes

The dynamic run completed 32/32 non‑empty responses without an empty torch.cat; it took 60 decode steps. This workload did not trigger trimming (trim_count=0, trimmed_columns=0, trim_latency_ms=0.0, trim_bytes_moved=0).

These uniform short‑prompt measurements are not a CB speedup claim. There is no padded‑prefill reduction to offset scheduler, KV‑copy, mask/cache maintenance, and Python decode overhead. Aligned prefill is also currently performed per request, which adds forward‑call overhead for short prompts. The batch‑8 dynamic case has only eight active rows and four refill waves while all requests arrive at once, so it is not comparable to fixed batch‑32 utilization.

Validated: aligned long‑tail workload

I also measured a deliberately long‑tail workload where aligned prefill has the expected benefit. The prompts were constructed from a repeated non‑special token ID: one 512‑token row and 31 16‑token rows, left‑padded to [32, 512], with max_new_tokens=16. Both repetitions produced 512 generated tokens, 32/32 non‑empty responses, and token‑exact outputs against the single‑request eager oracle.

Case Mean time (2 runs) Mean throughput Per‑run throughput
no‑CB 9539.69 ms 53.67 tok/s 53.86, 53.49
CB equal‑width, batch 32 9668.58 ms 52.96 tok/s 53.13, 52.78
aligned CB, batch 32 3711.05 ms 137.97 tok/s 138.08, 137.86

Aligned CB is 2.57× faster (+157.1%) than no‑CB for this long‑tail workload. The gain comes from avoiding repeated prefill computation on 31 padded rows; it is workload‑dependent, not a general CB speedup claim. This workload also did not trigger trimming: trim_count=0, trimmed_columns=0, trim_latency_ms=0.0, and trim_bytes_moved=0.

The focused scheduler/rollout suite passes 14 tests, and changed‑file pre‑commit checks pass.

Pending validation

BF16 is not being presented as token‑exact correctness. Its previously observed token divergence remains a separate limitation and needs independent disclosure/validation.

@nathon-lee
nathon-lee force-pushed the feat/cb-decode-align-trim branch from 2519d70 to 9ddbdac Compare September 20, 2026 10:24
nathon-lee and others added 5 commits September 20, 2026 10:25
…hing

Use the model generation path for prefill when cache-class capability is unknown, then apply the configured repetition penalty during continuous decode. This keeps Qwen mixed left-padded requests token-exact with eager generation.

Prevent cache trimming from reclaiming columns required by newly admitted padded prompts, and document the equal-width baseline behavior.

Signed-off-by: nathon-lee <[email protected]>
Restore the continuous profiling contract while integrating aligned batching and update the StaticCache test mock for explicit cache positions.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: nathon-lee <[email protected]>
@nathon-lee
nathon-lee force-pushed the feat/cb-decode-align-trim branch from 9ddbdac to 440bc14 Compare September 20, 2026 10:27
@nathon-lee

Copy link
Copy Markdown
Contributor Author

I’ll construct an additional workload with staggered EOS and measure the real trimming overhead, including trim count, latency, and bytes moved.

@nathon-lee
nathon-lee marked this pull request as draft September 23, 2026 03:36
@nathon-lee

Copy link
Copy Markdown
Contributor Author

Performance, trim-overhead, and cache-capacity validation

The measurements now separate three effects:

  1. Aligned prefill can improve long-tail prompt workloads by avoiding padded prefill work.
  2. Trimming is not a throughput optimization. It introduces measurable KV/metadata copy overhead.
  3. Trimming provides a capacity benefit for staggered-completion workloads: it can keep a continuous batch within a smaller static cache span when the equivalent untrimmed execution would exhaust that span.

1. Aligned-prefill measurements

These runs used an NVIDIA RTX 4000 Ada (20 GB), FP32, PyTorch 2.9.1+cu128, CUDA 12.8, Transformers 4.57.1, and Qwen/Qwen2.5-3B-Instruct. All outputs were token-exact against the single-request eager oracle.

Workload no-CB Equal-width CB Aligned CB Conclusion
32 mixed left-padded requests, [32, 20], max_new_tokens=16 1183.46 ms / 432.63 tok/s 1418.99 ms / 360.82 tok/s 2600.53 ms / 196.88 tok/s Short uniform prompts do not amortize aligned-prefill and scheduler/cache-management overhead.
1 x 512-token prompt + 31 x 16-token prompts, padded to [32, 512], max_new_tokens=16 9539.69 ms / 53.67 tok/s 9668.58 ms / 52.96 tok/s 3711.05 ms / 137.97 tok/s Aligned CB is 2.57x faster (+157.1%) by avoiding padded prefill for the 31 short rows.

Neither workload triggered trimming (trim_count=0), so the aligned-prefill improvement must not be attributed to trimming.

2. Isolated trim-overhead measurement

For a controlled trim/no-trim comparison, I added enable_cache_trimming (default: True). Disabling it preserves aligned scheduling and decode-front layout while skipping left-prefix trimming.

This workload used an NVIDIA RTX A5000 (24 GB), BF16, Transformers 4.57.1, and Qwen/Qwen2.5-0.5B-Instruct. It has 32 left-padded requests with effective prompt lengths from 64 down to 33 and continuous_batch_size=8. A deterministic 2-5-token EOS schedule creates staggered completion and reliably exercises trimming. After one warm-up, three runs per case used the same 96-token cache capacity and produced token-exact outputs.

Case Mean E2E time Mean throughput Trim count Bytes moved Cumulative trim latency
Trim enabled 1157.62 ms 96.86 tok/s 12 109,188,480 23.68 ms
Trim disabled 1087.39 ms 103.00 tok/s 0 0 0.00 ms

With capacity held constant, trimming adds about 6.5% E2E time for this deliberately trim-heavy workload. It moves about 8.68 MiB per trim and costs about 1.97 ms per trim on average.

3. Capacity-reclamation measurement

I then used the same staggered-EOS pattern with max_new_tokens=8 to test the actual capacity benefit.

Case Static cache span KV cache + metadata allocation Peak cache span Result
Trim enabled 72 7,082,560 bytes 65 Completed
Trim disabled 72 7,082,560 bytes - cache exhausted before active requests retired
Trim disabled 80 7,869,504 bytes 80 Completed

The trim-enabled 72-token run and the untrimmed 80-token run are token-exact. For this workload, trimming reduces the required cache span by 8 tokens (10%) and the preallocated KV-cache/metadata footprint by approximately 11.1%. The trim-enabled run performed 12 trims, reclaimed 42 columns, moved 80,858,496 bytes, and recorded 25.52 ms cumulative trim latency.

Conclusion

Aligned decode-front processing and cache trimming solve different problems:

  • Aligned prefill can provide substantial performance gains for highly variable prompt lengths by removing padded-prefill work.
  • Trimming is a cache-span reclamation mechanism, not a throughput optimization. It trades KV and attention-metadata copy overhead for the ability to continue staggered-EOS/refill workloads within a smaller bounded static cache span.
  • When the untrimmed workload already fits in the available cache, disabling trim is faster in these measurements.
  • When cache capacity is constrained, trim can make a workload complete that would otherwise exhaust the configured cache span.

The deterministic EOS schedule is intended to isolate and reproducibly exercise the trim path; these results are not presented as a claim about the natural EOS distribution of production traffic.

Validation: the trim-enabled and no-trim paths are covered by rollout unit tests, including output equivalence and trim telemetry. tests/unit/runtime/rollout/test_hybrid_engine_rollout.py passes 37 tests, and changed-file pre-commit checks pass.

@delock

delock commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

@nathon-lee so in rollout case if we leave enough KV cache size then no trimming is better choice, this is the conclusion from trimming comparison data right?

@nathon-lee

Copy link
Copy Markdown
Contributor Author

@delock I also agree to a large extent: if the KV cache can sufficiently cover peak demand, skipping trimming will result in faster performance. However, in scenarios with limited cache capacity or significant variations in request completion times, trimming still holds value for capacity recovery.

@delock

delock commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

@delock I also agree to a large extent: if the KV cache can sufficiently cover peak demand, skipping trimming will result in faster performance. However, in scenarios with limited cache capacity or significant variations in request completion times, trimming still holds value for capacity recovery.

Agree. We should set trimming default as false. If user use this path and hit OOM and have to reduce cache capacity, they will investigate and will find the trimming option. On the other hand, if user never need to reduce cache capacity, user will bear trim overhead and not knowing it. So rational action might be set trimming default as false. Is it possible to direct user to turn on trimming or increase cache capacity, when cache capacity exthausted?

@nathon-lee

Copy link
Copy Markdown
Contributor Author

@delock I also agree to a large extent: if the KV cache can sufficiently cover peak demand, skipping trimming will result in faster performance. However, in scenarios with limited cache capacity or significant variations in request completion times, trimming still holds value for capacity recovery.

Agree. We should set trimming default as false. If user use this path and hit OOM and have to reduce cache capacity, they will investigate and will find the trimming option. On the other hand, if user never need to reduce cache capacity, user will bear trim overhead and not knowing it. So rational action might be set trimming default as false. Is it possible to direct user to turn on trimming or increase cache capacity, when cache capacity exthausted?

Agree. I will set cache trimming to disabled by default so users with sufficient KV-cache capacity do not pay the copy overhead.

On capacity exhaustion, I will make the failure actionable: the error will explicitly recommend either increasing the configured cache capacity, or enabling trimming for workloads where requests retire at different times and dead cache columns can be reclaimed.

I am also considering an opt-in auto mode: it would preserve the no-trim fast path and trim only when the next cache write would otherwise exhaust capacity and there is a reclaimable dead prefix. It would not try to recover after a real OOM, because trimming cannot help when no dead prefix exists. I’ll keep the initial change minimal unless we agree that auto belongs in this PR.

…ity and actionable exhaustion errors.

Signed-off-by: nathon-lee <[email protected]>
@nathon-lee
nathon-lee marked this pull request as ready for review September 24, 2026 02:01
@nathon-lee
nathon-lee marked this pull request as draft September 24, 2026 02:03
Treat a missing repetition penalty as the no-penalty case in continuous batching, add a regression test, and clarify that cache trimming is opt-in.

Signed-off-by: nathon-lee <[email protected]>
Avoid exhausting the derived continuous-batching cache span when staggered requests refill an active batch. Fall back to reclaiming a dead prefix only at capacity when periodic trimming is disabled, and handle an entirely reclaimable attention span.

Signed-off-by: nathon-lee <[email protected]>
@nathon-lee
nathon-lee force-pushed the feat/cb-decode-align-trim branch from faf4eb0 to 8bd2801 Compare September 26, 2026 13:09
@nathon-lee
nathon-lee marked this pull request as ready for review September 26, 2026 13:15
@nathon-lee

Copy link
Copy Markdown
Contributor Author

Hi @delock,

I addressed the cache-capacity issue for staggered refill when periodic trimming is disabled. The rollout now reclaims a dead prefix only when capacity would otherwise be exhausted, and I added a regression test covering this path.

The updated rollout tests (45 passed) and changed-file pre-commit checks pass locally. The latest commit is 8bd28019.

When you have time, could you please review the latest changes? Thank you!

@delock

delock commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Hi @nathon-lee , thank you for following on this. The latest change looks good to me. There is one conflict on document, can you resolve the conflict? Thanks!

@nathon-lee
nathon-lee force-pushed the feat/cb-decode-align-trim branch from 248bf89 to bcda012 Compare October 3, 2026 04:43
@nathon-lee

Copy link
Copy Markdown
Contributor Author

Hi @delock, sorry for the late reply. I was away for a few days. I've fixed the issue and run the pipeline locally. I think it should be ready to merge now.

nathon-lee#31

@delock
delock enabled auto-merge October 4, 2026 04:56
@delock

delock commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Hi @delock, sorry for the late reply. I was away for a few days. I've fixed the issue and run the pipeline locally. I think it should be ready to merge now.

nathon-lee#31

Thank you for following up!

@delock
delock added this pull request to the merge queue Oct 4, 2026
Merged via the queue into deepspeedai:master with commit a77aeb6 Oct 4, 2026
13 checks passed
@tohtana tohtana added needs-gpu-test GPU testing is recommended to validate the changes in this PR. and removed needs-gpu-test GPU testing is recommended to validate the changes in this PR. labels Nov 10, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Nov 15, 2026
…ost model (deepspeedai#8786)

## November 11 update

Rebased onto master `b9100c5`, which includes deepspeedai#8390 and deepspeedai#9008; new head
`82ac56e`.

Uses deepspeedai#8939's shared sampling kwargs, token selector and FP32
repetition-penalty helper. Manual graph and continuous-batching paths
retain upstream temperature/top-k/top-p sampling. Adaptive prefill
retains its single-sample greedy scope, including fixed-slot graph
decode. deepspeedai#9004's pre-built 4-D graph mask is preserved.

- **deepspeedai#8390 early stopping:** ordinary generation and Auto's batched
fallback share deepspeedai#8390's generation kwargs, so they stop on natural EOS;
ZeRO-3 keeps the group-aware synchronized stop. Auto remains
single-process.
- **Review fixes:** the graph path rejects `align_decode_fronts=True`
only when Auto is disabled, because Auto always uses aligned bucketed
prefill. The accelerator graph test covers both flag values. The
documentation states that Auto takes precedence over
`align_decode_fronts` on the graph path; cache trimming is still
rejected.
- **deepspeedai#9008 lazy KV layers:** the bucketed prefill copy initializes
uninitialized cache layers through the shared
`_continuous_cache_layer()` helper, like the equal-width path.
- **Codex P2 (preparer findings):** after a custom
`_prepare_generation_config()` runs, Auto rejects the prepared config if
it changes any request override (token budget, EOS, PAD or greedy
settings). Otherwise the batched route would honor the change inside
`generate()` while the CB routes keep the request values. A preparer
that replaces or edits the attention mask is rejected for the same
reason.
- **Codex P2 (mask lengths):** effective prompt lengths use
`count_nonzero`, so BF16/FP16 masks longer than their exact-integer
range no longer round the bucket width.
- **Codex P1 (test reliability):** the route-planning profile test uses
a frozen clock advanced by the model's generation-config preparer,
instead of wall-clock bounds or mocked rollout internals.
- The decode cost still uses the configured maximum generation budget.
Natural EOS can shorten decode, so the extra-CB estimate can be too
large; the documentation states this assumption.

Validation on an 8 × H200 node, PyTorch 2.9.1+cu128, CUDA 12.8,
Transformers 4.57.1: **232 passed, 3 skipped, 2 deselected** across the
runtime rollout, scheduler, interface, static-cache and v1 accelerator
suites, including deepspeedai#8390's 4-rank subgroup and 2-GPU ZeRO-3
synchronized-stop tests. Skips require the newer cache query-offset API;
deselections are the native shared-prefill kernel and optional
pretrained-model test. Pre-commit passed on all changed files. A small
natural-EOS check (Qwen2.5-3B-Instruct, four short questions) ended
every response at EOS, with Auto matching ordinary generation. This
update used AI assistance.

The performance tables below retain their original revision attribution;
large-model throughput and peak memory were not remeasured for this
rebase.

## Summary

Closes deepspeedai#8785. Related tracking:
[deepspeedai#8197](deepspeedai#8197),
[deepspeedai#8462](deepspeedai#8462).

**Performance follow-up to
[deepspeedai#8497](deepspeedai#8497 Aligned
CB avoids padding on long-tail prompts, but one prefill Forward per
request is expensive for short prompts. A padding-ratio threshold cannot
distinguish **128/4** from **512/16**, despite identical ratios and
different fastest baselines.

This change adds **one opt-in automatic policy** that compares ordinary
no-CB generation with aligned CB using **DP-selected prefill buckets**.
For 1×512 + 31×16, the selected buckets are `[1,512]` and `[31,16]`,
reducing prefill Forwards from 32 to 2.

**Auto is disabled by default.** Existing manual modes and positional
configuration arguments are preserved.

```python
cfg = HybridEngineRolloutConfig(adaptive_prefill=True)
rollout = HybridEngineRollout(engine, tokenizer, cfg)
sampling = SamplingConfig(max_new_tokens=16, temperature=0, continuous_batch_size=32)
```

## Selection and configuration

A length-sorted **O(n²)-time / O(n)-space DP** minimizes the additive
estimated cost of contiguous buckets. It explores one batch, singleton
batches and intermediate partitions without running the GPU for
candidate evaluation. Each bucket uses ordinary left padding and a 2D
attention mask; **no varlen kernel is needed**. Model geometry is read
once per plan.

```text
C(B,L) = fixed + token × B×L + attention × estimated_attention_GFLOPs
         + kv × estimated_KV_MiB
attention_GFLOPs = 4 × B × L² × layers × query_heads × head_dim / 1e9
KV_MiB = 2 × B × L × layers × KV_heads × head_dim × element_bytes / 2^20
```

| Parameter | Default | Meaning |
| --- | ---: | --- |
| `prefill_fixed_cost_ms` | 25.0 | Fixed milliseconds per prefill
Forward |
| `prefill_token_cost_ms` | 0.1 | Milliseconds per padded token
position; projection/MLP term |
| `prefill_attention_cost_ms` | 0.01 | Milliseconds per estimated
attention GFLOP |
| `prefill_kv_cost_ms` | 0.04 | Milliseconds per MiB of KV copied into
the CB cache |
| `continuous_decode_cost_ms` | 10.0 | Additional CB milliseconds per
remaining Decode step |
| `prefill_max_tokens` | 65536 | Maximum padded token positions per
prefill Forward; `None` removes this limit |

All cost coefficients must be finite and non-negative. **These are
starting estimates, not universal GPU constants**; calibrate them for
the model, device, precision and attention backend. Head dimension,
query/KV heads, layers and parameter element size are read
automatically. The DP is optimal for the configured estimates, not
guaranteed globally optimal measured latency.

Ordinary generation is eligible when every row fits
`continuous_batch_size`, the physical input fits the prefill/context
limits, and neither explicit static capacity nor trimming is requested.
Its estimated cost omits CB cache-copy traffic; CB adds
`continuous_decode_cost_ms × (max_new_tokens - 1)`. The first output
token comes from Prefill.

For CB, **every admission/refill replans the newly admitted requests**.
Effective KV spans are copied into the existing right-aligned cache;
surviving rows retain their layout. The policy does not change caller
configuration or active-row bounds. Disabling Auto restores manual
selection. Model/OOM errors propagate without trying another path.

## Correctness

- Apply repetition penalty from the first token using **original padded
history**, even when bucket model inputs are trimmed.
- Preserve generated PAD-valued tokens through the first EOS, excluding
structural padding after termination.
- Include route planning and result normalization in end-to-end
profiling; count **productive output tokens** and retain
active/configured capacity fields.
- Resolve HF's effective generation configuration before rejecting
unsupported processors/output modes, including legacy model-config
refresh; preserve explicit generation-config overrides. Auto requires
single-process greedy rollout, one sample per prompt and cache-class
support. With graph capture, Auto uses eager bucketed prefill and
fixed-slot graph decode; cache trimming and shared prefill remain
unsupported.

## Experiments

Related work: [TurboTransformers, §5 (PPoPP
2021)](https://arxiv.org/html/2010.05680) already uses length sorting,
profiled batch costs and an O(n²) DP to partition variable-length
inference requests. [DynaPipe, §4](https://arxiv.org/html/2311.10418)
applies cost-based DP partitioning to training micro-batches. This PR
applies that established scheduling approach to HybridEngine rollout,
with ordinary-generation comparison, refill planning and existing
aligned-cache semantics; it makes no claim of a new DP algorithm.

**Single GPU; Qwen/Qwen2.5-3B-Instruct; FP32 / SDPA; PyTorch
2.9.1+cu128; Transformers 4.57.1.** Each candidate receives **one warmup
and five interleaved timed runs**, 32 requests and 16 output tokens per
request. CB capacity is 32; trimming is disabled, cache capacity unset,
TF32 disabled. The default cost coefficients above are unchanged across
workloads.

| Workload | Effective prompt lengths | Reason |
| --- | --- | --- |
| Short, similar | 20..13 repeated four times | Normal workload shape
from deepspeedai#8497 |
| Long tail | 1×512 + 31×16 | Long-tail workload from deepspeedai#8497 |
| Short tail | 1×20 + 31×1 | Counterexample to ratio-only routing |
| Moderate tail | 1×128 + 31×4 | Same padding ratio as 512/16, different
best legacy path |
| Spread | 16, 32, …, 512 | General distribution with intermediate
buckets |

Inputs repeat non-special token ID 14990. deepspeedai#8497 did not publish complete
short-prompt sequences, so that case reconstructs its documented shape.
Every timed response matches independent single-request eager
generation; each workload produces **512 effective output tokens**.

GPU-synchronized wall-clock timing includes the full `generate()` call,
including Auto planning and normalization. Model loading and oracle work
are outside timing; profiling is disabled during timed runs. All
reported E2E values are **arithmetic means of five runs**.

| Workload | No-CB (ms) | Equal-width CB (ms) | Aligned CB (ms) | Auto
(ms) | Auto / best | Auto choice |
| --- | ---: | ---: | ---: | ---: | ---: | --- |
| Short, similar | **505.54** | 673.66 | 1549.74 | **501.77** | 0.9925×
| No-CB |
| 1×512 + 31×16 | 2458.46 | 2596.38 | **1682.10** | **840.06** | 0.4994×
| 2 buckets |
| 1×20 + 31×1 | **501.74** | 667.57 | 1331.82 | **497.57** | 0.9917× |
No-CB |
| 1×128 + 31×4 | **897.17** | 1053.26 | 1468.58 | **634.59** | 0.7073× |
2 buckets |
| 16, 32, …, 512 | 2458.13 | 2595.33 | **2337.75** | **1984.86** |
0.8490× | 6 buckets |

**All five workloads meet the 5% acceptance tolerance.** Auto chooses
ordinary generation for both short cases; their small timing differences
are not claimed as a kernel speedup. Relative to the fastest legacy
baseline, mean latency falls by **50.06%** for 1×512 + 31×16, **29.27%**
for 1×128 + 31×4, **15.10%** for 16, 32, …, 512. These improvements come
from fewer prefill Forwards and reduced padded work.

<details>
<summary>Throughput and peak allocated memory for every path</summary>

Throughput is 512 divided by mean latency. Memory is peak PyTorch
allocated memory during timed calls, including model weights.

| Workload | Path | Output tok/s | Peak allocated memory (MiB) |
| --- | --- | ---: | ---: |
| Short, similar | No-CB | 1012.77 | 11988.96 |
| Short, similar | Equal-width CB | 760.03 | 12036.75 |
| Short, similar | Aligned CB | 330.38 | 11928.77 |
| Short, similar | **Auto** | 1020.40 | 11988.96 |
| 1×512 + 31×16 | No-CB | 208.26 | 15558.69 |
| 1×512 + 31×16 | Equal-width CB | 197.20 | 16747.20 |
| 1×512 + 31×16 | Aligned CB | 304.38 | 13278.51 |
| 1×512 + 31×16 | **Auto** | 609.48 | 13278.49 |
| 1×20 + 31×1 | No-CB | 1020.46 | 11994.64 |
| 1×20 + 31×1 | Equal-width CB | 766.96 | 12033.50 |
| 1×20 + 31×1 | Aligned CB | 384.44 | 11925.07 |
| 1×20 + 31×1 | **Auto** | 1029.00 | 11994.64 |
| 1×128 + 31×4 | No-CB | 570.68 | 12742.62 |
| 1×128 + 31×4 | Equal-width CB | 486.11 | 13066.75 |
| 1×128 + 31×4 | Aligned CB | 348.64 | 12221.97 |
| 1×128 + 31×4 | **Auto** | 806.82 | 12221.95 |
| 16, 32, …, 512 | No-CB | 208.29 | 15558.69 |
| 16, 32, …, 512 | Equal-width CB | 197.28 | 16747.20 |
| 16, 32, …, 512 | Aligned CB | 219.01 | 13278.56 |
| 16, 32, …, 512 | **Auto** | 257.95 | 13682.33 |

</details>

<details>
<summary>Separate profiling breakdown</summary>

One additional profiling call per candidate, outside acceptance timing.
These diagnostics are not averages. Cache management is part of the
generation overhead, not an additional independent total.

| Workload | Path | Prefill (ms) | Decode (ms) | Cache management (ms) |
Prefill / Decode Forwards |
| --- | --- | ---: | ---: | ---: | ---: |
| Short, similar | No-CB | 91.01 | 401.30 | N/A | 1 / 15 |
| Short, similar | Equal-width CB | 91.99 | 499.53 | 26.80 | 1 / 15 |
| Short, similar | Aligned CB | 955.52 | 500.30 | 35.25 | 32 / 15 |
| Short, similar | **Auto** | 90.74 | 396.90 | N/A | 1 / 15 |
| 1×512 + 31×16 | No-CB | 1918.22 | 531.89 | N/A | 1 / 15 |
| 1×512 + 31×16 | Equal-width CB | 1919.76 | 595.14 | 27.09 | 1 / 15 |
| 1×512 + 31×16 | Aligned CB | 981.04 | 594.52 | 35.02 | 32 / 15 |
| 1×512 + 31×16 | **Auto** | 153.66 | 592.66 | 31.43 | 2 / 15 |
| 1×20 + 31×1 | No-CB | 91.06 | 402.45 | N/A | 1 / 15 |
| 1×20 + 31×1 | Equal-width CB | 91.84 | 494.84 | 26.88 | 1 / 15 |
| 1×20 + 31×1 | Aligned CB | 743.30 | 491.93 | 34.48 | 32 / 15 |
| 1×20 + 31×1 | **Auto** | 90.62 | 394.78 | N/A | 1 / 15 |
| 1×128 + 31×4 | No-CB | 490.08 | 399.13 | N/A | 1 / 15 |
| 1×128 + 31×4 | Equal-width CB | 490.85 | 482.08 | 27.09 | 1 / 15 |
| 1×128 + 31×4 | Aligned CB | 889.01 | 483.21 | 34.83 | 32 / 15 |
| 1×128 + 31×4 | **Auto** | 63.80 | 485.00 | 31.43 | 2 / 15 |
| 16, 32, …, 512 | No-CB | 1918.03 | 531.64 | N/A | 1 / 15 |
| 16, 32, …, 512 | Equal-width CB | 1918.95 | 594.68 | 27.32 | 1 / 15 |
| 16, 32, …, 512 | Aligned CB | 1648.16 | 593.25 | 35.91 | 32 / 15 |
| 16, 32, …, 512 | **Auto** | 1301.59 | 593.31 | 32.49 | 6 / 15 |

The 128/4 and 512/16 workloads reduce prefill Forwards from 32 on
aligned CB to **2 on Auto**. Spread uses **6 buckets**. All CB paths in
these performance runs have zero trims and zero moved bytes; ordinary
generation has no static-cache statistics. This makes no new
trimming-speedup claim.

</details>

## Validation and limits

A separate microbenchmark measures **planner overhead**, without model
Prefill: for 128/256 requests of 65,536 tokens, CPU DP takes **0.48/1.82
ms** on average with the default per-bucket token limit. It still checks
all 8,256/32,896 candidate segments; over-limit segments skip cost
evaluation. For 32 requests, DP takes 0.089 ms at length512 and 0.040 ms
at length65,536 (0.078 ms with no token limit). GPU mask
validation/length extraction for 32×65,536 takes 0.83 ms separately.
Measurements use ten repeated timing samples after warmup. **These are
not 64k model Prefill timings or the complete Auto-path overhead.**

**The original validation passed 105 related rollout/cache tests**,
including independent exhaustive partition comparisons for DP,
device-backed tiny-Qwen tests for grouped prefill and refill, both cache
APIs, effective generation-config/legacy refresh, repetition history,
EOS/PAD masks, capacity bounds and external-wall-time checks for
profiling. Existing tests cover retirement, compaction and trimming.
Independent mixed-bucket checks also exercised live-survivor refill and
trimming on/off against padded per-request HF generation.

One pre-existing native shared-prefill extension test is excluded
because the host has no CUDA compiler; **all added tests ran**.
Changed-file pre-commit and `git diff --check` pass. Repeated local
review ended with **no actionable findings**.

```sh
python -m pytest -q \
  tests/unit/runtime/rollout/test_hybrid_engine_rollout.py \
  tests/unit/runtime/rollout/test_continuous_batching.py \
  tests/unit/runtime/rollout/test_rollout_interface.py \
  tests/unit/utils/test_static_cache.py \
  -k 'not native_repeat_kv_cache_fp16_reverse_copy'
```

The prefill token limit does **not** bound allocated decode KV memory. A
single prompt over that limit raises an explicit error; there is no
chunked prefill. **64k prompts require a model context limit covering
prompt plus output budget, sufficient KV capacity and temporary
memory.** The measured checkpoint is configured for 32k.
Planner/position-limit boundaries are tested; real 64k inference
performance is not claimed.

Performance evidence uses unsharded HF models behind
`HybridEngineRollout`; the additional calibrated and real-question
checks are listed below. Other model/device configurations, FP16, native
kernel injection, ZeRO/TP training integration and production
arrival/EOS distributions remain unmeasured. Controlled performance runs
do not exercise staggered EOS or trimming; those have correctness
coverage.

No new dependency, kernel, online learner or trimming redesign. Related
foundation: [deepspeedai#8368](deepspeedai#8368);
profiling: [deepspeedai#8494](deepspeedai#8494).

## Reviewer follow-up

[Commit
65a9115](deepspeedai@65a9115)
propagates the resolved generation configuration through continuous
Prefill, Decode and refill, and omits `output_logits` from legacy manual
generation. **27 targeted tests pass**, including six new cases covering
both Auto routes, both cache APIs and manual equal-width/aligned
compatibility. The new tests reproduced four failures before the fix.
The compatibility test uses a strict generation wrapper that rejects the
unsupported keyword.

Calibration and evaluation tooling moved to [DeepSpeedExamples
deepspeedai#1018](deepspeedai/DeepSpeedExamples#1018), with
its fitting regression test and usage documentation. **Merge the
Examples companion before this PR so the documentation link is
available.** The Examples companion includes MHA-head fallback and
validates a source fingerprint covering rollout, planning and
static-cache code. A seeded tiny-model GPU smoke check completed
calibration and all four saved-config sweep workloads with matching
output IDs on every path. Two independent local reviews returned no
findings.

The calibrated sweep from the preceding routing-overhead fix is separate
from the original 16-token experiments above. It uses **two EOS-disabled
output tokens**, SDPA and one GPU, with independent uniform calibration
shapes and one unchanged coefficient set per configuration:

| Configuration | 128×16 | 1×128 + 31×4 | 16..512 | 1×512 + 31×16 |
Worst regression |
| --- | ---: | ---: | ---: | ---: | ---: |
| Tiny Qwen2 / FP32 | 1.0220× | 0.9685× | 0.9539× | 0.9647× | +2.205% |
| Qwen2.5-3B / FP32 | 1.0005× | 0.2345× | 0.7808× | 0.2265× | +0.054% |
| Qwen3-32B / BF16 | 1.0027× | 0.3881× | 0.7189× | 0.1811× | +0.273% |

Values are **Auto E2E / fastest manual baseline E2E**. The two larger
models select no-CB for 128×16 and bucketed CB for the other three
cases; the tiny model selects no-CB throughout. Auto matches ordinary
output IDs in all twelve cases. Target-model means use three interleaved
repetitions after warmup; the tiny test uses ten. [Full sweep details
and calibration
limits](deepspeedai#8786 (comment)).

A separate [Qwen3-32B real-question
check](deepspeedai#8786 (comment))
selected no-CB on both workloads: MATH-500 averaged **31.452 s / 31.393
s** for ordinary/Auto over three runs; ARC-AGI-2 measured **237.560 s /
237.888 s** in one paired run. All eight responses matched. These checks
cover complete generation calls, excluding tokenization, loading,
rewards and training.



A real tiny-OPT calibration additionally exposed a missing HF cache
capability flag. [Commit
ecacbce](deepspeedai@ecacbce)
adds `is_compileable=True` to the static-cache container, matching its
existing static layers. **Six targeted GPU checks pass**, including new
OPT manual/Auto comparisons with padded prompts and refill against the
HF oracle, plus existing Qwen bucket/cache-path checks. The three
Examples regression tests cover fitting, complete OPT calibration and
saved-source compatibility. Both local reviews found no issues in this
follow-up.

## Final correctness and performance acceptance

[Commit
d4def3d](deepspeedai@d4def3d)
includes the final fixes: non-default repetition penalties are
calculated in FP32 like HF, and unknown public generation switches are
rejected before routing. The default-penalty path retains its early
return. **Five focused BF16 checks and six unknown-setting CPU cases
pass.** The Examples CLI now requires complete metadata for saved
calibration files. Both independent correctness and performance reviews
returned no findings after these fixes.

A new calibration and sweep were run against measured commit `d4def3d`.
**Qwen3-32B, BF16, SDPA, one GPU; two EOS-disabled output tokens; one
warmup plus three interleaved timed runs per path.** Calibration and
held-out evaluation use separate shapes. The calibration file, sweep and
that measured revision's rollout/planner/static-cache sources have the
same combined SHA256:
`f9d79a43d2e6c0e3fef164ef11e81a26c643820beb1a0d3f1a86f41a46dbdd0c`.

| Workload | No-CB ms | Equal-width CB ms | Aligned CB ms | Auto ms |
Auto / best | Auto path |
| --- | ---: | ---: | ---: | ---: | ---: | --- |
| 128×16 | **269.83** | 439.91 | 5717.89 | 270.03 | 1.000743× | No-CB |
| 1×128 + 31×4 | **496.15** | 540.76 | 1501.57 | **191.96** | 0.386886×
| 2 buckets |
| 16,32,…,512 | 1962.87 | 2009.18 | **1864.55** | **1336.57** |
0.716828× | 6 buckets |
| 1×512 + 31×16 | 1962.56 | 2005.33 | **1575.08** | **288.31** |
0.183045× | 2 buckets |

**All four pass the 5% acceptance tolerance; worst measured regression
is +0.07428%. Auto output IDs match ordinary generation in every case**,
and both routes are exercised. Means and ratios were independently
recomputed from raw samples. The legacy aligned spread baseline retains
its previously recorded second-token difference on one row; Auto matches
no-CB there. Fixed output length prevents this numerical-sensitive
baseline difference from changing the amount of timed work.

The fitted coefficients are fixed=28.791679 ms, token=0.093362064 ms,
attention=0.010090454 ms/GFLOP, KV=0.056288540 ms/MiB and additional
decode=17.144541 ms/step. The earlier tables remain historical
measurements on their original versions. This final check does not
establish long-context, natural-EOS, 256-request E2E or dynamic-arrival
performance. Auto remains disabled by default.


[Commit
a0588b7](deepspeedai@a0588b7)
then fixes manual aligned prefill to retain the original padded
repetition history. Its focused GPU regression reproduced the mismatch
and now passes. Auto already passed this same mapping, and the measured
default-penalty paths do not read it; independent correctness and
performance delta reviews returned no findings. The table remains
attributed to `d4def3d`, with no timings or hashes relabeled as a
new-HEAD measurement. The Examples benchmark explicitly requires
Transformers >=4.56; the library's existing compatibility scope is
unchanged.

## Review invitation

cc @nathon-lee @delock — this follows your continuous-batching,
profiling, and aligned-cache work in deepspeedai#8368, deepspeedai#8494, and deepspeedai#8497. Could you
review the automatic route selection, bucket/refill cache semantics, and
cost defaults?


## November refresh

Rebased onto `b62afa8` and squashed to one commit; the PR is now
mergeable. The refresh keeps deepspeedai#8732 graph state, deepspeedai#8839 last-position
prefill logits, deepspeedai#8933 accelerator-neutral graph calls and deepspeedai#8852
`DeepSpeedStaticCache.get_query_offset()`. deepspeedai#8980's explicit accelerator
opt-in is respected: a backend without `is_graph_capture_supported()`
falls back to eager before any routing decision.

*Superseded by 8a9641b:* Auto now combines eager bucketed prefill with
fixed-slot graph decode (see the November 11 update). SegKI (deepspeedai#8602) is
not part of this PR.

Validation, 8×H200-class node with one GPU in use (NVIDIA H20Z, PyTorch
2.9.1+cu128, CUDA 12.8, Transformers 4.57.1):

| Check | Result |
| --- | --- |
| rollout/cache suite | 159 passed, 3 skipped, 1 deselected |
| Skips | Three Transformers 5.20-only query-offset cases added by deepspeedai#8852
|
| Deselected | Native shared-prefill kernel test; the test host has no
CUDA compiler |
| Changed-file pre-commit | Passed |

Graph replay, resolved-config propagation on both routes (FP32/BF16), DP
bucket/refill oracles, Auto/Graph rejection (since replaced by joint
routing) and manual graph dispatch all pass. No new performance numbers
are claimed: the tables above were measured before deepspeedai#8839/deepspeedai#8732, so
refreshed baselines need a separate calibration run if wanted.

AI assistance was used for the rebase, conflict resolution, test
execution and drafting of this description; the submitting human
reviewed the diff and the results above.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: SuperGoodGame <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants