Pytorch can not move tensor to cuda

System information:
PyTorch version: 1.5.1
CUDA used to build PyTorch: 9.2

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.10.2
Libc version: glibc-2.27

Python version: 3.8.10 (default, Jun 4 2021, 15:09:15) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-77-generic-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 3070 Laptop GPU
Nvidia driver version: 460.80
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.20.2
[pip3] pytorch3d==0.4.0
[pip3] torch==1.5.1
[pip3] torchvision==0.6.0a0+35d732a
[conda] blas 1.0 mkl
[conda] cudatoolkit 9.2 0
[conda] mkl 2021.2.0 h06a4308_296
[conda] mkl-service 2.3.0 py38h27cfd23_1
[conda] mkl_fft 1.3.0 py38h42c9631_2
[conda] mkl_random 1.2.1 py38ha9443f7_2
[conda] numpy 1.20.2 py38h2d18471_0
[conda] numpy-base 1.20.2 py38hfae3a4d_0
[conda] pytorch 1.5.1 py3.8_cuda9.2.148_cudnn7.6.3_0 pytorch
[conda] pytorch3d 0.4.0 py38_cu92_pyt151 pytorch3d
[conda] torchvision 0.6.1 py38_cu92 pytorch

When I run the following code, it simply gets stuck in the fourth statement.

import torch
device = torch.device("cpu" if False else "cuda")
a = torch.randn(10,10)
a =

The same code works properly if I use pytorch 1.7.1, cuda 11.0.

Since you are using an Ampere GPU (3070), you would need to use CUDA>=11.0, so the old PyTorch 1.5.1 release with CUDA9.2 won’t work.
Update to the latest release with CUDA11.1 and it should work.