|
|
import torch |
|
|
import torch.nn as nn |
|
|
import torch.nn.functional as F |
|
|
from flash_attn import flash_attn_func |
|
|
|
|
|
|
|
|
def drop_path(x, drop_prob=0., training=False, scale_by_keep=True): |
|
|
""" |
|
|
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). |
|
|
""" |
|
|
if drop_prob == 0. or not training: |
|
|
return x |
|
|
keep_prob = 1 - drop_prob |
|
|
shape = (x.shape[0],) + (1,) * (x.ndim - 1) |
|
|
random_tensor = x.new_empty(shape).bernoulli_(keep_prob) |
|
|
if keep_prob > 0.0 and scale_by_keep: |
|
|
random_tensor.div_(keep_prob) |
|
|
return x * random_tensor |
|
|
|
|
|
|
|
|
class DropPath(nn.Module): |
|
|
""" |
|
|
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). |
|
|
""" |
|
|
|
|
|
def __init__(self, drop_prob=0., scale_by_keep=True): |
|
|
super(DropPath, self).__init__() |
|
|
self.drop_prob = drop_prob |
|
|
self.scale_by_keep = scale_by_keep |
|
|
|
|
|
def forward(self, x): |
|
|
return drop_path(x, self.drop_prob, self.training, self.scale_by_keep) |
|
|
|
|
|
def extra_repr(self): |
|
|
return f'drop_prob={round(self.drop_prob, 3):0.3f}' |
|
|
|
|
|
|
|
|
class Mlp(nn.Module): |
|
|
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.): |
|
|
super().__init__() |
|
|
out_features = out_features or in_features |
|
|
hidden_features = hidden_features or in_features |
|
|
self.fc1 = nn.Linear(in_features, hidden_features) |
|
|
self.act = act_layer() |
|
|
self.fc2 = nn.Linear(hidden_features, out_features) |
|
|
self.drop = nn.Dropout(drop) |
|
|
|
|
|
def forward(self, x): |
|
|
x = self.fc1(x) |
|
|
x = self.act(x) |
|
|
x = self.drop(x) |
|
|
x = self.fc2(x) |
|
|
x = self.drop(x) |
|
|
return x |
|
|
|
|
|
|
|
|
def window_partition(x, window_size): |
|
|
""" |
|
|
Args: |
|
|
x: (B, L, C) |
|
|
window_size (int): window size |
|
|
|
|
|
Returns: |
|
|
windows: (num_windows*B, window_size, C) |
|
|
""" |
|
|
B, L, C = x.shape |
|
|
x = x.view(B, L // window_size, window_size, C) |
|
|
windows = x.permute(0, 1, 2, 3).contiguous().view(-1, window_size, C) |
|
|
return windows |
|
|
|
|
|
|
|
|
def window_reverse(windows, window_size, L): |
|
|
""" |
|
|
Args: |
|
|
windows: (num_windows*B, window_size, window_size, C) |
|
|
window_size (int): Window size |
|
|
L (int): sequence length |
|
|
|
|
|
Returns: |
|
|
x: (B, L, C) |
|
|
""" |
|
|
B = int(windows.shape[0] / (L / window_size)) |
|
|
x = windows.view(B, L // window_size, window_size, -1) |
|
|
x = x.permute(0, 1, 2, 3).contiguous().view(B, L, -1) |
|
|
return x |
|
|
|
|
|
|
|
|
class WindowAttention1D(nn.Module): |
|
|
""" |
|
|
Window based multi-head self attention (W-MSA) module with relative position bias. |
|
|
It supports both of shifted and non-shifted window. |
|
|
|
|
|
Args: |
|
|
dim (int): Number of input channels. |
|
|
window_size (int): The height and width of the window. |
|
|
num_heads (int): Number of attention heads. |
|
|
qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True |
|
|
qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set |
|
|
attn_drop (float, optional): Dropout ratio of attention weight. Default: 0.0 |
|
|
proj_drop (float, optional): Dropout ratio of output. Default: 0.0 |
|
|
""" |
|
|
|
|
|
def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.): |
|
|
super().__init__() |
|
|
self.dim = dim |
|
|
self.window_size = window_size |
|
|
self.num_heads = num_heads |
|
|
head_dim = dim // num_heads |
|
|
self.scale = qk_scale or head_dim ** -0.5 |
|
|
|
|
|
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) |
|
|
self.attn_drop = nn.Dropout(attn_drop) |
|
|
self.proj = nn.Linear(dim, dim) |
|
|
self.proj_drop = nn.Dropout(proj_drop) |
|
|
|
|
|
def forward(self, x): |
|
|
""" |
|
|
Args: |
|
|
x: input features with shape of (num_windows*B, N, C) |
|
|
""" |
|
|
B_, N, C = x.shape |
|
|
qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // |
|
|
self.num_heads).permute(2, 0, 1, 3, 4) |
|
|
q, k, v = qkv[0], qkv[1], qkv[2] |
|
|
q = q.to(torch.float16) |
|
|
k = k.to(torch.float16) |
|
|
v = v.to(torch.float16) |
|
|
x, _, _ = flash_attn_func(q, k, v, return_attn_probs=True) |
|
|
x = x.to(torch.float32) |
|
|
x = x.reshape(B_, N, C) |
|
|
x = self.proj(x) |
|
|
x = self.proj_drop(x) |
|
|
return x |
|
|
|
|
|
|
|
|
class ChromFoundTransformerBlock(nn.Module): |
|
|
""" |
|
|
Transformer Block for ChromFound. |
|
|
Args: |
|
|
dim (int): Number of input channels. |
|
|
input_resolution (int): Input resulotion. |
|
|
num_heads (int): Number of attention heads. |
|
|
window_size (int): Window size. |
|
|
shift_size (int): Shift size for SW-MSA. |
|
|
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. |
|
|
qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True |
|
|
drop (float, optional): Dropout rate. Default: 0.0 |
|
|
attn_drop (float, optional): Attention dropout rate. Default: 0.0 |
|
|
drop_path (float, optional): Stochastic depth rate. Default: 0.0 |
|
|
act_layer (nn.Module, optional): Activation layer. Default: nn.GELU |
|
|
norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm |
|
|
""" |
|
|
|
|
|
def __init__(self, dim, input_resolution, num_heads, window_size=7, shift_size=0, |
|
|
mlp_ratio=1., qkv_bias=True, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, |
|
|
act_layer=nn.GELU, norm_layer=nn.LayerNorm): |
|
|
super().__init__() |
|
|
self.dim = dim |
|
|
self.input_resolution = input_resolution |
|
|
self.num_heads = num_heads |
|
|
self.window_size = window_size |
|
|
self.shift_size = shift_size |
|
|
self.mlp_ratio = mlp_ratio |
|
|
if self.input_resolution <= self.window_size: |
|
|
|
|
|
self.shift_size = 0 |
|
|
self.window_size = self.input_resolution |
|
|
assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" |
|
|
|
|
|
self.norm1 = norm_layer(dim) |
|
|
self.attn = WindowAttention1D( |
|
|
dim, window_size=self.window_size, num_heads=num_heads, |
|
|
qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop) |
|
|
|
|
|
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() |
|
|
self.norm2 = norm_layer(dim) |
|
|
mlp_hidden_dim = int(dim * mlp_ratio) |
|
|
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) |
|
|
|
|
|
def forward(self, x, padding_mask=None): |
|
|
Li = self.input_resolution |
|
|
B, L, C = x.shape |
|
|
assert L == Li, "input feature has wrong size" |
|
|
|
|
|
shortcut = x |
|
|
|
|
|
|
|
|
|
|
|
pad_r = (self.window_size - L % self.window_size) % self.window_size |
|
|
x = F.pad(x, (0, 0, 0, pad_r)) |
|
|
_, Lp, _ = x.shape |
|
|
|
|
|
if padding_mask is not None: |
|
|
|
|
|
padding_mask = F.pad(padding_mask, (0, pad_r), value=1) |
|
|
if self.shift_size > 0: |
|
|
padding_mask = torch.roll(padding_mask, shifts=(-self.shift_size), dims=1) |
|
|
|
|
|
|
|
|
if self.shift_size > 0: |
|
|
shifted_x = torch.roll(x, shifts=(-self.shift_size), dims=(1)) |
|
|
else: |
|
|
shifted_x = x |
|
|
|
|
|
|
|
|
x_windows = window_partition(shifted_x, self.window_size) |
|
|
x_windows = x_windows.view(-1, self.window_size, C) |
|
|
|
|
|
|
|
|
attn_windows = self.attn(x_windows) |
|
|
|
|
|
|
|
|
attn_windows = attn_windows.view(-1, self.window_size, C) |
|
|
shifted_x = window_reverse(attn_windows, self.window_size, Lp) |
|
|
|
|
|
|
|
|
if self.shift_size > 0: |
|
|
x = torch.roll(shifted_x, shifts=(self.shift_size), dims=(1)) |
|
|
else: |
|
|
x = shifted_x |
|
|
x = x.view(B, Lp, C) |
|
|
|
|
|
x = x[:, :L, :].contiguous() |
|
|
x = shortcut + self.drop_path(self.norm1(x)) |
|
|
|
|
|
|
|
|
x = x + self.drop_path(self.norm2(self.mlp(x))) |
|
|
return x |
|
|
|