I would generally recommend to update to the latest PyTorch release and, if possible, also the NVIDIA drivers.
If this error is raised randomly, it could point towards a setup issue where the GPU is dropped.
This could indicate either a broken driver or e.g. overheating of the GPU, which will shut itself down to avoid damage. The output of dmesg might show additional XID error codes, which might be helpful for further debugging.