Skip to content

fix: normalization underflow in fp16 - #412

Merged
stephantul merged 2 commits into
mainfrom
fp16-quantization-fix
Nov 11, 2026
Merged

stephantul merged 2 commits into
mainfrom
fp16-quantization-fix

Conversation

@stephantul

@stephantul stephantul commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

closes #411

@greptile-apps

greptile-apps Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with one non-blocking memory-use improvement.

Reviews (2) · Last reviewed commit: "fix: normalization underflow in fp16" · Reviewed by Greptile

Comment thread model2vec/vocabulary_quantization.py Resolved
Comment thread model2vec/vocabulary_quantization.py Resolved
@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
model2vec/vocabulary_quantization.py 91.66% <100.00%> (+1.19%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stephantul
stephantul merged commit 253d598 into main Nov 11, 2026
11 checks passed
@stephantul
stephantul deleted the fp16-quantization-fix branch November 11, 2026 06:07
if weights is None:
weights = cast(np.ndarray, np.linalg.norm(embeddings, axis=1) + 1e-32)
float_dtype = embeddings.dtype if np.issubdtype(embeddings.dtype, np.floating) else np.dtype(np.float64)
compute_embeddings = embeddings.astype(np.result_type(embeddings.dtype, np.float32))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Clustering holds an extra matrix

For float32 embeddings without weights, astype copies the entire matrix even though its dtype is unchanged. compute_embeddings then keeps that extra matrix alive throughout KMeans.fit, adding avoidable memory use for large vocabularies. Use copy=False for the initial conversion and release the working array after normalization.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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.

Vocabulary quantization fails on finite FP16 embeddings containing a zero row

1 participant