feat(opsd): share prompt prefill across rollout samples - #8296
Conversation
645d5e3 to
2601fc9
Compare
Signed-off-by: nathon-lee <leejianwoo@gmail.com>
2601fc9 to
7ce74d1
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7ce74d1d46
ℹ️ 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".
|
Hi @nathon-lee thanks for your PR. The performance data looks great! Noting picking inference engine v1 means we need to invest into inference kernels for modern models (Qwen for example), which current v1 engine does not support yet. But from the perspective of OPD/OPSD rollout v1 engine should be a good start point. @sfc-gh-truwase @PKUWZP |
|
@nathon-lee one comments is we will need to run the test under GPU in order to cover C++ kernels. Your benchmark test validates GPU but batch size =1 does not cover the reverse copy algorithm that is targeted for situation when batch>1. Can you make UT cover C++ kernels as well? Thanks! |
|
Thanks, @delock! I agree this is a gap. The current CPU tests only cover the reverse-copy ordering through the Python fallback, while the existing GPU benchmark uses B=1 and therefore doesn’t exercise the overlapping source rows. I’ll add a CUDA-only native-kernel test with B=2 and repeats=2 to cover this case. It will verify that the C++ repeat_kv_cache implementation produces [A, A, B, B] and matches the fallback behavior. The test will also skip cleanly when CUDA or the native inference extension is unavailable. |
Exercise the FP16 native kernel with two source cache rows and two repeats to verify the overlap-safe reverse-copy ordering. Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Use the minimum FP16 vector width so the native transformer kernel launches with a valid CUDA block configuration. Signed-off-by: nathon-lee <leejianwoo@gmail.com>
|
Hi @delock, thanks very much for taking a look! I’ve added a CUDA-only native-kernel test with B=2 and repeats=2. It exercises the overlapping reverse-copy path and verifies that the resulting cache ordering is [A, A, B, B]. I also confirmed that the focused GPU test passes with the native inference extension: 1 passed, 18 deselected. Thanks again for your review! |
Summary
This PR adds an opt‑in shared‑prefill path for
HybridEngineRollout.When one prompt branches into multiple response samples, the current rollout
path expands the prompt batch before generation and computes the same prompt
prefill once for every response branch.
With shared prefill enabled, HybridEngine now:
For the tested OPT‑6.7B workload with four samples per prompt, this reduced
end‑to‑end rollout latency by 23.77% while preserving identical generated
tokens.
Motivation
For a rollout with:
the default path expands the prompt batch to
B * Nbefore the first modelforward.
This repeats the same prompt prefill
Ntimes even though all response brancheshave identical prompt tokens and prompt KV state.
Profiling showed that this redundant prefill work is a significant part of the
total latency when generating multiple responses per prompt.
Implementation
Shared prompt forward
This PR adds the default‑off configuration option:
For
samples_per_prompt > 1, the first model forward is reduced from theexpanded
B * Nprompt batch to the originalBunique prompts.The first‑forward logits are expanded back to
B * Nbefore sampling.Sampling and all subsequent decode forwards remain independent for every
response branch.
Native KV‑cache expansion
The transformer inference extension now records the allocated workspace
metadata required to address the KV cache independently from the current
forward batch:
The prompt KV cache is exposed using its native layout:
The cache is expanded from
BtoB * Nin place.Copies proceed from the destination batch in reverse order. This preserves
source rows that overlap the expanded destination range and avoids allocating
a full temporary clone of the prompt KV cache.
The returned key and value tensors are zero‑copy views into the HybridEngine
inference workspace.
Fallback implementation
The Python fallback workspace implements the same cache‑expansion ordering and
returned‑cache contract for environments that do not use the native transformer
inference extension.
Current restrictions
Shared prefill is disabled by default and currently requires:
It currently cannot be combined with:
release_inference_cacheUnsupported combinations fail explicitly instead of silently falling back to
an incorrect execution path.
Performance validation
The following A/B measurements were collected before the benchmark was moved
to DeepSpeedExamples. The benchmark itself is not included in this PR.
Environment
facebook/opt‑6.7bResults
For the tested deterministic workload, the baseline and shared‑prefill paths
produced the same response‑token hash:
The executable benchmark and its CLI integration will be submitted separately
to DeepSpeedExamples, following the repository‑maintainer guidance.
Validation
The native transformer inference extension was rebuilt and verified to export
the new operation:
Focused unit tests:
Result:
Tests cover:
The modified files also pass the repository pre‑commit hooks.
Scope
This PR implements prompt sharing only for multiple response samples derived
from the same prompt within one rollout call.
It does not implement:
The executable benchmark is being moved to DeepSpeedExamples as a separate
change.
Related to #8197.