Skip to content

perf(metal): floor the SDPA query-chunk row count to a Neural Accelerator tile - #3734

Open
hephaex wants to merge 1 commit into
lablup:mainfrom
hephaex:fix/3052-query-chunk-tile-align-clean
Open

hephaex wants to merge 1 commit into
lablup:mainfrom
hephaex:fix/3052-query-chunk-tile-align-clean

Conversation

@hephaex

@hephaex hephaex commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Addresses item 2 of #3052 (Tile-align the query-chunk row count).

At the 256 MiB default a head_dim 512 array-mask call is cut into
419-row chunks, which differs from the single call by 3.05e-5 (1 to 2 fp16
ulp) on M5. The hypothesis in the issue: a chunk size that is not a tile
multiple changes the kernel's reduction.

Change

query_chunk_len rounds its result down to a whole NA_TILE_SIZE tile
through a new align_to_na_tile_floor in utils.rs, the counterpart to the
existing align_to_na_tile ceiling.

One decision worth reviewing: the floor never exceeds its input, and a
count below one tile is returned unchanged.

  • Rounding up would spend budget the caller already decided it did not have.
    A one-tile-minimum version is wrong here, and the existing
    chunk * per_row <= budget assertion caught it at per_row 1000, q_len 100, budget 7000 (7 rows -> 32 rows = 32 000 B over 7 000 B).
  • A sub-tile count cannot be tiled without overspending the budget or
    returning zero rows and never advancing.

Verification

  • layers::tests::query_chunk_len_splits_evenly_within_budget asserts the
    alignment on cells where the floor does real work — the head_dim 512 shape
    from the issue (209 rows -> 192) and a smaller one (100 -> 96) — plus the
    budget bound for every cell. Mutation-checked by removing the floor, which
    fails it with chunk 209 is not a tile multiple.
  • utils::tests::test_align_to_na_tile_floor_never_exceeds_its_input sweeps
    0..=1024 and pins floor(len) <= len plus the sub-tile identity.
  • cargo test -p mlxcel-core --lib -- --test-threads=1 on this base:
    2720 passed, 7 ignored, 2 failed — and both failures are the
    pre-existing [nightly-verify] main is red #3652 flake, reproduced identically on upstream/main
    without this change (left: [3, 3, 0, 0, 7, ...] / right: [3, 3, 0, 0, 4, ...]). PR fix(kv): compare Turbo codebook boundaries in the integer domain #3733 fixes them; this branch is orthogonal to them.
    With fix(kv): compare Turbo codebook boundaries in the integer domain #3733 applied, the same suite is 2722 passed, 0 failed.
  • cargo fmt --check and
    cargo clippy -p mlxcel-core --lib --tests -- -D warnings clean.

Not verified here

The issue's first acceptance criterion needs an M5-class host. On this 32 GB
M1 Pro,

MLXCEL_SDPA_PROBE_DIM=512 MLXCEL_SDPA_PROBE_KV_HEADS=1 \
MLXCEL_SDPA_PROBE_HEADS=16 MLXCEL_SDPA_PROBE_LEN=20000 \
cargo test --release -p mlxcel-core --lib sdpa_chunk_peak_memory_probe -- --ignored --nocapture

aborts before reporting:

[METAL] Command buffer execution failed: Insufficient Memory
(00000008:kIOGPUCommandBufferCallbackErrorOutOfMemory)

so the "max abs diff 0 at 256 MiB" claim still needs an M5 session. The
alignment itself (the second criterion) is covered by the unit test.

…ator tile

At the 256 MiB default a head_dim 512 array-mask call is cut into 419-row
chunks, which differs from the single call by 3.05e-5 (1 to 2 fp16 ulp) on
M5. A chunk size that is not a tile multiple changes the kernel's reduction.

`query_chunk_len` now rounds its result down to a multiple of
`NA_TILE_SIZE` (32) through a new `align_to_na_tile_floor`, the counterpart
to the existing ceiling helper: rounding up would spend budget the caller
already decided it did not have, so the floor never exceeds its input. A
count below one tile is returned unchanged for the same reason.

Verified:

- `layers::tests::query_chunk_len_splits_evenly_within_budget` asserts the
  alignment on cells that really need it (209 -> 192, 100 -> 96 rows) and
  that the budget still bounds every chunk; mutation-checked by removing the
  floor, which fails it with `chunk 209 is not a tile multiple`.
- `utils::tests::test_align_to_na_tile_floor_never_exceeds_its_input` sweeps
  0..=1024 and pins that the floor never grows its input.
- `cargo test -p mlxcel-core --lib -- --test-threads=1`: 2688 passed,
  7 ignored, 0 failed. `cargo fmt --check` and
  `cargo clippy -p mlxcel-core --lib --tests -- -D warnings` clean.

Not verified here: the issue's
`sdpa_chunk_peak_memory_probe` run needs an M5-class host. On this 32 GB
M1 Pro the probe aborts with `Insufficient Memory
(kIOGPUCommandBufferCallbackErrorOutOfMemory)` before it can report a max
abs diff, so the 256 MiB equality claim still needs an M5 session.

Refs lablup#3052

This branch has not been deployed

No deployments
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