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

Question about Source Code #117

Open
Acatsama0871 opened this issue Jul 1, 2024 · 2 comments
Open

Question about Source Code #117

Acatsama0871 opened this issue Jul 1, 2024 · 2 comments

Comments

@Acatsama0871
Copy link

Hello,

I would first thank you for open-sourcing such a well-designed and high-quality code base.

I am reading the source code, and I have a question about this part(integrations.transformers.py):

def _build_regular_tokens_list(tokenizer: PreTrainedTokenizerBase) -> List[Tuple[int, str, bool]]:
    token_0 = tokenizer.encode("0")[-1]
    regular_tokens = []
    for token_idx in range(len(tokenizer)):
        if token_idx in tokenizer.all_special_ids:
            continue
        # We prepend token 0 and skip the first letter of the result to get a space if the token is a start word.
        decoded_after_0 = tokenizer.decode([token_0, token_idx])[1:]
        decoded_regular = tokenizer.decode([token_idx])
        is_word_start_token = len(decoded_after_0) > len(decoded_regular)
        regular_tokens.append((token_idx, decoded_after_0, is_word_start_token))
    return regular_tokens

Why the same token id is decoded twice here? And what does the "start word" mean in this context? thx

@noamgat
Copy link
Owner

noamgat commented Jul 1, 2024

We decode the token ID twice, once on its own, and once after the representation of 0, to understand whether its a start word token or not. Start word tokens will have an extra character when they are decoded after 0 compared to when they are not, and that is what we are checking.
Some tokenizers expose this directly, but this method is tokenizer agnostic.

@lone17
Copy link

lone17 commented Jul 30, 2024

We decode the token ID twice, once on its own, and once after the representation of 0, to understand whether its a start word token or not. Start word tokens will have an extra character when they are decoded after 0 compared to when they are not, and that is what we are checking. Some tokenizers expose this directly, but this method is tokenizer agnostic.

@noamgat Could you give an example of a tokenizer and input pair that has this behaviour ?

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

3 participants