Skip to content

Commit dc1ff38

Browse files
authored
chore: remove quant from distill (#404)
1 parent 2993f4f commit dc1ff38

2 files changed

Lines changed: 4 additions & 62 deletions

File tree

‎model2vec/distill/distillation.py‎

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from model2vec.quantization import DType, quantize_embeddings
1919
from model2vec.tokenizer import clean_and_create_vocabulary, turn_tokens_into_ids
2020
from model2vec.types import StaticModelConfig
21-
from model2vec.vocabulary_quantization import quantize_vocabulary
2221

2322
logger = logging.getLogger(__name__)
2423

@@ -32,7 +31,6 @@ def distill_from_model(
3231
sif_coefficient: float | None = 1e-4,
3332
token_remove_pattern: str | None = r"\[unused\d+\]",
3433
quantize_to: DType | str = DType.Float16,
35-
vocabulary_quantization: int | None = None,
3634
pooling: PoolingMode | str = PoolingMode.MEAN,
3735
) -> StaticModel:
3836
"""Distill a staticmodel from a sentence transformer.
@@ -58,8 +56,6 @@ def distill_from_model(
5856
If the pattern is so general that it removes all tokens, we throw an error. If the pattern can't be compiled
5957
into a valid regex, we also throw an error.
6058
:param quantize_to: The data type to quantize to. Can be any of the DType enum members or their string equivalents.
61-
:param vocabulary_quantization: The number of clusters to use for vocabulary quantization. If this is None, no
62-
quantization is performed.
6359
:param pooling: The pooling mode to use for creating embeddings. Can be one of:
6460
'mean' (default): mean over all tokens. Robust and works well in most cases.
6561
'last': use the last token's hidden state (often the [EOS] token). Common for decoder-style models.
@@ -89,20 +85,10 @@ def distill_from_model(
8985
pooling=pooling,
9086
)
9187

92-
# Apply quantization
93-
if vocabulary_quantization is not None:
94-
weights = compute_weights(len(embeddings), sif_coefficient=sif_coefficient)
95-
embeddings, token_mapping, weights = quantize_vocabulary(
96-
n_clusters=vocabulary_quantization, weights=weights, embeddings=embeddings
97-
)
98-
embeddings = apply_pca(embeddings, pca_dims)
99-
else:
100-
# Post-process the embeddings.
101-
weights = compute_weights(len(embeddings), sif_coefficient=sif_coefficient)
102-
embeddings = apply_pca(embeddings, pca_dims)
103-
embeddings = embeddings * weights[:, None]
104-
weights = None
105-
token_mapping = None
88+
# Post-process the embeddings.
89+
weights = compute_weights(len(embeddings), sif_coefficient=sif_coefficient)
90+
embeddings = apply_pca(embeddings, pca_dims)
91+
embeddings = embeddings * weights[:, None]
10692
# Quantize the embeddings.
10793
embeddings = quantize_embeddings(embeddings, quantize_to)
10894

@@ -122,8 +108,6 @@ def distill_from_model(
122108

123109
return _create_static_model(
124110
embeddings=embeddings,
125-
weights=weights,
126-
token_mapping=token_mapping,
127111
tokenizer_model=tokenizer_model,
128112
config=config,
129113
model_name=model_name,
@@ -227,8 +211,6 @@ def init_from_tokenizer(
227211

228212
return _create_static_model(
229213
embeddings=embeddings,
230-
weights=None,
231-
token_mapping=None,
232214
tokenizer_model=tokenizer_model,
233215
config=config,
234216
model_name=tokenizer_name,
@@ -270,17 +252,13 @@ def _prepare_tokenizer(
270252

271253
def _create_static_model(
272254
embeddings: np.ndarray,
273-
weights: np.ndarray | None,
274-
token_mapping: np.ndarray | None,
275255
tokenizer_model: TokenizerModel,
276256
config: StaticModelConfig,
277257
model_name: str,
278258
) -> StaticModel:
279259
"""Create a StaticModel, looking up the language of the base model on the Hugging Face Hub.
280260
281261
:param embeddings: The embeddings.
282-
:param weights: The token weights, if any.
283-
:param token_mapping: The mapping from tokens to embeddings, if any.
284262
:param tokenizer_model: The tokenizer model.
285263
:param config: The config of the model.
286264
:param model_name: The name or path of the base model.
@@ -305,8 +283,6 @@ def _create_static_model(
305283

306284
return StaticModel(
307285
vectors=embeddings,
308-
weights=weights,
309-
token_mapping=token_mapping,
310286
tokenizer=tokenizer_model.to_tokenizer(),
311287
config=config,
312288
base_model_name=model_name,
@@ -352,7 +328,6 @@ def distill(
352328
token_remove_pattern: str | None = r"\[unused\d+\]",
353329
trust_remote_code: bool = False,
354330
quantize_to: DType | str = DType.Float16,
355-
vocabulary_quantization: int | None = None,
356331
pooling: PoolingMode | str = PoolingMode.MEAN,
357332
) -> StaticModel:
358333
"""Distill a staticmodel from a sentence transformer.
@@ -377,8 +352,6 @@ def distill(
377352
:param trust_remote_code: Whether to trust the remote code. If this is False, we will only load components coming
378353
from `transformers`. If this is True, we will load all components.
379354
:param quantize_to: The data type to quantize to. Can be any of the DType enum members or their string equivalents.
380-
:param vocabulary_quantization: The number of clusters to use for vocabulary quantization. If this is None, no
381-
quantization is performed.
382355
:param pooling: The pooling mode to use for creating embeddings. Can be one of:
383356
'mean' (default): mean over all tokens. Robust and works well in most cases.
384357
'last': use the last token's hidden state (often the [EOS] token). Common for decoder-style models.
@@ -402,6 +375,5 @@ def distill(
402375
token_remove_pattern=token_remove_pattern,
403376
sif_coefficient=sif_coefficient,
404377
quantize_to=quantize_to,
405-
vocabulary_quantization=vocabulary_quantization,
406378
pooling=pooling,
407379
)

‎tests/test_distillation.py‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,6 @@ def test_distill_from_model(
9393
assert token in static_model.tokens or normalized in static_model.tokens
9494

9595

96-
@patch.object(import_module("model2vec.distill.distillation"), "model_info")
97-
@patch("transformers.AutoModel.from_pretrained")
98-
def test_distill_quantization(
99-
mock_auto_model: MagicMock,
100-
mock_model_info: MagicMock,
101-
mock_berttokenizer: PreTrainedTokenizerFast,
102-
mock_transformer: PreTrainedModel,
103-
) -> None:
104-
"""Test distill function with different parameters."""
105-
# Mock the return value of model_info to avoid calling the Hugging Face API
106-
mock_model_info.return_value = type("ModelInfo", (object,), {"cardData": {"language": "en"}})
107-
mock_auto_model.return_value = mock_transformer
108-
109-
static_model = distill_from_model(
110-
model=mock_transformer,
111-
tokenizer=mock_berttokenizer,
112-
vocabulary=None,
113-
device="cpu",
114-
pca_dims="auto",
115-
sif_coefficient=1e-4,
116-
token_remove_pattern=None,
117-
vocabulary_quantization=3,
118-
)
119-
120-
assert static_model.embedding.shape == (3, 768)
121-
assert static_model.weights is not None
122-
assert static_model.token_mapping is not None
123-
assert len(static_model.weights) == static_model.tokenizer.get_vocab_size()
124-
125-
12696
@patch.object(import_module("model2vec.distill.distillation"), "model_info")
12797
@patch("transformers.AutoModel.from_pretrained")
12898
def test_distill_removal_pattern_all_tokens(

0 commit comments

Comments
 (0)