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

fix: Add explicit errors for BPE and unigram, return tokenizer without cha… #54

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion model2vec/distill/distillation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
from huggingface_hub import model_info
from sklearn.decomposition import PCA
from tokenizers.models import BPE, Unigram, WordPiece
from transformers import AutoModel, AutoTokenizer, PreTrainedModel, PreTrainedTokenizerFast

from model2vec.distill.inference import (
Expand Down Expand Up @@ -50,6 +51,14 @@

# Load original tokenizer. We need to keep this to tokenize any tokens in the vocabulary.
original_tokenizer: PreTrainedTokenizerFast = AutoTokenizer.from_pretrained(model_name)

if vocabulary and isinstance(original_tokenizer.backend_tokenizer.model, (BPE, Unigram)):
raise ValueError(

Check warning on line 56 in model2vec/distill/distillation.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/distillation.py#L56

Added line #L56 was not covered by tests
"You passed a vocabulary, but the model you are using does not use a WordPiece tokenizer. "
"This is not supported yet."
"Feel free to open an issue if this is a blocker: https://github.com/MinishLab/model2vec/issues"
)

original_model: PreTrainedModel = AutoModel.from_pretrained(model_name)
# Make a base list of tokens.
tokens: list[str] = []
Expand Down Expand Up @@ -79,7 +88,7 @@
# We need to set embeddings to None because we don't know the dimensions of the embeddings yet.
embeddings = None

if vocabulary is not None:
if vocabulary:
# Preprocess the vocabulary with the original tokenizer.
preprocessed_vocabulary = preprocess_vocabulary(original_tokenizer.backend_tokenizer, vocabulary)
n_tokens_before = len(preprocessed_vocabulary)
Expand Down
69 changes: 52 additions & 17 deletions model2vec/distill/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@
:param tokenizer: The tokenizer to remove tokens from.
:param tokens_to_remove: The tokens to remove.
:return: The modified tokenizer.
:raises ValueError: If the tokenizer model type is not supported.
"""
model_vocab = set(tokenizer.get_vocab())
# This triggers when tokens_to_remove is empty or when there is no overlap
# between the tokens to remove and the model vocabulary.
if not set(tokens_to_remove).intersection(model_vocab):
# NOTE: return a copy.
if tokens_to_remove:
logger.info("No tokens to remove, none of the tokens were in the vocabulary.")

Check warning on line 34 in model2vec/distill/tokenizer.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/tokenizer.py#L33-L34

Added lines #L33 - L34 were not covered by tests
else:
logger.info("No tokens to remove.")
return Tokenizer.from_str(tokenizer.to_str())

Check warning on line 37 in model2vec/distill/tokenizer.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/tokenizer.py#L36-L37

Added lines #L36 - L37 were not covered by tests

tokenizer_data = json.loads(tokenizer.to_str())

# Find all added tokens
Expand All @@ -35,20 +47,31 @@
tokens_to_remove = [token for token in tokens_to_remove if token not in added_tokens_str]

# Load the vocabulary.
vocab: dict[str, int] = tokenizer_data["model"]["vocab"]
n_tokens = len(vocab)

# Remove the tokens.
for token in tokens_to_remove:
if vocab.pop(token, None) is None:
logger.warning(f"Token {token} was not in the vocabulary.")

n_removed = n_tokens - len(vocab)
logger.info(f"Removed {n_removed} tokens from the vocabulary.")

# Reindex the vocabulary so that it is contiguous.
reindexed = {token: idx for idx, (token, _) in enumerate(sorted(vocab.items(), key=lambda x: x[1]))}
tokenizer_data["model"]["vocab"] = reindexed
model_type = tokenizer_data["model"]["type"]

match model_type:
case "WordPiece":
# Vocab is a dictionary.
vocab: dict[str, int] = tokenizer_data["model"]["vocab"]
n_tokens = len(vocab)

# Remove the tokens.
for token in tokens_to_remove:
if vocab.pop(token, None) is None:
logger.warning(f"Token {token} was not in the vocabulary.")

Check warning on line 61 in model2vec/distill/tokenizer.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/tokenizer.py#L61

Added line #L61 was not covered by tests

n_removed = n_tokens - len(vocab)
logger.info(f"Removed {n_removed} tokens from the vocabulary.")

# Reindex the vocabulary so that it is contiguous.
reindexed = {token: idx for idx, (token, _) in enumerate(sorted(vocab.items(), key=lambda x: x[1]))}
tokenizer_data["model"]["vocab"] = reindexed
case "Unigram":
raise ValueError("Removing tokens from a unigram tokenizer is not supported.")
case "BPE":
raise ValueError("Removing tokens from a bpe tokenizer is not supported.")
case _:
raise ValueError(f"Unknown model type {model_type}")

Check warning on line 74 in model2vec/distill/tokenizer.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/tokenizer.py#L69-L74

Added lines #L69 - L74 were not covered by tests

# Reindex the special tokens (i.e., CLS and SEP for BertTokenizers.)
special_tokens_post_processor: dict[str, dict] = tokenizer_data["post_processor"]["special_tokens"]
Expand All @@ -68,12 +91,24 @@
:param tokenizer: The tokenizer to add tokens to.
:param tokens_to_add: The tokens to add.
:return: The modified tokenizer.
:raises ValueError: If the tokenizer model type is not supported.
"""
data = json.loads(tokenizer.to_str())

vocab: dict[str, int] = data["model"]["vocab"]
for token in tokens_to_add:
vocab[token] = len(vocab)
model = data["model"]["type"]

match model:
case "WordPiece":
wordpiece_vocab: dict[str, int] = data["model"]["vocab"]
for token in tokens_to_add:
if token not in wordpiece_vocab:
wordpiece_vocab[token] = len(wordpiece_vocab)
case "Unigram":
raise ValueError("Adding tokens to a unigram tokenizer is not supported.")
case "BPE":
raise ValueError("Adding tokens to a bpe tokenizer is not supported.")
case _:
raise ValueError(f"Unknown model type {model}")

Check warning on line 111 in model2vec/distill/tokenizer.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/tokenizer.py#L106-L111

Added lines #L106 - L111 were not covered by tests

tokenizer = Tokenizer.from_str(json.dumps(data))

Expand Down