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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
| import os import random
import torch import torch.nn as nn import torch.optim as optim import torchvision from torchvision import datasets, transforms
import time import copy import numpy as np
from resnet import resnet18
def set_random_seeds(random_seed=0):
torch.manual_seed(random_seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False np.random.seed(random_seed) random.seed(random_seed)
def prepare_dataloader(num_workers=8, train_batch_size=128, eval_batch_size=256):
train_transform = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ])
test_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ])
train_set = torchvision.datasets.CIFAR10(root="data", train=True, download=True, transform=train_transform) test_set = torchvision.datasets.CIFAR10(root="data", train=False, download=True, transform=test_transform)
train_sampler = torch.utils.data.RandomSampler(train_set) test_sampler = torch.utils.data.SequentialSampler(test_set)
train_loader = torch.utils.data.DataLoader( dataset=train_set, batch_size=train_batch_size, sampler=train_sampler, num_workers=num_workers)
test_loader = torch.utils.data.DataLoader( dataset=test_set, batch_size=eval_batch_size, sampler=test_sampler, num_workers=num_workers)
return train_loader, test_loader
def evaluate_model(model, test_loader, device, criterion=None):
model.eval() model.to(device)
running_loss = 0 running_corrects = 0
for inputs, labels in test_loader:
inputs = inputs.to(device) labels = labels.to(device)
outputs = model(inputs) _, preds = torch.max(outputs, 1)
if criterion is not None: loss = criterion(outputs, labels).item() else: loss = 0
running_loss += loss * inputs.size(0) running_corrects += torch.sum(preds == labels.data)
eval_loss = running_loss / len(test_loader.dataset) eval_accuracy = running_corrects / len(test_loader.dataset)
return eval_loss, eval_accuracy
def train_model(model, train_loader, test_loader, device, learning_rate=1e-1, num_epochs=200):
criterion = nn.CrossEntropyLoss()
model.to(device)
optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9, weight_decay=1e-4) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[100, 150], gamma=0.1, last_epoch=-1)
model.eval() eval_loss, eval_accuracy = evaluate_model(model=model, test_loader=test_loader, device=device, criterion=criterion) print("Epoch: {:02d} Eval Loss: {:.3f} Eval Acc: {:.3f}".format(-1, eval_loss, eval_accuracy))
for epoch in range(num_epochs):
model.train()
running_loss = 0 running_corrects = 0
for inputs, labels in train_loader:
inputs = inputs.to(device) labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs) _, preds = torch.max(outputs, 1) loss = criterion(outputs, labels) loss.backward() optimizer.step()
running_loss += loss.item() * inputs.size(0) running_corrects += torch.sum(preds == labels.data)
train_loss = running_loss / len(train_loader.dataset) train_accuracy = running_corrects / len(train_loader.dataset)
model.eval() eval_loss, eval_accuracy = evaluate_model(model=model, test_loader=test_loader, device=device, criterion=criterion)
scheduler.step()
print("Epoch: {:03d} Train Loss: {:.3f} Train Acc: {:.3f} Eval Loss: {:.3f} Eval Acc: {:.3f}".format(epoch, train_loss, train_accuracy, eval_loss, eval_accuracy))
return model
def calibrate_model(model, loader, device=torch.device("cpu:0")):
model.to(device) model.eval()
for inputs, labels in loader: inputs = inputs.to(device) labels = labels.to(device) _ = model(inputs)
def measure_inference_latency(model, device, input_size=(1, 3, 32, 32), num_samples=100, num_warmups=10):
model.to(device) model.eval()
x = torch.rand(size=input_size).to(device)
with torch.no_grad(): for _ in range(num_warmups): _ = model(x) torch.cuda.synchronize()
with torch.no_grad(): start_time = time.time() for _ in range(num_samples): _ = model(x) torch.cuda.synchronize() end_time = time.time() elapsed_time = end_time - start_time elapsed_time_ave = elapsed_time / num_samples
return elapsed_time_ave
def save_model(model, model_dir, model_filename):
if not os.path.exists(model_dir): os.makedirs(model_dir) model_filepath = os.path.join(model_dir, model_filename) torch.save(model.state_dict(), model_filepath)
def load_model(model, model_filepath, device):
model.load_state_dict(torch.load(model_filepath, map_location=device))
return model
def save_torchscript_model(model, model_dir, model_filename):
if not os.path.exists(model_dir): os.makedirs(model_dir) model_filepath = os.path.join(model_dir, model_filename) torch.jit.save(torch.jit.script(model), model_filepath)
def load_torchscript_model(model_filepath, device):
model = torch.jit.load(model_filepath, map_location=device)
return model
def create_model(num_classes=10):
model = resnet18(num_classes=num_classes, pretrained=False)
return model
class QuantizedResNet18(nn.Module): def __init__(self, model_fp32): super(QuantizedResNet18, self).__init__() self.quant = torch.quantization.QuantStub() self.dequant = torch.quantization.DeQuantStub() self.model_fp32 = model_fp32
def forward(self, x): x = self.quant(x) x = self.model_fp32(x) x = self.dequant(x) return x
def model_equivalence(model_1, model_2, device, rtol=1e-05, atol=1e-08, num_tests=100, input_size=(1,3,32,32)):
model_1.to(device) model_2.to(device)
for _ in range(num_tests): x = torch.rand(size=input_size).to(device) y1 = model_1(x).detach().cpu().numpy() y2 = model_2(x).detach().cpu().numpy() if np.allclose(a=y1, b=y2, rtol=rtol, atol=atol, equal_nan=False) == False: print("Model equivalence test sample failed: ") print(y1) print(y2) return False
return True
def main():
random_seed = 0 num_classes = 10 cuda_device = torch.device("cuda:0") cpu_device = torch.device("cpu:0")
model_dir = "saved_models" model_filename = "resnet18_cifar10.pt" quantized_model_filename = "resnet18_quantized_cifar10.pt" model_filepath = os.path.join(model_dir, model_filename) quantized_model_filepath = os.path.join(model_dir, quantized_model_filename)
set_random_seeds(random_seed=random_seed)
model = create_model(num_classes=num_classes)
train_loader, test_loader = prepare_dataloader(num_workers=8, train_batch_size=128, eval_batch_size=256)
print("Training Model...") model = train_model(model=model, train_loader=train_loader, test_loader=test_loader, device=cuda_device, learning_rate=1e-1, num_epochs=200) save_model(model=model, model_dir=model_dir, model_filename=model_filename) model = load_model(model=model, model_filepath=model_filepath, device=cuda_device) model.to(cpu_device) fused_model = copy.deepcopy(model)
model.train() fused_model.train()
fused_model = torch.quantization.fuse_modules(fused_model, [["conv1", "bn1", "relu"]], inplace=True) for module_name, module in fused_model.named_children(): if "layer" in module_name: for basic_block_name, basic_block in module.named_children(): torch.quantization.fuse_modules(basic_block, [["conv1", "bn1", "relu1"], ["conv2", "bn2"]], inplace=True) for sub_block_name, sub_block in basic_block.named_children(): if sub_block_name == "downsample": torch.quantization.fuse_modules(sub_block, [["0", "1"]], inplace=True)
print(model) print(fused_model)
model.eval() fused_model.eval() assert model_equivalence(model_1=model, model_2=fused_model, device=cpu_device, rtol=1e-03, atol=1e-06, num_tests=100, input_size=(1,3,32,32)), "Fused model is not equivalent to the original model!"
quantized_model = QuantizedResNet18(model_fp32=fused_model) quantization_config = torch.quantization.get_default_qconfig("fbgemm")
quantized_model.qconfig = quantization_config
print(quantized_model.qconfig)
torch.quantization.prepare_qat(quantized_model, inplace=True)
print("Training QAT Model...") quantized_model.train() train_model(model=quantized_model, train_loader=train_loader, test_loader=test_loader, device=cuda_device, learning_rate=1e-3, num_epochs=10) quantized_model.to(cpu_device)
quantized_model = torch.quantization.convert(quantized_model, inplace=True)
quantized_model.eval()
print(quantized_model)
save_torchscript_model(model=quantized_model, model_dir=model_dir, model_filename=quantized_model_filename)
quantized_jit_model = load_torchscript_model(model_filepath=quantized_model_filepath, device=cpu_device)
_, fp32_eval_accuracy = evaluate_model(model=model, test_loader=test_loader, device=cpu_device, criterion=None) _, int8_eval_accuracy = evaluate_model(model=quantized_jit_model, test_loader=test_loader, device=cpu_device, criterion=None)
print("FP32 evaluation accuracy: {:.3f}".format(fp32_eval_accuracy)) print("INT8 evaluation accuracy: {:.3f}".format(int8_eval_accuracy))
fp32_cpu_inference_latency = measure_inference_latency(model=model, device=cpu_device, input_size=(1,3,32,32), num_samples=100) int8_cpu_inference_latency = measure_inference_latency(model=quantized_model, device=cpu_device, input_size=(1,3,32,32), num_samples=100) int8_jit_cpu_inference_latency = measure_inference_latency(model=quantized_jit_model, device=cpu_device, input_size=(1,3,32,32), num_samples=100) fp32_gpu_inference_latency = measure_inference_latency(model=model, device=cuda_device, input_size=(1,3,32,32), num_samples=100)
print("FP32 CPU Inference Latency: {:.2f} ms / sample".format(fp32_cpu_inference_latency * 1000)) print("FP32 CUDA Inference Latency: {:.2f} ms / sample".format(fp32_gpu_inference_latency * 1000)) print("INT8 CPU Inference Latency: {:.2f} ms / sample".format(int8_cpu_inference_latency * 1000)) print("INT8 JIT CPU Inference Latency: {:.2f} ms / sample".format(int8_jit_cpu_inference_latency * 1000))
if __name__ == "__main__":
main()
|