How to multiply by a scalar with quantization observers

This simple script will illustrate what I’m trying to do

import torch
from torch import nn
from torch.quantization.quantize_fx import prepare_fx

class Model(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        B = x.shape[0]
        B = B * 1
        return x

model = Model()
model.eval()

qconfig = torch.quantization.get_default_qconfig("fbgemm")
model = prepare_fx(model, {"": qconfig})

x = torch.arange(4)

print(model(x))

Right now it raises AttributeError: 'int' object has no attribute 'numel' because of the B * 1 line.

By the way, the real example for what I’m trying to quantize is here.

Hi @Alexander_Soare, can you try this code out with the latest pytorch nightly build? I tried to reproduce the issue but wasn’t able to

      class Model(nn.Module):
            def __init__(self):
                super().__init__()

            def forward(self, x):
                B = x.shape[0]
                B = B * 1
                return x

        model = Model()
        model.eval()

        qconfig = torch.quantization.get_default_qconfig("fbgemm")
        model = prepare_fx(model, {"": qconfig})

        x = torch.arange(4)

        print(model(x))
        print(model)

Outputs

tensor([0, 1, 2, 3])
GraphModule()



def forward(self, x):
    getattr_1 = x.shape
    getitem = getattr_1[0];  getattr_1 = None
    mul = getitem * 1;  getitem = None
    return x
2 Likes

@supriyar you’re right, nightly works. It’s 1.8.1 that doesn’t