How to prune weights in PyTorch

luyuwang49: <p>I’m trying to replicate the work of Han <em>et al</em> (Learning both Weights and Connections for Efficient Neural Networks, 2015), where model compression for the deep CNN models is achieved by pruning close-to-zero weights and then retrain the model. It has two training phases: in the first stage the model is trained as usual, which is used to find weights below a certain threshold; then those insignificant weights are pruned, resulting in a simpler model, and the rest parameters are kept for another fine-tuning training session.</p>
<p>My idea of implementation using PyTorch is that given the trained model from the first stage, I set weights below the threshold to zero (memorized by <code>pruned_inds_by_layer</code>), and then start the second training stage, in which I don’t allow any gradient to be back-propagated to those zero-valued weights. But it seems modifying <code>p.grad.data</code> below doesn’t do the work. Those zero-valued weights still get gradients, making them non-zero again. Any idea how to solve this problem?</p>
<pre><code class="lang-auto">optimizer.zero_grad()
outputs = cnn(images)
loss = criterion(outputs, labels)
loss.backward()

# zero-out all the gradients corresponding to the pruned connections
for l,p in enumerate(cnn.parameters()):
    pruned_inds = pruned_inds_by_layer[l]
    p.grad.data[pruned_inds] = 0.

optimizer.step()
</code></pre>

luyuwang49: <p>I figure it out myself: I was using adam to optimize which accumulates gradients form previous steps and therefore setting gradients to zero by hand has no effect on the momentum term in adam. Using RMSprop solves this problem.</p>

lolongcovas: <p>hi, after pruning the model, did you notice the speed up in cpu/gpu?</p>

luyuwang49: <p>Not for my implementation. I was just creating a mask on the cnn module - weights are not actually pruned but just manually set to zero.</p>

lolongcovas: <p>I see. I think, after zeroing the weights, new architecture definition has to be created manually, for instance number of neurons per layer and copying the parameters with a bit of engineering.</p>
<p>However, the paper says that they train the connectivity between neurons of the previous and current layers. I guess it is a bit of different than your implementation, isn’t it?</p>

luyuwang49: <p>I think you are right, the code I have here prunes the weight gradients after the backpropagation. Contributions from pruned weights are still included, which is incorrect. Well, it is a hacky attempt I had - to do it better we need to create masks for each layer using <code>register_register_hook</code> I think.</p>
<p>Any thought?</p>

Edgar_Medina: <p>I read the paper available in this link ( <a href="https://arxiv.org/pdf/1506.02626.pdf" rel="nofollow noopener">https://arxiv.org/pdf/1506.02626.pdf</a> ), and he mencioned weights are masked also. I think zero weights may be implemented in hardware without problems.</p>
<p>I’m not sure about it. I’m trying to implement this method as well.</p>

IssamLaradji: <p>I am curious whether zeroing the gradients speeds up the operation, does the  Pytorch implementation of <code>.backward()</code> support sparse weight update ?</p>
<p>If there is no speed up, what stops you from zeroing the weights (instead of the gradients) after taking the optimization step ? in this case, the optimizer class doesn’t matter, unless you are trying to avoid accumulating meta information in the optimizer such as momentum.</p>
<p>You can prune the gradients by changing the forward pass as follows.</p>
<p>If you have <code>W = Variable(torch.randn(10), requires_grad=True)</code> and you would like the gradients of the first 5 coordinates only, maybe you can do something like this in the forward pass,</p>
<pre><code class="lang-python">loss = (torch.mv(X[:, :5], W[:5]) +
           torch.mv(X[:, 5:], Variable(W[5:].data)) - y)**2
loss = loss.sum()

# Compute grad for W[:5] only
loss.backward()
</code></pre>
<p>which computes the gradient for <code>W[:5]</code> only.</p>

Edgar_Medina: <p>Hello,</p>
<p>I think this is a better way to do it (mentioned as the beginning):</p>
<pre><code># zero-out all the gradients corresponding to the pruned connections
for l,p in enumerate(cnn.parameters()):
    pruned_inds = pruned_inds_by_layer[l]
    p.grad.data[pruned_inds] = 0.
</code></pre>
<p>Because I have the mask (zero-weights), so I can update only non zero weights. However, my weights continue updating. I will try other options as well <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

IssamLaradji: <p>If the goal is to update few parameters, then the method mentioned in the top post should be very slow. That’s because when you call <code>backwards()</code> all the gradients are computed anyway so you lose the speed advantage of computing only few gradients.</p>
<p>You can change the forward pass using the zero-weight mask so that only the gradients of the non-zero weights are computed.</p>

IssamLaradji: <p>On another note, I believe pruning the gradients using your code will result in the wrong update. That’s because gradients depend on each other. So if you zero some gradients for some parameters in some layer, then the gradients in the earlier layers should change (and might not necessarily be zero) due to the chain rule. Your code does not change the dependent gradients. So the code is probably slow and will result in the wrong update.</p>

Edgar_Medina: <p>I implemented a similar version to gradients update correctly (based on the top publication) after set some weights to zero. This update works because gradient must be <strong>variables</strong> or <strong>None</strong>:</p>
<pre><code>    for l,p in enumerate(net.parameters()):
        gradient_mask = (p!=0).data # Assuming some weights in 0
        p.grad = Variable(gradient_mask.float() * p.grad.data)
</code></pre>
<p>you are right in the first comment, and I am agree. The algorithm takes the same time as the non-pruned version, due to I continue computing the gradients in zero weights.</p>
<p>However in this:</p>
<blockquote>
<p>I believe pruning the gradients using your code will result in the wrong update.</p>
</blockquote>
<p>My original model had 91.77% accuracy, after pruning, the my model had 71.78%. I retrained the model (without updating the zero weights and setting gradients to zero) and I got 92.18% for 25 epochs using lr=0.05 with SGD  and learning rate is divided by 10 in 3, 10, 16 epoch. I’m not sure if it is a fine-tuning, but I improved the previous result. <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=12" title=":grinning:" class="emoji" alt=":grinning:" loading="lazy" width="20" height="20"></p>
<p>Moreover, could you explain a little bit more the example you gave? I get lose in some part when I try to employ it in my model.</p>
<p>Thank you in advance</p>

luyuwang49: <p>So we have an implementation of weights pruning on <a href="https://github.com/wanglouis49/pytorch-weights_pruning" rel="nofollow noopener">this repo</a>. The idea is to create a wrapper on the linear or conv layer, and apply the mask on the forward pass. Since multiplying the mask is a differentiable operation (multiplying constant essentially), PyTorch AutoDiff will take care of the backward pass automatically.</p>
<p>Well still you may not expect any acceleration, the goal of this implementation is to study the properties of pruned network.</p>

Awesome_Alex: <p>Hi, is your code final by now?<br>
And why won’t it be faster?</p>
<p>I have been struggling with similar question recently, I set the data below the threshold to zero and also set the gradient of them to zero. But based on your discussion, I think I should create mask for the loss instead?</p>
<p>So now both my gradient is incorrect and the operation is super slow, any thoughts on that?<br>
Thanks a lot.<br>
My code is:<br>
          <a href="https://ws1.sinaimg.cn/large/006tNc79gy1fmfljsbdt9j30qa0oy0w5.jpg" target="_blank" rel="nofollow noopener">
            <img src="https://ws1.sinaimg.cn/large/006tNc79gy1fmfljsbdt9j30qa0oy0w5.jpg" width="526" height="500">
          </a>
</p>

Edgar_Medina: <p>Unfortunatelly, I had the same time performance problem. I did not know how to solve it, but I worked <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

SHASHI_KUMAR: <p>After pruning the filters do you see any gpu speed-up?</p>

Valiox: <p>I actually don’t see any speedup from PyTorch on any network after zero-ing out the weights. I don’t know if the operations are meant to be constant in time regardless of the parameter values.</p>

RahimEntezari: <p>Did you find out the solution to the slow training? I meas setting gradient to zero takes a lot of time during training, especially when we are talking about  setting Million parameters to Zero.</p>

Edgar_Medina: <p>Hi <a class="mention" href="/u/rahimentezari">@RahimEntezari</a>, no, unfortunately , I didn’t try to solve that problem again. Check my github, I refer another interesting paper that reduces the model in a smaller one.</p>
<p>I know my code is still a kind mess, but could give you maybe some ideas.<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/emedinac/DeepCompression" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars2.githubusercontent.com/u/29845368?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/emedinac/DeepCompression" target="_blank" rel="nofollow noopener">emedinac/DeepCompression</a></h3>

<p>This is my own implementation and understanding of the original paper paper - emedinac/DeepCompression</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

AlexKoff88: <p>Hi, there is a nice repository for model compression in PyTorch here: <a href="https://github.com/opencv/openvino_training_extensions/tree/develop/pytorch_toolkit/nncf" rel="nofollow noopener">https://github.com/opencv/openvino_training_extensions/tree/develop/pytorch_toolkit/nncf</a>. Now it has two pruning methods and quantization support.</p>