Skip to content

Commit 26373f1

Browse files
authored
feat: make setters for max_length (#356)
* feat: make setters for max_length * also take care of onnx * remove repr from unset * fix tests * consistency * additional tests for coverage
1 parent ca622e5 commit 26373f1

15 files changed

Lines changed: 227 additions & 119 deletions

‎model2vec/distill/distillation.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from model2vec.model import StaticModel
1717
from model2vec.quantization import DType, quantize_embeddings
1818
from model2vec.tokenizer import clean_and_create_vocabulary, turn_tokens_into_ids
19+
from model2vec.types import StaticModelConfig
1920
from model2vec.vocabulary_quantization import quantize_vocabulary
2021

2122
logger = logging.getLogger(__name__)
@@ -125,7 +126,7 @@ def distill_from_model(
125126

126127
model_name = getattr(model, "name_or_path", "")
127128

128-
config = {
129+
config: StaticModelConfig = {
129130
"model_type": "model2vec",
130131
"architectures": ["StaticModel"],
131132
"tokenizer_name": model_name,

‎model2vec/inference/model.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections.abc import Sequence
55
from pathlib import Path
66
from tempfile import TemporaryDirectory
7-
from typing import Any, TypeVar, cast
7+
from typing import TypeVar, cast
88

99
import huggingface_hub
1010
import numpy as np
@@ -15,6 +15,7 @@
1515
from model2vec.inference.mlp import Activation, Layer, MLPHead
1616
from model2vec.model import PathLike, StaticModel
1717
from model2vec.persistence import save_pretrained
18+
from model2vec.types import _UNSET, StaticModelConfig, _UnsetType
1819

1920
_DEFAULT_HEAD_FILENAME = "head.safetensors"
2021
_LEGACY_HEAD_FILENAME = "pipeline.skops"
@@ -74,7 +75,7 @@ def _encode_and_coerce_to_2d(
7475
self,
7576
X: Sequence[str],
7677
show_progress_bar: bool,
77-
max_length: int | None,
78+
max_length: int | None | _UnsetType,
7879
batch_size: int,
7980
use_multiprocessing: bool,
8081
multiprocessing_threshold: int,
@@ -97,7 +98,7 @@ def predict(
9798
self,
9899
X: Sequence[str],
99100
show_progress_bar: bool = False,
100-
max_length: int | None = 512,
101+
max_length: int | None | _UnsetType = _UNSET,
101102
batch_size: int = 1024,
102103
use_multiprocessing: bool = True,
103104
multiprocessing_threshold: int = 10_000,
@@ -107,7 +108,8 @@ def predict(
107108
108109
:param X: The input data to predict. Can be a list of strings or a single string.
109110
:param show_progress_bar: Whether to display a progress bar during prediction. Defaults to False.
110-
:param max_length: The maximum length of the input sequences. Defaults to 512.
111+
:param max_length: The maximum length of the input sequences. If not passed, the encoder model's
112+
`max_length` is used. Pass `max_length=None` to disable truncation.
111113
:param batch_size: The batch size for prediction. Defaults to 1024.
112114
:param use_multiprocessing: Whether to use multiprocessing for encoding. Defaults to True.
113115
:param multiprocessing_threshold: The threshold for the number of samples to use multiprocessing. Defaults to 10,000.
@@ -139,7 +141,7 @@ def predict_proba(
139141
self,
140142
X: Sequence[str],
141143
show_progress_bar: bool = False,
142-
max_length: int | None = 512,
144+
max_length: int | None | _UnsetType = _UNSET,
143145
batch_size: int = 1024,
144146
use_multiprocessing: bool = True,
145147
multiprocessing_threshold: int = 10_000,
@@ -148,7 +150,8 @@ def predict_proba(
148150
149151
:param X: The input data to predict. Can be a list of strings or a single string.
150152
:param show_progress_bar: Whether to display a progress bar during prediction. Defaults to False.
151-
:param max_length: The maximum length of the input sequences. Defaults to 512.
153+
:param max_length: The maximum length of the input sequences. If not passed, the encoder model's
154+
`max_length` is used. Pass `max_length=None` to disable truncation.
152155
:param batch_size: The batch size for prediction. Defaults to 1024.
153156
:param use_multiprocessing: Whether to use multiprocessing for encoding. Defaults to True.
154157
:param multiprocessing_threshold: The threshold for the number of samples to use multiprocessing. Defaults to 10,000.
@@ -220,7 +223,7 @@ def _load_pipeline(folder_or_repo_path: PathLike, token: str | None = None) -> t
220223

221224
model = StaticModel.from_pretrained(folder_or_repo_path)
222225

223-
head_config = cast(dict[str, Any], model.config.get("head_config", {}))
226+
head_config = model.config.get("head_config", {})
224227
activation = Activation(head_config.get("activation", Activation.IDENTITY.value))
225228
n_layers = head_config.get("n_layers", 0)
226229
classes = head_config.get("classes")
@@ -337,7 +340,7 @@ def _save_pipeline(pipeline: StaticModelPipeline, folder_path: str | Path) -> No
337340
save_file(tensors, folder_path / _DEFAULT_HEAD_FILENAME)
338341

339342
model = pipeline.model
340-
config = dict(model.config)
343+
config: StaticModelConfig = {**model.config}
341344
config["head_config"] = {
342345
"n_layers": len(head.layers),
343346
"activation": head.activation.value,

0 commit comments

Comments
 (0)