-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflax_qconv_test.py
421 lines (388 loc) · 10.4 KB
/
flax_qconv_test.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# IMSL Lab - University of Notre Dame
# Author: Clemens JS Schaefer
# Unit Test for QuantConv
# 1. Unite test to compare nn.Conv layer with QuantConv. Simply training a one
# layer convolutional network initialized with all zeros and comparing the
# weight after update
from absl.testing import absltest
from absl.testing import parameterized
import functools
from flax import linen as nn
from flax import optim
from flax.core import freeze
from jax import random
from jax.nn import initializers
import jax
from jax import lax
import jax.numpy as jnp
from flax import jax_utils
from flax_qconv import QuantConv
class CQG(nn.Module):
"""A simple fully connected model with QuantConv and config = None for
no quantization"""
@nn.compact
def __call__(
self, x, features, kernel_size, strides, padding, config, rng
):
rng1, rng2 = jax.random.split(rng, 2)
x = QuantConv(
features=features[0],
kernel_size=kernel_size,
strides=strides,
padding=padding,
kernel_init=initializers.lecun_normal(),
config=config,
use_bias=False,
)(x, rng1)
x = QuantConv(
features=features[1],
kernel_size=kernel_size,
strides=strides,
padding=padding,
kernel_init=initializers.lecun_normal(),
config=config,
use_bias=False,
)(x, rng2)
return x
class Clinen(nn.Module):
"""Same model as above but with nn.Conv"""
@nn.compact
def __call__(self, x, features, kernel_size, strides, padding):
x = nn.Conv(
features=features[0],
kernel_size=kernel_size,
strides=strides,
padding=padding,
kernel_init=initializers.lecun_normal(),
use_bias=False,
)(x)
x = nn.Conv(
features=features[1],
kernel_size=kernel_size,
strides=strides,
padding=padding,
kernel_init=initializers.lecun_normal(),
use_bias=False,
)(x)
return x
def create_optimizer(params, learning_rate):
optimizer_def = optim.GradientDescent(learning_rate=learning_rate)
optimizer = optimizer_def.create(params)
return optimizer
def cross_entropy_loss(logits, labels):
return -jnp.mean(jnp.sum(labels * logits, axis=-1))
# Train step for QuantConv layer
def train_step_conv_quant_grad(
optimizer,
batch,
features,
kernel_size,
strides,
padding,
config,
rng,
):
"""Train for a single step."""
def loss_fn(params):
logits = CQG().apply(
{"params": params},
batch["image"],
features,
kernel_size,
strides,
padding,
config,
rng,
)
loss = cross_entropy_loss(logits, batch["label"])
return loss, logits
grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
(_, logits), grad = grad_fn(optimizer.target)
grad = lax.pmean(grad, axis_name="batch")
optimizer = optimizer.apply_gradient(grad)
return logits, optimizer
# Train step for nn.Conv layer
def train_step_conv(optimizer, batch, features, kernel_size, strides, padding):
"""Train for a single step."""
def loss_fn(params):
logits = Clinen().apply(
{"params": params},
batch["image"],
features,
kernel_size,
strides,
padding,
)
loss = cross_entropy_loss(logits, batch["label"])
return loss, logits
grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
(_, logits), grad = grad_fn(optimizer.target)
grad = lax.pmean(grad, axis_name="batch")
optimizer = optimizer.apply_gradient(grad)
return logits, optimizer
# Test data for QuantConv
def conv_test_data():
return (
dict(
testcase_name="base_case",
examples=128,
dimX=28,
dimY=28,
dimX_out=28,
dimY_out=28,
inp_channels=1,
features=(10, 20),
kernel_size=(2, 2),
strides=(1, 1),
padding="SAME",
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_1x1_input",
examples=128,
dimX=1,
dimY=1,
dimX_out=1,
dimY_out=1,
inp_channels=1,
features=(10, 20),
kernel_size=(2, 2),
strides=(1, 1),
padding="SAME",
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_13x17_input",
examples=128,
dimX=13,
dimY=17,
dimX_out=13,
dimY_out=17,
inp_channels=1,
features=(10, 20),
kernel_size=(2, 2),
strides=(1, 1),
padding="SAME",
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_1x1_kernel",
examples=128,
dimX=28,
dimY=28,
dimX_out=28,
dimY_out=28,
inp_channels=1,
features=(10, 20),
kernel_size=(1, 1),
strides=(1, 1),
padding="SAME",
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_3x7_kernel",
examples=128,
dimX=28,
dimY=28,
dimX_out=28,
dimY_out=28,
inp_channels=1,
features=(10, 20),
kernel_size=(3, 7),
strides=(1, 1),
padding="SAME",
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_valid_padding",
examples=128,
dimX=28,
dimY=28,
dimX_out=26,
dimY_out=26,
inp_channels=1,
features=(10, 20),
kernel_size=(2, 2),
strides=(1, 1),
padding="VALID",
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_3715_padding",
examples=128,
dimX=28,
dimY=28,
dimX_out=46,
dimY_out=38,
inp_channels=1,
features=(10, 20),
kernel_size=(2, 2),
strides=(1, 1),
padding=((3, 7), (1, 5)),
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_2_2_stride",
examples=128,
dimX=28,
dimY=28,
dimX_out=7,
dimY_out=7,
inp_channels=1,
features=(10, 20),
kernel_size=(2, 2),
strides=(2, 2),
padding="SAME",
config={},
numerical_tolerance=0.0,
),
dict(
testcase_name="base_case_3_7_stride",
examples=128,
dimX=28,
dimY=28,
dimX_out=4,
dimY_out=1,
inp_channels=1,
features=(10, 20),
kernel_size=(2, 2),
strides=(3, 7),
padding="SAME",
config={},
numerical_tolerance=0.0,
),
)
class QuantConvTest(parameterized.TestCase):
@parameterized.named_parameters(*conv_test_data())
def test_QuantConv_vs_nnConv(
self,
examples,
dimX,
dimY,
dimX_out,
dimY_out,
inp_channels,
features,
kernel_size,
strides,
padding,
config,
numerical_tolerance,
):
"""
Unit test to check whether QuantConv does exactly the same as
nn.Conv when gradient quantization is turned off.
"""
# create initial data
key = random.PRNGKey(8627169)
key, subkey1, subkey2 = random.split(key, 3)
data_x = (
random.uniform(
subkey1,
(jax.device_count(), examples, dimX, dimY, inp_channels),
minval=-1,
maxval=1,
dtype=jnp.float32,
) * 100
)
data_y = (
random.uniform(
subkey2,
(
jax.device_count(),
examples,
dimX_out,
dimY_out,
features[-1],
),
minval=-1,
maxval=1,
dtype=jnp.float32,
) * 100
)
# setup QuantConv
key, subkey1, subkey2, subkey3 = random.split(key, 4)
params_quant_grad = CQG().init(
subkey1,
jnp.take(data_x, 0, axis=0),
features,
kernel_size,
strides,
padding,
config,
subkey2,
)["params"]
optimizer_qgrad = create_optimizer(params_quant_grad, 1)
p_train_step_conv_quant_grad = jax.pmap(
functools.partial(
train_step_conv_quant_grad,
features=features,
kernel_size=kernel_size,
strides=strides,
padding=padding,
config=config,
rng=subkey3,
),
axis_name="batch",
)
p_train_step_conv = jax.pmap(
functools.partial(
train_step_conv,
features=features,
kernel_size=kernel_size,
strides=strides,
padding=padding,
),
axis_name="batch",
)
# setup nn.Conv
params = freeze(
{
"Conv_0": params_quant_grad["QuantConv_0"],
"Conv_1": params_quant_grad["QuantConv_1"],
}
)
optimizer = create_optimizer(params, 1)
# check that weights are initially equal
assert (
optimizer_qgrad.target["QuantConv_0"] == optimizer.target["Conv_0"]
) and (
optimizer_qgrad.target["QuantConv_1"] == optimizer.target["Conv_1"]
), "Initial parameters not equal"
optimizer_qgrad = jax_utils.replicate(optimizer_qgrad)
optimizer = jax_utils.replicate(optimizer)
# one backward pass
logits_quant, optimizer_qgrad = p_train_step_conv_quant_grad(
optimizer_qgrad,
{"image": data_x, "label": data_y},
)
logits, optimizer = p_train_step_conv(
optimizer,
{"image": data_x, "label": data_y},
)
# determine difference between nn.Conv and QuantConv
diff_conv0 = optimizer.target["Conv_0"]["kernel"] - (
optimizer_qgrad.target["QuantConv_0"]["kernel"])
diff_conv1 = optimizer.target["Conv_1"]["kernel"] - (
optimizer_qgrad.target["QuantConv_1"]["kernel"])
self.assertLessEqual(
(
(
jnp.mean(
abs(diff_conv0)
)
) / jnp.mean(abs(optimizer.target["Conv_0"]["kernel"])) + (
jnp.mean(abs(diff_conv1))
) / jnp.mean(abs(optimizer.target["Conv_1"]["kernel"]))
) / 2,
numerical_tolerance,
)
if __name__ == "__main__":
absltest.main()