Skip to content

fix(core): pack3 size-contract asserts vanish in release builds and the should_panic test fails #234

Closed

Description

@inureyes

Problem

packed_bytes_per_token_3bit (src/lib/mlxcel-core/src/cache/turbo/pack3.rs:87) validates its "head_dim must be a positive multiple of 8" contract with debug_assert!. In release builds the assert compiles out and the function silently returns a truncated byte count (7 * 3 / 8 = 2), which would mis-size every downstream packed buffer in the Turbo3 V-side quant pipeline (quant3.rs:224 and the packing/unpacking round trip). The sibling pack_3bit_indices guards its indices.len() multiple-of-8 contract the same way, with the same silent-corruption consequence.

Visible symptom: cache::turbo::pack3::tests::packed_bytes_rejects_non_multiple_of_8 is a #[should_panic] test, so the full core suite run with --release (the profile used for Metal-gated local runs) fails on every invocation. This failure predates the recent #224-#228 queue and shows up in every release-mode suite run as noise that can mask real failures.

Reachability today is low (supported Turbo3 head dims are 64/80/128/192/256, all multiples of 8, and the WHT constraint screens dims upstream), but the contract is buffer-sizing arithmetic: if an unvetted dim ever reaches the path in release, the result is silent memory corruption rather than a panic.

Proposed solution

Promote the size-contract checks from debug_assert! to always-on assert!:

  1. packed_bytes_per_token_3bit: assert!(head_dim > 0 && (head_dim as usize).is_multiple_of(COORDS_PER_GROUP), ...). The function is called once per quantize call, not per element; two integer comparisons are negligible.
  2. pack_3bit_indices (and its unpack counterpart if symmetric): promote the length-contract debug_assert! the same way. These run per packed row, still trivially cheap next to the packing loop itself.
  3. Leave the deliberate masking behavior (out-of-range index values lose high bits, documented as defensive) unchanged; this issue is only about the size contracts that determine buffer lengths.
  4. Do NOT cfg-gate the test: with always-on asserts, packed_bytes_rejects_non_multiple_of_8 passes in both debug and release profiles unchanged.

Acceptance criteria

  • cargo test -p mlxcel-core --release --lib cache::turbo::pack3 -- --test-threads=1 passes 100 percent, including the should_panic test.
  • Full release-mode core suite has zero failures (this was the only one).
  • Size-contract violations panic in release builds instead of truncating.
  • Existing Turbo3 quant round-trip tests stay green (no behavior change for valid dims).

Related

Follow-up from the #224-#228 memory-footprint queue (see #224 for the partial mitigation this replaces / see the queue for context). Filed together with #235.

Activity

  1. added
    status:ready
    type:bug
    priority:medium
    area:core
    on Jun 12, 2026
  2. added this to the 0.2 milestone on Jun 21, 2026
  3. self-assigned this
    on Aug 31, 2026
  4. added
    status:done
    and removed
    status:ready
    on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corepriority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions