Post Training Static Quantization API still uses float weights instead of int?

ashmallow: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fb898a6d5a37d577a4cacf5726ac2a19bdc8b289.png" data-download-href="https://discuss.pytorch.org/uploads/default/fb898a6d5a37d577a4cacf5726ac2a19bdc8b289" title="Screen Shot 2022-08-09 at 3.38.16 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb898a6d5a37d577a4cacf5726ac2a19bdc8b289_2_393x500.png" alt="Screen Shot 2022-08-09 at 3.38.16 PM" data-base62-sha1="zTcwx6eLOS6mIgXVVvHl8xDuZId" width="393" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb898a6d5a37d577a4cacf5726ac2a19bdc8b289_2_393x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb898a6d5a37d577a4cacf5726ac2a19bdc8b289_2_589x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb898a6d5a37d577a4cacf5726ac2a19bdc8b289_2_786x1000.png 2x" data-dominant-color="EEEEEE"><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">Screen Shot 2022-08-09 at 3.38.16 PM</span><span class="informations">1174×1492 190 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>I find that floating point values are still being stored and used during inference in a quantized model</p>
<p>I understand that quantization is to convert model weights from floating point to integer weights (specifically float32 to quint8). When I print out the quantized inputs, outputs, and weights of an example given on the <a href="https://pytorch.org/docs/stable/quantization.html" rel="noopener nofollow ugc">Pytorch Quantization documentation</a>, you can see in the picture that they are stored as float32 numbers with scale and zero_point. I understand that the integer representation ( .int_repr() ) of these quantized tensors are calculated by dividing these float32 numbers by scale and adding zero_point, and then rounding. However, in my experimenting, I find that these integer representations of the tensors are not being used during inferencing. The float32 parts of the quantized input, weights and outputs are the only parts being used during inferencing without using these zero_point and scale associated with each tensor.</p>
<p>So my question is, what is the meaning of quantization if floating point values are still involved with the inferencing process, and how does that reduce the memory by a quarter of the original size if these floating point values still need to be stored in memory?</p>
<p>Thank you in advance. Any input would be hugely appreciated.</p>

ddang: <p>the integer values are used for computations (e.g., when convolving or during matmul). the values you’re printing are the dequantized values. to print the integer values, you need to call int_repr()</p>

ashmallow: <p>Hi David, Thank you for the reply. Yes I understand I can get the integer values using int_repr(). However, when I try to replicate the results produced by forward() manually, I find that int_repr() are not used for computation, because that would cause the output numbers to be in the 100,000s, which is way out of range of 8 bits. I find that if I use the dequantized float numbers to manually do the convolution / linear matrix multiplications, then I would create the same results produced by the forward().</p>
<p>As you can see in the image below, the first output is produced by me using the dequantized floating point values. (for the convolution I just did a simple multiply because it’s a 1x1 kernel with 1 channel).<br>
And the second cell outputs the results produced by the forward() when you run model_int8(input). You can see that they pretty much match up with each other, meaning that the forward() function also uses the floating point numbers for computation instead of the int_repr().<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/4/1409653cd0347e54f1da259b69215cb36c6d94d9.png" data-download-href="https://discuss.pytorch.org/uploads/default/1409653cd0347e54f1da259b69215cb36c6d94d9" title="Screen Shot 2022-08-10 at 2.45.05 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1409653cd0347e54f1da259b69215cb36c6d94d9_2_460x499.png" alt="Screen Shot 2022-08-10 at 2.45.05 PM" data-base62-sha1="2RfG6pynlaHTEoa266RSkV95REd" width="460" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1409653cd0347e54f1da259b69215cb36c6d94d9_2_460x499.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1409653cd0347e54f1da259b69215cb36c6d94d9_2_690x748.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1409653cd0347e54f1da259b69215cb36c6d94d9_2_920x998.png 2x" data-dominant-color="F8F8F8"><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">Screen Shot 2022-08-10 at 2.45.05 PM</span><span class="informations">1316×1428 102 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>For reference, the model and quantization method I used is from the example given by the Pytorch quantization documentation: <a href="https://pytorch.org/docs/stable/quantization.html" class="inline-onebox" rel="noopener nofollow ugc">Quantization — PyTorch 2.1 documentation</a>.</p>
<pre><code class="lang-auto">import torch

class M(torch.nn.Module):
    def __init__(self):
        super(M, self).__init__()
        # QuantStub converts tensors from floating point to quantized
        self.quant = torch.quantization.QuantStub()
        self.conv = torch.nn.Conv2d(1, 1, 1)
        self.relu = torch.nn.ReLU()
        # DeQuantStub converts tensors from quantized to floating point
        self.dequant = torch.quantization.DeQuantStub()

    def forward(self, x):
        # manually specify where tensors will be converted from floating
        # point to quantized in the quantized model
        x = self.quant(x)
        x = self.conv(x)
        x = self.relu(x)
        # manually specify where tensors will be converted from quantized
        # to floating point in the quantized model
        x = self.dequant(x)
        return x

# create a model instance
model_fp32 = M()

# model must be set to eval mode for static quantization logic to work
model_fp32.eval()

model_fp32.qconfig = torch.quantization.get_default_qconfig('fbgemm')

model_fp32_fused = torch.quantization.fuse_modules(model_fp32, [['conv', 'relu']])

model_fp32_prepared = torch.quantization.prepare(model_fp32_fused)

input_fp32 = torch.randn(4, 1, 4, 4)
model_fp32_prepared(input_fp32)

model_int8 = torch.quantization.convert(model_fp32_prepared)

res = model_int8(input_fp32)
</code></pre>
<p>Hope this all makes sense. And thanks for taking the time to answer my question.</p>

ashmallow: <p>I’ve attached the code and output I used to find out that they actually use the dequantized float values for computation during forward pass.</p>
<pre><code class="lang-auto">import torch

# define a floating point model where some layers could be statically quantized
class M(torch.nn.Module):
    def __init__(self):
        super(M, self).__init__()
        # QuantStub converts tensors from floating point to quantized
        self.quant = torch.quantization.QuantStub()
        self.conv = torch.nn.Conv2d(1, 1, 1)
        self.relu = torch.nn.ReLU()
        # DeQuantStub converts tensors from quantized to floating point
        self.dequant = torch.quantization.DeQuantStub()

    def forward(self, x):
        # manually specify where tensors will be converted from floating
        # point to quantized in the quantized model
        x = self.quant(x)
        x = self.conv(x)
        x = self.relu(x)
        # manually specify where tensors will be converted from quantized
        # to floating point in the quantized model
        x = self.dequant(x)
        return x

# create a model instance
model_fp32 = M()

# model must be set to eval mode for static quantization logic to work
model_fp32.eval()

# attach a global qconfig, which contains information about what kind
# of observers to attach. Use 'fbgemm' for server inference and
# 'qnnpack' for mobile inference. Other quantization configurations such
# as selecting symmetric or assymetric quantization and MinMax or L2Norm
# calibration techniques can be specified here.
model_fp32.qconfig = torch.quantization.get_default_qconfig('fbgemm')


# Fuse the activations to preceding layers, where applicable.
# This needs to be done manually depending on the model architecture.
# Common fusions include `conv + relu` and `conv + batchnorm + relu`
model_fp32_fused = torch.quantization.fuse_modules(model_fp32, [['conv', 'relu']])

# Prepare the model for static quantization. This inserts observers in
# the model that will observe activation tensors during calibration.
model_fp32_prepared = torch.quantization.prepare(model_fp32_fused)

# calibrate the prepared model to determine quantization parameters for activations
# in a real world setting, the calibration would be done with a representative dataset
input_fp32 = torch.randn(4, 1, 4, 4)
model_fp32_prepared(input_fp32)

# Convert the observed model to a quantized model. This does several things:
# quantizes the weights, computes and stores the scale and bias value to be
# used with each activation tensor, and replaces key operators with quantized
# implementations.
model_int8 = torch.quantization.convert(model_fp32_prepared)

# hooks to retrieve inputs, outputs and weights of conv layer (fused conv + relu)
conv_inputs = []
conv_weights = []
conv_outputs = []
hooks = []
for hook in hooks:
    hook.remove()
def hook_fn(m, i, o):
    global conv_inputs, conv_outputs, conv_weights
    conv_inputs = i[0] # [0] because conv_inputs is a tuple and we only care about the first item
    conv_weights = m.weight()
    conv_outputs = o
hooks.append(model_int8.conv.register_forward_hook(hook_fn))

# run forward pass
res = model_int8(input_fp32)

relu = torch.nn.ReLU()

# Manually dequantize and manually compute output 
# Note that convolution is just a simple multiplication because it's a 1x1 kernel with 1 channel
conv_float_input = (conv_inputs.int_repr().int() - conv_inputs.q_zero_point())*conv_inputs.q_scale()
conv_float_weight = conv_weights.int_repr() * conv_weights.q_per_channel_scales()
conv_float_output = conv_float_input * conv_float_weight + model_int8.conv.bias()
manual_output_1 = (relu(conv_float_output / model_int8.conv.scale)).round()
print("manual_output_1:\n", manual_output_1)

# Use built-in dequantize() and manually compute output
# Note that convolution is just a simple multiplication because it's a 1x1 kernel with 1 channel
conv_float_output = conv_inputs.dequantize()*conv_weights.dequantize() + model_int8.conv.bias()
manual_output_2 = relu((conv_float_output / model_int8.conv.scale).round())
print("manual_output_2:\n", manual_output_2)

# Output produced by forward() pass
print("output produced by forward():\n", conv_outputs.int_repr())

# print the difference between manual input and output generated by forward (0.0 means no difference)
print("manual_output_1 and conv_outputs differ by: ", end = "")
print((manual_output_1 - conv_outputs.int_repr()).sum().item())

print("manual_output_2 and conv_outputs differ by: ", end = "")
print((manual_output_2 - conv_outputs.int_repr()).sum().item())
</code></pre>
<p>Here’s the output (will differ every time because input is randomly generated):</p>
<pre><code class="lang-auto">manual_output_1:
 tensor([[[[ 96.,   0.,  15.,  15.],
          [104.,   0.,   0.,  64.],
          [  0.,   0.,   0.,  31.],
          [  0.,   0.,   0.,   0.]]],


        [[[  0.,  58.,   0.,   0.],
          [ 54.,   0.,  35.,  23.],
          [  0.,   0.,   0.,  17.],
          [ 23.,   8., 125.,  83.]]],


        [[[  0., 125.,   0.,  77.],
          [  0.,  69., 127.,   4.],
          [  8.,   0.,   0.,   0.],
          [ 58.,   0.,  35.,   0.]]],


        [[[ 46.,  15.,  52.,   0.],
          [  4.,  27.,  69.,  29.],
          [  0.,  56.,   0.,   2.],
          [  0.,  54.,  33.,  21.]]]], dtype=torch.float64,
       grad_fn=&lt;RoundBackward0&gt;)
manual_output_2:
 tensor([[[[ 96.,   0.,  15.,  15.],
          [104.,   0.,   0.,  64.],
          [  0.,   0.,   0.,  31.],
          [  0.,   0.,   0.,   0.]]],


        [[[  0.,  58.,   0.,   0.],
          [ 54.,   0.,  35.,  23.],
          [  0.,   0.,   0.,  17.],
          [ 23.,   8., 125.,  83.]]],


        [[[  0., 125.,   0.,  77.],
          [  0.,  69., 127.,   4.],
          [  8.,   0.,   0.,   0.],
          [ 58.,   0.,  35.,   0.]]],


        [[[ 46.,  15.,  52.,   0.],
          [  4.,  27.,  69.,  29.],
          [  0.,  56.,   0.,   2.],
          [  0.,  54.,  33.,  21.]]]], grad_fn=&lt;ReluBackward0&gt;)
output produced by forward():
 tensor([[[[ 96,   0,  15,  15],
          [104,   0,   0,  64],
          [  0,   0,   0,  31],
          [  0,   0,   0,   0]]],


        [[[  0,  58,   0,   0],
          [ 54,   0,  35,  23],
          [  0,   0,   0,  17],
          [ 23,   8, 125,  83]]],


        [[[  0, 125,   0,  77],
          [  0,  69, 127,   4],
          [  8,   0,   0,   0],
          [ 58,   0,  35,   0]]],


        [[[ 46,  15,  52,   0],
          [  4,  27,  69,  29],
          [  0,  56,   0,   2],
          [  0,  54,  33,  21]]]], dtype=torch.uint8)
manual_output_1 and conv_outputs differ by: 0.0
manual_output_2 and conv_outputs differ by: 0.0
</code></pre>

Y_Simson: <p>I have the same issue. In the forward pass the quantized version is using floating point values for inference.</p>
<p>Any conclusions regarding this?</p>

Francisco_Romaldo_M: <p>I have recreated the result you can find it on my blog</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://franciscormendes.github.io/2024/05/16/quantization-1/">
  <header class="source">
      <img src="https://franciscormendes.github.io/img/favicon.svg" class="site-icon" width="256" height="256">

      <a href="https://franciscormendes.github.io/2024/05/16/quantization-1/" target="_blank" rel="noopener nofollow ugc" title="05:00AM - 16 May 2024">Hexo – 16 May 24</a>
  </header>

  <article class="onebox-body">
    <img width="" height="" src="http://example.com/img/og_image.png" class="thumbnail">

<h3><a href="https://franciscormendes.github.io/2024/05/16/quantization-1/" target="_blank" rel="noopener nofollow ugc">A Manual Implementation of Quantization in PyTorch</a></h3>

  <p>IntroductionThe packaging of extremely complex techniques inside convenient wrappers in PyTorch often makes quick implementations fairly easy, it also removes the need to understand the inner workings</p>


  </article>

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

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


HDCharles: <p>Quantization isn’t as simple as converting your model weights to integers. Its a complex system that represents float values using integers, alongside kernels that can utilize various tricks to make the resulting operations faster than if they were performed using the represented floats.</p>
<p>in this you have a scale and a zero point which, along side the integer matrix, defines which float values your quantized matrix represents. (Integer_value-zero_point)*scale=float_value</p>