-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet_quant_dyn.py
362 lines (234 loc) · 10.4 KB
/
resnet_quant_dyn.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
#!/usr/bin/env python
# coding: utf-8
# # Federated Learning + Linear Quantization for RESNET18 & CIFAR10
# In[2]:
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
import numpy as np
from tqdm import tqdm
import seaborn as sns
from fedlern.utils import *
from fedlern.train_utils import *
from fedlern.quant_utils import *
from fedlern.models.resnet_v2 import ResNet18
import csv
import argparse
parser = argparse.ArgumentParser(description="Run FedLern+Quantization")
parser.add_argument("arg1", type=int, help="Head Size")
parser.add_argument("arg2", type=int, help="Tail Size.")
parser.add_argument("arg3", type=int, help="Bits Head")
parser.add_argument("arg4", type=int, help="Bits Body")
parser.add_argument("arg5", type=int, help="Bits Tail.")
args = parser.parse_args()
csv_file_path = "./report_dyn.csv"
# ## Relevant Parameters
# In[14]:
epoch = 5
rounds = 30#40#25
num_clients = 10
lrn_rate = 0.1
clients_sample_size = int(.3 * num_clients) # Use 30% of available clients
num_workers = 8
train_batch_size = 32 #128
eval_batch_size= 32 #256
head = args.arg1
tail = args.arg2
bits_head = args.arg3
bits_body = args.arg4
bits_tail = args.arg5
stats = (0.49139968, 0.48215841, 0.44653091), (0.24703223, 0.24348513, 0.26158784)
# ## Data Loaders
# - Divide the test & training data
# - Divide the training data among the clients
# In[4]:
# Data augmentation and normalization for training
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(*stats)
])
# Normalization for testing
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(*stats)
])
# CIFAR-10 dataset
train_dataset = datasets.CIFAR10(root='./data', train=True, transform=transform_train, download=True)
test_dataset = datasets.CIFAR10(root='./data', train=False, transform=transform_test)
# split the training data
train_splits = torch.utils.data.random_split(train_dataset, [int(train_dataset.data.shape[0]/num_clients) for i in range(num_clients)])
# Data loaders
train_loaders = [DataLoader(dataset=split, batch_size=train_batch_size, shuffle=True, num_workers=num_workers) for split in train_splits]
test_loader = DataLoader(dataset=test_dataset, batch_size=eval_batch_size, shuffle=False, num_workers=num_workers)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#
# - `server_aggregate` function aggregates the model weights received from every client and updates the global model with the updated weights.
# ## Global & Clients instatiation
# Implement the same elements as before, but:
# - We need more instances of the model
# - An optimizer for each model
# In[5]:
global_model = ResNet18().to(device)
client_models = [ResNet18()
.to(device)
#.load_state_dict(global_model.state_dict())
for _ in range(num_clients)]
# # Define the criterion, and pair of optimizers for global model
# The first optimizer holds the regular weights, while the second one holds the quantized parameters, which are to be propagated to the clients.
# In[6]:
# Optimizer & criterion based on gobal model
global_optimizer = get_model_optimizer(global_model,
learning_rate=lrn_rate,
weight_decay=5e-4)
criterion = nn.CrossEntropyLoss().to(device) # computes the cross-entropy loss between the predicted and true labels
# ----Optimizer for quantized model on global model----
# Copy the parameters
all_G_kernels = [kernel.data.clone().requires_grad_(True)
for kernel in global_optimizer.param_groups[1]['params']]
kernels = [{'params': all_G_kernels}]
# New optimizer for the quantized weights
goptimizer_quant = optim.SGD(kernels, lr=0)
# In[17]:
quantize_bits = [bits_body] * len(goptimizer_quant.param_groups[0]['params'])
# head
quantize_bits[:head] = [bits_head] * head
# tail
quantize_bits[-tail:] = [bits_tail] * tail
print(quantize_bits)
# ## Define optimizers for the clients.
# Each clients has a pair of optimizers
# In[8]:
optimizers = [get_model_optimizer(model,learning_rate=lrn_rate, weight_decay=5e-4) for model in client_models]
optimizers_quant = []
for optimizer in optimizers:
# Copy the parameters
all_G_kernels = [kernel.data.clone().requires_grad_(True)
for kernel in optimizer.param_groups[1]['params']]
# Handle of the optimizer parameters
all_W_kernels = optimizer.param_groups[1]['params']
kernels = [{'params': all_G_kernels}]
# New optimizer for the quantized weights
optimizer_quant = optim.SGD(kernels, lr=0)
optimizers_quant.append(optimizer_quant)
# In[9]:
server_test = to_quantized_model_decorator(global_optimizer, goptimizer_quant)(evaluate_model)
# Decorators magic
# first we wrap the update function with the decorator for the global model switch
server_switch_aggregate = to_quantized_model_decorator(global_optimizer, goptimizer_quant)(server_update)
# then we wrap the update function with the decorator for each of the clients switch
switch_client_weights = [to_quantized_model_decorator(fp32_optimizer, quant_optimizer) for fp32_optimizer, quant_optimizer in zip(optimizers, optimizers_quant)]
# In[10]:
server_update_quant = switch_client_weights[0](server_switch_aggregate)
for funct in switch_client_weights[1:]:
server_update_quant = funct(server_update_quant)
# In[11]:
server_update_quant(global_model, client_models)
# In[12]:
# initialize lists to store the training and testing losses and accuracies
train_losses = []
test_losses = []
train_accs = []
test_accs = []
for round in tqdm(range(rounds)):
# Select n random clients
selected_clients = np.random.permutation(num_clients)[:clients_sample_size]
# Train the selected clients
for client in selected_clients:
# Individual criterion and optimizer
print(f"Client {client} training")
train_loss, train_acc = qtrain_model(model = client_models[client],
train_loader = train_loaders[client],
device = device,
criterion = criterion,
optimizer = optimizers[client],
optimizer_quant = optimizers_quant[client],
num_epochs=epoch,
bits = quantize_bits)
train_losses.append(train_loss)
train_accs.append(train_acc)
# Aggregate in 3 steps
server_aggregate(global_model, client_models)
server_quantize(global_optimizer, goptimizer_quant, quantize_bits)
server_update_quant(global_model, client_models)
# Test the global model
test_loss, test_acc = server_test(model=global_model,
test_loader=test_loader,
device=device,
criterion=criterion)
test_losses.append(test_loss)
test_accs.append(test_acc)
print(f"{round}-th ROUND: average train loss {(train_loss / clients_sample_size):0.3g} | test loss {test_loss:0.3g} | test acc: {test_acc:0.3f}")
# In[ ]:
quantized_histogram = to_quantized_model_decorator(global_optimizer, goptimizer_quant)(histogram_conv1)
# Evaluate the model's histogram
before = histogram_conv1(global_model)
plot_histogram(before[0], before[1],f"CONV1 - fp32")
after = quantized_histogram(global_model)
plot_histogram(after[0], after[1], f"CONV1 - {quantize_bits} bits")
# In[ ]:
print(before[0].shape, "\n",after[0].shape)
# In[ ]:
# plot the training loss
sns.set(style='darkgrid')
plt.plot(train_losses)
plt.title('Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
#plt.show()
plt.savefig(f'./plots/training_loss_{quantize_bits}.png')
plt.close()
# In[ ]:
train_accs = [d.item() for d in train_accs]
test_accs = [d.item() for d in test_accs]
# In[ ]:
# plot the training and testing accuracies
sns.set(style='darkgrid')
plt.plot(train_accs, label='Train Acc')
plt.plot(test_accs, label='Test Acc')
plt.title('Training and Testing Accuracies')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
#plt.show()
plt.savefig(f'./plots/training_testing_{quantize_bits}.png')
plt.close()
# In[ ]:
cuda_device = torch.device("cuda:0")
cpu_device = torch.device("cpu:0")
# Test the model
loss, acc = server_test(global_model, test_loader, device,criterion=criterion)
print(f'Loss: {loss}, Accuracy: {acc*100}%')
print_model_size(global_model)
with open(csv_file_path, mode='a', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
# Write the data to the CSV file
new_data = [quantize_bits, loss, acc]
csv_writer.writerow(new_data)
# Measure inference latency
cpu_inference_latency = measure_inference_latency(model=global_model, device=cpu_device, input_size=(1,3,32,32), num_samples=100)
gpu_inference_latency = measure_inference_latency(model=global_model, device=cuda_device, input_size=(1,3,32,32), num_samples=100)
print("CPU Inference Latency: {:.2f} ms / sample".format(cpu_inference_latency * 1000))
print("CUDA Inference Latency: {:.2f} ms / sample".format(gpu_inference_latency * 1000))
# In[ ]:
cuda_device = torch.device("cuda:0")
cpu_device = torch.device("cpu:0")
# Test the model
loss, acc = evaluate_model(global_model, test_loader, device,criterion=criterion)
print(f'Loss: {loss}, Accuracy: {acc*100}%')
print_model_size(global_model)
# Measure inference latency
cpu_inference_latency = measure_inference_latency(model=global_model, device=cpu_device, input_size=(1,3,32,32), num_samples=100)
gpu_inference_latency = measure_inference_latency(model=global_model, device=cuda_device, input_size=(1,3,32,32), num_samples=100)
print("CPU Inference Latency: {:.2f} ms / sample".format(cpu_inference_latency * 1000))
print("CUDA Inference Latency: {:.2f} ms / sample".format(gpu_inference_latency * 1000))
# In[ ]:
#save_model(global_model, "saved_models", f'resnet_fedlern_global_{time_stamp()}.pth')
# In[ ]:
save_quantized_model = to_quantized_model_decorator(global_optimizer, goptimizer_quant)(save_model)
save_quantized_model(global_model, "saved_models", f'resnet_fedlern_{quantize_bits}bits.pth')