-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_map.py
370 lines (319 loc) · 12.3 KB
/
train_map.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
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
367
368
369
370
"""
Description:
Author: Jiaqi Gu ([email protected])
Date: 2021-10-24 16:07:39
LastEditors: Jiaqi Gu ([email protected])
LastEditTime: 2021-10-24 16:07:39
"""
#!/usr/bin/env python
# coding=UTF-8
import argparse
import logging
import os
from typing import Iterable
import mlflow
import numpy as np
import torch
import torch.nn as nn
from pyutils.config import configs
from pyutils.general import logger as lg
from pyutils.plot import set_ms
from pyutils.torch_train import (
count_parameters,
get_learning_rate,
load_model,
set_torch_deterministic,
)
from pyutils.typing import Criterion, DataLoader, Optimizer, Scheduler
from core import builder
from core.models.devices.mrr_configs import lambda_res, radius_list
from core.models.layers.utils import (
CrosstalkScheduler,
GlobalTemperatureScheduler,
PhaseVariationScheduler,
calculate_grad_hessian,
)
set_ms()
logging.getLogger("matplotlib.font_manager").disabled = True
def train(
model: nn.Module,
train_loader: DataLoader,
optimizer: Optimizer,
scheduler: Scheduler,
epoch: int,
criterion: Criterion,
device: torch.device,
) -> None:
model.train()
step = epoch * len(train_loader)
correct = 0
for batch_idx, (data, target) in enumerate(train_loader):
data = data.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
optimizer.zero_grad()
output = model(data)
pred = output.data.max(1)[1]
correct += pred.eq(target.data).cpu().sum()
classify_loss = criterion(output, target)
loss = classify_loss
loss.backward()
optimizer.step()
step += 1
if batch_idx % configs.run.log_interval == 0:
lg.info(
"Train Epoch: {} [{:7d}/{:7d} ({:3.0f}%)] Loss: {:.4f} Class Loss: {:.4f}".format(
epoch,
batch_idx * len(data),
len(train_loader.dataset),
100.0 * batch_idx / len(train_loader),
loss.data.item(),
classify_loss.data.item(),
)
)
mlflow.log_metrics({"train_loss": loss.item()}, step=step)
scheduler.step()
accuracy = 100.0 * correct.float() / len(train_loader.dataset)
lg.info(f"Train Accuracy: {correct}/{len(train_loader.dataset)} ({accuracy:.2f})%")
mlflow.log_metrics(
{"train_acc": accuracy.data.item(), "lr": get_learning_rate(optimizer)},
step=epoch,
)
def validate(
model: nn.Module,
validation_loader: DataLoader,
epoch: int,
criterion: Criterion,
loss_vector: Iterable,
accuracy_vector: Iterable,
device: torch.device,
) -> None:
model.eval()
val_loss, correct = 0, 0
counter_probe = 0
# prob_frq = 100
# prob_history = 0.
# probe_error = 1e-3
with torch.no_grad():
for data, target in validation_loader:
data = data.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
output = model(data)
val_loss += criterion(output, target).data.item()
pred = output.data.max(1)[1]
correct += pred.eq(target.data).cpu().sum()
counter_probe = counter_probe + 1
val_loss /= len(validation_loader)
loss_vector.append(val_loss)
accuracy = 100.0 * correct.float() / len(validation_loader.dataset)
accuracy_vector.append(accuracy.item())
lg.info(
"\nValidation set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n".format(
val_loss, correct, len(validation_loader.dataset), accuracy
)
)
mlflow.log_metrics(
{"val_acc": accuracy.data.item(), "val_loss": val_loss}, step=epoch
)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("config", metavar="FILE", help="config file")
# parser.add_argument('--run-dir', metavar='DIR', help='run directory')
# parser.add_argument('--pdb', action='store_true', help='pdb')
args, opts = parser.parse_known_args()
configs.load(args.config, recursive=True)
configs.update(opts)
if torch.cuda.is_available() and int(configs.run.use_cuda):
torch.cuda.set_device(configs.run.gpu_id)
device = torch.device("cuda:" + str(configs.run.gpu_id))
torch.backends.cudnn.benchmark = True
else:
device = torch.device("cpu")
torch.backends.cudnn.benchmark = False
if configs.run.deterministic == True:
set_torch_deterministic()
model = builder.make_model(device)
print(model)
train_loader, validation_loader = builder.make_dataloader()
criterion = builder.make_criterion().to(device)
lg.info(f"Number of parameters: {count_parameters(model)}")
model_name = f"{configs.model.name}_wb-{configs.quantize.weight_bit}_ib-{configs.quantize.input_bit}_icalg-{configs.ic.alg}_icadapt-{configs.ic.adaptive}_icbest-{configs.ic.best_record}"
checkpoint = f"./checkpoint/{configs.checkpoint.checkpoint_dir}/{model_name}_{configs.checkpoint.model_comment}.pt"
lg.info(f"Current checkpoint: {checkpoint}")
mlflow.set_experiment(configs.run.experiment)
experiment = mlflow.get_experiment_by_name(configs.run.experiment)
mlflow.start_run(run_name=model_name)
mlflow.log_params(
{
"exp_name": configs.run.experiment,
"exp_id": experiment.experiment_id,
"run_id": mlflow.active_run().info.run_id,
"inbit": configs.quantize.input_bit,
"wbit": configs.quantize.weight_bit,
"init_lr": configs.optimizer.lr,
"ic_alg": configs.ic.alg,
"ic_adapt": configs.ic.adaptive,
"ic_best_record": configs.ic.best_record,
"checkpoint": checkpoint,
"restore_checkpoint": configs.checkpoint.restore_checkpoint,
"pid": os.getpid(),
}
)
lg.info(
f"Experiment {configs.run.experiment} ({experiment.experiment_id}) starts. Run ID: ({mlflow.active_run().info.run_id}). PID: ({os.getpid()}). PPID: ({os.getppid()}). Host: ({os.uname()[1]})"
)
try:
lg.info(configs)
load_model(
model,
configs.checkpoint.restore_checkpoint,
ignore_size_mismatch=int(configs.checkpoint.no_linear),
)
lg.info("Validate pre-trained model (MODE = weight)...")
validate(model, validation_loader, -3, criterion, [], [], device)
loss = 0
for data, target in validation_loader:
data = data.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
output = model(data)
loss = criterion(output, target)
loss.backward()
if configs.noise.PV_schedule == "low":
mean_schedule_fn = lambda x: 0.0025 * x
std_schedule_fn = lambda x: 0.004 * x + 0.002
elif configs.noise.PV_schedule == "high":
mean_schedule_fn = lambda x: 0.01 * x
std_schedule_fn = lambda x: 0.005 * x + 0.005
phase_variation_scheduler = PhaseVariationScheduler(
size=[4, 4, 8, 8],
T_max=100000,
mean_schedule_fn=mean_schedule_fn,
std_schedule_fn=std_schedule_fn,
smoothing_kernel_size=5,
smoothing_factor=0.05,
smoothing_mode="arch",
min_std=0.001,
momentum=0.9,
noise_scenario_src=configs.noise.noise_scenario_src,
noise_scenario_tgt=configs.noise.noise_scenario_tgt,
random_state=0,
device=device,
)
if configs.noise.TD_schedule == "linear":
# schedule = "Spatial"
TD_schedule_fn = lambda x: x + 300
elif configs.noise.TD_schedule == "cosine":
# schedule = "perturbation"
TD_schedule_fn = lambda x: (0.5 * np.cos(10 * x) + 0.5) + 300
elif configs.noise.TD_schedule == "uneven":
TD_schedule_fn = lambda x: x**3 + 300
else:
raise NotImplementedError
global_temperature_scheduler = GlobalTemperatureScheduler(
T_max=100000,
schedule_fn=TD_schedule_fn,
T0=300,
lambda_res=lambda_res,
L_list=2 * np.pi * radius_list,
device=torch.device("cuda"),
)
crosstalk_scheduler = CrosstalkScheduler(
Size=[4, 4, 8, 8],
crosstalk_coupling_factor=configs.noise.crosstalk_factor,
interv_h=configs.noise.inter_h,
interv_v=configs.noise.inter_v,
)
model.set_noise_schedulers(
scheduler_dict={
"phase_variation_scheduler": phase_variation_scheduler,
"global_temp_scheduler": global_temperature_scheduler,
"crosstalk_scheduler": crosstalk_scheduler,
}
)
model.step_noise_scheduler(T=configs.noise.delta_T)
model.set_phase_variation(configs.noise.set_PV)
if configs.noise.set_PV:
lg.info(
"Validate converted pre-trained model (MODE = phase) with phase variation..."
)
validate(model, validation_loader, -1, criterion, [], [], device)
# set global temp drift
model.set_global_temp_drift(configs.noise.set_GTD)
if configs.noise.set_GTD:
lg.info(
"Validate converted pre-trained model (MODE = phase) with global temperature drift..."
)
validate(model, validation_loader, -1, criterion, [], [], device)
model.set_crosstalk_noise(configs.noise.set_Crosstalk)
if configs.noise.set_Crosstalk:
lg.info(
"Validate converted pre-trained model (MODE = phase) with crosstalk noise..."
)
validate(model, validation_loader, -1, criterion, [], [], device)
def build_validate_callback(
model,
num_steps,
step_vector=[],
acc_vector=[],
loss_vector=[],
cycle_vector=[],
interval=10,
):
def validate_callback(step, cycle, loss):
if step % interval == 0 or step == num_steps - 1:
step_vector.append(step)
cycle_vector.append(cycle)
loss_vector.append(loss)
validate(
model=model,
validation_loader=validation_loader,
epoch=-1,
criterion=criterion,
loss_vector=[],
accuracy_vector=acc_vector,
device=device,
)
return validate_callback
step_vector = []
acc_vector = []
loss_vector = []
cycle_vector = []
validate_callback_fn = build_validate_callback(
model,
configs.mapping.num_steps,
step_vector=step_vector,
acc_vector=acc_vector,
loss_vector=loss_vector,
cycle_vector=cycle_vector,
interval=configs.mapping.validate_interval,
)
calculate_grad_hessian(
model, train_loader, criterion, num_samples=10, device="cuda:0"
)
model.gen_weight_salience(mode=configs.mapping.salience_mode)
# model.gen_sparsity_mask(sparsity=configs.mapping.sparsity, mode=configs.mapping.sparsity_mode)
model.map_to_hardware(
[
1,
configs.dataset.in_channel,
configs.dataset.img_height,
configs.dataset.img_width,
],
lr=configs.mapping.lr,
num_steps=configs.mapping.num_steps,
stop_thres=configs.mapping.stop_thres,
average_times=configs.mapping.average_times,
criterion=configs.mapping.criterion,
validation_callback=validate_callback_fn,
sparsity=configs.mapping.sparsity,
sparsity_mode=configs.mapping.sparsity_mode,
)
print("steps", step_vector)
print("cycle", cycle_vector)
print("loss", loss_vector)
print("acc", acc_vector)
lg.info("Validate converted pre-trained model (MODE = phase) with Mapping")
validate(model, validation_loader, -1, criterion, [], [], device)
except KeyboardInterrupt:
lg.warning("Ctrl-C Stopped")
if __name__ == "__main__":
main()