docstr: Filters with the minimum importance approxiamtion based on the first order 
  taylor expansion on the weights (w*grad)**2 are pruned in this paper:
  Importance Estimation for Neural Network Pruning,
  http://jankautz.com/publications/Importance4NNPruning_CVPR19.pdf
  
  So if sparsity of conv1 is 0.2, the expected masks should mask out filter 0, this can be verified through:
  `all(torch.sum(mask1['weight_mask'], (1, 2, 3)).numpy() == np.array([0., 25., 25., 25., 25.]))`
  
  If sparsity of conv2 is 0.6, the expected masks should mask out filter 4,5,6,7,8,9 this can be verified through:
  `all(torch.sum(mask2['weight_mask'], (1, 2, 3)).numpy() == np.array([125., 125., 125., 125., 0., 0., 0., 0., 0., 0., ]))`
docstr_tok: ["Filters","with","the","minimum","importance","approxiamtion","based","on","the","first","order","taylor","expansion","on","the","weights","(","w","*","grad",")","*","*","2","are","pruned","in","this","paper",":","Importance","Estimation","for","Neural","Network","Pruning",",","http",":","\/\/jankautz.com\/publications\/Importance4NNPruning_CVPR19.pdf","So","if","sparsity","of","conv1","is","0.2",",","the","expected","masks","should","mask","out","filter","0",",","this","can","be","verified","through",":","`","all","(","torch.sum","(","mask1","[","'weight_mask","'","]",",","(","1",",","2",",","3",")",")",".numpy","(",")","==","np.array","(","[","0.",",","25.",",","25.",",","25.",",","25",".","]",")",")","`","If","sparsity","of","conv2","is","0.6",",","the","expected","masks","should","mask","out","filter","4,5,6,7,8,9","this","can","be","verified","through",":","`","all","(","torch.sum","(","mask2","[","'weight_mask","'","]",",","(","1",",","2",",","3",")",")",".numpy","(",")","==","np.array","(","[","125.",",","125.",",","125.",",","125.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","]",")",")","`"]
code: def test_torch_taylorFOweight_pruner(self):
      """
      Filters with the minimum importance approxiamtion based on the first order
      taylor expansion on the weights (w*grad)**2 are pruned in this paper:
      Importance Estimation for Neural Network Pruning,
      http://jankautz.com/publications/Importance4NNPruning_CVPR19.pdf
  
      So if sparsity of conv1 is 0.2, the expected masks should mask out filter 0, this can be verified through:
      `all(torch.sum(mask1['weight_mask'], (1, 2, 3)).numpy() == np.array([0., 25., 25., 25., 25.]))`
  
      If sparsity of conv2 is 0.6, the expected masks should mask out filter 4,5,6,7,8,9 this can be verified through:
      `all(torch.sum(mask2['weight_mask'], (1, 2, 3)).numpy() == np.array([125., 125., 125., 125., 0., 0., 0., 0., 0., 0., ]))`
      """
  
      w1 = np.array(
          [
              np.zeros((1, 5, 5)),
              np.ones((1, 5, 5)),
              np.ones((1, 5, 5)) * 2,
              np.ones((1, 5, 5)) * 3,
              np.ones((1, 5, 5)) * 4,
          ]
      )
      w2 = np.array([[[[i + 1] * 5] * 5] * 5 for i in range(10)[::-1]])
  
      grad1 = np.array(
          [
              np.ones((1, 5, 5)) * -1,
              np.ones((1, 5, 5)) * 1,
              np.ones((1, 5, 5)) * -1,
              np.ones((1, 5, 5)) * 1,
              np.ones((1, 5, 5)) * -1,
          ]
      )
  
      grad2 = np.array([[[[(-1) ** i] * 5] * 5] * 5 for i in range(10)])
  
      config_list = [
          {
              "sparsity": 0.2,
              "op_types": ["Conv2d"],
              "op_names": ["conv1"],
          },
          {
              "sparsity": 0.6,
              "op_types": ["Conv2d"],
              "op_names": ["conv2"],
          },
      ]
  
      model = TorchModel()
      optimizer = torch.optim.SGD(
          model.parameters(), lr=0.01, momentum=0.5
      )
      pruner = TaylorFOWeightFilterPruner(
          model, config_list, optimizer, statistics_batch_num=1
      )
  
      x = torch.rand((1, 1, 28, 28), requires_grad=True)
      model.conv1.module.weight.data = torch.tensor(w1).float()
      model.conv2.module.weight.data = torch.tensor(w2).float()
  
      y = model(x)
      y.backward(torch.ones_like(y))
  
      model.conv1.module.weight.grad.data = torch.tensor(grad1).float()
      model.conv2.module.weight.grad.data = torch.tensor(grad2).float()
      optimizer.step()
  
      mask1 = pruner.calc_mask(model.conv1)
      mask2 = pruner.calc_mask(model.conv2)
      assert all(
          torch.sum(mask1["weight_mask"], (1, 2, 3)).numpy()
          == np.array([0.0, 25.0, 25.0, 25.0, 25.0])
      )
      assert all(
          torch.sum(mask2["weight_mask"], (1, 2, 3)).numpy()
          == np.array(
              [
                  125.0,
                  125.0,
                  125.0,
                  125.0,
                  0.0,
                  0.0,
                  0.0,
                  0.0,
                  0.0,
                  0.0,
              ]
          )
      )
  
imports: from unittest import TestCase, main
  import numpy
  import torch
  import torch.nn.functional
  import schema
  from aup.compression.torch.pruning import *
  from aup.compression.torch.quantization import *
  import math
label_desc: Use image node_id 7 for calling the CompressorTestCase obj's underlying member method code with example usage: obj.test_torch_taylorFOweight_pruner() without return types