-
Notifications
You must be signed in to change notification settings - Fork 527
Enable Gemma 4 E2B / E4B inference via vLLM RPA #4053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gagika
wants to merge
1
commit into
main
Choose a base branch
from
agagik-gemma4e-vllm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−15
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1099,7 +1099,7 @@ def __call__( | |||||||||||||||||||||||||
| kv_caches[index] = kv_cache | ||||||||||||||||||||||||||
| global_layer_idx_offset += num_layers | ||||||||||||||||||||||||||
| elif cfg.decoder_block == DecoderBlockType.GEMMA4_SMALL: | ||||||||||||||||||||||||||
| y = self._apply_gemma4_small_layers( | ||||||||||||||||||||||||||
| y, kv_caches = self._apply_gemma4_small_layers( | ||||||||||||||||||||||||||
| y, | ||||||||||||||||||||||||||
| decoder_input_tokens, | ||||||||||||||||||||||||||
| decoder_segment_ids, | ||||||||||||||||||||||||||
|
|
@@ -1378,8 +1378,12 @@ def _apply_gemma4_small_layers( | |||||||||||||||||||||||||
| * ``per_layer_inputs`` from PLE, sliced per layer. | ||||||||||||||||||||||||||
| * ``shared_kv_states``: donor-layer-index → (key, value) for | ||||||||||||||||||||||||||
| downstream KV-shared layers to consume. | ||||||||||||||||||||||||||
| * ``kv_caches``: when running via the vLLM RPA path, the per-layer | ||||||||||||||||||||||||||
| cache buffer threaded back from the kernel. KV-shared layers | ||||||||||||||||||||||||||
| redirect to the donor's cache slot via ``cache_index_of``. | ||||||||||||||||||||||||||
| Scan-over-layers and pipeline parallelism are not supported. | ||||||||||||||||||||||||||
| Returns ``(y, kv_caches)``. Scan-over-layers and pipeline | ||||||||||||||||||||||||||
| parallelism are not supported. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| cfg = self.config | ||||||||||||||||||||||||||
| mesh = self.mesh | ||||||||||||||||||||||||||
|
|
@@ -1397,6 +1401,18 @@ def _apply_gemma4_small_layers( | |||||||||||||||||||||||||
| num_kv_shared = cfg.num_kv_shared_layers | ||||||||||||||||||||||||||
| shared_kv_states: dict[int, tuple[jax.Array, jax.Array]] = {} | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # tpu-inference allocates one `kv_caches` slot per non-shared layer; | ||||||||||||||||||||||||||
| # KV-shared layers reuse the donor's slot. Map decoder layer index -> slot. | ||||||||||||||||||||||||||
| cache_index_of: dict[int, int] = {} | ||||||||||||||||||||||||||
| next_slot = 0 | ||||||||||||||||||||||||||
| for lyr in range(cfg.num_decoder_layers): | ||||||||||||||||||||||||||
| donor_idx = gemma4_small.kv_donor_layer_idx(lyr, layer_types, num_kv_shared) | ||||||||||||||||||||||||||
| if donor_idx is not None: | ||||||||||||||||||||||||||
| cache_index_of[lyr] = cache_index_of[donor_idx] | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 Rebuilding the `cache_index_of` dictionary on every call to `_apply_gemma4_small_layers` is redundant since the layer types and sharing pattern are static for a given model configuration. While the overhead is likely negligible for tracing, precomputing this mapping during initialization would be cleaner.
Suggested change
|
||||||||||||||||||||||||||
| cache_index_of[lyr] = next_slot | ||||||||||||||||||||||||||
| next_slot += 1 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| for lyr in range(cfg.num_decoder_layers): | ||||||||||||||||||||||||||
| attention_type = layer_types[lyr] | ||||||||||||||||||||||||||
| donor_idx = gemma4_small.kv_donor_layer_idx(lyr, layer_types, num_kv_shared) | ||||||||||||||||||||||||||
|
|
@@ -1433,8 +1449,9 @@ def _apply_gemma4_small_layers( | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ple_slice = per_layer_inputs[..., lyr, :] if per_layer_inputs is not None else None | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| kv_cache = kv_caches[lyr] if kv_caches is not None else None | ||||||||||||||||||||||||||
| y = layer( | ||||||||||||||||||||||||||
| cache_idx = cache_index_of[lyr] | ||||||||||||||||||||||||||
| kv_cache = kv_caches[cache_idx] if kv_caches is not None else None | ||||||||||||||||||||||||||
| y, kv_cache = layer( | ||||||||||||||||||||||||||
| y, | ||||||||||||||||||||||||||
| decoder_segment_ids, | ||||||||||||||||||||||||||
| decoder_positions, | ||||||||||||||||||||||||||
|
|
@@ -1449,8 +1466,10 @@ def _apply_gemma4_small_layers( | |||||||||||||||||||||||||
| shared_key=shared_key, | ||||||||||||||||||||||||||
| shared_value=shared_value, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| if kv_caches is not None and kv_cache is not None: | ||||||||||||||||||||||||||
| kv_caches[cache_idx] = kv_cache | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return y | ||||||||||||||||||||||||||
| return y, kv_caches | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # TODO(b/490118813): Relocate the following functions to their designated directories | ||||||||||||||||||||||||||
| # once the plug-in strategy is implemented: _find_next_boundary(), _apply_single_engram_layer() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.