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)^Tmodulates 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
The figure above illustrates the processing flow:
- Inputs: Two paths
x₁andx₂(e.g., adjacent scales in a circular topology), both shape(B, D, N) - Symmetric Q/K: Shared Mamba SSM with residual connections generates query from
x₁and key fromx₂ - Dual-Path Value: Independent linear projections → fusion via
concat(sum, product)→ linear projection - Multi-Head Reshape: Standard head splitting for attention computation
- Sigma Modulation:
σ(Q)·σ(K)ᵀelement-wise gating modulates attention logits before softmax - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sma_attention-0.1.1.tar.gz.
File metadata
- Download URL: sma_attention-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5adce217b34a4d13f59d1dc39d54d6b123f50e0505ed9102dd9fd336394ecfa9
|
|
| MD5 |
bcf26874020f4fa2d401ea270acae34a
|
|
| BLAKE2b-256 |
82fbb522dfab551b640644324fc3ba6ca91baad81a9e235df75c38f61d619ba9
|
File details
Details for the file sma_attention-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sma_attention-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e98246a69c4385d5fc2497cd58b7d493be306548ffe88ce12260480bbe6f25d
|
|
| MD5 |
9cee538c39de9266d0b85c97e2448253
|
|
| BLAKE2b-256 |
21c43dc0b470bb2e9fce99985d6b2b73ace8f79ab99a4b76da4a42a37b137ae1
|