-
Notifications
You must be signed in to change notification settings - Fork 15
Implement dropout for encode_minimal
#98
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
+387
−378
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
113a2fe
implement dropout for minimal encoding
marinegor db7dd30
make test text longer
marinegor aa49a37
make lint
marinegor a336729
implement review comments
marinegor 23c8ec9
update docs
marinegor 08d4200
implement dropout with forbidden_tokens
marinegor a10cce2
check single-len tokens as well
marinegor c215210
VERY Fast dropout implementation
aneubeck 8ad286a
merge aneubeck version
marinegor 5493b12
remove duplicated code
marinegor 3d63504
update README, docs, and include dropout into benchmarks
marinegor bba0765
update benchmarks and README description
marinegor 65eb519
Apply suggestions from code review
marinegor 3f0d4fe
implement review comments
marinegor 83dd2f1
remove dependency and update readme
marinegor 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
|---|---|---|
|
|
@@ -526,9 +526,9 @@ impl BytePairEncoding { | |
| /// tokenization produced by the original BPE algorithm. | ||
| pub fn encode_minimal(&self, text: &[u8]) -> Vec<u32> { | ||
| let mut last_token: Vec<(u32, u32)> = Vec::with_capacity(text.len()); | ||
| let mut state = self.overlapping_searcher.start_state(); | ||
| for (pos, c) in text.iter().enumerate() { | ||
| let (s, iter) = self.overlapping_searcher.consume(state, pos + 1, *c); | ||
| let mut state = self.overlapping_searcher_rev.start_state(); | ||
| for (pos, c) in text.iter().rev().enumerate() { | ||
| let (s, iter) = self.overlapping_searcher_rev.consume(state, pos + 1, *c); | ||
| state = s; | ||
| let mut best = (0, u32::MAX); | ||
| for m in iter { | ||
|
|
@@ -548,7 +548,66 @@ impl BytePairEncoding { | |
| encoded.push(token); | ||
| pos -= self.token_len(token); | ||
| } | ||
| encoded.reverse(); | ||
| encoded | ||
| } | ||
|
|
||
| /// This function computes the encoding while randomly rejecting some merges. | ||
| /// Result of the encoding will be non-deterministic unless `seed` is provided. | ||
| /// Implementation loosely follows original BPE dropout paper: https://arxiv.org/abs/1910.13267 | ||
| /// | ||
| /// In more detail: the tokenization uses dynamic programming, i.e. it models the tokenization as a graph, | ||
| /// where every position between text bytes is a node and two nodes are connected when the text slice between those two nodes matches a token. | ||
| // It then tries to find the shortest possible path from the beginning of the text till the end, i.e. it finds the shortest possible encoding. | ||
| // For this nodes are processed from right to left. At each node, edges starting at that node and ending on the right are tested and | ||
| // the one producing the shortest path is stored together with the length of the shortest path to that node. | ||
| // The length of the shortest path is stored as second value, the edge (or rather token) is stored as first value. | ||
| // Then, we walk in reverse direction through the table along the shortest path. | ||
| // Note: the reason for constructing the table from back to front is that | ||
| // the reconstruction outputs the path from start till end (i.e. we don't have to reverse the path afterwards). | ||
| // | ||
| // For the dropout (when dropout > 0.0), we uniformly drop edges from the graph, but always keep the one-byte tokens such that the graph stays connected. | ||
| // Note: this is very different from how BPE works and cannot produce the same output as the algorithm | ||
| // in the [paper's repository](https://github.com/VProv/BPE-Dropout/blob/master/bpe.py#L98), for two main reasons: | ||
| // - `encode_minimal` already doesn't follow the original heap-based BPE procedure | ||
| // - BPE-dropout authors discard all multi-byte tokens for each word separately, while this implementation does not split the "sentence" into words first | ||
| // and hence may include previously discarded token later down the byte stream. At the sentence level though we don't expect it to make much difference. | ||
|
Comment on lines
+572
to
+573
Collaborator
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. These are at least two/three distinct points in here
Contributor
Author
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. I think 1 and 3 were covered, I added 2. |
||
| // Also, this implementation of BPE constructs merges on the fly from the set of tokens, hence might come up with a different set of merges with the same dictionary. | ||
| #[cfg(feature = "rand")] | ||
| pub fn encode_minimal_dropout<R: rand::Rng>( | ||
| &self, | ||
| text: &[u8], | ||
| dropout: f32, | ||
| mut rng: R, | ||
| ) -> Vec<u32> { | ||
| assert!(0.0 <= dropout); | ||
| assert!(dropout <= 1.0); | ||
|
|
||
| let mut last_token: Vec<(u32, u32)> = Vec::with_capacity(text.len()); | ||
| let mut state = self.overlapping_searcher_rev.start_state(); | ||
| for (pos, c) in text.iter().rev().enumerate() { | ||
| let (s, iter) = self.overlapping_searcher_rev.consume(state, pos + 1, *c); | ||
| state = s; | ||
| let mut best = (0, u32::MAX); | ||
| for m in iter { | ||
| if m.end() > m.start() + 1 && dropout >= rng.random() { | ||
| continue; | ||
| } | ||
| if m.start() == 0 { | ||
| best = (m.value(), 1); | ||
| break; | ||
| } else if last_token[m.start() - 1].1 + 1 < best.1 { | ||
| best = (m.value(), last_token[m.start() - 1].1 + 1); | ||
| } | ||
| } | ||
| last_token.push(best); | ||
| } | ||
| let mut encoded = Vec::with_capacity(last_token.last().map(|l| l.1 as usize).unwrap_or(0)); | ||
| let mut pos = text.len(); | ||
| while pos > 0 { | ||
| let token = last_token[pos - 1].0; | ||
| encoded.push(token); | ||
| pos -= self.token_len(token); | ||
| } | ||
| encoded | ||
| } | ||
| } | ||
|
|
||
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
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.
Since you described all this, you should also add the last step where we walk in reverse direction through the table along the shortest path.
Note: the reason for constructing the table from back to front is that the reconstruction outputs the path from start till end (i.e. we don't have to reverse the path afterwards).
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.
added, thanks!