-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Unigram tokenizer fixes #7409
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
Unigram tokenizer fixes #7409
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,8 +62,27 @@ public SentencePieceUnigramModel(ModelProto modelProto, bool addBos, bool addEos | |
|
|
||
| _trie = new DoubleArrayTrie(_vocab); | ||
|
|
||
| _vocabReverse[BeginningOfSentenceId] = (BeginningOfSentenceToken, 0f, 0); | ||
| _vocabReverse[EndOfSentenceId] = (EndOfSentenceToken, 0f, 0); | ||
| // Once the trie is built, we need to add the special tokens to the vocabulary. | ||
| // Including these special tokens ensures they are mapped like regular tokens. | ||
| // SentencePiece specifically handles the BOS, EOS, and UNK tokens, while the PAD token is optional. | ||
|
|
||
| Debug.Assert(modelProto.TrainerSpec.UnkId >= 0); | ||
| Debug.Assert(modelProto.TrainerSpec.BosId >= 0); | ||
| Debug.Assert(modelProto.TrainerSpec.EosId >= 0); | ||
|
|
||
| _vocab[modelProto.TrainerSpec.UnkPiece] = modelProto.TrainerSpec.UnkId; | ||
|
Member
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. Was this present in the original tokenizer or is it special to our port? Asking because I don't understand why this isn't handled via
Member
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. This is special to our port to ensure adding these special tokens to the vocabs for easier lookup. Adding these here are not changing any behavior more than allowing the vocabulary to map these tokens which help us in some operations like decoding for example. I'll add some detailed comment here. Thanks!
Member
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. Can you know for sure that
Member
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.
This is done this way to avoid adding these special tokens to the
I can check but I want to know what you suggest doing when for any reason have wrong data, just throw exception?
Member
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. Explicit exception might be better than index out of range, but your call. This has made me wonder twice - in initial PR and here - so this logic warrants a comment in source to explain what's going on. |
||
| _vocab[modelProto.TrainerSpec.BosPiece] = modelProto.TrainerSpec.BosId; | ||
| _vocab[modelProto.TrainerSpec.EosPiece] = modelProto.TrainerSpec.EosId; | ||
|
|
||
| _vocabReverse[modelProto.TrainerSpec.BosId] = (modelProto.TrainerSpec.BosPiece, 0f, ModelProto.Types.SentencePiece.Types.Type.Control); | ||
| _vocabReverse[modelProto.TrainerSpec.EosId] = (modelProto.TrainerSpec.EosPiece, 0f, ModelProto.Types.SentencePiece.Types.Type.Control); | ||
| _vocabReverse[modelProto.TrainerSpec.UnkId] = (modelProto.TrainerSpec.UnkPiece, 0f, ModelProto.Types.SentencePiece.Types.Type.Unknown); | ||
|
|
||
| if (modelProto.TrainerSpec.PadId >= 0) | ||
| { | ||
| _vocab[modelProto.TrainerSpec.PadPiece] = modelProto.TrainerSpec.PadId; | ||
| _vocabReverse[modelProto.TrainerSpec.PadId] = (modelProto.TrainerSpec.PadPiece, 0f, ModelProto.Types.SentencePiece.Types.Type.Control); | ||
| } | ||
| } | ||
|
|
||
| public SentencePieceUnigramModel(SentencePieceOptions options) : base(options) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.