Repository navigation
fix: quantize int8 in float32 to avoid float16 rounding and overflow - #406
Closed
selimfedakar wants to merge 1 commit into
Closed
selimfedakar wants to merge 1 commit into
selimfedakar wants to merge 1 commit into
Conversation
The int8 path in quantize_embeddings scaled the embeddings through a float16 buffer before rounding. float16 keeps only ~3 significant digits, so the scaled values round differently than the exact quotient and a non-trivial fraction of codes flip by +/-1 even for ordinary, small-magnitude embeddings (about 0.65% of codes, touching ~80% of rows on a normal 2000x256 matrix at std 0.1). Any embedding value above the float16 maximum (65504) also overflowed to inf before scaling. Compute the scaled values in float32 instead. This matches the reference int8 codes exactly and removes the overflow, at the cost of a transient float32 buffer instead of float16. Adds regression tests: the precision test fails on the previous float16 buffer, and an overflow test covers values above the float16 maximum.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The int8 path in
quantize_embeddingsscaled the embeddings through afloat16buffer before rounding to integers:float16 keeps only ~3 significant decimal digits, so
embeddings / scalerounds to a different nearest integer than the exact quotient for a fair number of entries. The result is that a non-trivial fraction of int8 codes end up off by ±1 versus the mathematically correct quantization — for ordinary, small-magnitude embeddings, not just extreme inputs.On a normal
2000 × 256matrix atstd = 0.1, ~0.65% of codes (3337 / 512000) differ from the float32-correct result, touching ~80% of token rows. Measured against the true unrounded quotient, the float32 computation is closer on every differing entry and never worse, so the float16 buffer was strictly less accurate with no accuracy upside — its only benefit was a smaller transient buffer.As a side effect, casting to float16 first also turned any embedding value above the float16 maximum (65504) into
infbefore scaling (RuntimeWarning: overflow encountered in cast), withcliphappening to rescue the dominant code.Change
Compute the scaled values in
float32instead offloat16. Still a single copy; the transient buffer is 2× larger, but the codes now match the exact computation and the overflow is gone.Tests
test_quantize_embeddings_int8_matches_float32_reference— quantizes a2000 × 256matrix and asserts the codes equal the float32 reference. Fails on the previous float16 buffer, passes with this change.test_quantize_embeddings_int8_handles_values_above_float16_max— covers values above the float16 maximum.Full
tests/test_quantization.pyandtests/test_model.pypass (83 tests);ruff check,ruff format --check, andmypy model2vec/quantization.pyare clean.Closes #405.
Verification level: measured and reproduced locally on
main; regression test fails on revert of the fix. AI-assisted investigation, reviewed and verified by me.