-
Notifications
You must be signed in to change notification settings - Fork 28
/
standalone_cifar.py
290 lines (245 loc) · 10.2 KB
/
standalone_cifar.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
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
'''
Train a long conv model on sequential CIFAR10 / sequential MNIST with PyTorch for demonstration purposes.
This code borrows heavily from https://github.com/kuangliu/pytorch-cifar and is based on https://github.com/HazyResearch/state-spaces.
* Train standard sequential CIFAR:
python -m standalone_cifar
* Train sequential CIFAR grayscale:
python -m standalone_cifar --grayscale
It imports LongConv from the `standalone_long_convs.py` file.
The default CIFAR10 model trained by this file should get
~81% accuracy on the CIFAR10 val set.
Each epoch takes approximately 43s on an H100 GPU.
'''
import torch
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import os
import argparse
from tqdm.auto import tqdm
from flashfftconv_long_convs import LongConvModel
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
# Optimizer
parser.add_argument('--lr', default=0.001, type=float, help='Learning rate')
parser.add_argument('--weight_decay', default=0.05, type=float, help='Weight decay')
# Scheduler
parser.add_argument('--epochs', default=300, type=float, help='Training epochs')
# Dataset
parser.add_argument('--grayscale', action='store_true', help='Use grayscale CIFAR10')
# Dataloader
parser.add_argument('--num_workers', default=4, type=int, help='Number of workers to use for dataloader')
parser.add_argument('--batch_size', default=96, type=int, help='Batch size')
# Model
parser.add_argument('--n_layers', default=6, type=int, help='Number of layers')
parser.add_argument('--d_model', default=256, type=int, help='Model dimension')
parser.add_argument('--dropout', default=0.1, type=float, help='Model Dropout')
parser.add_argument('--kernel_dropout', default=0.2, type=float, help='Kernel Dropout')
parser.add_argument('--kernel_lr', default=0.001, type=float, help='Kernel Learning Rate')
parser.add_argument('--kernel_lam', default=0.001, type=float, help='Kernel Squash Parameter')
parser.add_argument('--prenorm', action='store_true', help='Prenorm')
parser.add_argument('--seed', default=0, type=int, help='Random seed')
# General
parser.add_argument('--resume', '-r', action='store_true', help='Resume from checkpoint')
args = parser.parse_args()
torch.manual_seed(args.seed)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
best_acc = 0 # best test accuracy
start_epoch = 0 # start from epoch 0 or last checkpoint epoch
# Data
print(f'==> Preparing data..')
def split_train_val(train, val_split):
train_len = int(len(train) * (1.0-val_split))
train, val = torch.utils.data.random_split(
train,
(train_len, len(train) - train_len),
generator=torch.Generator().manual_seed(42),
)
return train, val
if args.grayscale:
transform = transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize(mean=122.6 / 255.0, std=61.0 / 255.0),
transforms.Lambda(lambda x: x.view(1, 1024).t())
])
else:
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
transforms.Lambda(lambda x: x.view(3, 1024).t())
])
# Train with no data augmentation
transform_train = transform_test = transform
trainset = torchvision.datasets.CIFAR10(
root='./data/cifar/', train=True, download=True, transform=transform_train)
trainset, _ = split_train_val(trainset, val_split=0.1)
valset = torchvision.datasets.CIFAR10(
root='./data/cifar/', train=True, download=True, transform=transform_test)
_, valset = split_train_val(valset, val_split=0.1)
testset = torchvision.datasets.CIFAR10(
root='./data/cifar/', train=False, download=True, transform=transform_test)
d_input = 3 if not args.grayscale else 1
d_output = 10
# Dataloaders
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, drop_last=True)
valloader = torch.utils.data.DataLoader(
valset, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, drop_last=True)
testloader = torch.utils.data.DataLoader(
testset, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, drop_last=True)
# Model
print('==> Building model..')
long_conv_args = {
'kernel_dropout': args.kernel_dropout,
'kernel_learning_rate': args.kernel_lr,
'kernel_lam': args.kernel_lam,
'channels': 1,
}
model = LongConvModel(
d_input=d_input,
d_output=d_output,
d_model=args.d_model,
n_layers=args.n_layers,
dropout=args.dropout,
prenorm=args.prenorm,
**long_conv_args,
)
model = model.to(device)
if device == 'cuda':
cudnn.benchmark = True
if args.resume:
# Load checkpoint.
print('==> Resuming from checkpoint..')
assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!'
checkpoint = torch.load('./checkpoint/ckpt.pth')
model.load_state_dict(checkpoint['model'])
best_acc = checkpoint['acc']
start_epoch = checkpoint['epoch']
def setup_optimizer(model, lr, weight_decay, epochs):
"""
Following S4, train the convolution layers with a smaller learning rate, with no weight decay.
The rest of the model can be trained with a higher learning rate (e.g. 0.004, 0.01)
and weight decay (if desired).
"""
# All parameters in the model
all_parameters = list(model.parameters())
# General parameters don't contain the special _optim key
params = [p for p in all_parameters if not hasattr(p, "_optim")]
# Create an optimizer with the general parameters
optimizer = optim.AdamW(params, lr=lr, weight_decay=weight_decay)
# Add parameters with special hyperparameters
hps = [getattr(p, "_optim") for p in all_parameters if hasattr(p, "_optim")]
hps = [
dict(s) for s in sorted(list(dict.fromkeys(frozenset(hp.items()) for hp in hps)))
] # Unique dicts
for hp in hps:
params = [p for p in all_parameters if getattr(p, "_optim", None) == hp]
optimizer.add_param_group(
{"params": params, **hp}
)
# Create a lr scheduler
# scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=patience, factor=0.2)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, epochs)
# Print optimizer info
keys = sorted(set([k for hp in hps for k in hp.keys()]))
for i, g in enumerate(optimizer.param_groups):
group_hps = {k: g.get(k, None) for k in keys}
print(' | '.join([
f"Optimizer group {i}",
f"{len(g['params'])} tensors",
] + [f"{k} {v}" for k, v in group_hps.items()]))
return optimizer, scheduler
criterion = nn.CrossEntropyLoss()
optimizer, scheduler = setup_optimizer(
model, lr=args.lr, weight_decay=args.weight_decay, epochs=args.epochs
)
###############################################################################
# Everything after this point is standard PyTorch training!
###############################################################################
# Training
def train():
model.train()
train_loss = 0
correct = 0
total = 0
pbar = tqdm(enumerate(trainloader))
for batch_idx, (inputs, targets) in pbar:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
model.batch_idx = batch_idx
inputs = inputs.to(torch.bfloat16)
with torch.autocast(device, dtype=torch.bfloat16, enabled=True):
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
for p in model.parameters():
if torch.any(torch.isnan(p)):
breakpoint()
for p in model.parameters():
if torch.any(torch.isnan(p.grad)):
breakpoint()
# breakpoint()
optimizer.step()
for p in model.parameters():
if torch.any(torch.isnan(p)):
breakpoint()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
pbar.set_description(
'Batch Idx: (%d/%d) | Loss: %.3f | Train Acc: %.3f%% (%d/%d)' %
(batch_idx, len(trainloader), train_loss/(batch_idx+1), 100.*correct/total, correct, total)
)
def eval(epoch, dataloader, checkpoint=False):
global best_acc
model.eval()
eval_loss = 0
correct = 0
total = 0
with torch.no_grad():
pbar = tqdm(enumerate(dataloader))
for batch_idx, (inputs, targets) in pbar:
model.batch_idx = batch_idx
inputs, targets = inputs.to(device), targets.to(device)
inputs = inputs.to(torch.bfloat16)
with torch.autocast(device, dtype=torch.bfloat16, enabled=True):
outputs = model(inputs)
loss = criterion(outputs, targets)
eval_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
pbar.set_description(
'Batch Idx: (%d/%d) | Loss: %.3f | Eval Acc: %.3f%% (%d/%d)' %
(batch_idx, len(dataloader), eval_loss/(batch_idx+1), 100.*correct/total, correct, total)
)
# Save checkpoint.
if checkpoint:
acc = 100.*correct/total
if acc > best_acc:
state = {
'model': model.state_dict(),
'acc': acc,
'epoch': epoch,
}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, './checkpoint/ckpt.pth')
best_acc = acc
return acc, eval_loss/(batch_idx + 1)
pbar = tqdm(range(start_epoch, args.epochs))
for epoch in pbar:
if epoch == 0:
pbar.set_description('Epoch: %d' % (epoch))
else:
pbar.set_description('Epoch: %d | Val acc: %1.3f | Val loss: %1.3f' % (epoch, val_acc, val_loss))
train()
val_acc, val_loss = eval(epoch, valloader, checkpoint=True)
eval(epoch, testloader)
print()
print('Epoch: %d | Val acc: %1.3f | Val loss: %1.3f' % (epoch, val_acc, val_loss))
print()
scheduler.step()