-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
366 lines (289 loc) · 10.6 KB
/
models.py
File metadata and controls
366 lines (289 loc) · 10.6 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import pack, rearrange, unpack
from torch.backends.cuda import sdp_kernel, SDPBackend
from utils import set_seed
from constants import *
backend_map = {
SDPBackend.MATH: {
"enable_math": True,
"enable_flash": False,
"enable_mem_efficient": False,
},
SDPBackend.FLASH_ATTENTION: {
"enable_math": False,
"enable_flash": True,
"enable_mem_efficient": False,
},
SDPBackend.EFFICIENT_ATTENTION: {
"enable_math": False,
"enable_flash": False,
"enable_mem_efficient": True,
},
}
#######################################
class Net(nn.Module):
"""
Standard convolutional neural network for image classification.
"""
def __init__(self, return_act=False):
super().__init__()
self.conv1 = nn.Conv2d(3, 64, 3, 1)
self.conv2 = nn.Conv2d(64, 128, 3, 1)
self.conv3 = nn.Conv2d(128, 256, 3, 1)
self.conv4 = nn.Conv2d(256, 512, 3, 1)
self.dropout = nn.Dropout(0.2)
self.batchnorm2d_1 = nn.BatchNorm2d(128)
self.batchnorm2d_2 = nn.BatchNorm2d(512)
self.fc1 = nn.Linear(512 * 2 * 2, 128)
self.fc2 = nn.Linear(128, 100) # 100 classes for fine labels
self.return_act = return_act
def forward(self, x):
x = self.conv1(x)
act1 = x # (BATCH_SIZE, 64, 30, 30)
x = F.relu(x)
x = self.conv2(x)
act2 = x # (BATCH_SIZE, 128, 28, 28)
x = F.relu(x)
x = self.batchnorm2d_1(x)
x = F.max_pool2d(x, 2)
x = self.dropout(x)
x = self.conv3(x)
act3 = x # (BATCH_SIZE, 256, 12, 12)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.conv4(x)
act4 = x # (BATCH_SIZE, 512, 4, 4)
x = F.relu(x)
x = self.batchnorm2d_2(x)
x = F.max_pool2d(x, 2)
x = self.dropout(x)
x = torch.flatten(x, 1)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
if self.return_act:
return x, (act1, act2, act3, act4)
else:
return x
def get_model_and_optimizer(seed=None, student=False, ct_model=None, return_act=False):
"""
student (bool=False): If True, indicates instantiation of a student model to be unlearned via STUDENT_LR
return_act (bool=False): If True, instantiated model will return activation after every conv layer
"""
if student:
assert (
ct_model is not None
), "If initializing a student model, must pass in a competent teacher `ct_model`."
if seed:
set_seed(seed)
model = Net(return_act=return_act)
if student:
model.load_state_dict(ct_model.state_dict())
optimizer = torch.optim.AdamW(model.parameters(), lr=STUDENT_LR)
else:
optimizer = torch.optim.AdamW(model.parameters(), lr=LR)
return model, optimizer
class AttackNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(100, 256) # input = shadow model probs for CIFAR-100
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 1)
def forward(self, x):
x = F.dropout(F.relu(self.fc1(x)))
x = F.dropout(F.relu(self.fc2(x)))
x = self.fc3(x)
return F.log_softmax(x, dim=1)
def get_attack_model_and_optimizer():
model = AttackNet()
optimizer = torch.optim.AdamW(model.parameters())
return model, optimizer
#############
# Vision Transformer
class MLP(nn.Module):
def __init__(self, n_embd, n_ff, dropout=0.1):
super().__init__()
self.net = nn.Sequential(
nn.Linear(n_embd, n_ff),
nn.GELU(),
nn.Dropout(p=dropout),
nn.Linear(n_ff, n_embd),
)
def forward(self, x):
return self.net(x)
class MultiHeadAttention(nn.Module):
def __init__(
self,
n_embd,
n_head,
n_kv_head,
device,
dropout=0.1,
):
super().__init__()
self.n_embd = n_embd
self.n_head = n_head
self.head_dim = n_embd // n_head
self.drop = nn.Dropout(p=dropout)
self.n_kv_head = n_kv_head
self.n_repeat = self.n_head // self.n_kv_head
self.query = nn.Linear(n_embd, n_embd, bias=False)
self.key = nn.Linear(n_embd, n_kv_head * self.head_dim, bias=False)
self.value = nn.Linear(n_embd, n_kv_head * self.head_dim, bias=False)
self.out = nn.Linear(n_embd, n_embd, bias=False)
self.device = device
def split_heads(self, x, n_head):
B, S, D = x.size()
# split dimension into n_head * head_dim, then transpose the sequence length w/ n_head
# output: [B, n_head, S, head_dim]
return x.view(B, S, n_head, self.head_dim).transpose(1, 2)
def combine_heads(self, x):
B, _, S, head_dim = x.size() # _ is n_head which we will merge
# output: [B, S, n_embd]
return x.transpose(1, 2).contiguous().view(B, S, self.n_embd)
def forward(self, x):
# x: (B, S, n_embd)
# Step 1 and 2: Project query, key, value, then split via reshaping
q = self.split_heads(self.query(x), self.n_head)
k = self.split_heads(self.key(x), self.n_kv_head)
v = self.split_heads(self.value(x), self.n_kv_head)
## GQA
k, v = repeat_kv(k, v, self.n_repeat)
assert (
k.shape[1] == self.n_head and v.shape[1] == self.n_head
), "key and value n_head do not match query n_head"
# q, k, v [B, n_head, S, head_dim)
# Step 3: Compute scaled dot-product attention
with sdp_kernel(**backend_map[SDPBackend.FLASH_ATTENTION]):
try:
attn = F.scaled_dot_product_attention(
q, k, v, dropout_p=self.drop.p if self.device.type == "cuda" else 0
) # ViT: not causal ofc
# CPU: Both fused kernels do not support non-zero dropout. (Dec 2023)
except RuntimeError:
print("FlashAttention is not supported. See warnings for reasons.")
# Step 4 and 5: Concatenate attention scores, return projected output matrix
out = self.out(self.combine_heads(attn)) # (B, S, n_embd)
return out
# helper function for GQA
def repeat_kv(k, v, n_repeat):
k = torch.repeat_interleave(k, repeats=n_repeat, dim=1)
v = torch.repeat_interleave(v, repeats=n_repeat, dim=1)
return k, v
class Block(nn.Module):
def __init__(self, n_embd, n_head, n_ff, n_kv_head, device, norm_first, dropout):
super().__init__()
self.sa = MultiHeadAttention(
n_embd,
n_head,
n_kv_head,
device,
dropout,
)
self.ff = MLP(n_embd, n_ff, dropout=dropout)
self.ln1 = nn.LayerNorm(n_embd)
self.ln2 = nn.LayerNorm(n_embd)
self.norm_first = norm_first
self.drop = nn.Dropout(p=dropout)
def forward(self, x):
# residual connection (stream)
# pre layer norm
if self.norm_first:
x = x + self.drop(self.sa(self.ln1(x)))
x = x + self.drop(self.ff(self.ln2(x)))
else:
x = self.ln1(x + self.drop(self.sa(x)))
x = self.ln2(x + self.drop(self.ff(x)))
return x
###########################################
class PatchEmbedding(nn.Module):
"""
Applies patch embeddings to an image.
"""
def __init__(self, patch_size, n_embd, in_channels=3):
super().__init__()
self.patch_size = patch_size
self.conv = nn.Conv2d(
in_channels, n_embd, kernel_size=patch_size, stride=patch_size
)
# self.num_patches = (img_size // patch_size) ** 2
def forward(self, x):
# (B, C, img_size, img_size) -> (B, num_patches, n_embd)
x = self.conv(x) # (B, n_embd, img_size//patch_size, img_size//patch_size)
x = rearrange(x, "b c h w -> b (h w) c")
# equivalent to above line: x = x.flatten(2).transpose(-1, -2)
return x
class ViT(nn.Module):
"""
Standard Vision Transformer.
TODO
img_size (int): Width/height of input images (assuming square), e.g. 32 for CIFAR-10
"""
def __init__(
self,
n_embd,
n_head,
n_ff,
n_layer,
n_class,
img_size,
patch_size,
device,
norm_first,
n_kv_head=None,
dropout=0.1,
):
super().__init__()
self.patch_embedding = PatchEmbedding(patch_size, n_embd)
self.num_patches = (img_size // patch_size) ** 2
self.cls_token = nn.Parameter(torch.randn(1, 1, n_embd))
self.pos_embedding = nn.Parameter(torch.randn(1, 1 + self.num_patches, n_embd))
self.blocks = nn.Sequential(
*[
Block(n_embd, n_head, n_ff, n_kv_head, device, norm_first, dropout)
for i in range(n_layer)
]
)
self.mlp_head = nn.Linear(n_embd, n_class)
self.drop = nn.Dropout(dropout)
self.device = device
self.init_params()
# weight initialization (Xavier uniform)
def init_params(self, default_initialization=False):
if not default_initialization:
for name, p in self.named_parameters():
if p.dim() > 1:
# excludes layer norm and biases
nn.init.xavier_uniform_(p)
elif "bias" in name:
nn.init.zeros_(p)
# Remark: Xavier normal is not supported at this time.
def forward(self, x):
B = x.shape[0] # (B, num_channels, img_size, img_size)
x = self.patch_embedding(x)
cls_token = self.cls_token.expand(
B, -1, -1
) # prepares cls_token from (1,1,n_embd) to (B, 1, n_embd)
x, ps = pack(
(cls_token, x), "b * n_embd"
) # expand into dim=1 to form num_patches+1
# identical to x = torch.cat((cls_token, x), dim=1)
x += self.pos_embedding # identical shape, no broadcasting
x = self.drop(x)
# (B, 1+num_patches, n_embd)
for block in self.blocks:
x = block(x) # (B, 1+num_patches, n_embd)
# retrieve logits on the class token only
x, _ = unpack(x, ps, "b * n_embd")
x = rearrange(x, "b 1 n_embd -> b n_embd")
logits = self.mlp_head(x) # (B, n_class)
return logits
def get_vit_and_optimizer(seed=None, **kwargs):
if seed:
set_seed(seed)
model = ViT(**kwargs)
optimizer = torch.optim.AdamW(model.parameters(), lr=LR)
return model, optimizer