Skip to main content

Symmetric Mamba Attention: Dual-path attention with Mamba-enhanced Q/K generation and sigma-modulated attention weighting

Project description

SMA-Attention

Symmetric Mamba Attention — a dual-path attention module for multi-scale feature fusion.

Unlike standard attention where query and key come from the same source, SMA takes two distinct inputs (e.g., different temporal scales, modalities, or branches) and produces a single fused output. Both Q and K are generated through a shared Mamba state-space model — the symmetric design ensures neither input path is privileged.

Key Features

  • Symmetric Q/K via Mamba: A single shared Mamba layer processes both inputs. The selective state-space mechanism captures long-range dependencies that standard projections miss.
  • Fused Value Projection: V1 and V2 are merged via concat(V1+V2, V1*V2) — capturing both additive and multiplicative cross-path interactions.
  • Sigma-Modulated Attention: Element-wise gating sigma(Q) * sigma(K)^T modulates attention logits, enabling dimension-aware soft-thresholding.
  • Multiple Residual Paths: Residual connections at Q/K, V fusion, and final output ensure stable gradient flow.

Installation

pip install sma-attention

Note: This package depends on mamba-ssm, which requires a CUDA-compatible GPU and CUDA toolkit. See mamba-ssm installation guide for details.

Quick Start

import torch
from sma_attention import SymmetricMambaAttention

# B=batch, D=feature_dim, N=sequence_length
x1 = torch.randn(2, 128, 60)   # e.g., high-frequency component
x2 = torch.randn(2, 128, 60)   # e.g., low-frequency component

sma = SymmetricMambaAttention(d_model=128, n_heads=4)
output = sma(x1, x2)            # shape: (2, 128, 60)

API Reference

SymmetricMambaAttention

SymmetricMambaAttention(
    d_model: int,        # feature dimension
    n_heads: int,        # number of attention heads
    d_state: int = 16,   # Mamba state dimension
    d_conv: int = 4,     # Mamba conv kernel size
    expand: int = 2,     # Mamba inner expansion factor
)
Parameter Description
d_model Feature dimension (must be divisible by n_heads)
n_heads Number of attention heads
d_state Mamba SSM state expansion factor
d_conv Mamba 1D convolution kernel width
expand Inner dimension multiplier for Mamba blocks

forward(x1, x2)

Argument Shape Description
x1 (B, D, N) First input (e.g., one temporal scale)
x2 (B, D, N) Second input (e.g., another temporal scale)
Returns (B, D, N) Fused output

Architecture

SMA Architecture

The figure above illustrates the processing flow:

  1. Inputs: Two paths x₁ and x₂ (e.g., adjacent scales in a circular topology), both shape (B, D, N)
  2. Symmetric Q/K: Shared Mamba SSM with residual connections generates query from x₁ and key from x₂
  3. Dual-Path Value: Independent linear projections → fusion via concat(sum, product) → linear projection
  4. Multi-Head Reshape: Standard head splitting for attention computation
  5. Sigma Modulation: σ(Q)·σ(K)ᵀ element-wise gating modulates attention logits before softmax
  6. Output: Weighted V aggregation + average residual from both inputs

Requirements

  • Python >= 3.10
  • PyTorch >= 2.0
  • einops >= 0.6
  • mamba-ssm >= 2.0 (CUDA required)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sma_attention-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sma_attention-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file sma_attention-0.1.0.tar.gz.

File metadata

  • Download URL: sma_attention-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for sma_attention-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6c7946c3614747beb669d174debf0e1cd9940708e63abb536031bfe13d26b538
MD5 da93db9ae8abe62163768f76c19fda42
BLAKE2b-256 ef7cce45054fdcbb2f5f45d7c99b280a468f8bbd06aef557b8fee1529d6c6976

See more details on using hashes here.

File details

Details for the file sma_attention-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sma_attention-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for sma_attention-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d15bce1b69fc1c56497cd03277dbfade008527c247fef32ac683dcb33222ce32
MD5 486d1fc52bf2399997e01648240927b9
BLAKE2b-256 9bbf58ed3ba8fcab65fd91b1a44cc48ec178974f87974b52dc65229b7c5c55f7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page