From b1dd0970ee8d652419766035f98b9f5ef32daefa Mon Sep 17 00:00:00 2001 From: Aling33 <3489986145@qq.com> Date: Sun, 18 Oct 2026 02:55:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(static):=20=E4=BF=AE=E5=A4=8D=20float16?= =?UTF-8?q?=20=E9=9D=99=E6=80=81=E5=90=91=E9=87=8F=E5=8A=A0=E6=9D=83?= =?UTF-8?q?=E6=B1=A0=E5=8C=96=E7=B2=BE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastembed/text/static_embedding.py | 6 ++++- tests/test_static_embeddings.py | 35 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/fastembed/text/static_embedding.py b/fastembed/text/static_embedding.py index 853d8214..60f44cde 100644 --- a/fastembed/text/static_embedding.py +++ b/fastembed/text/static_embedding.py @@ -235,7 +235,11 @@ def _sum_rows(self, ids: NDArray[np.int64], lengths: NDArray[np.int64]) -> Numpy # a count matrix, which dominates for short texts. vectors = self.embeddings[rows] if self.weights is not None: - vectors = vectors * self.weights[ids, None] + vectors = np.multiply( + vectors, + self.weights[ids, None], + dtype=np.result_type(vectors.dtype, self.weights.dtype, np.float32), + ) return vectors.sum(axis=0, keepdims=True, dtype=np.float32) distinct_rows, row_index = np.unique(rows, return_inverse=True) if len(lengths) > 1 and len(lengths) * len(distinct_rows) > self.POOLING_CELLS: diff --git a/tests/test_static_embeddings.py b/tests/test_static_embeddings.py index 60f695e7..d18b3f01 100644 --- a/tests/test_static_embeddings.py +++ b/tests/test_static_embeddings.py @@ -5,6 +5,8 @@ import numpy as np import pytest +from tokenizers import Tokenizer +from tokenizers.models import WordLevel from fastembed.common.safetensors_utils import load_safetensors from fastembed.text.static_embedding import StaticTextEmbedding @@ -118,6 +120,39 @@ def _write_safetensors(path, tensors: dict[str, tuple[str, list[int], bytes]]) - f.write(data) +@pytest.mark.parametrize("mmap", [False, True]) +@pytest.mark.parametrize("dtype", ["F16", "F32"]) +@pytest.mark.parametrize("vector_scale,weight", [(1e-4, 1e-4), (1e4, 10.0)]) +def test_weighted_pooling_matches_across_batch_sizes(tmp_path, mmap, dtype, vector_scale, weight): + tokenizer = Tokenizer(WordLevel({"[UNK]": 0, "hello": 1}, unk_token="[UNK]")) + tokenizer.save(str(tmp_path / "tokenizer.json")) + numpy_dtype = " None: values = np.arange(6, dtype=np.float32).reshape(2, 3) / 4 From ed143d38bd4e234ba494dbaaa3a376b1ef81bd76 Mon Sep 17 00:00:00 2001 From: Aling33 <3489986145@qq.com> Date: Mon, 19 Oct 2026 18:17:24 +0800 Subject: [PATCH 2/3] test(static): document weighted pooling regression coverage --- tests/test_static_embeddings.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_static_embeddings.py b/tests/test_static_embeddings.py index d18b3f01..08048b3f 100644 --- a/tests/test_static_embeddings.py +++ b/tests/test_static_embeddings.py @@ -103,6 +103,7 @@ def test_embedding_size() -> None: def _write_safetensors(path, tensors: dict[str, tuple[str, list[int], bytes]]) -> None: + """Write a minimal safetensors fixture without downloading a model.""" header, data, offset = {}, b"", 0 for name, (dtype, shape, raw) in tensors.items(): header[name] = { @@ -124,6 +125,7 @@ def _write_safetensors(path, tensors: dict[str, tuple[str, list[int], bytes]]) - @pytest.mark.parametrize("dtype", ["F16", "F32"]) @pytest.mark.parametrize("vector_scale,weight", [(1e-4, 1e-4), (1e4, 10.0)]) def test_weighted_pooling_matches_across_batch_sizes(tmp_path, mmap, dtype, vector_scale, weight): + """Keep weighted pooling finite and batch-independent for float16 and float32.""" tokenizer = Tokenizer(WordLevel({"[UNK]": 0, "hello": 1}, unk_token="[UNK]")) tokenizer.save(str(tmp_path / "tokenizer.json")) numpy_dtype = " None: + """Decode supported tensor types equally with and without memory mapping.""" values = np.arange(6, dtype=np.float32).reshape(2, 3) / 4 bf16 = (values.view(np.uint32) >> 16).astype(" Date: Mon, 19 Oct 2026 18:17:48 +0800 Subject: [PATCH 3/3] style(tests): import Iterator from collections.abc --- tests/test_static_embeddings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_static_embeddings.py b/tests/test_static_embeddings.py index 08048b3f..22aa0df3 100644 --- a/tests/test_static_embeddings.py +++ b/tests/test_static_embeddings.py @@ -1,7 +1,7 @@ import json import os import warnings -from typing import Iterator +from collections.abc import Iterator import numpy as np import pytest