test_runner_lightning_quantization.py 17.3 KB
Newer Older
Kai Zhang's avatar
Kai Zhang committed
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python3

# pyre-unsafe
import os
import unittest

import mock
import torch
from d2go.runner.callbacks.quantization import (
10
11
    get_default_qat_qconfig,
    ModelTransform,
Kai Zhang's avatar
Kai Zhang committed
12
13
    PostTrainingQuantization,
    QuantizationAwareTraining,
Kai Zhang's avatar
Kai Zhang committed
14
15
    rgetattr,
    rhasattr,
16
    rsetattr,
Kai Zhang's avatar
Kai Zhang committed
17
18
)
from d2go.utils.misc import mode
Yanghan Wang's avatar
Yanghan Wang committed
19
20
from d2go.utils.testing.helper import tempdir
from d2go.utils.testing.lightning_test_module import TestModule
21
from pytorch_lightning import seed_everything, Trainer
Kai Zhang's avatar
Kai Zhang committed
22
from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint
23
from torch.ao.quantization import (  # @manual; @manual
Kai Zhang's avatar
Kai Zhang committed
24
25
26
    default_dynamic_qconfig,
    get_default_qconfig,
)
27
from torch.ao.quantization.quantize_fx import convert_fx, prepare_fx, prepare_qat_fx
Kai Zhang's avatar
Kai Zhang committed
28
29


Kai Zhang's avatar
Kai Zhang committed
30
class TestUtilities(unittest.TestCase):
31
    """Test some basic utilities we rely on."""
Kai Zhang's avatar
Kai Zhang committed
32
33

    def test_get_set_has(self):
34
        """Trivial test for generic behavior. Only support pre-existing deeply nested values."""
Kai Zhang's avatar
Kai Zhang committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

        class TestObject(object):
            def __init__(self):
                self.object = None
                self.set_to_five = 5

        obj = TestObject()
        obj.object = TestObject()
        obj.object.set_to_five = 10

        rsetattr(obj, "object.set_to_five", 1)
        self.assertTrue(rhasattr(obj, "object.set_to_five"))
        self.assertEqual(1, rgetattr(obj, "object.set_to_five"))
        self.assertEqual(5, rgetattr(obj, "set_to_five"))

        with self.assertRaises(AttributeError):
            rsetattr(obj, "object.does_not_exist.five", 5)


Kai Zhang's avatar
Kai Zhang committed
54
class TestModelTransform(unittest.TestCase):
55
    """Tests ModelTransforms."""
Kai Zhang's avatar
Kai Zhang committed
56
57

    def test_invalid_construction_type_error(self):
58
        """Validate construction of ModelTransforms. Always have fn, msg, and one of [step, interval]."""
Kai Zhang's avatar
Kai Zhang committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        with self.assertRaises(TypeError):
            _ = ModelTransform()
        with self.assertRaises(TypeError):
            _ = ModelTransform(fn=lambda x: x)
        with self.assertRaises(TypeError):
            _ = ModelTransform(message="No function defined")
        with self.assertRaises(TypeError):
            _ = ModelTransform(
                fn=lambda x: x,
                message="Specified both step and interval",
                step=1,
                interval=1,
            )

    def test_positivity_value_error(self):
74
        """Validates ModelTransforms are constructed with only valid arguments."""
Kai Zhang's avatar
Kai Zhang committed
75
76
77
78
79
80
81
82
83
84
85
86

        def identity(x):
            return x

        with self.assertRaises(ValueError):
            _ = ModelTransform(fn=identity, message="Negative step", step=-1)
        with self.assertRaises(ValueError):
            _ = ModelTransform(fn=identity, message="Zero interval", interval=0)
        with self.assertRaises(ValueError):
            _ = ModelTransform(fn=identity, message="Negative interval", interval=-1)


87
88
89
@unittest.skip(
    "FX Graph Mode Quantization API has been updated, re-enable the test after PyTorch 1.13 stable release"
)
Kai Zhang's avatar
Kai Zhang committed
90
91
class TestQuantizationAwareTraining(unittest.TestCase):
    def test_qat_misconfiguration(self):
92
        """Tests failure when misconfiguring the QAT Callback."""
Kai Zhang's avatar
Kai Zhang committed
93
94
95
96
97
98
99
100
101
102
103
104
        invalid_params = [
            {"start_step": -1},
            {"enable_observer": (42, 42)},
            {"enable_observer": (42, 21)},
            {"enable_observer": (-1, None)},
            {"freeze_bn_step": -1},
        ]
        for invalid_param in invalid_params:
            with self.assertRaises(ValueError):
                _ = QuantizationAwareTraining(**invalid_param)

    def test_qat_transforms(self):
105
        """Tests the appropropriate ModelTransforms are defined with QAT."""
Kai Zhang's avatar
Kai Zhang committed
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
        qat = QuantizationAwareTraining(
            start_step=300, enable_observer=(350, 500), freeze_bn_step=550
        )

        trainer = Trainer()
        module = TestModule()

        qat.setup(trainer, module, stage="train")

        self.assertGreater(len(qat.transforms), 0)

        def assertContainsTransformsAtStep(step):
            """
            Asserts at least one transform exists at the specified step and
            that it is removed after the step begins.
            """
            self.assertGreater(
                len(
                    [
                        transform
                        for transform in qat.transforms
                        if transform.step == step
                    ]
                ),
                0,
                f"step={step}",
            )
133
            trainer.fit_loop.global_step = step
Kai Zhang's avatar
Kai Zhang committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
            qat.on_train_batch_start(
                trainer, module, batch=None, batch_idx=0, dataloader_idx=0
            )

            self.assertEqual(
                len(
                    [
                        transform
                        for transform in qat.transforms
                        if transform.step == step
                    ]
                ),
                0,
                f"step={step}",
            )

        assertContainsTransformsAtStep(step=300)
        assertContainsTransformsAtStep(step=350)
        assertContainsTransformsAtStep(step=500)
        assertContainsTransformsAtStep(step=550)

    @tempdir
    def test_qat_interval_transform(self, root_dir):
157
        """Tests an interval transform is applied multiple times."""
Kai Zhang's avatar
Kai Zhang committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        seed_everything(100)

        def linear_fn_counter(mod):
            if isinstance(mod, torch.nn.Linear):
                linear_fn_counter.count += 1

        linear_fn_counter.count = 0

        model = TestModule()
        num_epochs = 2
        qat = QuantizationAwareTraining()
        qat.transforms.append(
            ModelTransform(fn=linear_fn_counter, message="Counter", interval=10)
        )
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
174
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
175
176
177
178
179
180
181
182
183
184
185
            callbacks=[qat],
            max_epochs=num_epochs,
            logger=False,
        )
        trainer.fit(model)

        # Model has 2 linear layers.
        self.assertEqual(linear_fn_counter.count, 2 * (trainer.global_step // 10 + 1))

    @tempdir
    def test_module_quantized_during_train(self, root_dir):
186
        """Validate quantized aware training works as expected."""
Kai Zhang's avatar
Kai Zhang committed
187
188
189
190
191
192
193
194
        seed_everything(100)

        model = TestModule()
        test_in = torch.randn(1, 32)
        before_train = model.eval()(test_in)
        num_epochs = 2
        qat = QuantizationAwareTraining()
        trainer = Trainer(
195
            accelerator="cpu",
196
            devices=1,
Kai Zhang's avatar
Kai Zhang committed
197
            default_root_dir=os.path.join(root_dir, "quantized"),
198
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
199
200
201
202
203
204
205
206
207
208
209
            callbacks=[qat],
            max_epochs=num_epochs,
            logger=False,
        )
        trainer.fit(model)

        self.assertIsNotNone(qat.prepared)
        self.assertIsNotNone(qat.quantized)

        test_out = model.eval()(test_in)
        self.assertGreater(
210
            (test_out**2).sum(), 0.03, "With the given seend, L2^2 should be > 0.03."
Kai Zhang's avatar
Kai Zhang committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
        )

        base_out = qat.quantized.eval()(test_in)
        self.assertTrue(torch.allclose(base_out, test_out))
        # Weight changed during training.
        self.assertFalse(torch.allclose(before_train, test_out))

        # Validate .test() call works as expected and does not change model weights.
        trainer.test(model)

        self.assertTrue(torch.allclose(test_out, model.eval()(test_in)))

    @tempdir
    def test_quantization_without_train(self, root_dir):
225
        """Validate quantization occurs even without a call to .fit() first."""
Kai Zhang's avatar
Kai Zhang committed
226
227
228
229
230
231
232
        seed_everything(100)

        model = TestModule()
        num_epochs = 2
        qat = QuantizationAwareTraining()
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
233
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
234
235
236
237
238
239
240
241
242
243
            callbacks=[qat],
            max_epochs=num_epochs,
            logger=False,
        )

        trainer.test(model)

        self.assertIsNotNone(qat.prepared)
        self.assertIsNotNone(qat.quantized)

Kai Zhang's avatar
Kai Zhang committed
244
245
    @tempdir
    def test_attribute_preservation_qat(self, root_dir):
246
        """Validates we can preserve specified properties in module."""
Kai Zhang's avatar
Kai Zhang committed
247
248
249
250
251
252
253
254
255
256
257
258
259
        seed_everything(100)

        model = TestModule()
        model.layer._added_property = 10
        model._not_preserved = 15
        model._added_property = 20

        num_epochs = 2
        qat = QuantizationAwareTraining(
            preserved_attrs=["_added_property", "layer._added_property"]
        )
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
260
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
            callbacks=[qat],
            max_epochs=num_epochs,
            logger=False,
        )

        trainer.fit(model)

        self.assertIsNotNone(qat.prepared)
        self.assertIsNotNone(qat.quantized)

        # Assert properties are maintained.
        self.assertTrue(hasattr(qat.prepared, "_added_property"))
        self.assertTrue(hasattr(qat.prepared.layer, "_added_property"))

        with self.assertRaises(AttributeError):
            qat.prepared._not_preserved

Kai Zhang's avatar
Kai Zhang committed
278
279
    @tempdir
    def test_quantization_and_checkpointing(self, root_dir):
280
        """Validate written checkpoints can be loaded back as expected."""
Kai Zhang's avatar
Kai Zhang committed
281
282
283
284
285
286
287
288
289
        seed_everything(100)

        model = TestModule()
        num_epochs = 2
        qat = QuantizationAwareTraining()
        checkpoint_dir = os.path.join(root_dir, "checkpoints")
        checkpoint = ModelCheckpoint(dirpath=checkpoint_dir, save_last=True)
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
290
            callbacks=[qat, checkpoint],
Kai Zhang's avatar
Kai Zhang committed
291
292
293
294
295
296
297
298
299
300
301
302
            max_epochs=num_epochs,
            logger=False,
        )
        # Mimick failing mid-training by not running on_fit_end.
        with mock.patch.object(qat, "on_fit_end"):
            trainer.fit(model)

        ckpt = torch.load(os.path.join(checkpoint_dir, "last.ckpt"))
        model.load_state_dict(ckpt["state_dict"])

    @tempdir
    def test_custom_qat(self, root_dir):
303
        """Tests that we can customize QAT by skipping certain layers."""
Kai Zhang's avatar
Kai Zhang committed
304
305

        class _CustomQAT(QuantizationAwareTraining):
306
            """Only quantize TestModule.another_layer."""
Kai Zhang's avatar
Kai Zhang committed
307

Kai Zhang's avatar
Kai Zhang committed
308
            def prepare(self, model, configs, attrs):
309
310
311
312
313
                example_inputs = (torch.rand(1, 2),)
                model.another_layer = prepare_qat_fx(
                    model.another_layer, configs[""], example_inputs
                )

Kai Zhang's avatar
Kai Zhang committed
314
315
                return model

Kai Zhang's avatar
Kai Zhang committed
316
            def convert(self, model, submodules, attrs):
Kai Zhang's avatar
Kai Zhang committed
317
318
319
320
321
322
323
324
325
326
327
                model.another_layer = convert_fx(model.another_layer)
                return model

        seed_everything(100)
        model = TestModule()
        test_in = torch.randn(1, 32)
        before_train = model.eval()(test_in)
        num_epochs = 2
        qat = _CustomQAT()
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
328
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
329
330
331
332
333
334
335
336
337
338
339
            callbacks=[qat],
            max_epochs=num_epochs,
            logger=False,
        )
        trainer.fit(model)

        self.assertIsNotNone(qat.prepared)
        self.assertIsNotNone(qat.quantized)

        test_out = model.eval()(test_in)
        self.assertGreater(
340
            (test_out**2).sum(), 0.03, "With the given seend, L2^2 should be > 0.03."
Kai Zhang's avatar
Kai Zhang committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
        )

        base_out = qat.quantized.eval()(test_in)
        self.assertTrue(torch.allclose(base_out, test_out))
        # Weight changed during training.
        self.assertFalse(torch.allclose(before_train, test_out))

        # Validate .test() call works as expected and does not change model weights.
        trainer.test(model)

        self.assertTrue(torch.allclose(test_out, model.eval()(test_in)))

    @tempdir
    def test_submodule_qat(self, root_dir):
355
        """Tests that we can customize QAT through exposed API."""
Kai Zhang's avatar
Kai Zhang committed
356
357
358
359
360
361
362
363
364
365
366
        seed_everything(100)

        model = TestModule()
        test_in = torch.randn(1, 32)
        before_train = model.eval()(test_in)
        num_epochs = 2
        qat = QuantizationAwareTraining(
            qconfig_dicts={"another_layer": {"": get_default_qat_qconfig()}}
        )
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
367
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
368
369
370
371
372
373
374
375
376
377
378
            callbacks=[qat],
            max_epochs=num_epochs,
            logger=False,
        )
        trainer.fit(model)

        self.assertIsNotNone(qat.prepared)
        self.assertIsNotNone(qat.quantized)

        test_out = model.eval()(test_in)
        self.assertGreater(
379
            (test_out**2).sum(), 0.03, "With the given seend, L2^2 should be > 0.03."
Kai Zhang's avatar
Kai Zhang committed
380
381
382
383
384
385
386
387
388
389
390
391
392
        )

        base_out = qat.quantized.eval()(test_in)
        self.assertTrue(torch.allclose(base_out, test_out))
        # Weight changed during training.
        self.assertFalse(torch.allclose(before_train, test_out))

        # Validate .test() call works as expected and does not change model weights.
        trainer.test(model)

        self.assertTrue(torch.allclose(test_out, model.eval()(test_in)))


393
394
395
@unittest.skip(
    "FX Graph Mode Quantization API has been updated, re-enable the test after PyTorch 1.13 stable release"
)
Kai Zhang's avatar
Kai Zhang committed
396
397
398
class TestPostTrainingQuantization(unittest.TestCase):
    @tempdir
    def test_post_training_static_quantization(self, root_dir):
399
        """Validate post-training static quantization."""
Kai Zhang's avatar
Kai Zhang committed
400
401
402
403
404
405
406
407
408
        seed_everything(100)

        model = TestModule()
        num_epochs = 4
        static_quantization = PostTrainingQuantization(
            qconfig_dicts={"": {"": get_default_qconfig()}}
        )
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
409
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
            callbacks=[static_quantization],
            max_epochs=num_epochs,
            logger=False,
        )
        # This will both train the model + quantize it.
        trainer.fit(model)

        self.assertIsNotNone(static_quantization.quantized)
        # Default qconfig requires calibration.
        self.assertTrue(static_quantization.should_calibrate)

        test_in = torch.randn(12, 32)
        with mode(model, training=False) as m:
            base_out = m(test_in)
        with mode(static_quantization.quantized, training=False) as q:
            test_out = q(test_in)

        # While quantized/original won't be exact, they should be close.
        self.assertLess(
            ((((test_out - base_out) ** 2).sum(axis=1)) ** (1 / 2)).mean(),
            0.015,
            "RMSE should be less than 0.015 between quantized and original.",
        )

    @tempdir
    def test_post_training_dynamic_quantization(self, root_dir):
436
        """Validates post-training dynamic quantization."""
Kai Zhang's avatar
Kai Zhang committed
437
438
439
440
441
442
443
444
445
        seed_everything(100)

        model = TestModule()
        num_epochs = 2
        dynamic_quant = PostTrainingQuantization(
            qconfig_dicts={"": {"": default_dynamic_qconfig}}
        )
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
446
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
            callbacks=[dynamic_quant],
            max_epochs=num_epochs,
            logger=False,
        )
        # This will both train the model + quantize it.
        trainer.fit(model)

        self.assertIsNotNone(dynamic_quant.quantized)
        # Default qconfig requires calibration.
        self.assertFalse(dynamic_quant.should_calibrate)

        test_in = torch.randn(12, 32)
        with mode(model, training=False) as m:
            base_out = m(test_in)
        with mode(dynamic_quant.quantized, training=False) as q:
            test_out = q(test_in)

        # While quantized/original won't be exact, they should be close.
        self.assertLess(
            ((((test_out - base_out) ** 2).sum(axis=1)) ** (1 / 2)).mean(),
            0.015,
            "RMSE should be less than 0.015 between quantized and original.",
        )

    @tempdir
    def test_custom_post_training_static_quant(self, root_dir):
473
        """Tests that we can customize Post-Training static by skipping certain layers."""
Kai Zhang's avatar
Kai Zhang committed
474
475

        class _CustomStaticQuant(PostTrainingQuantization):
476
            """Only quantize TestModule.another_layer."""
Kai Zhang's avatar
Kai Zhang committed
477

Kai Zhang's avatar
Kai Zhang committed
478
            def prepare(self, model, configs, attrs):
479
480
481
482
483
                example_inputs = (torch.randn(1, 2),)
                model.another_layer = prepare_fx(
                    model.another_layer, configs[""], example_inputs
                )

Kai Zhang's avatar
Kai Zhang committed
484
485
                return model

Kai Zhang's avatar
Kai Zhang committed
486
            def convert(self, model, submodules, attrs):
Kai Zhang's avatar
Kai Zhang committed
487
488
489
490
491
492
493
494
495
496
                model.another_layer = convert_fx(model.another_layer)
                return model

        seed_everything(100)

        model = TestModule()
        num_epochs = 2
        static_quantization = _CustomStaticQuant()
        trainer = Trainer(
            default_root_dir=os.path.join(root_dir, "quantized"),
497
            enable_checkpointing=False,
Kai Zhang's avatar
Kai Zhang committed
498
499
500
            callbacks=[static_quantization],
            max_epochs=num_epochs,
            logger=False,
501
            num_sanity_val_steps=0,
Kai Zhang's avatar
Kai Zhang committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
        )
        trainer.fit(model)

        self.assertIsNotNone(static_quantization.quantized)

        test_in = torch.randn(12, 32)
        with mode(model, training=False) as m:
            base_out = m(test_in)
        with mode(static_quantization.quantized, training=False) as q:
            test_out = q(test_in)

        # While quantized/original won't be exact, they should be close.
        self.assertLess(
            ((((test_out - base_out) ** 2).sum(axis=1)) ** (1 / 2)).mean(),
516
            0.02,
Kai Zhang's avatar
Kai Zhang committed
517
518
            "RMSE should be less than 0.007 between quantized and original.",
        )