The think is that your original code was modifying the temp tensor inplace (when doing temps[n,m] = xxx). The thing is that the original value of this tensor was needed for gradient computation, so inplace modification of it is not allowed !
Your other error is just that a Tensor is not on the gpu. make sure to only perform computation between tensors of the same type and on the same device.