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),
    nn.Linear(256, 256, bias=True),
    nn.Linear(256, 1, bias=True),

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

m.qconfig = torch.ao.quantization.get_default_qconfig('x86')
torch.ao.quantization.prepare_qat(m, inplace=True)
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)

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.