Skip to content

feat(phi3-small): implement blocksparse attention to replace the dense fallback #511

Description

@inureyes

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

  1. 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).
  2. 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.
  3. Thread the per-layer block_sparse flag (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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelsModel architectures, weights, loading, metadatapriority:lowLow prioritystatus:doneCompletedtype:enhancementNew features, capabilities, or significant additions

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions