-
Notifications
You must be signed in to change notification settings - Fork 1
update lm head last token pruning #288
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
rui-ren
wants to merge
6
commits into
main
Choose a base branch
from
ruiren/update-lm-head-last-token-pruning
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.
+107
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e198a68
update-genai integration
rui-ren b65192d
LM-head-prune
rui-ren 9ec4e7a
Merge remote-tracking branch 'origin/main' into ruiren/update-lm-head…
Copilot 573edb8
conflict
rui-ren bc1f6e8
source
rui-ren 0e78f1c
refactor: move prune_lm_head from global flag to CausalLMTask option
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,6 +181,67 @@ | |
| assert model.output_layer_indices == [0, 3] | ||
|
|
||
|
|
||
| class TestPruneLmHead: | ||
| """Tests for the ``prune_lm_head`` option in :class:`CausalLMTask`. | ||
|
|
||
| When ``True``, a ``Gather + Unsqueeze`` is inserted after the LM head | ||
| so logits are produced for only the last sequence position. | ||
| Mirrors onnxruntime-genai Model Builder's ``prune_lm_head`` opt-in. | ||
| """ | ||
|
|
||
| def _build(self, prune_lm_head: bool = False) -> ir.Model: | ||
| config = make_config() | ||
| module = CausalLMModel(config) | ||
| task = CausalLMTask(prune_lm_head=prune_lm_head) | ||
| return build_from_module(module, config, task=task)["model"] | ||
|
|
||
| def test_default_emits_no_lm_head_pruning(self): | ||
| """Default (prune_lm_head=False): graph emits full [B, S, vocab] logits.""" | ||
| model = self._build(prune_lm_head=False) | ||
|
|
||
| logits = next(v for v in model.graph.outputs if v.name == "logits") | ||
| # Logits must remain rank-3 [B, S, vocab] | ||
| assert len(logits.shape) == 3, ( | ||
| f"Expected rank-3 logits [B, S, V], got rank {len(logits.shape)}: " | ||
| f"shape={list(logits.shape)!r}" | ||
| ) | ||
| # The full path: shape[1] is a symbolic dim ("sequence_length"), not 1 | ||
| seq_dim = logits.shape[1] | ||
| assert seq_dim != 1, ( | ||
| f"Expected dynamic sequence_length in logits dim 1, got {seq_dim!r}" | ||
| ) | ||
| # Last dim is the vocabulary size | ||
| config = make_config() | ||
| assert logits.shape[2] == config.vocab_size | ||
|
|
||
| def test_prune_emits_gather_on_logits(self): | ||
| """With prune_lm_head=True, logits dim 1 collapses to 1 (still rank-3).""" | ||
| model = self._build(prune_lm_head=True) | ||
|
|
||
| logits = next(v for v in model.graph.outputs if v.name == "logits") | ||
| # Logits must still be rank-3 [B, 1, vocab] (NOT rank-4 [B, 1, 1, V]) | ||
| assert len(logits.shape) == 3, ( | ||
| f"Expected rank-3 logits [B, 1, V] after pruning, got rank " | ||
| f"{len(logits.shape)}: shape={list(logits.shape)!r}" | ||
| ) | ||
| # Pruned: dim 1 must be the literal integer 1 | ||
| seq_dim = logits.shape[1] | ||
| assert seq_dim == 1, f"Expected logits dim 1 to be 1 after pruning, got {seq_dim!r}" | ||
| # Last dim is still the vocabulary size | ||
| config = make_config() | ||
| assert logits.shape[2] == config.vocab_size | ||
|
|
||
| def test_prune_does_not_change_input_shapes(self): | ||
| """Pruning only affects output; input_ids still has dynamic sequence_length | ||
Check warningCode scanning / lintrunner RUFF/D205 Warning
1 blank line required between summary line and description.
See https://docs.astral.sh/ruff/rules/missing-blank-line-after-summary |
||
|
|
||
| so the model accepts arbitrary prompts. | ||
| """ | ||
| model = self._build(prune_lm_head=True) | ||
|
|
||
| input_ids = next(v for v in model.graph.inputs if v.name == "input_ids") | ||
| # input dim 1 (sequence_length) should still be dynamic, not 1 | ||
| assert input_ids.shape[1] != 1 | ||
|
|
||
|
|
||
| class TestModelRegistry: | ||
| def test_registry_not_empty(self): | ||
| assert len(registry) > 0 | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.