Clamp DiskANN int8 quantization to avoid float->int8 UB#311
Open
stumpylog wants to merge 1 commit into
Open
Conversation
diskann_quantize_vector's INT8 path cast the quantized float directly to i8. When the input vector is not normalized to [-1, 1] the intermediate value falls outside [-128, 127], and converting an out-of-range float to a signed integer type is undefined behavior in C (in practice it wraps to garbage). Saturate the value to [-128, 127] before the cast so out-of-range inputs quantize to the nearest representable int8. Surfaced by an ASan/UBSan run of the loadable test suite (test_diskann_insert_int8_quantizer_knn). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
diskann_quantize_vector'sVEC0_DISKANN_QUANTIZER_INT8path casts the quantized float directly toi8:When the input vector is not normalized to
[-1, 1], the intermediate value falls outside[-128, 127]. Converting an out-of-range floating-point value to a signed integer type is undefined behavior in C (C11 6.3.1.4); in practice it wraps to a garbage int8 instead of saturating.Fix
Saturate the value to
[-128, 127]before the cast, so out-of-range inputs quantize to the nearest representableint8:How it was found
An AddressSanitizer + UndefinedBehaviorSanitizer run of the loadable test suite, on
test_diskann_insert_int8_quantizer_knn:diskann_quantize_queryis the only other caller and goes through the same function, so both the index-build and query paths are covered by the one fix. No behavior change for already-normalized inputs.🤖 Generated with Claude Code