Fix RuntimeError: CUDA error: device-side assert triggered – PyTorch Tutorial

By | May 24, 2022

When we are running pytorch scripts, we may get this error: RuntimeError: CUDA error: device-side assert triggered. In this tutorial, we will introduce you how to fix it.

How to fix it?

The best way is to run pytorch scripts on cpu, not gpu. It will report where this error occur in script.

Usually, this error occurs when the shape of a pytorch tensor is not correct.

For example:

one_hot = torch.zeros_like(cosine)
one_hot.scatter_(1, label.view(-1, 1), 1.0)

If the shape of one_hot tensor is different from label tensor.

If this script is running on GPU, you will see:

RuntimeError: CUDA error: device-side assert triggered

However, if it is running on CPU, it will be reported:

RuntimeError: index 1 is out of bounds for dimension 1 with size 1

Fix RuntimeError: CUDA error: device-side assert triggered - PyTorch Tutorial

In order to fix this error, you should find where this error occur and check your script.