fix(larql-vindex): prevent overflow in lm_head vocab calculation on 32-bit hosts#100
Merged
Merged
Conversation
…2-bit hosts `bytes * Q4_BYTES_PER_ELEM_DEN` is computed in `usize`, which on 32-bit targets (e.g. armv7-android) wraps before the division for large lm_head mmaps and silently produces the wrong vocab_size. Promote the multiply to u64 with checked_mul / checked_div, then narrow back via usize::try_from. No behaviour change on 64-bit. Surfaced while reviewing PR #94's armv7-android cross-build attempt. Co-Authored-By: metavacua <metavacua@gmail.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.
Summary
bytes * Q4_BYTES_PER_ELEM_DENinload_lm_head_q4is computed inusize, which on 32-bit targets (e.g.armv7-linux-androideabi) wraps before the division for large lm_head mmaps and silently produces the wrongvocab_size.The fix promotes the multiply to
u64withchecked_mul/checked_div, then narrows back viausize::try_from. No behaviour change on 64-bit hosts.Cherry-picked from #94's review (credit @metavacua, who surfaced this while attempting an
armv7-androidcross-build).Test plan
cargo test -p larql-vindex --lib— 968 passed / 0 failed (including the 3load_lm_head_q4_*tests that pin the vocab-inference path)cargo test -p larql-vindex --tests— 212 passed / 0 failed across 20 integration binariesNote: the existing tests cover happy-path correctness on 64-bit but do not directly exercise the 32-bit overflow that this fix prevents — a 32-bit cross-build job in CI would be the canonical regression test. Out of scope for this PR.