学术
模块更新于 2026-11-20· 浏览 11· 下载 4

ASSM 注意力状态空间模块

非因果状态空间:发现状态空间方程的输出矩阵 C 相当于注意力里的 Query,把代表全图相似像素的可学习提示(prompt)残差注入 C,每个像素都能查询扫描序列外的像素,单次扫描即可全图感知

计算机视觉图像复原超分辨率Mamba

结构图

ASSM 注意力状态空间模块结构图
ASSM 注意力状态空间模块结构图

非因果状态空间:发现状态空间方程的输出矩阵 C 相当于注意力里的 Query,把代表全图相似像素的可学习提示(prompt)残差注入 C,每个像素都能查询扫描序列外的像素,单次扫描即可全图感知

论文信息

  • 论文:MambaIRv2: Attentive State Space Restoration
  • 出处:arXiv 2025

核心痛点

现有 Mamba 复原网络把图像展开成 1D 序列做因果建模,每个像素只能"看见"扫描序列里排在前面的像素,只能靠多方向扫描补救,开销大且冗余(不同方向扫描序列的余弦相似度甚至超过 0.7);再加上长程衰减,远距离相似像素难以利用。

模块亮点

  • 非因果状态空间:发现状态空间方程的输出矩阵 C 相当于注意力里的 Query,把代表全图相似像素的可学习提示(prompt)残差注入 C,每个像素都能查询扫描序列外的像素,单次扫描即可全图感知
  • 语义引导近邻:SGN 复用 ASE 的路由矩阵给每个像素分配语义标签,把同类像素排到相邻位置(SGN-unfold / SGN-fold 可逆),缓解长程衰减
  • 涨点还更轻量:轻量超分消融中,ASE 比纯窗口注意力基线在 Urban100 / Manga109 提升 0.05 / 0.09dB,加 SGN 后达 32.97 / 39.24dB;比 SRFormer-light 高 0.35dB、省 9.3% 参数,比 MambaIR 省 43% 参数、50% 计算量还高 0.34dB

适用场景

领域为底层视觉 / 图像复原,原文验证了轻量级超分、JPEG 伪影去除、彩色图像去噪;可迁移到去雨、去雾等 Mamba 复原网络,以及需要非因果全局建模的视觉 Mamba 模型。

插入位置建议

  • 替换 MambaIR、VmambaIR 等网络里多方向扫描的 Mamba 全局建模块,一次扫描顶替四向扫描
  • 仿照原文 ASSB 的搭法,作为全局 Token Mixer 与窗口 MHSA 串联成块:窗口注意力管局部、ASSM 管全局
  • 插进其他视觉 Mamba 骨干的深层 stage,补上非因果全局上下文

适用任务

  • 底层视觉
  • 图像复原
  • 原文验证了轻量级超分

完整代码

assm.py
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from mamba_ssm.ops.selective_scan_interface import selective_scan_fn, selective_scan_ref
from einops import rearrange, repeat


def to_3d(x):
    return rearrange(x, 'b c h w -> b (h w) c')

def to_4d(x, h, w):
    return rearrange(x, 'b (h w) c -> b c h w', h=h, w=w)


"""《MambaIRv2: Attentive State Space Restoration》CVPR 2025
基于Mamba的图像修复骨干网络最近在平衡全局感知和计算效率方面展现了显著潜力。
然而,Mamba固有的因果建模限制——即每个标记仅依赖于扫描序列中的前驱标记——限制了图像中像素的充分利用,从而为图像修复带来了新的挑战。
在本研究中,我们提出了MambaIRv2,它赋予Mamba类似于视觉Transformer(ViTs)的非因果建模能力,以达到注意力状态空间修复模型。
具体而言,所提出的注意力状态空间方程允许关注超出扫描序列的范围,并通过单次扫描促进图像展开。此外,我们进一步引入了语义引导的邻近机制,以鼓励远距离但相似的像素之间的交互。
大量实验表明,我们的MambaIRv2在轻量级超分辨率(SR)任务中,即使参数减少了9.3%,其PSNR仍比SRFormer高出0.35dB,并在经典SR任务中以高达0.29dB的优势超越了HAT。
"""


def index_reverse(index):
    index_r = torch.zeros_like(index)
    ind = torch.arange(0, index.shape[-1]).to(index.device)
    for i in range(index.shape[0]):
        index_r[i, index[i, :]] = ind
    return index_r


def semantic_neighbor(x, index):
    dim = index.dim()
    assert x.shape[:dim] == index.shape, "x ({:}) and index ({:}) shape incompatible".format(x.shape, index.shape)

    for _ in range(x.dim() - index.dim()):
        index = index.unsqueeze(-1)
    index = index.expand(x.shape)

    shuffled_x = torch.gather(x, dim=dim - 1, index=index)
    return shuffled_x


class Selective_Scan(nn.Module):
    def __init__(
            self,
            d_model,
            d_state=16,
            expand=2.,
            dt_rank="auto",
            dt_min=0.001,
            dt_max=0.1,
            dt_init="random",
            dt_scale=1.0,
            dt_init_floor=1e-4,
            device=None,
            dtype=None,
            **kwargs,
    ):
        factory_kwargs = {"device": device, "dtype": dtype}
        super().__init__()
        self.d_model = d_model
        self.d_state = d_state
        self.expand = expand
        self.d_inner = int(self.expand * self.d_model)
        self.dt_rank = math.ceil(self.d_model / 16) if dt_rank == "auto" else dt_rank

        self.x_proj = (
            nn.Linear(self.d_inner, (self.dt_rank + self.d_state * 2), bias=False, **factory_kwargs),
        )
        self.x_proj_weight = nn.Parameter(torch.stack([t.weight for t in self.x_proj], dim=0))  # (K=4, N, inner)
        del self.x_proj

        self.dt_projs = (
            self.dt_init(self.dt_rank, self.d_inner, dt_scale, dt_init, dt_min, dt_max, dt_init_floor,
                         **factory_kwargs),
        )
        self.dt_projs_weight = nn.Parameter(torch.stack([t.weight for t in self.dt_projs], dim=0))  # (K=4, inner, rank)
        self.dt_projs_bias = nn.Parameter(torch.stack([t.bias for t in self.dt_projs], dim=0))  # (K=4, inner)
        del self.dt_projs
        self.A_logs = self.A_log_init(self.d_state, self.d_inner, copies=1, merge=True)  # (K=4, D, N)
        self.Ds = self.D_init(self.d_inner, copies=1, merge=True)  # (K=4, D, N)
        self.selective_scan = selective_scan_fn

    @staticmethod
    def dt_init(dt_rank, d_inner, dt_scale=1.0, dt_init="random", dt_min=0.001, dt_max=0.1, dt_init_floor=1e-4,
                **factory_kwargs):
        dt_proj = nn.Linear(dt_rank, d_inner, bias=True, **factory_kwargs)

        # Initialize special dt projection to preserve variance at initialization
        dt_init_std = dt_rank ** -0.5 * dt_scale
        if dt_init == "constant":
            nn.init.constant_(dt_proj.weight, dt_init_std)
        elif dt_init == "random":
            nn.init.uniform_(dt_proj.weight, -dt_init_std, dt_init_std)
        else:
            raise NotImplementedError

        # Initialize dt bias so that F.softplus(dt_bias) is between dt_min and dt_max
        dt = torch.exp(
            torch.rand(d_inner, **factory_kwargs) * (math.log(dt_max) - math.log(dt_min))
            + math.log(dt_min)
        ).clamp(min=dt_init_floor)
        # Inverse of softplus: https://github.com/pytorch/pytorch/issues/72759
        inv_dt = dt + torch.log(-torch.expm1(-dt))
        with torch.no_grad():
            dt_proj.bias.copy_(inv_dt)
        # Our initialization would set all Linear.bias to zero, need to mark this one as _no_reinit
        dt_proj.bias._no_reinit = True

        return dt_proj

    @staticmethod
    def A_log_init(d_state, d_inner, copies=1, device=None, merge=True):
        # S4D real initialization
        A = repeat(
            torch.arange(1, d_state + 1, dtype=torch.float32, device=device),
            "n -> d n",
            d=d_inner,
        ).contiguous()
        A_log = torch.log(A)  # Keep A_log in fp32
        if copies > 1:
            A_log = repeat(A_log, "d n -> r d n", r=copies)
            if merge:
                A_log = A_log.flatten(0, 1)
        A_log = nn.Parameter(A_log)
        A_log._no_weight_decay = True
        return A_log

    @staticmethod
    def D_init(d_inner, copies=1, device=None, merge=True):
        # D "skip" parameter
        D = torch.ones(d_inner, device=device)
        if copies > 1:
            D = repeat(D, "n1 -> r n1", r=copies)
            if merge:
                D = D.flatten(0, 1)
        D = nn.Parameter(D)  # Keep in fp32
        D._no_weight_decay = True
        return D

    def forward_core(self, x: torch.Tensor, prompt):
        B, L, C = x.shape
        K = 1  # mambairV2 needs noly 1 scan
        xs = x.permute(0, 2, 1).view(B, 1, C, L).contiguous()  # B, 1, C ,L

        x_dbl = torch.einsum("b k d l, k c d -> b k c l", xs.view(B, K, -1, L), self.x_proj_weight)
        dts, Bs, Cs = torch.split(x_dbl, [self.dt_rank, self.d_state, self.d_state], dim=2)
        dts = torch.einsum("b k r l, k d r -> b k d l", dts.view(B, K, -1, L), self.dt_projs_weight)
        xs = xs.float().view(B, -1, L)
        dts = dts.contiguous().float().view(B, -1, L)  # (b, k * d, l)
        Bs = Bs.float().view(B, K, -1, L)
        #  our ASE here ---
        Cs = Cs.float().view(B, K, -1, L) + prompt  # (b, k, d_state, l)
        Ds = self.Ds.float().view(-1)
        As = -torch.exp(self.A_logs.float()).view(-1, self.d_state)
        dt_projs_bias = self.dt_projs_bias.float().view(-1)  # (k * d)
        out_y = self.selective_scan(
            xs, dts,
            As, Bs, Cs, Ds, z=None,
            delta_bias=dt_projs_bias,
            delta_softplus=True,
            return_last_state=False,
        ).view(B, K, -1, L)
        assert out_y.dtype == torch.float

        return out_y[:, 0]

    def forward(self, x: torch.Tensor, prompt, **kwargs):
        b, l, c = prompt.shape
        prompt = prompt.permute(0, 2, 1).contiguous().view(b, 1, c, l)
        y = self.forward_core(x, prompt)  # [B, L, C]
        y = y.permute(0, 2, 1).contiguous()
        return y


class ASSM(nn.Module):
    def __init__(self, dim, d_state, input_resolution, num_tokens=64, inner_rank=128, mlp_ratio=2.):
        super().__init__()
        self.dim = dim
        self.input_resolution = input_resolution
        self.num_tokens = num_tokens
        self.inner_rank = inner_rank

        # Mamba params
        self.expand = mlp_ratio
        hidden = int(self.dim * self.expand)
        self.d_state = d_state
        self.selectiveScan = Selective_Scan(d_model=hidden, d_state=self.d_state, expand=1)
        self.out_norm = nn.LayerNorm(hidden)
        self.act = nn.SiLU()
        self.out_proj = nn.Linear(hidden, dim, bias=True)  # 投影回原始维度

        self.in_proj = nn.Sequential(
            nn.Conv2d(self.dim, hidden, 1, 1, 0),
        )

        self.CPE = nn.Sequential(
            nn.Conv2d(hidden, hidden, 3, 1, 1, groups=hidden),
        )

        self.embeddingB = nn.Embedding(self.num_tokens, self.inner_rank)  # [64, 128]
        self.embeddingB.weight.data.uniform_(-1 / self.num_tokens, 1 / self.num_tokens)

        self.route = nn.Sequential(
            nn.Linear(self.dim, self.dim // 3),
            nn.GELU(),
            nn.Linear(self.dim // 3, self.num_tokens),
            nn.LogSoftmax(dim=-1)
        )

    def forward(self, x, x_size, token):
        B, n, C = x.shape
        H, W = x_size

        # 计算 full_embedding
        full_embedding = self.embeddingB.weight @ token.weight  # [num_tokens, d_state]

        # 计算 cls_policy
        pred_route = self.route(x)  # [B, n, num_tokens]
        cls_policy = F.gumbel_softmax(pred_route, hard=True, dim=-1)  # [B, n, num_tokens]

        # 计算 prompt
        prompt = torch.matmul(cls_policy, full_embedding).view(B, n, self.d_state)  # [B, n, d_state]

        # 计算 detached_index 和排序索引
        detached_index = torch.argmax(cls_policy.detach(), dim=-1, keepdim=False).view(B, n)  # [B, n]
        x_sort_values, x_sort_indices = torch.sort(detached_index, dim=-1, stable=False)
        x_sort_indices_reverse = index_reverse(x_sort_indices)

        # 将输入投影到隐藏维度
        x = x.permute(0, 2, 1).reshape(B, C, H, W).contiguous()
        x = self.in_proj(x)  # [B, hidden, H, W]
        x = x * torch.sigmoid(self.CPE(x))  # [B, hidden, H, W]
        cc = x.shape[1]
        x = x.view(B, cc, -1).contiguous().permute(0, 2, 1)  # [B, n, hidden]

        # 使用 SGN-unfold 和 selective scan
        semantic_x = semantic_neighbor(x, x_sort_indices)  # SGN-unfold
        y = self.selectiveScan(semantic_x, prompt)  # [B, n, hidden]
        y = self.out_proj(self.out_norm(y))  # [B, n, dim]

        # 使用 SGN-fold 恢复原始顺序
        x = semantic_neighbor(y, x_sort_indices_reverse)  # [B, n, dim]

        # 返回与输入形状相同的输出
        return x

if __name__ == '__main__':
    # Define the input dimensions
    dim = 64  # 输入维度
    d_state = 32  # 状态维度
    input_resolution = (16, 16)
    num_tokens = 64  # token数量
    inner_rank = 128  # 内部rank

    # Create the model
    block = ASSM(
        dim=dim,
        d_state=d_state,
        input_resolution=input_resolution,
        num_tokens=num_tokens,
        inner_rank=inner_rank
    ).to('cuda')

    # 修改 token embedding 的维度为 (inner_rank, d_state)
    token = nn.Embedding(inner_rank, d_state).to('cuda')

    # Create random input tensor
    B = 2  # Batch size
    H, W = input_resolution
    input = torch.rand(B, H * W, dim).to('cuda')  # Input shape: [B, H*W, dim]

    # Forward pass
    output = block(input, input_resolution, token)

    # Print shapes for debugging
    print("Input size:", input.size())
    print("Token embedding size:", token.weight.size())
    print("Output size:", output.size())

环境与依赖

Python 3.9+

继续浏览

下一篇BAT 通用双向适配器双向交叉提示:不预设主/辅模态,一个共享参数的通用适配器同时服务两条模态分支,把 A 模态特征经"降维-线性投影-升维"生成特征提示喂给 B 模态,反向同理,主导权随环境切换

相关资源