File: test_sparsifier.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (462 lines) | stat: -rw-r--r-- 19,871 bytes parent folder | download
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# -*- coding: utf-8 -*-
# Owner(s): ["module: unknown"]

import itertools
import logging
import re

import torch
from torch import nn
from torch.ao.sparsity import BaseSparsifier, WeightNormSparsifier, FakeSparsity, NearlyDiagonalSparsifier
from torch.nn.utils.parametrize import is_parametrized

from torch.testing._internal.common_utils import TestCase

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.seq = nn.Sequential(
            nn.Linear(16, 16)
        )
        self.linear = nn.Linear(16, 16)
        self.head = nn.Linear(16, 4)

    def forward(self, x):
        x = self.seq(x)
        x = self.linear(x)
        x = self.head(x)
        return x


class ImplementedSparsifier(BaseSparsifier):
    def __init__(self, **kwargs):
        super().__init__(defaults=kwargs)

    def update_mask(self, module, **kwargs):
        module.parametrizations.weight[0].mask[0] = 0
        linear_state = self.state['linear.weight']
        linear_state['step_count'] = linear_state.get('step_count', 0) + 1


class TestBaseSparsifier(TestCase):
    def test_constructor(self):
        # Cannot instantiate the abstract base
        self.assertRaises(TypeError, BaseSparsifier)
        # Can instantiate the model with no configs
        model = Model()
        sparsifier = ImplementedSparsifier(test=3)
        sparsifier.prepare(model, config=None)
        assert len(sparsifier.groups) == 3
        sparsifier.step()
        # Can instantiate the model with configs
        sparsifier = ImplementedSparsifier(test=3)
        sparsifier.prepare(model, [{'tensor_fqn': 'linear.weight'}])
        assert len(sparsifier.groups) == 1
        assert sparsifier.groups[0]['tensor_fqn'] == 'linear.weight'
        assert 'test' in sparsifier.groups[0]
        assert sparsifier.groups[0]['test'] == 3

    def test_prepare_config(self):
        model = Model()
        sparsifier = ImplementedSparsifier(test=3)
        # Make sure there are no parametrizations before `prepare`
        assert not hasattr(model.seq[0], 'parametrizations')
        assert not hasattr(model.linear, 'parametrizations')
        assert not hasattr(model.head, 'parametrizations')
        sparsifier.prepare(model, config=[
            {'tensor_fqn': 'seq.0.weight', 'test': 42},
            # No 'linear' to make sure it will be skipped in the sparsification
            {'tensor_fqn': 'head.weight'}
        ])
        assert len(sparsifier.groups) == 2
        # Check if default argument is not assigned if explicit
        assert sparsifier.groups[0]['tensor_fqn'] == 'seq.0.weight'
        assert sparsifier.groups[0]['test'] == 42
        # Check if FQN and module are pointing to the same location
        assert sparsifier.groups[1]['tensor_fqn'] == 'head.weight'
        assert sparsifier.groups[1]['module'] == model.head
        # Check if parameterizations are attached
        assert hasattr(model.seq[0], 'parametrizations')
        assert not hasattr(model.linear, 'parametrizations')
        assert hasattr(model.head, 'parametrizations')

    def test_step(self):
        model = Model()
        sparsifier = ImplementedSparsifier(test=3)
        sparsifier.enable_mask_update = True
        sparsifier.prepare(model, [{'tensor_fqn': 'linear.weight'}])
        sparsifier.step()
        assert torch.all(model.linear.parametrizations.weight[0].mask[0] == 0)

    def test_state_dict(self):
        step_count = 3
        model0 = Model()
        sparsifier0 = ImplementedSparsifier(test=3)
        sparsifier0.prepare(model0, [{'tensor_fqn': 'linear.weight'}])
        mask = model0.linear.parametrizations['weight'][0].mask
        mask.data = torch.arange(mask.shape[0] * mask.shape[1]).reshape(mask.shape)
        for step in range(step_count):
            sparsifier0.step()
        state_dict = sparsifier0.state_dict()

        # Check the expected keys in the state_dict
        assert 'state' in state_dict
        assert 'step_count' in state_dict['state']['linear.weight']
        assert state_dict['state']['linear.weight']['step_count'] == 3
        assert 'groups' in state_dict
        assert 'test' in state_dict['groups'][0]
        assert 'tensor_fqn' in state_dict['groups'][0]
        assert state_dict['groups'][0]['tensor_fqn'] == 'linear.weight'

        # Check loading static_dict creates an equivalent model
        model1 = Model()
        sparsifier1 = ImplementedSparsifier()
        sparsifier1.prepare(model1, None)

        assert sparsifier0.state != sparsifier1.state

        # Make sure the masks are different in the beginning
        for mg in sparsifier0.groups:
            if mg['tensor_fqn'] == 'linear.weight':
                mask0 = mg['module'].parametrizations.weight[0].mask
        for mg in sparsifier1.groups:
            if mg['tensor_fqn'] == 'linear.weight':
                mask1 = mg['module'].parametrizations.weight[0].mask
        self.assertNotEqual(mask0, mask1)

        sparsifier1.load_state_dict(state_dict)

        # Make sure the states are loaded, and are correct
        assert sparsifier0.state == sparsifier1.state

        # Make sure the masks (and all dicts) are the same after loading
        assert len(sparsifier0.groups) == len(sparsifier1.groups)
        for idx in range(len(sparsifier0.groups)):
            mg0 = sparsifier0.groups[idx]
            mg1 = sparsifier1.groups[idx]
            for key in mg0.keys():
                assert key in mg1
                if key == 'module':
                    # We cannot compare modules as they are different
                    param0 = mg0[key].parametrizations.weight[0]
                    param1 = mg1[key].parametrizations.weight[0]
                    assert hasattr(param0, 'mask')
                    assert hasattr(param1, 'mask')
                    self.assertEqual(param0.__dict__, param1.__dict__)
                else:
                    assert mg0[key] == mg1[key]

    def test_mask_squash(self):
        model = Model()
        sparsifier = ImplementedSparsifier(test=3)
        sparsifier.prepare(model, [{'tensor_fqn': 'linear.weight'}])
        assert hasattr(model.linear.parametrizations.weight[0], 'mask')
        assert is_parametrized(model.linear, 'weight')
        assert not is_parametrized(model.seq[0], 'weight')

        sparsifier.squash_mask()
        assert not is_parametrized(model.seq[0], 'weight')
        assert not is_parametrized(model.linear, 'weight')

    def test_mask_squash_with_params1(self):
        model = Model()
        sparsifier = ImplementedSparsifier(foo=3, bar=2, baz=1)
        sparsifier.prepare(model, [{'tensor_fqn': 'linear.weight'}, {'tensor_fqn': 'seq.0.weight'}])
        sparsifier.squash_mask(
            params_to_keep_per_layer={
                'linear': ('foo', 'bar'),
                'seq.0': ('baz',)
            })
        assert not is_parametrized(model.seq[0], 'weight')
        assert not is_parametrized(model.linear, 'weight')
        assert hasattr(model.seq[0], 'sparse_params')
        assert hasattr(model.linear, 'sparse_params')
        assert model.seq[0].sparse_params.get('foo', None) is None
        assert model.seq[0].sparse_params.get('bar', None) is None
        assert model.seq[0].sparse_params.get('baz', None) == 1
        assert model.linear.sparse_params.get('foo', None) == 3
        assert model.linear.sparse_params.get('bar', None) == 2
        assert model.linear.sparse_params.get('baz', None) is None

    def test_mask_squash_with_params2(self):
        model = Model()
        sparsifier = ImplementedSparsifier(foo=3, bar=2, baz=1)
        sparsifier.prepare(model, [{'tensor_fqn': 'linear.weight'}, {'tensor_fqn': 'seq.0.weight'}])
        sparsifier.squash_mask(params_to_keep=('foo', 'bar'))
        assert not is_parametrized(model.seq[0], 'weight')
        assert not is_parametrized(model.linear, 'weight')
        assert hasattr(model.seq[0], 'sparse_params')
        assert hasattr(model.linear, 'sparse_params')
        assert model.seq[0].sparse_params.get('foo', None) == 3
        assert model.seq[0].sparse_params.get('bar', None) == 2
        assert model.seq[0].sparse_params.get('baz', None) is None
        assert model.linear.sparse_params.get('foo', None) == 3
        assert model.linear.sparse_params.get('bar', None) == 2
        assert model.linear.sparse_params.get('baz', None) is None

    def test_mask_squash_with_params3(self):
        model = Model()
        sparsifier = ImplementedSparsifier(foo=3, bar=2, baz=1)
        sparsifier.prepare(model, [{'tensor_fqn': 'linear.weight'}, {'tensor_fqn': 'seq.0.weight'}])
        sparsifier.squash_mask(
            params_to_keep=('foo', 'bar'),
            params_to_keep_per_layer={'seq.0': ('baz',)})
        assert not is_parametrized(model.seq[0], 'weight')
        assert not is_parametrized(model.linear, 'weight')
        assert hasattr(model.seq[0], 'sparse_params')
        assert hasattr(model.linear, 'sparse_params')
        assert model.seq[0].sparse_params.get('foo', None) == 3
        assert model.seq[0].sparse_params.get('bar', None) == 2
        assert model.seq[0].sparse_params.get('baz', None) == 1
        assert model.linear.sparse_params.get('foo', None) == 3
        assert model.linear.sparse_params.get('bar', None) == 2
        assert model.linear.sparse_params.get('baz', None) is None


class TestWeightNormSparsifier(TestCase):
    def test_constructor(self):
        model = Model()
        sparsifier = WeightNormSparsifier()
        sparsifier.prepare(model, config=None)
        for g in sparsifier.groups:
            assert isinstance(g['module'], nn.Linear)
            # The groups are unordered
            assert g['module_fqn'] in ('seq.0', 'linear', 'head')

    def test_step(self):
        model = Model()
        sparsifier = WeightNormSparsifier(sparsity_level=0.5)
        sparsifier.prepare(model, config=[{'tensor_fqn': 'linear.weight'}])
        for g in sparsifier.groups:
            # Before step
            module = g['module']
            assert (1.0 - module.parametrizations['weight'][0].mask.mean()) == 0  # checking sparsity level is 0
        sparsifier.enable_mask_update = True
        sparsifier.step()
        self.assertAlmostEqual(model.linear.parametrizations['weight'][0].mask.mean().item(), 0.5, places=2)
        for g in sparsifier.groups:
            # After step
            module = g['module']
            assert (1.0 - module.parametrizations['weight'][0].mask.mean()) > 0  # checking sparsity level has increased
        # Test if the mask collapses to all zeros if the weights are randomized
        iters_before_collapse = 1000
        for _ in range(iters_before_collapse):
            model.linear.weight.data = torch.randn(model.linear.weight.shape)
            sparsifier.step()
        for g in sparsifier.groups:
            # After step
            module = g['module']
            assert (1.0 - module.parametrizations['weight'][0].mask.mean()) > 0  # checking sparsity level did not collapse

    def test_step_2_of_4(self):
        model = Model()
        sparsifier = WeightNormSparsifier(sparsity_level=1.0,
                                          sparse_block_shape=(1, 4),
                                          zeros_per_block=2)
        sparsifier.prepare(model, config=[{'tensor_fqn': 'linear.weight'}])
        sparsifier.step()
        # make sure the sparsity level is approximately 50%
        mask = model.linear.parametrizations['weight'][0].mask.to(torch.float)  # mean works on float only
        self.assertAlmostEqual(mask.mean().item(), 0.5, places=2)
        # Make sure each block has exactly 50% zeros
        module = sparsifier.groups[0]['module']
        mask = module.parametrizations['weight'][0].mask
        for row in mask:
            for idx in range(0, len(row), 4):
                block = row[idx:idx + 4]
                block, _ = block.sort()
                assert (block[:2] == 0).all()
                assert (block[2:] != 0).all()

    def test_prepare(self):
        model = Model()
        sparsifier = WeightNormSparsifier()
        sparsifier.prepare(model, config=None)
        for g in sparsifier.groups:
            module = g['module']
            # Check mask exists
            assert hasattr(module.parametrizations['weight'][0], 'mask')
            # Check parametrization exists and is correct
            assert is_parametrized(module, 'weight')
            assert type(module.parametrizations.weight[0]) == FakeSparsity

    def test_mask_squash(self):
        model = Model()
        sparsifier = WeightNormSparsifier()
        sparsifier.prepare(model, config=None)
        sparsifier.squash_mask()
        for g in sparsifier.groups:
            module = g['module']
            assert not is_parametrized(module, 'weight')
            assert not hasattr(module, 'mask')

    def test_sparsity_levels(self):
        sparsity_levels = [-1.0, 0.0, 0.5, 1.0, 2.0]
        sparse_block_shapes = [(1, 1), (1, 4), (2, 2), (4, 1)]
        zeros_per_blocks = [0, 1, 2, 3, 4]

        testcases = itertools.tee(itertools.product(sparsity_levels,
                                                    sparse_block_shapes,
                                                    zeros_per_blocks))
        # Create a config and model with all the testcases
        model = nn.Sequential()
        sparsifier = WeightNormSparsifier()

        sparsity_per_layer_config = []
        p = re.compile(r'[-\.\s]')
        for sl, sbs, zpb in testcases[0]:
            # Make sure the number of zeros is not > values in a block
            if zpb > sbs[0] * sbs[1]:
                continue
            layer_name = f'{sl}_{sbs}_{zpb}'
            layer_name = p.sub('_', layer_name)

            layer = nn.Linear(12, 12, bias=False)
            layer.weight = nn.Parameter(torch.ones(12, 12))
            model.add_module(layer_name, layer)
            config = {
                'tensor_fqn': layer_name + ".weight",
                'sparsity_level': sl,
                'sparse_block_shape': sbs,
                'zeros_per_block': zpb
            }
            sparsity_per_layer_config.append(config)

        sparsifier.prepare(model, sparsity_per_layer_config)
        sparsifier.step()
        sparsifier.squash_mask()
        model.eval()

        for sl, sbs, zpb in testcases[1]:
            if zpb > sbs[0] * sbs[1]:
                continue
            layer_name = f'{sl}_{sbs}_{zpb}'
            layer_name = p.sub('_', layer_name)
            layer = getattr(model, layer_name)

            # Level of sparsity is achieved
            sparse_mask = (layer.weight == 0).float()
            if zpb == 0:
                assert sparse_mask.mean() == 0
            else:
                # Ratio of individual zeros in the tensor
                true_sl = min(max(sl, 0.0), 1.0)
                true_sl = true_sl * zpb / sbs[0] / sbs[1]
                assert sparse_mask.mean() == true_sl


class TestNearlyDiagonalSparsifier(TestCase):
    def test_constructor(self):
        model = Model()
        sparsifier = NearlyDiagonalSparsifier(nearliness=1)
        sparsifier.prepare(model, config=None)
        for g in sparsifier.groups:
            assert isinstance(g['module'], nn.Linear)
            # The groups are unordered
            assert g['module_fqn'] in ('seq.0', 'linear', 'head')

    def test_step(self):
        model = Model()
        sparsifier = NearlyDiagonalSparsifier(nearliness=1)
        sparsifier.prepare(model, config=[{'tensor_fqn': 'linear.weight'}])

        for g in sparsifier.groups:
            # Before step
            module = g['module']
            assert (1.0 - module.parametrizations['weight'][0].mask.mean()) == 0  # checking sparsity level is 0

        sparsifier.enable_mask_update = True
        sparsifier.step()
        mask = module.parametrizations['weight'][0].mask
        height, width = mask.shape
        assert torch.all(mask == torch.eye(height, width))

        for g in sparsifier.groups:
            # After step
            module = g['module']
            assert (1.0 - module.parametrizations['weight'][0].mask.mean()) > 0  # checking sparsity level has increased

        # Test if the mask collapses to all zeros if the weights are randomized
        iters_before_collapse = 1000
        for _ in range(iters_before_collapse):
            model.linear.weight.data = torch.randn(model.linear.weight.shape)
            sparsifier.step()
        for g in sparsifier.groups:
            # After step
            module = g['module']
            assert (1.0 - module.parametrizations['weight'][0].mask.mean()) > 0  # checking sparsity level did not collapse

    def test_prepare(self):
        model = Model()
        sparsifier = NearlyDiagonalSparsifier(nearliness=1)
        sparsifier.prepare(model, config=None)
        for g in sparsifier.groups:
            module = g['module']
            # Check mask exists
            assert hasattr(module.parametrizations['weight'][0], 'mask')
            # Check parametrization exists and is correct
            assert is_parametrized(module, 'weight')
            assert type(module.parametrizations.weight[0]) == FakeSparsity

    def test_mask_squash(self):
        model = Model()
        sparsifier = NearlyDiagonalSparsifier(nearliness=1)
        sparsifier.prepare(model, config=None)
        sparsifier.step()
        sparsifier.squash_mask()
        for g in sparsifier.groups:
            module = g['module']
            assert not is_parametrized(module, 'weight')
            assert not hasattr(module, 'mask')
            weights = module.weight
            height, width = weights.shape
            assert torch.all(weights == torch.eye(height, width) * weights)  # only diagonal to be present

    def test_sparsity_levels(self):
        nearliness_levels = list(nearliness for nearliness in range(-1, 100))
        model = nn.Sequential()

        p = re.compile(r'[-\.\s]')
        for nearliness in nearliness_levels:
            sparsifier = NearlyDiagonalSparsifier(nearliness=1)
            layer_name = f'{nearliness}'
            layer_name = p.sub('_', layer_name)

            layer = nn.Linear(32, 32, bias=False)
            layer.weight = nn.Parameter(torch.ones(32, 32))
            width, height = layer.weight.shape
            model.add_module(layer_name, layer)
            config = {
                'tensor_fqn': layer_name + ".weight",
                'nearliness': nearliness
            }

            sparsifier.prepare(model, [config])
            # should raise a ValueError when nearliness arg is illegal
            if (nearliness > 0 and nearliness % 2 == 0) or (nearliness // 2 >= min(width, height)):
                with self.assertRaises(ValueError):
                    sparsifier.step()
            else:
                sparsifier.step()
                sparsifier.squash_mask()
                model.eval()

                layer = getattr(model, layer_name)
                # verify that mask created corresponds to the nearliness
                self._verify_nearliness(layer.weight, nearliness)

    # helper function to verify nearliness of a mask
    def _verify_nearliness(self, mask: torch.Tensor, nearliness: int):
        if nearliness <= 0:
            assert torch.all(mask == torch.zeros(mask.shape[0], mask.shape[1]))
        else:
            height, width = mask.shape
            dist_to_diagonal = nearliness // 2
            for row in range(0, height):
                for col in range(0, width):
                    if abs(row - col) <= dist_to_diagonal:
                        assert mask[row, col] == 1
                    else:
                        assert mask[row, col] == 0