If I rerun the code on the CPU only I directly get the error " CUDA error: device-side assert triggered" in line torch.sqrt(dist2). There are no nan values or values smaller than 0 in dist2. The error could result from the function itself since the knnquery is a C++ and Cuda implementation.