Unable to quantize the model due to RuntimeError

Htut_Lynn_Aung: <p>I am trying to quantize a model which has upsampling layers at specific parts of network but unable to quantize it due to this Error.</p>
<blockquote>
<p>Expected a value of type ‘Tensor’ for argument ‘target_size’ but instead found type ‘List[int]’.<br>
Inferred ‘target_size’ to be of type ‘Tensor’ because it was not annotated with an explicit type.</p>
</blockquote>
<p>Implementation of the upsampling layer</p>
<pre><code class="lang-auto">class Upsample(nn.Module):
    def __init__(self):
        super(Upsample, self).__init__()

    def forward(self, x, target_size):
        # assert (x.data.dim() == 4)

        _, _, tH, tW = target_size[0], target_size[1], target_size[2], target_size[3]

        B = x.data.size(0)
        C = x.data.size(1)
        H = x.data.size(2)
        W = x.data.size(3)

        return x.view(B, C, H, 1, W, 1).expand(B, C, H, tH // H, W, tW // W).contiguous().view(B, C, tH, tW)

</code></pre>
<p>Upsampling function usage</p>
<pre><code class="lang-auto">up = self.upsample1(x7, downsample4.size())
</code></pre>
<p>Quantization code</p>
<pre><code class="lang-auto">model.qconfig = torch.quantization.get_default_qconfig('fbgemm')
print(model.qconfig)

torch.quantization.prepare(model, inplace=True)

print('Post Training Quantization Prepare : Inserting Observers')
print('\n Downsampling1 Block: After observer insertion \n\n', model.down1.conv1)

torch.quantization.convert(model, inplace=True)
print("Post Training Quantization : Convert Done!")
print("\n Downsampling1Block: After quantization \n\n", model.down1.conv1)
torch.jit.save(torch.jit.script(model), quantized_model_path)
</code></pre>
<p>This is my first time trying to quantize a model in pytorch so I am totally clueless on how to solve this. Thanks in advance.</p>

supriyar: <p>The error doesn’t seem related to quantization. Since the <code>upsample1</code> is a custom module, you cannot quantize it currently so the quant, dequant stubs need to be inserted in the model correctly.</p>

jerryzh168: <aside class="quote no-group" data-username="Htut_Lynn_Aung" data-post="1" data-topic="86042">
<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/htut_lynn_aung/48/6027_2.png" class="avatar"> Htut_Lynn_Aung:</div>
<blockquote>
<pre><code class="lang-auto"> def forward(self, x, target_size):
        # assert (x.data.dim() == 4)
</code></pre>
</blockquote>
</aside>
<p>this is an error from <code>torch.jit.script</code>, you can annotate the forward like following:</p>
<pre><code class="lang-auto"> def forward(self, x, target_size):
        # type : (Tensor, List[int]) -&gt; Tensor
        # assert (x.data.dim() == 4)
</code></pre>
<p>to make it scriptable.</p>

Htut_Lynn_Aung: <p>Thanks for the reply. Yes, that seems to be the case. I converted the target_size to torch tensor before passing it to upsample function so that kinda solved the problem.</p>
<p>From this</p>
<pre><code class="lang-auto">x7 = self.conv7(x6)
# UPSAMPLE
 up = self.upsample1(x7, downsample4.size())
</code></pre>
<p>to</p>
<pre><code class="lang-auto">x7 = self.conv7(x6)
# UPSAMPLE
featuremap_size = torch.tensor(downsample4.size())
up = self.upsample1(x7, featuremap_size, self.inference)
</code></pre>
<p>But the model that I am trying to optimize is YoloV4 and it has some activation functions(mish and softplus) not supported on pytorch’s quantization. Therefore, even after this solution, I was not able to quantize the model in the end.</p>

jerryzh168: <p>you can still surround these ops with DequantStub and QuantStub</p>