-
Notifications
You must be signed in to change notification settings - Fork 194
feat(peagle): add random anchor subsampling (max_anchors) #687
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a3d9cc
feat(peagle): add random anchor subsampling (max_anchors)
orestis-z 0b1a398
Merge branch 'main' into peagle-max-anchors
orestis-z c973461
address PR #687 review: move max_anchors to trainer kwargs, default 3072
orestis-z 0d92463
fix(train): update --max-anchors default to 256 and help text
orestis-z 567d515
style: format dflash/core.py, revert unneeded peagle fallback
orestis-z 97142f7
Merge branch 'main' into peagle-max-anchors
orestis-z a4be571
Merge branch 'main' into peagle-max-anchors
orestis-z 113d297
refactor(peagle): list max_anchors as explicit forward param
orestis-z 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
Some comments aren't visible on the classic Files Changed page.
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
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,104 @@ | ||
| """Unit tests for P-EAGLE COD sampling with max_anchors.""" | ||
|
|
||
| import torch | ||
|
|
||
| from speculators.models.peagle.data import generate_cod_sample_indices | ||
|
|
||
|
|
||
| def _loss_mask(seq_length: int) -> torch.Tensor: | ||
| return torch.ones(1, seq_length, dtype=torch.float32) | ||
|
|
||
|
|
||
| class TestMaxAnchors: | ||
| def test_depth0_is_full_sequence(self): | ||
| """Depth 0 should always be the full sequence regardless of max_anchors.""" | ||
| seq_len = 32 | ||
| anchor_pos, depth = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=_loss_mask(seq_len), | ||
| num_depths=4, | ||
| max_anchors=4, | ||
| ) | ||
| depth0_positions = anchor_pos[depth == 0] | ||
| assert depth0_positions.shape[0] == seq_len | ||
| assert torch.equal(depth0_positions, torch.arange(seq_len)) | ||
|
|
||
| def test_max_anchors_caps_chains(self): | ||
| """With max_anchors, depth-1+ chains should not exceed max_anchors.""" | ||
| seq_len = 64 | ||
| max_anchors = 8 | ||
| anchor_pos, depth = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=_loss_mask(seq_len), | ||
| num_depths=4, | ||
| max_anchors=max_anchors, | ||
| ) | ||
| for d in range(1, 4): | ||
| assert (depth == d).sum().item() <= max_anchors | ||
|
|
||
| def test_max_anchors_preserves_full_depth0(self): | ||
| """Depth 0 count should equal seq_length even with small max_anchors.""" | ||
| seq_len = 128 | ||
| anchor_pos, depth = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=_loss_mask(seq_len), | ||
| num_depths=8, | ||
| max_anchors=4, | ||
| ) | ||
| assert (depth == 0).sum().item() == seq_len | ||
|
|
||
| def test_max_anchors_none_uses_all(self): | ||
| """max_anchors=None should use all valid positions (default behavior).""" | ||
| seq_len = 32 | ||
| torch.manual_seed(42) | ||
| anchor_pos_none, depth_none = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=_loss_mask(seq_len), | ||
| num_depths=4, | ||
| max_anchors=None, | ||
| ) | ||
| torch.manual_seed(42) | ||
| anchor_pos_default, depth_default = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=_loss_mask(seq_len), | ||
| num_depths=4, | ||
| ) | ||
| assert torch.equal(anchor_pos_none, anchor_pos_default) | ||
| assert torch.equal(depth_none, depth_default) | ||
|
|
||
| def test_max_anchors_fewer_valid_than_cap(self): | ||
| """When valid positions < max_anchors, all valid positions are used.""" | ||
| seq_len = 16 | ||
| loss_mask = torch.zeros(1, seq_len) | ||
| loss_mask[0, :5] = 1 | ||
| torch.manual_seed(42) | ||
| anchor_pos_capped, depth_capped = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=loss_mask, | ||
| num_depths=4, | ||
| max_anchors=100, | ||
| ) | ||
| torch.manual_seed(42) | ||
| anchor_pos_none, depth_none = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=loss_mask, | ||
| num_depths=4, | ||
| max_anchors=None, | ||
| ) | ||
| assert (depth_capped == 0).sum().item() == seq_len | ||
| assert torch.equal(anchor_pos_capped, anchor_pos_none) | ||
| assert torch.equal(depth_capped, depth_none) | ||
|
|
||
|
orestis-z marked this conversation as resolved.
|
||
| def test_max_anchors_sorted_order(self): | ||
| """Subsampled anchors should be in sorted order for causal masking.""" | ||
| seq_len = 64 | ||
| anchor_pos, depth = generate_cod_sample_indices( | ||
| seq_length=seq_len, | ||
| loss_mask=_loss_mask(seq_len), | ||
| num_depths=4, | ||
| max_anchors=8, | ||
| ) | ||
| for d in range(1, 4): | ||
| d_anchors = anchor_pos[depth == d] | ||
| if d_anchors.shape[0] > 1: | ||
| assert torch.all(d_anchors[1:] >= d_anchors[:-1]) | ||
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.