Printing Quantized Model Weights

Joseph_Konan: <p>How do we print quantized model weights in PyTorch?</p>
<p>To print using normal PyTorch representation, I understand we use the following approach…</p>
<pre><code class="lang-auto">print_parameters = lambda model: [print(name, param.data) for name, param in model.named_parameters() if param.requires_grad]
</code></pre>
<p>Similarly, if I defined a model as follows…</p>
<pre><code class="lang-auto">import torch

class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = torch.nn.Conv2d(2, 3, 1, bias=False)
        self.conv2 = torch.nn.Conv2d(3, 1, 2, bias=False)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        return x

model = Model()
</code></pre>
<p>Then we can print using pretty straight-forward syntax…</p>
<pre><code class="lang-auto">print(model.conv1.weight)
print(model.conv2.weight)
</code></pre>
<p>However, both of these approaches fail when the model is converted to a quantized form. Specifically, after the following procedures…</p>
<pre><code class="lang-auto">model.qconfig = torch.quantization.default_qconfig
torch.quantization.prepare(model, inplace=True)
torch.quantization.convert(model, inplace=True)
</code></pre>
<p>Printing <code>model.conv1.weight</code> returns a method and the loop provided at the beginning does not print anything.</p>

hx89: <p>We have recently prototyped PyTorch Numeric Suite that can compare the numerics between float and quantized model. It’s in nightly build and will be in 1.6.</p>
<p>You can take a look at the following example to see how to compare the weights using it:<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/9c66c88e19e8c6cc90c59409a8074cc31cb4654c/test/quantization/test_numeric_suite.py#L81" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/9c66c88e19e8c6cc90c59409a8074cc31cb4654c/test/quantization/test_numeric_suite.py#L81" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/9c66c88e19e8c6cc90c59409a8074cc31cb4654c/test/quantization/test_numeric_suite.py#L81</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="71" style="counter-reset: li-counter 70 ;">
<li>        x = self.mymul.mul(x, x)</li>
<li>        x = self.myadd_relu.add_relu(x, x)</li>
<li>        w = self.my_scalar_add.add_scalar(x, -0.5)</li>
<li>        w = self.my_scalar_mul.mul_scalar(w, 0.5)</li>
<li>
</li><li>        w = self.dequant(w)</li>
<li>        return w</li>
<li>
</li><li>
</li><li>class TestEagerModeNumericSuite(QuantizationTestCase):</li>
<li class="selected">    def test_compare_weights(self):</li>
<li>        r"""Compare the weights of float and quantized conv layer</li>
<li>        """</li>
<li>
</li><li>        def compare_and_validate_results(float_model, q_model):</li>
<li>            weight_dict = compare_weights(</li>
<li>                float_model.state_dict(), q_model.state_dict()</li>
<li>            )</li>
<li>            self.assertEqual(len(weight_dict), 1)</li>
<li>            for k, v in weight_dict.items():</li>
<li>                self.assertTrue(v["float"].shape == v["quantized"].shape)</li>
</ol></code></pre>


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