Skip to content

fix(static): 修复 float16 静态向量加权池化精度 - #828

Open
Aling33 wants to merge 3 commits into
qdrant:mainfrom
Aling33:codex/fix-static-weighted-pooling-precision
Open

Aling33 wants to merge 3 commits into
qdrant:mainfrom
Aling33:codex/fix-static-weighted-pooling-precision

Conversation

@Aling33

@Aling33 Aling33 commented Oct 17, 2026 •

Copy link
Copy Markdown

提交检查

  • 已遵循贡献指南。
  • 已检查相同问题的开放和关闭 PR,未发现重复。

问题与修复

载入 float16 静态向量和权重时,同一文本的嵌入结果会随 batch_size 改变。单条路径先用 float16 相乘,小数值会下溢成零,大数值会溢出并在归一化后产生 NaN;批量路径则使用 float32 计数矩阵,得到正常的单位向量。

本次用 np.multiply 显式选择至少为 float32 的乘法精度,并保留更高精度输入的类型提升行为。没有新增依赖。

回归测试通过公开的 TextEmbedding 接口载入本地生成的 safetensors 和 tokenizer,覆盖 float16/float32、小数值/大数值、普通读取/内存映射。测试同时比较单条与批量结果,并检查有限性和预期方向。修复前 4 个 float16 用例失败,4 个 float32 对照通过;修复后全部通过。

验证

Python 3.12.9,NumPy 2.5.4。

CI=1 HF_HUB_DISABLE_TELEMETRY=1 FASTEMBED_CACHE_PATH=<独立临时目录> python -m pytest tests/test_static_embeddings.py tests/test_common.py -q
38 passed

mypy fastembed --disallow-incomplete-defs --disallow-untyped-defs --disable-error-code=import-untyped
Success: no issues found in 68 source files

pyright tests/type_stub.py
0 errors, 0 warnings, 0 informations

ruff check fastembed/text/static_embedding.py tests/test_static_embeddings.py
All checks passed!

ruff format --check fastembed/text/static_embedding.py tests/test_static_embeddings.py
2 files already formatted

git diff --check
通过

Ruff 使用项目 pre-commit 固定的 0.3.4 版本。相关 #826 修改 ColBERT/LateOn 的归一化,不涉及本次静态模型加权池化路径。

本次问题调查、代码修改和回归验证使用了 Codex AI 辅助。

2026-10-19 验证补充

  • 为精度回归测试、safetensors 测试夹具及相邻解码测试补充文档字符串;精度计算实现保持不变。
  • 更新测试文件中 Iterator 的标准库导入,满足当前 Ruff 规则。
  • 本地定向验证:pytest tests/test_static_embeddings.py -q -k "weighted_pooling_matches_across_batch_sizes or load_safetensors":13 项通过,20 项未选中;Ruff 与差异空白检查通过。
  • 上游 Tests/type-checkers 的 GitHub Actions 仍需有写入权限的维护者授权;本地验证不替代尚未运行的上游检查。

@Aling33
Aling33 requested a review from joein as a code owner October 17, 2026 18:55
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 59c0c126-bd86-46c6-b33d-e7ecbf12f259
📥 Commits

Reviewing files that changed from the base of the PR and between b1dd097 and 45b85f6.

📒 Files selected for processing: 1
  • tests/test_static_embeddings.py
🚧 Files skipped from review as they are similar to previous changes: 1
  • tests/test_static_embeddings.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The single-document weighted-vector path now selects an explicit result dtype for multiplication. A regression test checks output dtype, finiteness, batch-size agreement, and normalized values across tensor types, mmap settings, and scale combinations.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 45b85

The precision fix is mergeable after normal checks; no actionable regression was identified.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed 标题简洁、具体,并准确概括了修复 float16 静态向量加权池化精度问题这一主要变更。
Description check Passed 描述与代码变更直接相关,说明了 float16 下溢和溢出问题、修复方案、回归测试范围及验证结果。
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant