Recommended practice for reusing quantized models?

talh: <p>Hello,</p>
<p>To my knowledge, reusing networks in Pytorch typically requires a network class definition and a weights file (i.e., <code>.pth</code>), which is saved and loaded using the <code>state_dict</code> mechanism.</p>
<p>In quantization, the problem is that the quantization process (e.g., post-training quantization) modifies the network class instance. This means that in order to reproduce the quantized model, either a programmer needs to define a new class that will be compatible with the modified instance, or the quantization process must be repeated on every new instance of the original FP32 model.</p>
<p>On Linux machines, it might be a reasonable workaround to post-training-quantize every new instance of the network. However, this scenario is not possible on Windows machines, since performing quantization is not currently supported on them.</p>
<p>What is therefore a recommended practice for really saving and loading an already-quantized network?</p>
<p>For example, is using Python’s pickle mechanism going to do the work? Can I quantize a network on Linux, save it using Pickle and reload it on a Windows machine? Is it a recommended approach?</p>

ynjiun_wang: <p>you might want to save the quantized_model using</p>
<pre><code class="lang-auto">save_torchscript_model(model=quantized_model, model_dir=model_dir, model_filename=quantized_model_filename)
</code></pre>
<p>and load the script in the other place for inference:</p>
<pre><code class="lang-auto">quantized_jit_model = load_torchscript_model(model_filepath=quantized_model_filepath, device=cpu_device)
quantized_jit_model.eval()
outputs = quantized_jit_model(inputs)
</code></pre>

HDCharles: <p>quantization previously didn’t work on windows because fbgemm wasn’t supported on windows. This is no longer the case so there should be no issue (<a href="https://github.com/pytorch/FBGEMM/issues/150" class="inline-onebox" rel="noopener nofollow ugc">Is this available on windows? · Issue #150 · pytorch/FBGEMM · GitHub</a>). Additionally, the issue wasn’t that you couldn’t do quantization on windows, its that windows didn’t have kernels for quantized ops, so you could still do the quantization, you just couldn’t run the quantized model.</p>
<p>note: torchscript wouldn’t get around this, but it is a good solution to not wanting to go through the quantization process each time you want to load a quantized model.</p>

talh: <p>I tried to post-training quantize a model on Windows using Pytorch version <code>1.9.1+cu102</code>, following a <a href="https://pytorch.org/tutorials/recipes/quantization.html#post-training-static-quantization" rel="noopener nofollow ugc">tutorial</a> on <a href="http://pytorch.org" rel="noopener nofollow ugc">pytorch.org</a>.</p>
<p>The application crashes when I run <code>torch.backends.quantized.engine = 'qnnpack'</code>. I concluded that it is just not supported on Windows.</p>
<p>Is there a way to overcome it?</p>

talh: <p>I was sure it also crashed when I used <code>fbgemm</code>, but somehow it hasn’t crashed now as I executed <code>torch.backends.quantized.engine = 'fbgemm'</code>. <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=10" title=":smiley:" class="emoji" alt=":smiley:"> Thanks!</p>

talh: <p>So I still cannot run a quantized model on Windows? I was finally able to quantize it, but now cannot inference it. I was able to inference the quantized models from Torchvision.</p>

Vasiliy_Kuznetsov: <p>what error are you seeing?</p>