It it possible to quantize a scripted model?

Nanton: <p>Hi, I was wondering if it is possible to quantize a scripted model with either eager or graph mode.</p>
<p>For example, there is a model scripted by <code>torch.jit.trace()</code> in advance. Technically, the scripted model should already both structure and weights. With that, is it possible to quantize it with either eager or graph mode?</p>
<p>Thanks all!</p>

Zafar: <p>cc <a class="mention" href="/u/jerryzh168">@jerryzh168</a> <a class="mention" href="/u/james_reed">@James_Reed</a></p>

jerryzh168: <p>we do have an api for Torchscript models before: <a href="https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/quantize_jit.py#L65" class="inline-onebox" rel="noopener nofollow ugc">pytorch/quantize_jit.py at master · pytorch/pytorch · GitHub</a> but it’s been de-prioritized and deprecated. The current recommendation is to quantize the model in python, with either eager mode quantization or FX Graph Mode Quantization.</p>

saurabh-shandilya: <p><a class="mention" href="/u/jerryzh168">@jerryzh168</a> How exactly FX Graph model quantization considered as replacement for jit_quantize? Can we import a torchscript model via symbolic tracing and then quantize in torch.fx?</p>
<p>What’s the reason for deprecating/deprioritizing jit_quantize?</p>

jerryzh168: <aside class="quote no-group" data-username="jerryzh168" data-post="3" data-topic="154623" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jerryzh168/48/15217_2.png" class="avatar"> jerryzh168:</div>
<blockquote>
<p>we do have an api for Torchscript models before: <a href="https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/quantize_jit.py#L65" rel="noopener nofollow ugc">pytorch/quantize_jit.py at master · pytorch/pytorch · GitHub </a> but it’s been de-prioritized and deprecated. The current recommendation is to quantize the model in python, with either eager mode quantization or FX Graph Mode Quantization.</p>
</blockquote>
</aside>
<p>FX Graph Mode Quantization is a replacement in the sense that it’s also the automatic or graph mode quantization flow provided by PyTorch Quantization, it can’t quantize a TorchScript model.</p>
<p>The reason for the change is because of internal strategy changes of PyTorch Compiler team, they decided to deprecate TorchScript and we have dependency on their support, so we have to switch. Actually we are in the middle of discussion to switch to a new IR due to upcoming changes to PyTorch Compiler/PyTorch Core, but that is still in discussion.</p>

saurabh-shandilya: <p><a class="mention" href="/u/jerryzh168">@jerryzh168</a> Thanks for a quick response. Do you mean whole torchscript IR is going to be deprecated or just the quantize_jit api? And Is there any public discussion about the new (hopefully export/FX friendly) IR?</p>
<p>Is there way to export torch.fx IR to a self-contained file/checkpoint with no code dependency , such that it can be loaded in a PyTorch environment for torch.fx transformation on a different machine with no access to original source?</p>

jerryzh168: <p>by deprecated I’m mostly talking about investment at Meta, I’m not sure what is the message to OSS actually. at Meta, both TorchScript IR and quantize_jit api are deprecated. at least for quantize_jit, we are not adding new features/fixes anymore, not exactly sure about Torchscript though.</p>
<p>“And Is there any public discussion about the new (hopefully export/FX friendly) IR?” which one are you referring to here? if you are referring to the next IR after FX IR, we do not have public information yet.</p>
<aside class="quote no-group" data-username="saurabh-shandilya" data-post="6" data-topic="154623">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saurabh-shandilya/48/51864_2.png" class="avatar"> saurabh-shandilya:</div>
<blockquote>
<p>Is there way to export torch.fx IR to a self-contained file/checkpoint with no code dependency , such that it can be loaded in a PyTorch environment for torch.fx transformation on a different machine with no access to original source?</p>
</blockquote>
</aside>
<p>Yes, what you described is torch.package (for packaging code into a self-contained format) and torch::deploy (for deploying code in a different environment).</p>
<p>So basically you can roughly think of TorchScript being decomposed into torch.fx, torch.package (<a href="https://pytorch.org/docs/stable/package.html" class="inline-onebox" rel="noopener nofollow ugc">torch.package — PyTorch 2.1 documentation</a>) and torch::deploy (<a href="https://pytorch.org/docs/stable/deploy.html" class="inline-onebox" rel="noopener nofollow ugc">torch::deploy has been moved to pytorch/multipy — PyTorch 2.1 documentation</a>), you can also checkout <a href="https://www.youtube.com/watch?v=vXbbaEZbrOI" rel="noopener nofollow ugc">https://www.youtube.com/watch?v=vXbbaEZbrOI</a> (starting at 1:01:49) for the presentation.</p>

jerryzh168: <p>I watched the presentation and looks like TorchScript is still in use, it’s not been deprecated</p>