Skip to content

Commit dd8f595

Browse files
committed
fix: remove all added tokens except important ones
1 parent 35cca62 commit dd8f595

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

model2vec/distill/distillation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ def distill_from_model(
9090
tokenizer_model = clean_and_create_vocabulary(tokenizer_model, vocabulary, token_remove_regex=token_remove_regex)
9191
# Remove the post processor, this is not necessary.
9292
tokenizer_model.post_processor = None
93-
# Reshape the model
93+
# Prune again now that the post processor is gone.
94+
# We can't do this before because we need the post preocessor and associated
95+
# tokens before to add eos/bos.
96+
tokenizer_model = tokenizer_model.prune_added_tokens()
9497

9598
# All tokens in a single list.
9699
all_tokens = tokenizer_model.sorted_vocabulary

tests/test_distillation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ def test_distill_removal_pattern(
115115
mock_model_info.return_value = type("ModelInfo", (object,), {"cardData": {"language": "en"}})
116116
mock_auto_model.return_value = mock_transformer
117117

118-
# Because the added [MASK] gets removed
119-
expected_vocab_size = mock_berttokenizer.vocab_size - 1
118+
# Because the added [MASK], [CLS] and [SEP] get removed
119+
expected_vocab_size = mock_berttokenizer.vocab_size - 3
120120

121121
static_model = distill_from_model(
122122
model=mock_transformer,
@@ -161,14 +161,14 @@ def test_distill_removal_pattern(
161161
@pytest.mark.parametrize(
162162
"vocabulary, pca_dims, sif_coefficient, expected_shape",
163163
[
164-
(None, 256, None, (30521, 256)), # PCA applied, SIF off
165-
(None, "auto", None, (30521, 768)), # PCA 'auto', SIF off
166-
(None, "auto", 1e-4, (30521, 768)), # PCA 'auto', SIF on
164+
(None, 256, None, (30519, 256)), # PCA applied, SIF off
165+
(None, "auto", None, (30519, 768)), # PCA 'auto', SIF off
166+
(None, "auto", 1e-4, (30519, 768)), # PCA 'auto', SIF on
167167
(None, "auto", 0, None), # invalid SIF (too low) -> raises
168168
(None, "auto", 1, None), # invalid SIF (too high) -> raises
169-
(None, 1024, None, (30521, 768)), # PCA set high (no reduction)
170-
(["wordA", "wordB"], 4, None, (30523, 4)), # Custom vocab, PCA applied
171-
(None, None, None, (30521, 768)), # No PCA, SIF off
169+
(None, 1024, None, (30519, 768)), # PCA set high (no reduction)
170+
(["wordA", "wordB"], 4, None, (30521, 4)), # Custom vocab, PCA applied
171+
(None, None, None, (30519, 768)), # No PCA, SIF off
172172
],
173173
)
174174
@patch.object(import_module("model2vec.distill.distillation"), "model_info")

0 commit comments

Comments
 (0)