Skip to content

fix: issue with unk in unigram #227

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

Merged
merged 1 commit into from
Apr 28, 2025
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
16 changes: 5 additions & 11 deletions model2vec/distill/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@
tokenizer_json: dict[str, Any], pre_tokenized_tokens: list[str], unk_token: str | None
) -> dict[str, Any]:
"""Process the WordPiece tokenizer JSON."""
unk_token = unk_token or tokenizer_json["model"]["unk_token"]
tokenizer_json["model"]["unk_token"] = "[UNK]" if unk_token else None
tokenizer_json["model"]["unk_token"] = unk_token
tokenizer_json["model"]["vocab"] = {token: idx for idx, token in enumerate(pre_tokenized_tokens)}

return tokenizer_json
Expand All @@ -128,20 +127,15 @@
return tokenizer_json


def _process_unigram(
tokenizer_json: dict[str, Any], pre_tokenized_tokens: list[str], unk_token: str | None
) -> dict[str, Any]:
def _process_unigram(tokenizer_json: dict[str, Any], pre_tokenized_tokens: list[str], unk_token: str) -> dict[str, Any]:
"""Process the Unigram tokenizer JSON."""
unk_id = tokenizer_json["model"]["unk_id"]
vocab = tokenizer_json["model"]["vocab"]
unk_token = vocab[unk_id][0] if unk_id is not None else None
current_probas = dict(tokenizer_json["model"]["vocab"])
avg_proba = sum(current_probas.values()) / len(current_probas)
new_probas = {word: current_probas.get(word, avg_proba) for word in pre_tokenized_tokens}
tokenizer_json["model"]["vocab"] = sorted(new_probas.items(), key=lambda x: x[1], reverse=True)

tokens, _ = zip(*tokenizer_json["model"]["vocab"])
tokenizer_json["model"]["unk_id"] = list(tokens).index(unk_token) if unk_token in tokens else None
tokenizer_json["model"]["unk_id"] = list(tokens).index(unk_token)

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

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/tokenizer.py#L138

Added line #L138 was not covered by tests

return tokenizer_json

Expand All @@ -168,11 +162,11 @@
tokenizer_json["added_tokens"] = [x for x in added_tokens if x["content"] in {"[UNK]", "[PAD]"}]

if model_type == "WordPiece":
tokenizer_json = _process_wordpiece(tokenizer_json, pre_tokenized_tokens, unk_token)
tokenizer_json = _process_wordpiece(tokenizer_json, pre_tokenized_tokens, "[UNK]")
elif model_type == "BPE":
tokenizer_json = _process_bpe(tokenizer_json, pre_tokenized_tokens)
elif model_type == "Unigram":
tokenizer_json = _process_unigram(tokenizer_json, pre_tokenized_tokens, unk_token)
tokenizer_json = _process_unigram(tokenizer_json, pre_tokenized_tokens, "[UNK]")

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

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/tokenizer.py#L169

Added line #L169 was not covered by tests
else:
raise ValueError(f"Unknown model type {model_type}")

Expand Down