Skip to content
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

Issue with SentencePieceUnigramTokenizer Handling Unknown Tokens #1576

Open
Munikumar09 opened this issue Jul 22, 2024 · 1 comment
Open

Comments

@Munikumar09
Copy link

Munikumar09 commented Jul 22, 2024

Description:

When using the SentencePieceUnigramTokenizer with a custom vocabulary, there is no attribute to handle the unk_id, causing errors when encoding text not present in the vocabulary.

Example:

  • Vocabulary: {'a': -1.23, 'b': -1.34, 'c': -1.45}
  • Encoding: tokenizer.encode("bcd")
    image

Suggested Fix:

class SentencePieceUnigramTokenizer(BaseTokenizer):
    def __init__(
        self,
        vocab: Optional[List[Tuple[str, float]]] = None,
        replacement: str = "▁",
        add_prefix_space: bool = True,
        unk_id: int = 0,
    ):
        if vocab is not None:
            tokenizer = Tokenizer(Unigram(vocab, unk_id=unk_id))
        else:
            tokenizer = Tokenizer(Unigram())

        tokenizer.normalizer = normalizers.Sequence([
            normalizers.Nmt(),
            normalizers.NFKC(),
            normalizers.Replace(Regex(" {2,}"), " "),
        ])
        tokenizer.pre_tokenizer = pre_tokenizers.Metaspace(replacement=replacement)
        tokenizer.decoder = decoders.Metaspace(replacement=replacement)

        parameters = {
            "model": "SentencePieceUnigram",
            "replacement": replacement,
            "add_prefix_space": add_prefix_space,
        }

        super().__init__(tokenizer, parameters)

Issue:

The current implementation does not handle the unk_id, leading to errors when encountering unknown tokens. Adding support for unk_id in the tokenizer initialization would resolve this issue.

Please let me know is there any existing solution to it.

@ArthurZucker
Copy link
Collaborator

Well, in this case the unk_id is missing from the vocab that you passed to the Tokenizer. Why not adding "<unk>":0 in the vocab?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants