Skip to content

fix(larql-vindex): prevent overflow in lm_head vocab calculation on 32-bit hosts#100

Merged
chrishayuk merged 1 commit into
mainfrom
fix/lm-head-vocab-overflow-32bit
May 15, 2026
Merged

fix(larql-vindex): prevent overflow in lm_head vocab calculation on 32-bit hosts#100
chrishayuk merged 1 commit into
mainfrom
fix/lm-head-vocab-overflow-32bit

Conversation

@chrishayuk
Copy link
Copy Markdown
Owner

Summary

bytes * Q4_BYTES_PER_ELEM_DEN in load_lm_head_q4 is computed in usize, which on 32-bit targets (e.g. armv7-linux-androideabi) wraps before the division for large lm_head mmaps and silently produces the wrong vocab_size.

The fix promotes the multiply to u64 with checked_mul / checked_div, then narrows back via usize::try_from. No behaviour change on 64-bit hosts.

Cherry-picked from #94's review (credit @metavacua, who surfaced this while attempting an armv7-android cross-build).

Test plan

  • cargo test -p larql-vindex --lib — 968 passed / 0 failed (including the 3 load_lm_head_q4_* tests that pin the vocab-inference path)
  • cargo test -p larql-vindex --tests — 212 passed / 0 failed across 20 integration binaries

Note: 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.

…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>
@chrishayuk chrishayuk merged commit 4e69a47 into main May 15, 2026
20 of 21 checks passed
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