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:21 documents 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_sparse flag 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-208 notes "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_sparse layers: attend to (a) the local window of blocksparse_num_local_blocks blocks of size blocksparse_block_size ending at the current query block, plus (b) earlier blocks selected by blocksparse_vert_stride. Keep the existing dense path for layers where block_sparse == false (the every-dense_attention_every_n_layers dense 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_sparse flag (already computed) into attention so dense and sparse layers diverge correctly.
Acceptance criteria
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).
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)".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).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
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).L==1) path over building a full sparse mask each step, consistent with how other long-context paths avoid per-step mask allocation.block_sparseflag (already computed) into attention so dense and sparse layers diverge correctly.Acceptance criteria
Files
src/models/phi3small.rs(attention path, mask construction)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).