Skip to content

Commit 49022e7

Browse files
fix: simplify CustomTextEmbedding __init__ parameter list
* fix(text): reduce parameter count in CustomTextEmbedding.__init__ Co-authored-by: n24q02m <135627235+n24q02m@users.noreply.github.com> * fix(text): reduce parameter count and fix formatting - Refactor CustomTextEmbedding.__init__ to use **kwargs. - Implement CustomTextEmbeddingWorker for parallel processing support. - Add unit test for CustomTextEmbeddingWorker. - Fix ruff formatting in tests/test_custom_text_embedding.py. Co-authored-by: n24q02m <135627235+n24q02m@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 5a57c96 commit 49022e7

2 files changed

Lines changed: 34 additions & 20 deletions

File tree

qwen3_embed/text/custom_text_embedding.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
from collections.abc import Iterable, Sequence
1+
from collections.abc import Iterable
22
from dataclasses import dataclass
33
from typing import Any
44

55
import numpy as np
66
from numpy.typing import NDArray
77

8-
from qwen3_embed.common import OnnxProvider
98
from qwen3_embed.common.model_description import (
109
DenseModelDescription,
1110
PoolingType,
1211
)
1312
from qwen3_embed.common.onnx_model import OnnxOutputContext
14-
from qwen3_embed.common.types import Device, NumpyArray
13+
from qwen3_embed.common.types import NumpyArray
1514
from qwen3_embed.common.utils import last_token_pool, mean_pooling, normalize
16-
from qwen3_embed.text.onnx_embedding import OnnxTextEmbedding
15+
from qwen3_embed.text.onnx_embedding import OnnxTextEmbedding, OnnxTextEmbeddingWorker
1716

1817

1918
@dataclass(frozen=True)
@@ -29,26 +28,10 @@ class CustomTextEmbedding(OnnxTextEmbedding):
2928
def __init__(
3029
self,
3130
model_name: str,
32-
cache_dir: str | None = None,
33-
threads: int | None = None,
34-
providers: Sequence[OnnxProvider] | None = None,
35-
cuda: bool | Device = Device.AUTO,
36-
device_ids: list[int] | None = None,
37-
lazy_load: bool = False,
38-
device_id: int | None = None,
39-
specific_model_path: str | None = None,
4031
**kwargs: Any,
4132
):
4233
super().__init__(
4334
model_name=model_name,
44-
cache_dir=cache_dir,
45-
threads=threads,
46-
providers=providers,
47-
cuda=cuda,
48-
device_ids=device_ids,
49-
lazy_load=lazy_load,
50-
device_id=device_id,
51-
specific_model_path=specific_model_path,
5235
**kwargs,
5336
)
5437
self._pooling = self.POSTPROCESSING_MAPPING[model_name].pooling
@@ -58,6 +41,10 @@ def __init__(
5841
def _list_supported_models(cls) -> list[DenseModelDescription]:
5942
return cls.SUPPORTED_MODELS
6043

44+
@classmethod
45+
def _get_worker_class(cls) -> type["CustomTextEmbeddingWorker"]:
46+
return CustomTextEmbeddingWorker
47+
6148
def _post_process_onnx_output(
6249
self, output: OnnxOutputContext, **kwargs: Any
6350
) -> Iterable[NumpyArray]:
@@ -103,3 +90,18 @@ def add_model(
10390
cls.POSTPROCESSING_MAPPING[model_description.model] = PostprocessingConfig(
10491
pooling=pooling, normalization=normalization
10592
)
93+
94+
95+
class CustomTextEmbeddingWorker(OnnxTextEmbeddingWorker):
96+
def init_embedding(
97+
self,
98+
model_name: str,
99+
cache_dir: str,
100+
**kwargs: Any,
101+
) -> CustomTextEmbedding:
102+
return CustomTextEmbedding(
103+
model_name=model_name,
104+
cache_dir=cache_dir,
105+
threads=1,
106+
**kwargs,
107+
)

tests/test_custom_text_embedding.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ def test_lazy_load_does_not_call_load(self, tmp_path: Path) -> None:
201201
CustomTextEmbedding(model_name=_MODEL_NAME, lazy_load=True)
202202
assert not called
203203

204+
def test_custom_worker_returns_correct_type(self, tmp_path: Path) -> None:
205+
from qwen3_embed.text.custom_text_embedding import CustomTextEmbeddingWorker
206+
207+
_register(pooling=PoolingType.CLS)
208+
with (
209+
patch.object(CustomTextEmbedding, "download_model", return_value=tmp_path),
210+
patch.object(CustomTextEmbedding, "load_onnx_model"),
211+
):
212+
worker = CustomTextEmbeddingWorker(_MODEL_NAME, str(tmp_path))
213+
emb = worker.init_embedding(_MODEL_NAME, str(tmp_path))
214+
assert isinstance(emb, CustomTextEmbedding)
215+
204216

205217
# ===========================================================================
206218
# CustomTextEmbedding._pool (lines 66-89)

0 commit comments

Comments
 (0)