Could not run 'aten::add_.Tensor' with arguments from the 'QuantizedCPU' backend

username: arpan_banerjee01
cooked: <p>Hi All,</p>
<p>I was trying to quantize a resnet model. I thought of starting with some toy models, so, instead of coding one myself (also, I am slightly scared with the big model <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:">) I did the following</p>
<pre><code class="lang-auto">class CustomResNet(nn.Module): 

    def __init__(self,num_classes,feature_extract):

        super(CustomResNet, self).__init__()

        self.resnet = models.resnet18(pretrained=False)

        self.resnet.conv1 = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=(7,7), stride=(2,2)) #I am working only with b/w images

        self.resnet.num_classes = num_classes #This is specific to my test case

        set_parameter_requires_grad(self.resnet, feature_extract)

        self.quant = torch.quantization.QuantStub()

        self.dequant = torch.quantization.DeQuantStub()

    

    def forward(self, x):

      x = self.quant(x)

      x = self.resnet(x)

      x = self.dequant(x)

      return x
</code></pre>
<p>Then I quantized it and after printing the model, I could see that all the modules have been quantized.<br>
However, when I try to evaluate it using a sample dataset, then, I am getting the following error:</p>
<p><code>RuntimeError: Could not run 'aten::add_.Tensor' with arguments from the 'QuantizedCPU' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'aten::add_.Tensor' is only available for these backends: [CPU, CUDA, MkldnnCPU, SparseCPU, SparseCUDA, Meta, BackendSelect, Named, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradNestedTensor, UNKNOWN_TENSOR_TYPE_ID, AutogradPrivateUse1, AutogradPrivateUse2, AutogradPrivateUse3, Tracer, Autocast, Batched, VmapMode].</code></p>
<p>I have assumed that this is because the modules have been quantized, but the skip connections in the model and the corresponding operation have not been done.<br>
Can anyone kindly review and let me know, whether this is the correct assumption?</p>
<p>Regards,Arpan</p>

username: HDCharles
cooked: <p>Hi Arpan,</p>
<p>The error you are getting is more or less from here:</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/native_functions.yaml#L358-L365" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/native_functions.yaml#L358-L365" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/master/aten/src/ATen/native/native_functions.yaml#L358-L365</a></h4>
<pre class="onebox"><code class="lang-yaml"><ol class="start lines" start="358" style="counter-reset: li-counter 357 ;">
<li>- func: add_.Tensor(Tensor(a!) self, Tensor other, *, Scalar alpha=1) -&gt; Tensor(a!)</li>
<li>  device_check: NoCheck   # TensorIterator</li>
<li>  variants: method</li>
<li>  structured_delegate: add.out</li>
<li>  dispatch:</li>
<li>    SparseCPU, SparseCUDA: add_sparse_</li>
<li>    SparseCsrCPU: add_sparse_csr_</li>
<li>    MkldnnCPU: mkldnn_add_</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>i.e. that op seems to be for sparse tensors, not quantized ones. Its complaining because it doens’t list QuantizedCPU in the dispatch section of that op. I’m not sure exactly what you are doing for the system to be giving you that error without a full repro since the error doesn’t include the full stack trace. Your code shows the model bus is missing the actual quantization process you are using.</p>
<p>Otherwise, I’d recommend taking another look at the quantization docs/tutorials to make sure you’re doing things in the right order, it can be quite finnicky. That tutorial may be a better place to start and once you understand that, you can apply those techniques to your image classification problem.</p>
<p>tutorials<br>
<a href="https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html</a></p>
<p>you can find the other tutorials under Model Optimization in the sidebar within the pytorch tutorials pages.</p>

username: raghuramank100
cooked: <p>For tensor operations, you will need to use FloatFunctionals to ensure that quantization works correctly. See for example here: <a href="https://github.com/pytorch/vision/blob/master/torchvision/models/quantization/resnet.py#L25" class="inline-onebox" rel="noopener nofollow ugc">vision/resnet.py at master · pytorch/vision · GitHub</a></p>