Repository navigation
Fix Qwen3.5 static-shape prefill (#18832) #21873
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
Open
yenhao-huang
wants to merge
8
commits into
pytorch:main
Choose a base branch
from
yenhao-huang:fix/18832-qwen35-dynamic-shape
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
776ee17
Fix Qwen3.5 static-shape prefill (#18832)
yenhao-huang 21999bd
Merge remote-tracking branch 'upstream/main' into fix/18832-qwen35-dy…
yenhao-huang f2f245c
Merge remote-tracking branch 'upstream/main' into fix/18832-qwen35-dy…
yenhao-huang 3b45450
Simplify sequential prefill into a single loop
yenhao-huang 1100aed
Merge branch 'main' into fix/18832-qwen35-dynamic-shape
yenhao-huang 6e97ce0
Merge branch 'main' into fix/18832-qwen35-dynamic-shape
yenhao-huang 85377a7
Merge branch 'main' into fix/18832-qwen35-dynamic-shape
yenhao-huang d2bc53d
Merge branch 'main' into fix/18832-qwen35-dynamic-shape
yenhao-huang 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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import torch | ||
| from executorch.examples.models.llama.runner.generation import LlamaRunner | ||
|
|
||
|
|
||
| class _Tokenizer: | ||
| n_words = 100 | ||
| eos_id = 99 | ||
|
|
||
| def decode_token(self, token: int) -> str: | ||
| return str(token) | ||
|
|
||
|
|
||
| class _RecordingRunner(LlamaRunner): | ||
| def __init__( | ||
| self, | ||
| *, | ||
| use_kv_cache: bool, | ||
| enable_dynamic_shape: bool, | ||
| ): | ||
| with patch( | ||
| "executorch.examples.models.llama.runner.generation.get_tokenizer", | ||
| return_value=_Tokenizer(), | ||
| ): | ||
| super().__init__( | ||
| tokenizer_path="unused", | ||
| max_seq_len=5, | ||
| max_batch_size=1, | ||
| use_kv_cache=use_kv_cache, | ||
| vocab_size=100, | ||
| enable_dynamic_shape=enable_dynamic_shape, | ||
| ) | ||
| self.calls = [] | ||
|
|
||
| def forward(self, tokens, input_pos=None): | ||
| if ( | ||
| self.use_kv_cache | ||
| and not self.enable_dynamic_shape | ||
| and tokens.shape != (1, 1) | ||
| ): | ||
| raise RuntimeError( | ||
| f"static input requires shape (1, 1), got {tokens.shape}" | ||
| ) | ||
| self.calls.append( | ||
| ( | ||
| tokens.flatten().tolist(), | ||
| None if input_pos is None else input_pos.item(), | ||
| ) | ||
| ) | ||
| last_token = tokens[0, -1].item() | ||
| sampled_token = 20 if last_token == 12 else 99 if last_token == 20 else 0 | ||
| logits = torch.full((1, 100), -1.0) | ||
| logits[0, sampled_token] = 1.0 | ||
| return logits | ||
|
|
||
|
|
||
| class GenerationTest(unittest.TestCase): | ||
| def test_static_kv_cache_prefills_one_token_at_a_time(self): | ||
| runner = _RecordingRunner(use_kv_cache=True, enable_dynamic_shape=False) | ||
|
|
||
| generated = runner.generate( | ||
| prompt_tokens=[10, 11, 12], | ||
| max_seq_len=5, | ||
| temperature=0, | ||
| pos_base=7, | ||
| ) | ||
|
|
||
| self.assertEqual(generated, [20, 99]) | ||
| self.assertEqual( | ||
| runner.calls, | ||
| [([10], 7), ([11], 8), ([12], 9), ([20], 10)], | ||
| ) | ||
|
|
||
| def test_dynamic_kv_cache_preserves_parallel_prefill(self): | ||
| runner = _RecordingRunner(use_kv_cache=True, enable_dynamic_shape=True) | ||
|
|
||
| generated = runner.generate( | ||
| prompt_tokens=[10, 11, 12], | ||
| max_seq_len=5, | ||
| temperature=0, | ||
| pos_base=7, | ||
| ) | ||
|
|
||
| self.assertEqual(generated, [20, 99]) | ||
| self.assertEqual(runner.calls, [([10, 11, 12], 7), ([20], 10)]) | ||
|
|
||
| def test_static_non_kv_cache_preserves_full_sequence_calls(self): | ||
| runner = _RecordingRunner(use_kv_cache=False, enable_dynamic_shape=False) | ||
|
|
||
| generated = runner.generate( | ||
| prompt_tokens=[10, 11, 12], | ||
| max_seq_len=5, | ||
| temperature=0, | ||
| pos_base=7, | ||
| ) | ||
|
|
||
| self.assertEqual(generated, [20, 99]) | ||
| self.assertEqual( | ||
| runner.calls, | ||
| [([10, 11, 12], None), ([10, 11, 12, 20], None)], | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
Uh oh!
There was an error while loading. Please reload this page.