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

Correctly handle sentencepiece byte-fallback tokens #3

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/deepfocus/vocab_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dataclasses import dataclass

import numpy as np
import regex
from torch import Tensor
from tqdm import tqdm
from transformers import PreTrainedTokenizer
Expand Down Expand Up @@ -59,6 +60,11 @@ def replace_space(tokenizer: PreTrainedTokenizer, token_id: int):
"""For XLM-R tokenizer (sentencepiece-style)"""
decoded_token = tokenizer.decode(token_id)
token = tokenizer.convert_ids_to_tokens(token_id)

# For sentencepiece ByteFallback tokens used in Llama, Mistral et al.
if regex.match(r"<0x[0-9,A-F]{2}>", token):
return token, False

is_beginning_of_word = token.startswith(XLMR_WHITESPACE)
if is_beginning_of_word:
return XLMR_WHITESPACE + decoded_token.lstrip(), True
Expand Down