test Sparsity (ASP) and QAT toolkits together
(self)
| 93 | assert loss.dtype == torch.float32 |
| 94 | |
| 95 | def test_asp(self): |
| 96 | """test Sparsity (ASP) and QAT toolkits together""" |
| 97 | try: |
| 98 | from apex.contrib.sparsity import ASP |
| 99 | except ImportError: |
| 100 | pytest.skip("ASP is not available.") |
| 101 | |
| 102 | quant_modules.initialize() |
| 103 | model = LeNet() |
| 104 | quant_modules.deactivate() |
| 105 | |
| 106 | optimizer = optim.SGD(model.parameters(), lr=0.01) |
| 107 | |
| 108 | ASP.init_model_for_pruning( |
| 109 | model, |
| 110 | mask_calculator="m4n2_1d", |
| 111 | verbosity=2, |
| 112 | whitelist=[torch.nn.Linear, torch.nn.Conv2d, torch.nn.Conv3d, quant_nn.modules.quant_linear.QuantLinear], |
| 113 | allow_recompute_mask=False, |
| 114 | custom_layer_dict={ |
| 115 | quant_nn.QuantConv1d: ['weight'], |
| 116 | quant_nn.QuantConv2d: ['weight'], |
| 117 | quant_nn.QuantConv3d: ['weight'], |
| 118 | quant_nn.QuantConvTranspose1d: ['weight'], |
| 119 | quant_nn.QuantConvTranspose2d: ['weight'], |
| 120 | quant_nn.QuantConvTranspose3d: ['weight'], |
| 121 | quant_nn.QuantLinear: ['weight'] |
| 122 | }, |
| 123 | allow_permutation=False) |
| 124 | ASP.init_optimizer_for_pruning(optimizer) |
| 125 | ASP.compute_sparse_masks() |
| 126 | |
| 127 | model = model.to('cuda') |
| 128 | output = model(torch.empty(16, 1, 28, 28).to('cuda')) |
| 129 | optimizer.zero_grad() |
| 130 | loss = F.nll_loss(output, torch.randint(10, (16,), dtype=torch.int64)) |
| 131 | loss.backward() |
| 132 | optimizer.step() |
| 133 | |
| 134 | def test_quant_module_replacement(self): |
| 135 | """test monkey patching of modules with their quantized versions""" |
nothing calls this directly
no test coverage detected