Skip to content

SSM / Mamba kernel #28

Description

@superposition

Description

Selective State Space Model kernel for efficient sequence modeling (linear time complexity).

Operation

# Mamba selective scan
# x: (B, T, D), A, B, C, D: SSM parameters
# dt: (B, T, D) input-dependent time step
output = selective_scan(x, dt, A, B, C, D)

Implementation

  • Parallel associative scan
  • Input-dependent discretization
  • Fused with input projection
  • Memory efficient (recompute in backward)

Key Algorithm

h[t] = A_bar * h[t-1] + B_bar * x[t]
y[t] = C * h[t] + D * x[t]
where A_bar, B_bar depend on dt[t]

Tests

  • Matches sequential loop implementation
  • Linear time complexity O(T*D)
  • Gradient correct for all parameters
  • gradcheck passes
  • Works with long sequences (8K+)
  • Handles variable sequence lengths

Used In

Mamba, Mamba-2, state space world models, efficient transformers

References

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 request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions