33import json
44import math
55import os
6- from collections .abc import Iterator , Mapping , Sequence
6+ from collections .abc import Iterator , Sequence
77from logging import getLogger
88from pathlib import Path
99from tempfile import TemporaryDirectory
10- from typing import Any , cast , overload
10+ from typing import Any , Mapping , TypeVar , cast , overload
1111
1212import numpy as np
1313from joblib import delayed
@@ -191,7 +191,7 @@ def tokenize(self, sentences: Sequence[str], max_length: int | None = None) -> l
191191
192192 @classmethod
193193 def from_pretrained (
194- cls : type [StaticModel ],
194+ cls : type [T ],
195195 path : PathLike ,
196196 token : str | None = None ,
197197 normalize : bool | None = None ,
@@ -201,7 +201,7 @@ def from_pretrained(
201201 vocabulary_quantization : int | None = None ,
202202 max_length : int | None | _UnsetType = _UNSET ,
203203 force_download : bool = True ,
204- ) -> StaticModel :
204+ ) -> T :
205205 """Load a StaticModel from a local path or huggingface hub path.
206206
207207 NOTE: if you load a private model from the huggingface hub, you need to pass a token.
@@ -226,6 +226,7 @@ def from_pretrained(
226226 """
227227 return _loading_helper (
228228 cls = cls ,
229+ max_length = max_length ,
229230 path = path ,
230231 token = token ,
231232 vocabulary_quantization = vocabulary_quantization ,
@@ -234,7 +235,6 @@ def from_pretrained(
234235 normalize = normalize ,
235236 subfolder = subfolder ,
236237 force_download = force_download ,
237- max_length = max_length ,
238238 )
239239
240240 @overload
@@ -468,11 +468,11 @@ def push_to_hub(
468468
469469
470470def quantize_model (
471- model : StaticModel ,
471+ model : T ,
472472 vocabulary_quantization : int | None = None ,
473473 quantize_to : str | DType | None = None ,
474474 dimensionality : int | None = None ,
475- ) -> StaticModel :
475+ ) -> T :
476476 """Quantize the model to a lower precision and possibly lower dimensionality.
477477
478478 :param model: The model to quantize.
@@ -505,7 +505,7 @@ def quantize_model(
505505 dimensionality = dimensionality ,
506506 )
507507
508- return StaticModel (
508+ return type ( model ) (
509509 vectors = embeddings ,
510510 tokenizer = model .tokenizer ,
511511 config = model .config ,
@@ -519,7 +519,7 @@ def quantize_model(
519519
520520
521521def _loading_helper (
522- cls : type [StaticModel ],
522+ cls : type [T ],
523523 path : PathLike ,
524524 token : str | None ,
525525 vocabulary_quantization : int | None ,
@@ -529,7 +529,7 @@ def _loading_helper(
529529 subfolder : str | None ,
530530 force_download : bool ,
531531 max_length : int | None | _UnsetType ,
532- ) -> StaticModel :
532+ ) -> T :
533533 """Helper function to load a model from a directory."""
534534 from model2vec .persistence import load_pretrained
535535
@@ -581,3 +581,6 @@ def _get_unk_token_id(tokenizer: Tokenizer) -> int | None:
581581 return tokenizer .token_to_id (token )
582582 # Unigram
583583 return json .loads (tokenizer .to_str ())["model" ].get ("unk_id" )
584+
585+
586+ T = TypeVar ("T" , bound = StaticModel )
0 commit comments