forked from AIFengheshu/Plug-play-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path(ICCV 2023) LiteMLA.py
More file actions
260 lines (235 loc) · 8.24 KB
/
(ICCV 2023) LiteMLA.py
File metadata and controls
260 lines (235 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Any, Union, Optional, Tuple, List
# 论文来源:ICCV 2023
# 论文题目:EfficientViT: Multi-Scale Linear Attention for High-Resolution Dense Prediction
# 中文题目: EfficientViT: 用于高分辨率密集预测的多尺度线性注意力
# 论文链接:https://arxiv.org/pdf/2205.14756
# 官方github:https://github.com/mit-han-lab/efficientvit
# 所属机构:麻省理工,浙江大学,清华大学,麻省理工- ibm沃森人工智能实验室
# 关键词:EfficientViT, 多尺度线性关注,高分辨率密集预测,视觉转换,语义分割,超分辨率,分割一切
# 微信公众号:AI缝合术
def val2tuple(x: Union[List, Tuple, Any], min_len: int = 1, idx_repeat: int = -1) -> Tuple:
x = val2list(x)
# repeat elements if necessary
if len(x) > 0:
x[idx_repeat:idx_repeat] = [x[idx_repeat] for _ in range(min_len - len(x))]
return tuple(x)
def val2list(x: Union[List, Tuple, Any], repeat_time=1) -> List:
if isinstance(x, (list, tuple)):
return list(x)
return [x for _ in range(repeat_time)]
def get_same_padding(kernel_size: Union[int, Tuple[int, ...]]) -> Union[int, Tuple[int, ...]]:
if isinstance(kernel_size, tuple):
return tuple([get_same_padding(ks) for ks in kernel_size])
else:
assert kernel_size % 2 > 0, "kernel size should be odd number"
return kernel_size // 2
class ConvLayer(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size=3,
stride=1,
dilation=1,
groups=1,
use_bias=False,
dropout=0,
norm="bn2d",
act_func="relu",
):
super(ConvLayer, self).__init__()
padding = get_same_padding(kernel_size)
padding *= dilation
self.dropout = nn.Dropout2d(dropout, inplace=False) if dropout > 0 else None
self.conv = nn.Conv2d(
in_channels,
out_channels,
kernel_size=(kernel_size, kernel_size),
stride=(stride, stride),
padding=padding,
dilation=(dilation, dilation),
groups=groups,
bias=use_bias,
)
self.norm = nn.BatchNorm2d(out_channels)
self.act = nn.ReLU(act_func)
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.dropout is not None:
x = self.dropout(x)
x = self.conv(x)
if self.norm:
x = self.norm(x)
if self.act:
x = self.act(x)
return x
class LiteMLA(nn.Module):
"""Lightweight multi-scale linear attention"""
def __init__(
self,
in_channels: int,
out_channels: int,
heads: Optional[int] = None,
heads_ratio: float = 1.0,
dim=8,
use_bias=False,
norm=(None, "bn2d"),
act_func=(None, None),
kernel_func="relu",
scales: tuple[int, ...] = (5,),
eps=1.0e-15,
):
super(LiteMLA, self).__init__()
self.eps = eps
heads = int(in_channels // dim * heads_ratio) if heads is None else heads
total_dim = heads * dim
use_bias = val2tuple(use_bias, 2)
norm = val2tuple(norm, 2)
act_func = val2tuple(act_func, 2)
self.dim = dim
self.qkv = ConvLayer(
in_channels,
3 * total_dim,
1,
use_bias=use_bias[0],
norm=norm[0],
act_func=act_func[0],
)
self.aggreg = nn.ModuleList(
[
nn.Sequential(
nn.Conv2d(
3 * total_dim,
3 * total_dim,
scale,
padding=get_same_padding(scale),
groups=3 * total_dim,
bias=use_bias[0],
),
nn.Conv2d(3 * total_dim, 3 * total_dim, 1, groups=3 * heads, bias=use_bias[0]),
)
for scale in scales
]
)
self.kernel_func = nn.ReLU(kernel_func)
self.proj = ConvLayer(
total_dim * (1 + len(scales)),
out_channels,
1,
use_bias=use_bias[1],
norm=norm[1],
act_func=act_func[1],
)
@torch.autocast(device_type="cuda", enabled=False)
def relu_linear_att(self, qkv: torch.Tensor) -> torch.Tensor:
B, _, H, W = list(qkv.size())
if qkv.dtype == torch.float16:
qkv = qkv.float()
qkv = torch.reshape(
qkv,
(
B,
-1,
3 * self.dim,
H * W,
),
)
q, k, v = (
qkv[:, :, 0 : self.dim],
qkv[:, :, self.dim : 2 * self.dim],
qkv[:, :, 2 * self.dim :],
)
# lightweight linear attention
q = self.kernel_func(q)
k = self.kernel_func(k)
# linear matmul
trans_k = k.transpose(-1, -2)
v = F.pad(v, (0, 0, 0, 1), mode="constant", value=1)
vk = torch.matmul(v, trans_k)
out = torch.matmul(vk, q)
if out.dtype == torch.bfloat16:
out = out.float()
out = out[:, :, :-1] / (out[:, :, -1:] + self.eps)
out = torch.reshape(out, (B, -1, H, W))
return out
@torch.autocast(device_type="cuda", enabled=False)
def relu_quadratic_att(self, qkv: torch.Tensor) -> torch.Tensor:
B, _, H, W = list(qkv.size())
qkv = torch.reshape(
qkv,
(
B,
-1,
3 * self.dim,
H * W,
),
)
q, k, v = (
qkv[:, :, 0 : self.dim],
qkv[:, :, self.dim : 2 * self.dim],
qkv[:, :, 2 * self.dim :],
)
q = self.kernel_func(q)
k = self.kernel_func(k)
att_map = torch.matmul(k.transpose(-1, -2), q) # b h n n
original_dtype = att_map.dtype
if original_dtype in [torch.float16, torch.bfloat16]:
att_map = att_map.float()
att_map = att_map / (torch.sum(att_map, dim=2, keepdim=True) + self.eps) # b h n n
att_map = att_map.to(original_dtype)
out = torch.matmul(v, att_map) # b h d n
out = torch.reshape(out, (B, -1, H, W))
return out
def forward(self, x: torch.Tensor) -> torch.Tensor:
# generate multi-scale q, k, v
qkv = self.qkv(x)
multi_scale_qkv = [qkv]
for op in self.aggreg:
multi_scale_qkv.append(op(qkv))
qkv = torch.cat(multi_scale_qkv, dim=1)
H, W = list(qkv.size())[-2:]
if H * W > self.dim:
out = self.relu_linear_att(qkv).to(qkv.dtype)
else:
out = self.relu_quadratic_att(qkv)
out = self.proj(out)
return out
if __name__ == "__main__":
# 测试输入张量
input_tensor = torch.randn(1, 32, 256, 256)
# LiteMLA 参数
out_channels = 64 # 输出通道数
heads = None # 默认根据 heads_ratio 自动计算
heads_ratio = 1.0
dim = 8 # 单个头的维度
use_bias = False
norm = (None, "bn2d")
act_func = (None, None)
kernel_func = "relu"
scales = (5, 3) # 多尺度卷积核大小
eps = 1.0e-15
# 初始化 LiteMLA 模块
mla_module = LiteMLA(
in_channels=32,
out_channels=out_channels,
heads=heads,
heads_ratio=heads_ratio,
dim=dim,
use_bias=use_bias,
norm=norm,
act_func=act_func,
kernel_func=kernel_func,
scales=scales,
eps=eps,
)
# 将模型和张量移动到 GPU(如果可用)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
mla_module.to(device)
input_tensor = input_tensor.to(device)
# 前向传播
output = mla_module(input_tensor)
# 输出结果
print(f"输入张量形状: {input_tensor.shape}")
print(f"输出张量形状: {output.shape}")