You will need to set the environment variable CUDA_LAUNCH_BLOCKING=1 before running any pytorch code.
So if it is simpler for your, you can set in the first line of your python script import os; os.environ["CUDA_LAUNCH_BLOCKING"] = "1"