1818from model2vec .quantization import DType , quantize_embeddings
1919from model2vec .tokenizer import clean_and_create_vocabulary , turn_tokens_into_ids
2020from model2vec .types import StaticModelConfig
21- from model2vec .vocabulary_quantization import quantize_vocabulary
2221
2322logger = 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
271253def _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 )
0 commit comments