How to quantize a model with both CNN and LSTM

Nanton: <p>Hi there,</p>
<p>If there is a model with CNN as backbone, LSTM as its head, how to quantize this whole model with post training quantization? It seems we can apply static quantization to CNN and dynamic quantization to LSTM( <a href="https://pytorch.org/docs/stable/quantization.html#quantization-mode-support" rel="noopener nofollow ugc">Quantization — PyTorch 1.12 documentation</a>). But not very sure how to deal with cases like above one.</p>
<p>Thanks in advance!</p>

Vasiliy_Kuznetsov: <p>Hi <a class="mention" href="/u/nanton">@Nanton</a> ,</p>
<p>What you said is correct, we have official support for static quantization for CNNs and dynamic quantization for LSTMs.</p>
<p>There is an unreleased prototype of static quantization of LSTMs, an example of how to use it is here: <a href="https://github.com/pytorch/pytorch/blob/a9ba3fe1dbf2cea45c9a7e723010c27c211f7fe3/test/quantization/core/test_quantized_op.py#L2750" class="inline-onebox" rel="noopener nofollow ugc">pytorch/test_quantized_op.py at a9ba3fe1dbf2cea45c9a7e723010c27c211f7fe3 · pytorch/pytorch · GitHub</a> .  There is no documentation or tutorial on this feature yet, but we hope to get to it in the future.</p>

Nanton: <p>Hi <a class="mention" href="/u/vasiliy_kuznetsov">@Vasiliy_Kuznetsov</a> ,</p>
<p>Appreciate your reply. I look forward to this feature as well.</p>
<p>Based on what we have now, I was wondering how to quantize a model with both CNN and LSTM . Is there any tutorial available?</p>
<p>Thanks</p>

jerryzh168: <aside class="quote no-group" data-username="Nanton" data-post="1" data-topic="159351">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/e5b9ba/48.png" class="avatar"> Nanton:</div>
<blockquote>
<p>If there is a model with CNN as backbone, LSTM as its head, how to quantize this whole model with post training quantization? It seems we can apply static quantization to CNN and dynamic quantization to LSTM( <a href="https://pytorch.org/docs/stable/quantization.html#quantization-mode-support" rel="noopener nofollow ugc">Quantization — PyTorch 1.12 documentation </a>). But not very sure how to deal with cases like above one.</p>
</blockquote>
</aside>
<p>we don’t have a tutorial yet. Are you using eager mode quantization? if so LSTM is supported by default, you can follow the original flow: <a href="https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">(beta) Static Quantization with Eager Mode in PyTorch — PyTorch Tutorials 2.1.1+cu121 documentation</a></p>
<p>fx graph mode quantization is not fully supporting static quantization for LSTM yet I think</p>

Komail_Mehrgan: <p>Simply don’t quantize LSTM part . try search quantize specific layer in pytorch.</p>

farough: <p>How can I quantize the whole model if I have ResNet Blocks followed by LSTM layer?  When I did not quantize the LSTM, the accuracy of the model was halved, and when I only quantized the LSTM by PTDQ, the acceleration of the model was negligible.</p>

HDCharles: <p>generally quantizing more modules isn’t going to improve the accuracy, if NOT quantizing the LSTMs is less accurate something is going very wrong. I would take a deeper look at it because it should be close to impossible for that to happen.</p>