Empty_strided not supported on quantized tensors

I’m trying to get the following code to work:

from torch.nn.modules import Module
class Sine(Module):
    def forward(self, z):
        return torch.sin(100 * z)
        
act_fn = Sine()
m = nn.Sequential(
    nn.Linear(1, 256, bias=True),
    act_fn,
    nn.Linear(256, 256, bias=True),
    act_fn,
    nn.Linear(256, 1, bias=True),
)

m = nn.Sequential(torch.ao.quantization.QuantStub(), 
                  *m, 
                  torch.ao.quantization.DeQuantStub())

m.train()
m.qconfig = torch.ao.quantization.get_default_qconfig('x86')
torch.ao.quantization.prepare_qat(m, inplace=True)
m.cuda()
n_epochs = 500
optim = torch.optim.AdamW(lr=1e-4, params=m.parameters(), weight_decay=1e-5)
for epoch in tqdm(range(n_epochs)):
    x = timestamps
    out = m(x)
    loss = F.mse_loss(out, ground_truth)
    optim.zero_grad()
    loss.backward()
    optim.step()

m.eval()
m.cpu()
torch.ao.quantization.convert(m, inplace=True)
res = m(timestamps.cpu())

The code runs until res = m(timestamps.cpu()). There, I get the error:

Traceback (most recent call last):
  File "qat.py", line 132, in <module>
    res = m(model_input.cpu())
  File "anaconda3/envs/py39/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "anaconda3/envs/py39/lib/python3.9/site-packages/torch/nn/modules/container.py", line 217, in forward
    input = module(input)
  File "anaconda3/envs/py39/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "qat.py", line 94, in forward
    return torch.sin(100 * z)
RuntimeError: empty_strided not supported on quantized tensors yet see https://github.com/pytorch/pytorch/issues/74540

How do I solve this? I’d really like to test quantization aware training on this net…

So it seems there are two issues: multiplying z by 100 throws the empty_strided not supported error.

If I remove this and only use torch.sin(z) I get the error that torch.sin is not implemented in QuantizedCPU backend.

Hi @lucala,

I don’t think torch.sin is implemented for QuantizedTensor, so that is expected. If the model works on cuda maybe try moving the timestamps to cuda and then moving the result to cpu.

I am not sure exactly what is causing the empty_strided not supported error.

Yeah the issue is that torch.sin isn’t implement for QuantizedTensors, if you replace the sine with ReLU or something that is supported, the code works without issue.

Similarly multiplying quantized tensors with arbitrary scalars isn’t supported.

Hello there.

I am dealing with the same issue with the operation r = 1.2 * torch.atan(u)
And this is the error:

RuntimeError: empty_strided not supported on quantized tensors yet see https://github.com/pytorch/pytorch/issues/74540

I am stuck on this. Could someone share any guidelines to multiply these values?

FYI: **u** is a tensor

Hi @Miguel_Campos,

As we mentioned above, torch.sin and torch.atan are not implemented yet for QuantizedTensors. You can convert the quantized representation to it’s float form using a DeQuantStub and then do your atan and then call into QuantStub to requantize the tensor.

1 Like

Thanks, I will give it a try =)