-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Add implementation of typical sampling #15504
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
+94
−3
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f24c94
typical decoding
cimeister 30a7aba
changing arg name
cimeister b8ac67e
add test config params
cimeister 631ab51
forgotten arg rename
cimeister 33775ef
fix edge case where scores are same
cimeister 1b04f05
test for typical logits warper
cimeister 3e676cf
code quality fixes
cimeister 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
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 |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |
| TemperatureLogitsWarper, | ||
| TopKLogitsWarper, | ||
| TopPLogitsWarper, | ||
| TypicalLogitsWarper, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -191,6 +192,51 @@ def test_top_p_dist_warper(self): | |
| # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. | ||
| self.assertListEqual((filtered_dist != 0.0).to(torch.long).sum(dim=-1).tolist(), [3, 2]) | ||
|
|
||
| def test_typical_dist_warper(self): | ||
|
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. Very nice test! |
||
| input_ids = None | ||
| vocab_size = 10 | ||
| batch_size = 2 | ||
|
|
||
| # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) | ||
| dist = torch.log( | ||
| torch.tensor([[0.97, 0.01, 0.01, 0.01], [0.4, 0.2, 0.2, 0.2]], device=torch_device, dtype=torch.float) | ||
| ) | ||
|
|
||
| typical_warp = TypicalLogitsWarper(0.5) | ||
| filtered_dist = torch.exp(typical_warp(input_ids, dist)) | ||
|
|
||
| # dist should be filtered to keep min num values so that sum is >= 0.7 | ||
| # exp (-inf) => 0 | ||
| EXPECTED_FILTERED_DIST = torch.tensor( | ||
| [[0.97, 0.0, 0.0, 0.0], [0.0, 0.2, 0.2, 0.2]], device=torch_device, dtype=torch.float | ||
| ) | ||
| self.assertTrue(torch.allclose(filtered_dist, EXPECTED_FILTERED_DIST, atol=1e-3)) | ||
|
|
||
| # check special cases | ||
| length = 5 | ||
|
|
||
| logits = self._get_uniform_logits(batch_size=batch_size, length=length) | ||
| typical_warp_safety_check = TypicalLogitsWarper(mass=0.5, filter_value=0.0, min_tokens_to_keep=3) | ||
|
|
||
| scores = typical_warp_safety_check(input_ids, logits) | ||
| # uniform dist is not changed | ||
| self.assertListEqual((scores == 0.0).to(torch.long).sum(dim=-1).tolist(), [0, 0]) | ||
|
|
||
| # check edge cases with negative and extreme logits | ||
| ramp_logits = torch.arange(vocab_size, device=torch_device, dtype=torch.float).unsqueeze(0).repeat( | ||
| batch_size, 1 | ||
| ) - (vocab_size // 2) | ||
|
|
||
| # make ramp_logits more extreme | ||
| ramp_logits[1] = ramp_logits[1] * 100.0 | ||
|
|
||
| # make sure at least 2 tokens are kept | ||
| typical_warp = TypicalLogitsWarper(0.7, min_tokens_to_keep=2, filter_value=0.0) | ||
| filtered_dist = typical_warp(input_ids, ramp_logits) | ||
|
|
||
| # first batch should keep two tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. | ||
| self.assertListEqual((filtered_dist != 0.0).to(torch.long).sum(dim=-1).tolist(), [2, 2]) | ||
|
|
||
| def test_no_repeat_ngram_dist_processor(self): | ||
| vocab_size = 3 | ||
| batch_size = 2 | ||
|
|
||
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.
typical_p should not be equal to 1.0 IMO cc @harubaru