-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy path30_external_hardware_aware_model.py
267 lines (228 loc) · 8.44 KB
/
30_external_hardware_aware_model.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
# type: ignore
# pylint: disable-all
# -*- coding: utf-8 -*-
# (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved.
#
# Licensed under the MIT license. See LICENSE file in the project root for details.
"""aihwkit example 30: Importing and using external hardware-aware trained models.
This example demonstrates how to import and perform inference using a model which has been trained
in a hardware-aware fashion using an external library (i.e., not the AIHWKIT).
The external model is in the form of a standard pytorch model with hardware-aware trained weights.
Input and output bounds, in addition to output scales are not defined.
"""
# pylint: disable=invalid-name
import os
import torch
import torch.nn.functional as F
import torchvision
from torchvision.datasets.utils import download_url
from aihwkit.nn.conversion import convert_to_analog
from aihwkit.simulator.presets import StandardHWATrainingPreset
from aihwkit.inference.calibration import (
calibrate_input_ranges,
InputRangeCalibrationType,
)
class LambdaLayer(torch.nn.Module):
def __init__(self, lambd):
super(LambdaLayer, self).__init__()
self.lambd = lambd
def forward(self, x):
return self.lambd(x)
class BasicBlock(torch.nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, option="A"):
super(BasicBlock, self).__init__()
self.conv1 = torch.nn.Conv2d(
in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False
)
self.bn1 = torch.nn.BatchNorm2d(planes)
self.conv2 = torch.nn.Conv2d(
planes, planes, kernel_size=3, stride=1, padding=1, bias=False
)
self.bn2 = torch.nn.BatchNorm2d(planes)
self.shortcut = torch.nn.Sequential()
if stride != 1 or in_planes != planes:
if option == "A":
"""
For CIFAR10 ResNet paper uses option A.
"""
self.shortcut = LambdaLayer(
lambda x: F.pad(
x[:, :, ::2, ::2],
(0, 0, 0, 0, planes // 4, planes // 4),
"constant",
0,
)
)
elif option == "B":
self.shortcut = torch.nn.Sequential(
torch.nn.Conv2d(
in_planes,
self.expansion * planes,
kernel_size=1,
stride=stride,
bias=False,
),
torch.nn.BatchNorm2d(self.expansion * planes),
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class Resnet9(torch.nn.Module):
"""
From https://github.com/matthias-wright/cifar10-resnet/
"""
def __init__(self, channels):
super(Resnet9, self).__init__()
self.channels = channels
# resnet9 [56,112,224,224]
# resnet9s [28,28,28,56]
self.bn1 = torch.nn.BatchNorm2d(num_features=channels[0], momentum=0.9)
self.bn2 = torch.nn.BatchNorm2d(num_features=channels[1], momentum=0.9)
self.bn3 = torch.nn.BatchNorm2d(num_features=channels[2], momentum=0.9)
self.bn4 = torch.nn.BatchNorm2d(num_features=channels[3], momentum=0.9)
self.conv = torch.nn.Sequential(
# prep
torch.nn.Conv2d(
in_channels=3,
out_channels=channels[0],
kernel_size=3,
stride=1,
padding=1,
bias=False,
),
self.bn1,
torch.nn.ReLU(inplace=True),
# Layer 1
torch.nn.Conv2d(
in_channels=channels[0],
out_channels=channels[1],
kernel_size=3,
stride=1,
padding=1,
bias=False,
),
self.bn2,
torch.nn.ReLU(inplace=True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
BasicBlock(in_planes=channels[1], planes=channels[1], stride=1),
# Layer 2
torch.nn.Conv2d(
in_channels=channels[1],
out_channels=channels[2],
kernel_size=3,
stride=1,
padding=1,
bias=False,
),
self.bn3,
torch.nn.ReLU(inplace=True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# Layer 3
torch.nn.Conv2d(
in_channels=channels[2],
out_channels=channels[3],
kernel_size=3,
stride=1,
padding=1,
bias=False,
),
self.bn4,
torch.nn.ReLU(inplace=True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
BasicBlock(in_planes=channels[3], planes=channels[3], stride=1),
torch.nn.MaxPool2d(kernel_size=4, stride=4),
)
self.fc = torch.nn.Linear(in_features=channels[3], out_features=10, bias=True)
def forward(self, x):
out = self.conv(x)
out = out.view(-1, self.channels[3])
out = self.fc(out)
return out
def resnet9s():
return Resnet9(channels=[28, 28, 28, 56])
def get_test_loader(batch_size=128):
transform_test = torchvision.transforms.Compose(
[
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)
),
]
)
testset = torchvision.datasets.CIFAR10(
root="data/cifar10", train=False, download=True, transform=transform_test
)
test_loader = torch.utils.data.DataLoader(
testset,
batch_size=batch_size,
shuffle=False,
num_workers=0,
pin_memory=True,
)
return test_loader
class Sampler:
"""Example of a sampler used for calibration."""
def __init__(self, loader, device):
self.device = device
self.loader = iter(loader)
self.idx = 0
def __iter__(self):
return self
def __next__(self):
x, _ = next(self.loader)
self.idx += 1
if self.idx > 100:
raise StopIteration
return ([x.to(self.device)], {})
def evaluate_model(model, test_loader, device):
model.eval()
with torch.no_grad():
correct = 0
total = 0
for inputs, targets in test_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
return 100.0 * correct / total
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = resnet9s().to(device)
download_url(
"https://aihwkit-tutorial.s3.us-east.cloud-object-storage.appdomain.cloud/resnet9s.th",
os.getcwd(),
)
state_dict = torch.load("resnet9s.th", device)
# The state dict of the model with hardware-aware trained weights is stored in the
# model_state_dict key of the external checkpoint.
model.load_state_dict(state_dict["model_state_dict"], strict=True)
model = convert_to_analog(model, StandardHWATrainingPreset())
model.eval()
test_loader = get_test_loader()
t_inferences = [0.0, 3600.0, 86400.0] # Times to perform infernece.
n_reps = 5 # Number of inference repetitions.
# Calibrate input ranges
print("Performing input range calibration")
calibrate_input_ranges(
model=model,
calibration_type=InputRangeCalibrationType.CACHE_QUANTILE,
dataloader=Sampler(test_loader, device),
)
# Determine the inference accuracy with the specified rpu configuration.
print("Evaluating imported model.")
inference_accuracy_values = torch.zeros((len(t_inferences), n_reps))
for t_id, t in enumerate(t_inferences):
for i in range(n_reps):
model.drift_analog_weights(t)
inference_accuracy_values[t_id, i] = evaluate_model(
model, test_loader, device
)
print(
f"Test set accuracy (%) at t={t}s: mean: {inference_accuracy_values[t_id].mean()}, \
std: {inference_accuracy_values[t_id].std()}"
)