-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
177 lines (156 loc) · 6.67 KB
/
block.py
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
import torch.nn as nn
from timm.layers import DropPath
from timm.models.vision_transformer import LayerScale
from timm.layers.trace_utils import _assert
from model.adapter import Adapter
from model.convpass import ConvPass
from model.repadapter import RepAdapter
from model.ssf import init_ssf_scale_shift, ssf_ada
import torch
from typing import Any, Callable, Dict, Optional, Set, Tuple, Type, Union, List
from model.mlp import MlpPETL
from model.attention import AttentionPETL
MODULE_REGISTRY = {
'adapter': Adapter,
'convpass': ConvPass,
'repadapter': RepAdapter
}
class BlockPETL(nn.Module):
def __init__(
self,
dim: int,
num_heads: int,
mlp_ratio: float = 4.,
qkv_bias: bool = False,
qk_norm: bool = False,
proj_drop: float = 0.,
attn_drop: float = 0.,
init_values: Optional[float] = None,
drop_path: float = 0.,
act_layer: nn.Module = nn.GELU,
norm_layer: nn.Module = nn.LayerNorm,
mlp_layer: nn.Module = MlpPETL,
params=None,
fact=None
) -> None:
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = AttentionPETL(
dim,
num_heads=num_heads,
qkv_bias=qkv_bias,
qk_norm=qk_norm,
attn_drop=attn_drop,
proj_drop=proj_drop,
norm_layer=norm_layer,
############# Added module #############
params=params,
fact=fact
############# Added module end #############
)
self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
self.drop_path1 = DropPath(drop_path) if drop_path > 0. else nn.Identity()
self.norm2 = norm_layer(dim)
self.mlp = mlp_layer(
in_features=dim,
hidden_features=int(dim * mlp_ratio),
act_layer=act_layer,
drop=proj_drop,
############# Added module #############
params=params,
fact=fact
############# Added module end #############
)
self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
self.drop_path2 = DropPath(drop_path) if drop_path > 0. else nn.Identity()
############# Added module #############
self.params = params
if params.ft_attn_module:
self.ft_attn_module = MODULE_REGISTRY[params.ft_attn_module](dim=dim, params=params)
if params.ft_mlp_module:
self.ft_mlp_module = MODULE_REGISTRY[params.ft_mlp_module](dim=dim, params=params)
if self.params.ssf:
self.ssf_scale_1, self.ssf_shift_1 = init_ssf_scale_shift(dim)
self.ssf_scale_2, self.ssf_shift_2 = init_ssf_scale_shift(dim)
if self.params.difffit:
self.difffit_gamma1 = nn.Parameter(torch.ones(dim))
self.difffit_gamma2 = nn.Parameter(torch.ones(dim))
self.fact = fact
############# Added module end #############
def forward(self, x: torch.Tensor, idx) -> torch.Tensor:
# MHSA path
residual_attn = x
if self.params.ssf:
x_norm1 = ssf_ada(self.norm1(x), self.ssf_scale_1, self.ssf_shift_1)
else:
x_norm1 = self.norm1(x)
# ft attention module
if self.params.ft_attn_module:
if self.params.ft_attn_mode == 'parallel':
x_original = self.drop_path1(self.ls1(self.attn(x_norm1, idx)))
if self.params.ft_attn_ln == 'before':
x_ft_attn = self.drop_path1(self.ls1(self.ft_attn_module(x))) + x_original
elif self.params.ft_attn_ln == 'after':
x_ft_attn = self.drop_path1(self.ls1(self.ft_attn_module(x_norm1))) + x_original
else:
raise NotImplementedError
del x_original
elif self.params.ft_attn_mode == 'sequential_after':
x_original = self.drop_path1(self.ls1(self.attn(x_norm1, idx)))
x_ft_attn = self.drop_path1(self.ls1(self.ft_attn_module(x_original, add_residual=True)))
del x_original
elif self.params.ft_attn_mode == 'sequential_before':
x_ft_attn = self.drop_path1(self.ls1(self.attn(self.ft_attn_module(x_norm1), idx)))
else:
raise NotImplementedError
torch.cuda.empty_cache()
else:
# no tuning
x_ft_attn = self.drop_path1(self.ls1(self.attn(x_norm1, idx)))
# residual for attention module
if self.params.difffit:
x = self.difffit_gamma1 * x_ft_attn + residual_attn
else:
x = x_ft_attn + residual_attn
del x_norm1, x_ft_attn, residual_attn
torch.cuda.empty_cache()
# MLP path
residual_mlp = x
if self.params.ssf:
x_norm2 = ssf_ada(self.norm2(x), self.ssf_scale_2, self.ssf_shift_2)
else:
x_norm2 = self.norm2(x)
# ft mlp module
if self.params.ft_mlp_module:
if self.params.ft_mlp_mode == 'parallel':
x_original = self.drop_path2(self.ls2(self.mlp(x_norm2, idx)))
if self.params.ft_mlp_ln == 'before':
x_ft_mlp = self.drop_path2(self.ls2(self.ft_mlp_module(x))) + x_original
elif self.params.ft_mlp_ln == 'after':
x_ft_mlp = self.drop_path2(self.ls2(self.ft_mlp_module(x_norm2))) + x_original
else:
raise NotImplementedError
del x_original
elif self.params.ft_mlp_mode == 'sequential_after':
x_original = self.drop_path2(self.ls2(self.mlp(x_norm2, idx)))
x_ft_mlp = self.drop_path2(self.ls2(self.ft_mlp_module(x_original, add_residual=True)))
del x_original
elif self.params.ft_attn_mode == 'sequential_before':
x_ft_mlp = self.drop_path2(self.ls2(self.mlp(self.ft_mlp_module(x_norm2), idx)))
else:
raise NotImplementedError
torch.cuda.empty_cache()
else:
# no tuning
x_ft_mlp = self.drop_path2(self.ls2(self.mlp(x_norm2, idx)))
# residual for mlp module
if self.params.difffit:
x = self.difffit_gamma2 * x_ft_mlp + residual_mlp
else:
x = x_ft_mlp + residual_mlp
del x_norm2, x_ft_mlp, residual_mlp
torch.cuda.empty_cache()
# Original forward
# x = x + self.drop_path1(self.ls1(self.attn(self.norm1(x))))
# x = x + self.drop_path2(self.ls2(self.mlp(self.norm2(x))))
return x