Skip to content

Support Transformers 5.20 cache query offsets in DeepSpeedStaticCache - #8852

Draft
nathon-lee wants to merge 13 commits into
deepspeedai:masterfrom
nathon-lee:fix/static-cache-query-offset
Draft

nathon-lee wants to merge 13 commits into
deepspeedai:masterfrom
nathon-lee:fix/static-cache-query-offset

Conversation

@nathon-lee

@nathon-lee nathon-lee commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Background

Native Llama continuous rollout on Transformers 5.20.1 failed because DeepSpeedStaticCache lacked get_query_offset(layer_idx). The query offset must exclude the current write; using get_seq_length() would be off by one.

Changes

Implemented and validated on fix/static-cache-query-offset:

  • Added layer and cache query-offset methods: zero when unset, the scalar write position otherwise, or the maximum per-row position with row validity handled by attention masks.
  • Preserved cache-length and KV-write behavior without capture-time host synchronization.
  • Added independent masking and CUDA replay regressions, plus native Llama generation comparisons covering eager / SDPA attention, padding, slot refill, and graph reuse.

Validation

RTX A4500 (20 GiB), PyTorch 2.9.1+cu128 / CUDA 12.8, FP16:

Transformers Results
5.20.1 76 passed, 1 skipped
4.57.6 73 passed, 4 skipped

Actual CUDA capture/replay passed. Randomly initialized tiny Llama rollouts matched native generate() tokens, attention masks, and response offsets. Both versions skipped the optional pretrained-model test; 4.57.6 also skipped three tests requiring the newer masking API. Modified-file pre-commit checks passed.

Run in each Transformers environment:

python -m pytest \
    tests/unit/utils/test_static_cache.py \
    tests/unit/runtime/rollout/test_hybrid_engine_rollout.py \
    tests/unit/runtime/rollout/test_continuous_batching.py \
    tests/unit/v1/test_hybrid_engine_rollout_v1.py -q

Scope and dependencies

Depends on the cache code from #8732, not the benchmark. Changes are limited to compatibility, tests, and documentation; this does not claim full Transformers 5.20 cache API support. Issue 1 depends on this fix in the tested 5.20.1 environment.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Cache continuous-rollout CUDA graph state across matching generate calls.
Rebuild the graph when the model parameters or static decode shape changes,
and reset static buffers and KV cache rows before reuse.

Use eager attention and a preallocated additive causal mask during capture
to avoid Transformers capture-time mask allocations. Relax cache-class
detection for models that support the cache interface without declaring the
private capability flag.

Add coverage for eager-attention capture, graph reuse, weight invalidation,
and current StaticCache update semantics.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Use the preallocated additive causal mask to make CUDA Graph capture safe
without overriding the model attention implementation. This preserves default
SDPA behavior and restores eager/graph token parity for pretrained GPT-2.

Update graph-capture coverage to verify the configured attention
implementation remains unchanged.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Populate continuous rollout cache statistics after CUDA Graph decoding so callers do not receive stale eager-path metrics. Cover graph cache capacity, peak usage, decode steps, and trimming metrics.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Validate fixed-slot CUDA Graph options before capture and cover unsupported decode alignment, cache trimming, and insufficient cache capacity.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Add CUDA rollout coverage for pretrained Hugging Face models behind DEEPSPEED_PRETRAINED_ROLLOUT_MODEL so regular CI does not download weights.
Validate eager and CUDA Graph parity across prompts and verify graph reuse.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Route dummy writes for non-decoding graph slots to a scratch cache position so refilled prompts retain their prefetched KV state.
Add CUDA coverage for a slot refill while another request continues decoding.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Reject flash_attention_2 before continuous CUDA Graph capture because
the fixed replay path requires a four-dimensional additive attention mask.
Document the restriction and cover the configuration error.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Reject models that explicitly lack cache-class support before the
continuous CUDA Graph path creates static cache state.
Add regression coverage for the graph-specific validation path.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
…raction

Keep static_cache_position in _ContinuousGraphState so it remains alive
for the lifetime of the captured graph.

Add is_current_stream_capturing() to the accelerator interface, with a
default False implementation and a CUDA override. Route the static cache
capture check through this interface instead of calling torch.cuda directly.

Add regression coverage for tensor lifetime, graph replay, capture state,
and eager cache bounds checks, and document the new accelerator method.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
…cCache

Signed-off-by: nathon-lee <leejianwoo@gmail.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant