RuntimeError: "normal_kernel_cpu" not implemented for 'Byte'

Having trouble with torchattacks

1 Like

torch.randn_like(images) will try to sample random values from a gaussian distribution using the passed images properties such as its dtype. I don’t think it would make a lot of sense to try to use unit8 for this sampling operation, so you might either want to transform the dtype of images to a floating point type or use another sampling (e.g. torch.randint).

@ptrblck is there a function like randint_like for for more ‘normal distribution’?

torch.randn_like will sample from a Normal/Gaussian distribution as explained above, so it this what you are looking for?

Thanks for the quick response and for the awesome work you guys are doing here!
So when we use quantized models, for example Llama model, and have weights that are uint8, if I want to use randn_like, I cannot since it’s int8. So what I am asking if there’s an equivalent function to randn_like that supports uint8 tensors. If not, I guess I need to case it first to float, do the randn_like, and case back to int?

This would be an approach, but note that you might also want to scale the normal distribution, since you would end up with a lot of 0s and 255s otherwise:

x = torch.randn(1000 * 1000 * 1000)

plt.hist(x.numpy())
plt.hist(x.byte().numpy())

Output:
image

image

1 Like