Repository navigation
Description
Summary
Phi-3-small uses a blocksparse attention pattern (local blocks plus a vertical stride) on its non-dense layers, with periodic fully-dense layers. mlxcel currently runs dense attention everywhere as a simplicity fallback. This is correct at short context but diverges from the trained sparsity pattern at long context and forgoes the intended long-sequence efficiency.
Current state in mlxcel
src/models/phi3small.rs:21documents it: "Blocksparse attention pattern (with dense fallback)".- The blocksparse config is parsed but unused for actual sparsity:
blocksparse_block_size(phi3small.rs:66),blocksparse_num_local_blocks(phi3small.rs:69),blocksparse_vert_stride(phi3small.rs:72),dense_attention_every_n_layers(phi3small.rs:38). - A per-layer
block_sparseflag is computed (phi3small.rs:257:!(layer_idx + 1).is_multiple_of(dense_attention_every_n_layers)) and stored on the layer (phi3small.rs:140,:269), but the attention path ignores it:phi3small.rs:207-208notes "block sparse attention would be more efficient for long sequences, but we use dense attention for simplicity (fallback)".
Consequence: outputs match a dense reference at short context but diverge from the Phi-3-small reference once the sequence exceeds the local-block window, and the long-sequence speedup the architecture targets is unrealized.
What needs to be built
- Blocksparse mask / gather for
block_sparselayers: attend to (a) the local window ofblocksparse_num_local_blocksblocks of sizeblocksparse_block_sizeending at the current query block, plus (b) earlier blocks selected byblocksparse_vert_stride. Keep the existing dense path for layers whereblock_sparse == false(the every-dense_attention_every_n_layersdense layers). - Prefer a gather-based KV selection for the decode (
L==1) path over building a full sparse mask each step, consistent with how other long-context paths avoid per-step mask allocation. - Thread the per-layer
block_sparseflag (already computed) into attention so dense and sparse layers diverge correctly.
Acceptance criteria
- Blocksparse pattern implemented for non-dense layers; dense layers unchanged.
- Mask/selection matches the Phi-3-small reference pattern (local blocks + vertical stride) on a constructed case.
- Short-context parity: when the sequence fits within the local window, output is RMS-equivalent to the current dense fallback.
- Long-context output matches the reference sparsity (not dense).
- Validated on a real Phi-3-small checkpoint: generation runs and stays coherent beyond the local-block window.
Files
src/models/phi3small.rs(attention path, mask construction)- a shared blocksparse-mask helper if warranted
Context
Discovered during an upstream-sync review while auditing sparse-attention deferrals (the DeepSeek-V3.2 indexer deferral comment explicitly references this Phi-3-small blocksparse deferral as a sibling case).
Activity