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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
from __future__ import annotations

import math
import os
from logging import getLogger
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any, Union, overload

import numpy as np
from joblib import delayed
from tqdm import tqdm

from .quantization import DType, quantize_and_reduce_dim
from .utils import ProgressParallel, load_local_model

if TYPE_CHECKING:
	from collections.abc import Iterator, Sequence

	from tokenizers import Encoding, Tokenizer

PathLike = Union[Path, str]

logger = getLogger(__name__)


class StaticModel:
	def __init__(
		self,
		vectors: np.ndarray,
		tokenizer: Tokenizer,
		config: dict[str, Any] | None = None,
		normalize: bool | None = None,
		base_model_name: str | None = None,
		language: list[str] | None = None,
	) -> None:
		"""
		Initialize the StaticModel.

		:param vectors: The vectors to use.
		:param tokenizer: The Transformers tokenizer to use.
		:param config: Any metadata config.
		:param normalize: Whether to normalize the embeddings.
		:param base_model_name: The used base model name. Used for creating a model card.
		:param language: The language of the model. Used for creating a model card.
		:raises: ValueError if the number of tokens does not match the number of vectors.
		"""
		super().__init__()
		tokens, _ = zip(*sorted(tokenizer.get_vocab().items(), key=lambda x: x[1]), strict=False)
		self.tokens = tokens

		self.embedding = vectors

		if len(tokens) != vectors.shape[0]:
			msg = f"Number of tokens ({len(tokens)}) does not match number of vectors ({vectors.shape[0]})"
			raise ValueError(msg)

		self.tokenizer = tokenizer
		self.unk_token_id: int | None
		if hasattr(self.tokenizer.model, "unk_token") and self.tokenizer.model.unk_token is not None:
			self.unk_token_id = tokenizer.get_vocab()[self.tokenizer.model.unk_token]
		else:
			self.unk_token_id = None  # pragma: no cover  # Doesn't actually happen, but can happen.

		self.median_token_length = int(np.median([len(token) for token in self.tokens]))
		self.config = config or {}
		self.base_model_name = base_model_name
		self.language = language
		if hasattr(self.tokenizer, "encode_batch_fast"):
			self._can_encode_fast = True
		else:
			self._can_encode_fast = False

		if normalize is not None:
			self.normalize = normalize
		else:
			self.normalize = self.config.get("normalize", False)

	@property
	def dim(self) -> int:
		"""Get the dimension of the model."""
		return self.embedding.shape[1]

	@property
	def normalize(self) -> bool:
		"""
		Get the normalize value.

		:return: The normalize value.
		"""
		return self._normalize

	@normalize.setter
	def normalize(self, value: bool) -> None:
		"""Update the config if the value of normalize changes."""
		config_normalize = self.config.get("normalize")
		self._normalize = value
		if config_normalize is not None and value != config_normalize:
			logger.warning(
				f"Set normalization to `{value}`, which does not match config value `{config_normalize}`. Updating config."
			)
		self.config["normalize"] = value

	def save_pretrained(self, path: PathLike, model_name: str | None = None, subfolder: str | None = None) -> None:
		"""
		Save the pretrained model.

		:param path: The path to save to.
		:param model_name: The model name to use in the Model Card.
		:param subfolder: The subfolder to save to.
		"""
		from .hf_utils import save_pretrained

		save_pretrained(
			folder_path=Path(path),
			embeddings=self.embedding,
			tokenizer=self.tokenizer,
			config=self.config,
			base_model_name=self.base_model_name,
			language=self.language,
			model_name=model_name,
			subfolder=subfolder,
		)

	def tokenize(self, sentences: Sequence[str], max_length: int | None = None) -> list[list[int]]:
		"""
		Tokenize a list of sentences.

		:param sentences: The sentences to tokenize.
		:param max_length: The maximum length of the sentences in tokens. If this is None, sequences
		    are not truncated.
		:return: A list of list of tokens.
		"""
		if max_length is not None:
			m = max_length * self.median_token_length
			sentences = [sentence[:m] for sentence in sentences]

		if self._can_encode_fast:
			encodings: list[Encoding] = self.tokenizer.encode_batch_fast(sentences, add_special_tokens=False)
		else:
			encodings = self.tokenizer.encode_batch(sentences, add_special_tokens=False)

		encodings_ids = [encoding.ids for encoding in encodings]

		if self.unk_token_id is not None:
			# NOTE: Remove the unknown token: necessary for word-level models.
			encodings_ids = [
				[token_id for token_id in token_ids if token_id != self.unk_token_id] for token_ids in encodings_ids
			]
		if max_length is not None:
			encodings_ids = [token_ids[:max_length] for token_ids in encodings_ids]

		return encodings_ids

	@classmethod
	def from_pretrained(
		cls: type[StaticModel],
		path: PathLike,
		token: str | None = None,
		normalize: bool | None = None,
		subfolder: str | None = None,
		quantize_to: str | DType | None = None,
		dimensionality: int | None = None,
	) -> StaticModel:
		"""
		Load a StaticModel from a local path or huggingface hub path.

		NOTE: if you load a private model from the huggingface hub, you need to pass a token.

		:param path: The path to load your static model from.
		:param token: The huggingface token to use.
		:param normalize: Whether to normalize the embeddings.
		:param subfolder: The subfolder to load from.
		:param quantize_to: The dtype to quantize the model to. If None, no quantization is done.
		    If a string is passed, it is converted to a DType.
		:param dimensionality: The dimensionality of the model. If this is None, use the dimensionality of the model.
		    This is useful if you want to load a model with a lower dimensionality.
		    Note that this only applies if you have trained your model using mrl or PCA.
		:return: A StaticModel.
		"""
		from .hf_utils import load_pretrained

		embeddings, tokenizer, config, metadata = load_pretrained(
			folder_or_repo_path=path,
			token=token,
			from_sentence_transformers=False,
			subfolder=subfolder,
		)

		embeddings = quantize_and_reduce_dim(
			embeddings=embeddings,
			quantize_to=quantize_to,
			dimensionality=dimensionality,
		)

		return cls(
			embeddings,
			tokenizer,
			config,
			normalize=normalize,
			base_model_name=metadata.get("base_model"),
			language=metadata.get("language"),
		)

	@classmethod
	def from_sentence_transformers(
		cls: type[StaticModel],
		path: PathLike,
		token: str | None = None,
		normalize: bool | None = None,
		quantize_to: str | DType | None = None,
		dimensionality: int | None = None,
	) -> StaticModel:
		"""
		Load a StaticModel trained with sentence transformers from a local path or huggingface hub path.

		NOTE: if you load a private model from the huggingface hub, you need to pass a token.

		:param path: The path to load your static model from.
		:param token: The huggingface token to use.
		:param normalize: Whether to normalize the embeddings.
		:param quantize_to: The dtype to quantize the model to. If None, no quantization is done.
		    If a string is passed, it is converted to a DType.
		:param dimensionality: The dimensionality of the model. If this is None, use the dimensionality of the model.
		    This is useful if you want to load a model with a lower dimensionality.
		    Note that this only applies if you have trained your model using mrl or PCA.
		:return: A StaticModel.
		"""
		from .hf_utils import load_pretrained

		embeddings, tokenizer, config, metadata = load_pretrained(
			folder_or_repo_path=path,
			token=token,
			from_sentence_transformers=True,
			subfolder=None,
		)

		embeddings = quantize_and_reduce_dim(
			embeddings=embeddings,
			quantize_to=quantize_to,
			dimensionality=dimensionality,
		)

		return cls(
			embeddings,
			tokenizer,
			config,
			normalize=normalize,
			base_model_name=metadata.get("base_model"),
			language=metadata.get("language"),
		)

	@overload
	def encode_as_sequence(
		self,
		sentences: str,
		max_length: int | None = None,
		batch_size: int = 1024,
		show_progress_bar: bool = False,
		use_multiprocessing: bool = True,
		multiprocessing_threshold: int = 10_000,
	) -> np.ndarray: ...

	@overload
	def encode_as_sequence(
		self,
		sentences: list[str],
		max_length: int | None = None,
		batch_size: int = 1024,
		show_progress_bar: bool = False,
		use_multiprocessing: bool = True,
		multiprocessing_threshold: int = 10_000,
	) -> list[np.ndarray]: ...

	def encode_as_sequence(
		self,
		sentences: str | list[str],
		max_length: int | None = None,
		batch_size: int = 1024,
		show_progress_bar: bool = False,
		use_multiprocessing: bool = True,
		multiprocessing_threshold: int = 10_000,
	) -> list[np.ndarray] | np.ndarray:
		"""
		Encode a list of sentences as a list of numpy arrays of tokens.

		This is useful if you want to use the tokens for further processing, or if you want to do sequence
		modeling.
		Note that if you just want the mean, you should use the `encode` method.
		This is about twice as slow.
		Sentences that do not contain any tokens will be turned into an empty array.

		NOTE: the input type is currently underspecified. The actual input type is `Sequence[str] | str`, but this
		    is not possible to implement in python typing currently.

		:param sentences: The list of sentences to encode.
		:param max_length: The maximum length of the sentences. Any tokens beyond this length will be truncated.
		    If this is None, no truncation is done.
		:param batch_size: The batch size to use.
		:param show_progress_bar: Whether to show the progress bar.
		:param use_multiprocessing: Whether to use multiprocessing.
		    By default, this is enabled for inputs > multiprocessing_threshold sentences and disabled otherwise.
		:param multiprocessing_threshold: The threshold in number of sentences for using multiprocessing.
		:return: The encoded sentences with an embedding per token.
		"""
		was_single = False
		if isinstance(sentences, str):
			sentences = [sentences]
			was_single = True

		# Prepare all batches
		sentence_batches = list(self._batch(sentences, batch_size))
		total_batches = math.ceil(len(sentences) / batch_size)

		# Use joblib for multiprocessing if requested, and if we have enough sentences
		if use_multiprocessing and len(sentences) > multiprocessing_threshold:
			# Disable parallelism for tokenizers
			os.environ["TOKENIZERS_PARALLELISM"] = "false"

			results = ProgressParallel(n_jobs=-1, use_tqdm=show_progress_bar, total=total_batches)(
				delayed(self._encode_batch_as_sequence)(batch, max_length) for batch in sentence_batches
			)
			out_array: list[np.ndarray] = []
			for r in results:
				out_array.extend(r)
		else:
			out_array = []
			for batch in tqdm(
				sentence_batches,
				total=total_batches,
				disable=not show_progress_bar,
			):
				out_array.extend(self._encode_batch_as_sequence(batch, max_length))

		if was_single:
			return out_array[0]
		return out_array

	def _encode_batch_as_sequence(self, sentences: Sequence[str], max_length: int | None) -> list[np.ndarray]:
		"""Encode a batch of sentences as a sequence."""
		ids = self.tokenize(sentences=sentences, max_length=max_length)
		out: list[np.ndarray] = []
		for id_list in ids:
			if id_list:
				out.append(self.embedding[id_list])
			else:
				out.append(np.zeros((0, self.dim)))

		return out

	def encode(
		self,
		sentences: Sequence[str],
		show_progress_bar: bool = False,
		max_length: int | None = 512,
		batch_size: int = 1024,
		use_multiprocessing: bool = True,
		multiprocessing_threshold: int = 10_000,
		**kwargs: Any,
	) -> np.ndarray:
		"""
		Encode a list of sentences.

		This function encodes a list of sentences by averaging the word embeddings of the tokens in the sentence.
		For ease of use, we don't batch sentences together.

		NOTE: the return type is currently underspecified. In the case of a single string, this returns a 1D array,
		    but in the case of a list of strings, this returns a 2D array. Not possible to implement in numpy currently.

		:param sentences: The list of sentences to encode. You can also pass a single sentence.
		:param show_progress_bar: Whether to show the progress bar.
		:param max_length: The maximum length of the sentences. Any tokens beyond this length will be truncated.
		    If this is None, no truncation is done.
		:param batch_size: The batch size to use.
		:param use_multiprocessing: Whether to use multiprocessing.
		    By default, this is enabled for inputs > multiprocessing_threshold sentences and disabled otherwise.
		:param multiprocessing_threshold: The threshold in number of sentences for using multiprocessing.
		:param **kwargs: Any additional arguments. These are ignored.
		:return: The encoded sentences. If a single sentence was passed, a vector is returned.
		"""
		was_single = False
		if isinstance(sentences, str):
			sentences = [sentences]
			was_single = True

		# Prepare all batches
		sentence_batches = list(self._batch(sentences, batch_size))
		total_batches = math.ceil(len(sentences) / batch_size)

		# Use joblib for multiprocessing if requested, and if we have enough sentences
		if use_multiprocessing and len(sentences) > multiprocessing_threshold:
			# Disable parallelism for tokenizers
			os.environ["TOKENIZERS_PARALLELISM"] = "false"

			results = ProgressParallel(n_jobs=-1, use_tqdm=show_progress_bar, total=total_batches)(
				delayed(self._encode_batch)(batch, max_length) for batch in sentence_batches
			)
			out_array = np.concatenate(results, axis=0)
		else:
			# Don't use multiprocessing
			out_arrays: list[np.ndarray] = []
			for batch in tqdm(
				sentence_batches,
				total=total_batches,
				disable=not show_progress_bar,
			):
				out_arrays.append(self._encode_batch(batch, max_length))
			out_array = np.concatenate(out_arrays, axis=0)

		if was_single:
			return out_array[0]
		return out_array

	def _encode_batch(self, sentences: Sequence[str], max_length: int | None) -> np.ndarray:
		"""Encode a batch of sentences."""
		ids = self.tokenize(sentences=sentences, max_length=max_length)
		out: list[np.ndarray] = []
		for id_list in ids:
			if id_list:
				out.append(self.embedding[id_list].mean(0))
			else:
				out.append(np.zeros(self.dim))

		out_array = np.stack(out)
		if self.normalize:
			norm = np.linalg.norm(out_array, axis=1, keepdims=True) + 1e-32
			out_array = out_array / norm

		return out_array

	@staticmethod
	def _batch(sentences: Sequence[str], batch_size: int) -> Iterator[Sequence[str]]:
		"""Batch the sentences into equal-sized."""
		return (sentences[i : i + batch_size] for i in range(0, len(sentences), batch_size))

	def push_to_hub(
		self, repo_id: str, private: bool = False, token: str | None = None, subfolder: str | None = None
	) -> None:
		"""
		Push the model to the huggingface hub.

		NOTE: you need to pass a token if you are pushing a private model.

		:param repo_id: The repo id to push to.
		:param private: Whether the repo, if created is set to private.
		    If the repo already exists, this doesn't change the visibility.
		:param token: The huggingface token to use.
		:param subfolder: The subfolder to push to.
		"""
		from .hf_utils import push_folder_to_hub

		with TemporaryDirectory() as temp_dir:
			self.save_pretrained(temp_dir, model_name=repo_id)
			push_folder_to_hub(Path(temp_dir), subfolder=subfolder, repo_id=repo_id, private=private, token=token)

	@classmethod
	def load_local(cls: type[StaticModel], path: PathLike) -> StaticModel:
		"""
		Loads a model from a local path.

		You should only use this code path if you are concerned with start-up time.
		Loading via the `from_pretrained` method is safer, and auto-downloads, but
		also means we import a whole bunch of huggingface code that we don't need.

		Additionally, huggingface will check the most recent version of the model,
		which can be slow.

		:param path: The path to load the model from. The path is a directory saved by the
		    `save_pretrained` method.
		:return: A StaticModel
		:raises: ValueError if the path is not a directory.
		"""
		path = Path(path)
		if not path.is_dir():
			msg = f"Path {path} is not a directory."
			raise ValueError(msg)

		embeddings, tokenizer, config = load_local_model(path)

		return StaticModel(embeddings, tokenizer, config)