Skip to content

Sparse attention as a first-class API #9

Description

@DarshanFofadiya

Context

demo_14 and demo_15 (all-sparse path) proved that sparse attention projections (qkv and o at 70% sparsity) work end-to-end and are additive with FFN sparsity. But the current implementation requires the user to manually construct SparseLinear layers inside a custom attention module.

A first-class API would let users write something like:

attn = sparselab.SparseAttention(d_model=384, n_heads=6, attn_sparsity=0.7)

What needs to happen

  1. Design the SparseAttention API — should it wrap PyTorch's F.scaled_dot_product_attention or replace it?
  2. Decide: do we sparsify the attention weights (qkv/o projections) or the attention pattern (the softmax output)? v0.1 does the former. The latter is a different research direction (sparse attention masks like BigBird, Longformer).
  3. Implement as an nn.Module in sparselab/nn.py.
  4. Tests + a demo showing the API.

Files to read

  • examples/demo_14_sparse_attention.py — current manual implementation
  • examples/demo_15_mini_gpt.pyCausalSelfAttention class that switches between dense and sparse projections
  • docs/demos/milestone_10.md — results showing sparse attention works at 10k-step scale

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestv0.3Planned for v0.3 release

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions