Duing Quantize Aware Training the model paramter datatype is float32

Rami_Ismael: <p>After calling torch quantization convert doing Quantize Aware Training. I loop through each parameter of the model. Each parameter in the model datatype is float32, not int8.</p>
<p>This show a quick and small example of Quantize Aware Training I did to understand how it work <a href="https://colab.research.google.com/drive/1O9qRw7s6W4uCNK7iDZxRy2N7uukqaL3d?usp=sharing" class="inline-onebox" rel="noopener nofollow ugc">Google Colab</a></p>

jerryzh168: <p>this is expected, quantization aware training only simulates the numerics of quantization with fake quantization modules, it’s not really quantizing the model, to get a real quantized model you’ll need to call convert on the model</p>

Rami_Ismael: <p>In my understanding, I called torch.ao.quantization ( model ) to transform my model int8. Second, how do I ensure my model is quantized, aware, and trained for int8?</p>

jerryzh168: <p>can you take a look at <a href="https://pytorch.org/docs/master/quantization.html#quantization-api-summary" class="inline-onebox" rel="noopener nofollow ugc">Quantization — PyTorch master documentation</a></p>

christophezei: <p>once you use the convert call a quick way to make sure your model is quantized is to check its size using:</p>
<pre><code class="lang-auto">param_size = 0
for param in converted_model.parameters():
    param_size += param.nelement() * param.element_size()
buffer_size = 0
for buffer in model.buffers():
    buffer_size += buffer.nelement() * buffer.element_size()

size_all_mb = (param_size + buffer_size) / 1024**2
</code></pre>
<p>so using this converted model you can do your inference and currently in pytorch it only support the 8bits quantization for the method convert.</p>

Rami_Ismael: <p>I used your approach. However, when I called the parameters in the converted model, is empty. While the buffer in the converted model return two elements where the model is a Resnet.</p>

christophezei: <p>I saw your code in colab this should fix your problem:</p>
<pre><code class="lang-auto">model = train_model(model=model, train_loader=train_loader, test_loader=test_loader, device=cuda_device, learning_rate=1e-3, num_epochs=5)

# then do this 
"""Convert"""
model.to("cpu")
model.eval()
model = torch.ao.quantization.convert(model , inplace = True)
</code></pre>
<p>try this and let me know if it works</p>

Rami_Ismael: <p>I made the modification where model is now the new train model.  The same problem  still exist. Getting the parameter of the converted model is None.</p>

christophezei: <p>I am not sure why this is happening I tried to debug your code a little bit but its hard to debug in colab environment especially while working on quantization. Anw I suggests that you try a very simple example at first just the one in the documentation and with this example you can understand more your questions</p>

Rami_Ismael: <p>I selected a single example from PyTorch Quantization Documentation. This example is the demonstration of PyTorch Quantize Aware Training. The converted model does have parameters.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1TX1JYdjTHDg36VXvnrIqntkyaKrz8Lzv?usp=sharing">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20220913-060100-RC00_473976297" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1TX1JYdjTHDg36VXvnrIqntkyaKrz8Lzv?usp=sharing" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/drive/1TX1JYdjTHDg36VXvnrIqntkyaKrz8Lzv?usp=sharing" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


christophezei: <p>Yes that’s normal, and this why I want you to print your new model size this can confirm that your model is compressed. Let me explain If you print a quantized tensor, you will see the floating point values, a scale and a zero point. The built-in representation is stored in integers, and you can see this with <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.int_repr.html#torch.Tensor.int_repr" rel="noopener nofollow ugc">int_rep()</a>.</p>
<pre><code class="lang-auto">model.name_of_the_quantized_layer.weight().int_repr()
</code></pre>
<p>check <a href="https://pytorch.org/blog/quantization-in-practice/#mapping-function" rel="noopener nofollow ugc">mapping formula</a> to understand how to represent your quantized weights either in floating or int so you can verify by yourself that the weights you are seeing are just displayed in different format. I hope I was clear if you still have questions let me know, I recommend that you read more about quantization in theory and on the process how it works in pytorch using the documentation before digging in the code so in this way you can understand more whats happening.</p>