Adding an offset to qint8 tensor

Hi,

I want to add certain offset or error value to a quantized tensor qint8, I want each value in quantized tensor to be updated by error times its value + old value. Below code shows this manipulation with normal tensor, can please anyone suggest how similar thing can be achieved for qint8.
@HDCharles @jerryzh168

import torch
t = torch.tensor([-1.0, 0.0, 1.0, 2.0])
error = 3
# add an error of 3%
t = t + (0.03)*t

qt = torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0, 10, torch.quint8)
# How can such error addition be done on quantized tensors

mathematically, this is the same as multipling the scale by (1+.03) or whatever your error is.

i.e. t.set_q_scale(t.q_scale()*(1.03)) but there’s no way to directly alter q_scale to my knowledge, set_q_scale doesn’t exist.

at worst you can do torch.quantize_per_tensor(t.dequantize(), t.q_scale()*(1+.03), t.q_zero_point(), t.dtype)

1 Like