Why does the quantization after pruning results in worse performance?

username: Shisho_Sama
cooked: <p>I have resnet18 model which I quantized using graph quantization, each forward pass on my system takes about 100 ms (on cpu) and its size shrunk from 85 to 45 MBs.<br>
I then went on and poruned this model to 14.5M parameters from the initial 25M and its size shrunk from 85 to 58MB. I then went on and quantized the resulting model hoping for improvements.<br>
but in fact I’m seeing dimnishing results. That is, I dont see what I expected in inference speed.  instead of being better, the newer model simply is worse than the initial model (quantized from initial model)</p>
<p>Is this expected behavior?<br>
Here are the  two models for comparision :<br>
</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://gofile.io/dist/img/favicon16.png" class="site-icon" width="16" height="16">
      <a href="https://gofile.io/d/zCzHAF" target="_blank" rel="noopener nofollow ugc">gofile.io</a>
  </header>
  <article class="onebox-body">
    <img src="" class="thumbnail" width="" height="">

<h3><a href="https://gofile.io/d/zCzHAF" target="_blank" rel="noopener nofollow ugc">Gofile</a></h3>



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

<p>and this is their runtime benchmark results :</p>
<p>pruned-quantized model :</p>
<pre><code class="lang-auto">1&gt;[ RUN      ] EmbedderModelForwardFixture.ModelEmbedderBench (10 runs, 10 iterations per run)
1&gt;[     DONE ] EmbedderModelForwardFixture.ModelEmbedderBench (5778.116020 ms)
1&gt;[   RUNS   ]        Average time: 577811.602 us (~26296.168 us)
1&gt;                    Fastest time: 537246.562 us (-40565.040 us / -7.020 %)
1&gt;                    Slowest time: 617859.662 us (+40048.060 us / +6.931 %)
1&gt;                     Median time: 585275.362 us (1st quartile: 554311.262 us | 3rd quartile: 594753.362 us)
1&gt;
1&gt;             Average performance: 1.73067 runs/s
1&gt;                Best performance: 1.86134 runs/s (+0.13067 runs/s / +7.55054 %)
1&gt;               Worst performance: 1.61849 runs/s (-0.11218 runs/s / -6.48174 %)
1&gt;              Median performance: 1.70860 runs/s (1st quartile: 1.80404 | 3rd quartile: 1.68137)
1&gt;
1&gt;[ITERATIONS]        Average time: 57781.160 us (~2629.617 us)
1&gt;                    Fastest time: 53724.656 us (-4056.504 us / -7.020 %)
1&gt;                    Slowest time: 61785.966 us (+4004.806 us / +6.931 %)
1&gt;                     Median time: 58527.536 us (1st quartile: 55431.126 us | 3rd quartile: 59475.336 us)
1&gt;
1&gt;             Average performance: 17.30668 iterations/s
1&gt;                Best performance: 18.61343 iterations/s (+1.30675 iterations/s / +7.55054 %)
1&gt;               Worst performance: 16.18491 iterations/s (-1.12177 iterations/s / -6.48174 %)
1&gt;              Median performance: 17.08597 iterations/s (1st quartile: 18.04041 | 3rd quartile: 16.81369)
</code></pre>
<p>quantized from normal model (no pruning done beforehand) :</p>
<pre><code class="lang-auto">1&gt;[ RUN      ] EmbedderModelForwardFixture.ModelEmbedderBench (10 runs, 10 iterations per run)
1&gt;[     DONE ] EmbedderModelForwardFixture.ModelEmbedderBench (5672.357520 ms)
1&gt;[   RUNS   ]        Average time: 567235.752 us (~31674.053 us)
1&gt;                    Fastest time: 530900.462 us (-36335.290 us / -6.406 %)
1&gt;                    Slowest time: 640024.562 us (+72788.810 us / +12.832 %)
1&gt;                     Median time: 561095.762 us (1st quartile: 548392.562 us | 3rd quartile: 577176.062 us)
1&gt;
1&gt;             Average performance: 1.76294 runs/s
1&gt;                Best performance: 1.88359 runs/s (+0.12066 runs/s / +6.84409 %)
1&gt;               Worst performance: 1.56244 runs/s (-0.20050 runs/s / -11.37282 %)
1&gt;              Median performance: 1.78223 runs/s (1st quartile: 1.82351 | 3rd quartile: 1.73257)
1&gt;
1&gt;[ITERATIONS]        Average time: 56723.575 us (~3167.405 us)
1&gt;                    Fastest time: 53090.046 us (-3633.529 us / -6.406 %)
1&gt;                    Slowest time: 64002.456 us (+7278.881 us / +12.832 %)
1&gt;                     Median time: 56109.576 us (1st quartile: 54839.256 us | 3rd quartile: 57717.606 us)
1&gt;
1&gt;             Average performance: 17.62935 iterations/s
1&gt;                Best performance: 18.83592 iterations/s (+1.20657 iterations/s / +6.84409 %)
1&gt;               Worst performance: 15.62440 iterations/s (-2.00495 iterations/s / -11.37282 %)
1&gt;              Median performance: 17.82227 iterations/s (1st quartile: 18.23511 | 3rd quartile: 17.32574)
</code></pre>
<p>Or to put it simply after 10 iterations:</p>
<pre><code class="lang-auto">r18_default : 805.72 ms (mean) 
quantized_model : 560 ms (mean)
r18_pruned : 7,466.78  ms
pruned_then_quantized: 578 ms (mean) 
</code></pre>
<p>Not only the second model is not faster, its worse, its become slower!<br>
You can also see that the pruned model is extremely slow! 10x slower than the default model!</p>
<p><strong>Note:</strong><br>
in case it matters, training (pruning, and finetuning the model) is done using pytorch 1.5.1 and the final graph quantization is done in windows using pytorch 1.6</p>
<p><strong>Note2:</strong><br>
This is being tested and evaluted using libtorch(1.6) on windows 10 machine.</p>
<p>I’d greatly appreciate any kind of feedback on this.<br>
Thank you all in advance</p>

username: raziel
cooked: <p>Pruning is a good way to reduce model size, but it won’t automatically give you performance improvements.</p>
<p>The reason is that when pruning a model you’re introducing zeros to Tensors (i.e. they become sparse). These tensors can be compressed (with some extra overhead) without much problem. Hence the reduced model size.</p>
<p>However, the underlying kernels (i.e. code that operates on the Tensors holding the values, like a matrix multiply) are by default written assuming dense Tensors, because hardware provides instructions to make those dense operations fast (e.g via vectorized on adjacent values in memory).</p>
<p>Unfortunately there’s not a lot of hardware, including CPUs, that can benefit directly from sparse Tensors.</p>
<p>There’s some work arounds to still speeding up sparse operations in some cases. In CPU, for example, doing structured pruning (i.e. removing blocks of values rather than one by one) in a way that matches the size of the registers used in those vectorized operations such that you can skip entire blocks of continuous zeros. For example, pruning blocks of 1x16 or 4x4 and quantizing afterwards, matches the register sizes of vectorized instructions in CPUs (128bit).</p>
<p>However, even if you use structured pruning, you will still depend on having underlying kernels for the operations used in your model being implemented to take advantage of the structured nature of your tensors (i.e. to know that they can skip entire sequences of 16 values when doing things like a matrix multiply).</p>
<p>Why is it slower?<br>
In theory a pruned model shouldn’t be slower since the dense operations could ignore tensors coming in that happen to have a bunch of zeros. However, if a special representation is used to represent the sparse Tensor (i.e. to avoid having to represent the zeros), as it’s likely the case here, then that will cause inefficiencies when operating with those special sparse Tensors (e.g. some copying of values may be needed to restore the original structure of the Tensor to go through the dense kernels).</p>
<p>Hope this helps.</p>

username: Shisho_Sama
cooked: <p>Thanks a lot really appreciate it.<br>
But when the whole channel is removed, we are basically left with a dense model at the end, so we still need to be able to benefit from the dense operation efficiency! In my case I’m using <a href="https://github.com/VainF/Torch-Pruning" rel="noopener nofollow ugc">Torch-Pruning</a> which removes entire channels and not setting neurons to zeros only  as far as I understand.  So to me it should be able to run as fast at the very least.<br>
Also note that the final pruned model is then retrained therefore even if we have sparse neurons (set to zero) at first, we should be dealing with a dense model at the end that shouldn’t be performing like this.<br>
Am I missing something here?</p>

username: dskhudia
cooked: <p>Could you print out and check the dimensions of the weight for the pruned quantized model during inference? If they are the same as before pruning you should see a similar performance.</p>

username: Shisho_Sama
cooked: <p>Here is the live run of the process, showing the pruned model is indeed faster, but the quantized model for some reason is extremely slow :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/0/800a95e245e9b00a3f75141ef87f40169f287cae.gif" data-download-href="https://discuss.pytorch.org/uploads/default/800a95e245e9b00a3f75141ef87f40169f287cae" title="pytorch_prune_neomL_pruned_quantzed"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/0/800a95e245e9b00a3f75141ef87f40169f287cae_2_690x357.gif" alt="pytorch_prune_neomL_pruned_quantzed" data-base62-sha1="igHNjWOLkJH1Cm9f6WkvYJfkE7I" width="690" height="357" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/0/800a95e245e9b00a3f75141ef87f40169f287cae_2_690x357.gif, https://discuss.pytorch.org/uploads/default/optimized/3X/8/0/800a95e245e9b00a3f75141ef87f40169f287cae_2_1035x535.gif 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/0/800a95e245e9b00a3f75141ef87f40169f287cae_2_1380x714.gif 2x" data-dominant-color="232426"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pytorch_prune_neomL_pruned_quantzed</span><span class="informations">1596×828 426 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
I also tested this with another model, and the outcome is the same. the quantized model takes much more to finish:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/d/adca42dc1d6530137ab3c77cf22ecf1edc16c198.gif" data-download-href="https://discuss.pytorch.org/uploads/default/adca42dc1d6530137ab3c77cf22ecf1edc16c198" title="pytorch_prune_neomL_pruned_quantzed_simpnet"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/d/adca42dc1d6530137ab3c77cf22ecf1edc16c198_2_690x357.gif" alt="pytorch_prune_neomL_pruned_quantzed_simpnet" data-base62-sha1="oNpWAnF14zcfZGZCTB4B3Gr4LdC" width="690" height="357" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/d/adca42dc1d6530137ab3c77cf22ecf1edc16c198_2_690x357.gif, https://discuss.pytorch.org/uploads/default/optimized/3X/a/d/adca42dc1d6530137ab3c77cf22ecf1edc16c198_2_1035x535.gif 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/d/adca42dc1d6530137ab3c77cf22ecf1edc16c198_2_1380x714.gif 2x" data-dominant-color="232426"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pytorch_prune_neomL_pruned_quantzed_simpnet</span><span class="informations">1596×828 488 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><strong>Note :</strong><br>
As you can see, we do not finetune the pruned model here. we are just testing whether the pruning by nature results in faster inference in our case or not, and as t he results show, it indeed does.<br>
Finetuning an already pruned model for some reason, results in a sever slow down at inferece as you can see.</p>