Where deprecated torch.uint8 warning comes from, possibly from numpy conversion?

I have a rather large code base, and upon upgrading to pytorch 1.2 I started receiving thousands of warnings:

/pytorch/aten/src/ATen/native/IndexingUtils.h:20: UserWarning: indexing with dtype torch.uint8 is now deprecated, please use a dtype torch.bool instead.

Now, I was using torch.uint8 in one place, I replaced that with torch.bool. I was also using np.uint8 in one array which I later used to create PyTorch tensor. I changed that to np.bool_ now.

The problem is that I still have thousands of the same messages and as I am not explicitly using uint8 anywhere now, I wonder where it might come from? Does PyTorch still convert some numpy data to torch.uint8? I am converting a lot of numpy data to pytorch tensors. The problem is that there is no extra info or trace of this warning. Also, my code base is huge, so not possible to include it here.

Does anyone have any clue what might cause the error as I do not explicitly use torch.uint8 or numpy.uint8 anywhere?

Hi,

You can try to make python stop on warning to get the stack trace of where that happens or add something like that that prints a traceback for each warning.

3 Likes

Thanks! It was an np.array that used np.uint8 for booleans and that got automatically converted into torch.uint8 later when doing var.to(device), var being this np.array of type np.uint8. It was hard to trace as the numpy array was saved off to H5PY file and I only loaded this array from file using dataloader. Converting from np.uint8 to np.bool before doing var.to(device) solved the problem.

1 Like

Hello
I have the same problem, you could report the exact place where you changed from np.uint8 to np.bool, go to the h5py folder and change all the np.uint8 that I found but the problem still persists.

tanks!

h5py is the library that allows to save data in HDF5 format. I did not change anything in h5py library source code, but in the data file that I was loading with h5py. It had some data in it that was saved using np.uint8. If you can, change the file generation so that it will use np.bool instead. If not, you need to convert the data to np.bool or torch.bool when loading the data.

PS! If you are just running someone else’s code and do not care about maintaining it, then just suppress this warning when running:

python -W ignore::UserWarning pythonscript.py

It will also suppress all other UserWarnings though…

2 Likes

I’m not sure if this is still helpful to anyone, but I was getting this error in my code, so I changed from
torch.zeros(..., dtype=torch.uint8) to torch.zeros(..., dtype=torch.bool)