Im following all the steps but i still cant connect my pytorch to gpu

im using a nvidia geforce rtx 3050 gpu with cuda 12 as shown below

PS C:\Users\USER> nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Mon_Apr__3_17:36:15_Pacific_Daylight_Time_2023
Cuda compilation tools, release 12.1, V12.1.105
Build cuda_12.1.r12.1/compiler.32688072_0
PS C:\Users\USER> nvidia-smi
Fri Jul 26 21:51:51 2024
±----------------------------------------------------------------------------------------+
| NVIDIA-SMI 560.70 Driver Version: 560.70 CUDA Version: 12.6 |
|-----------------------------------------±-----------------------±---------------------+
| GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 3050 … WDDM | 00000000:01:00.0 Off | N/A |
| N/A 43C P8 3W / 60W | 0MiB / 8192MiB | 0% Default |
| | | N/A |
±----------------------------------------±-----------------------±---------------------+

±----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
±----------------------------------------------------------------------------------------+
PS C:\Users\USER> python --version
Python 3.12.0

my code shows its using cpu :import torch
import torch.nn as nn

Check if CUDA is available

device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)
print(f"Using device: {device}")

Define and move a model to GPU

class MyModel(nn.Module):
def init(self):
super(MyModel, self).init()
self.fc = nn.Linear(10, 5)

def forward(self, x):
return self.fc(x)

model = MyModel().to(device) # Ensure model is moved to GPU

Create an input tensor and move it to GPU

input_tensor = torch.randn(10, device=device) # Ensure tensor is created on GPU

Perform a forward pass through the model

output = model(input_tensor) # Forward pass through the model
print(output)

Check the device of the tensor

print(f"Input tensor device: {input_tensor.device}")

Check the device of the model parameters

for name, param in model.named_parameters():
print(f"Parameter ‘{name}’ device: {param.device}")

Using device: cpu
tensor([-0.5701, 0.1133, 0.0389, 0.0935, 0.8512], grad_fn=)
Input tensor device: cpu
Parameter ‘fc.weight’ device: cpu
Parameter ‘fc.bias’ device: cpu

fixed it by isolating my environment to ubuntu and everything’s fine now thank you all for your advice.

Have you installed it properly? Into your environment?

Also, what are the prints when you actually move the model and other stuff?

There are a few things you can do:

  1. Typing nvidia-smi just shows what Nvidia device you have, it doesn’t link at all to python. To see what version you currently support, type nvcc --version into the terminal.

  2. Also, you need to make sure your environment 1) has the GPU version of pytorch installed, and 2) you’ve activated the environment.

  3. You also need to make sure you’ve linked the correct PATH environment variables so you’re pointing to your correct cuda install. (Assuming this hasn’t been already on the install).

Could you post the outputs of torch.__version__ as well as torch.version.cuda?
Your locally installed CUDA toolkit won’t matter as the PyTorch binaries ship with their own CUDA dependencies.

Hi, I am also encountering a similar issue as OP regarding cuda 12.6, which I installed from nvidia. Then, I installed pytorch using conda via the command conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia. The output of torch.cuda.is_available() returns False. torch.__version__ returns 2.2.2 whereas torch.version.cuda doesn’t output anything.

The output of nvcc --version returns:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Wed_Aug_14_10:26:51_Pacific_Daylight_Time_2024
Cuda compilation tools, release 12.6, V12.6.68
Build cuda_12.6.r12.6/compiler.34714021_0

and the output of nvidia-smi is:

±----------------------------------------------------------------------------------------+
| NVIDIA-SMI 560.94 Driver Version: 560.94 CUDA Version: 12.6 |
|-----------------------------------------±-----------------------±---------------------+
| GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 3070 Ti WDDM | 00000000:01:00.0 On | N/A |
| 0% 34C P3 72W / 310W | 914MiB / 8192MiB | 4% Default |
| | | N/A |
±----------------------------------------±-----------------------±---------------------+

I have considered the possibility that my local installation of cuda is interfering with the conda installation somehow, perhaps through some environmental variables that need updating, but if what you say is true that the locally installed cuda doesn’t matter, then I am at a loss for what the issue could be. I have even restarted my computer after installation.

In this case it seems you have installed the CPU-only binary and should double check what the install log showed.

This doesn’t matter unless you build PyTorch from source or a custom CUDA extension since the PyTorch binaries with their own CUDA runtime dependencies.

Following the exact same steps but this time with pytorch-cuda=12.1 works now, so somehow this is a 12.4 issue.

I don’t think so as I don’t have any trouble installing the latest PyTorch 2.4.1 conda binary with CUDA 12.4 dependencies:

conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia
...
The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    blas-1.0                   |              mkl           6 KB
    brotli-python-1.0.9        |  py310h6a678d5_8         356 KB
    certifi-2024.7.4           |  py310h06a4308_0         158 KB
    cuda-cudart-12.4.127       |                0         198 KB  nvidia
    cuda-cupti-12.4.127        |                0        16.4 MB  nvidia
    cuda-libraries-12.4.0      |                0           2 KB  nvidia
    cuda-nvrtc-12.4.127        |                0        21.0 MB  nvidia
    cuda-nvtx-12.4.127         |                0          58 KB  nvidia
    cuda-opencl-12.6.68        |                0          26 KB  nvidia
    cuda-runtime-12.4.0        |                0           2 KB  nvidia
    cuda-version-12.6          |                3          16 KB  nvidia
    ffmpeg-4.3                 |       hf484d3e_0         9.9 MB  pytorch
    filelock-3.13.1            |  py310h06a4308_0          21 KB
    freetype-2.12.1            |       h4a9f257_0         626 KB
    gmp-6.2.1                  |       h295c915_3         544 KB
    gmpy2-2.1.2                |  py310heeb90bb_0         517 KB
    gnutls-3.6.15              |       he1e5248_0         1.0 MB
    idna-3.7                   |  py310h06a4308_0         130 KB
    intel-openmp-2023.1.0      |   hdb19cb5_46306        17.2 MB
    jinja2-3.1.4               |  py310h06a4308_0         278 KB
    jpeg-9e                    |       h5eee18b_3         262 KB
    lame-3.100                 |       h7b6447c_0         323 KB
    lcms2-2.12                 |       h3be6417_0         312 KB
    lerc-3.0                   |       h295c915_0         196 KB
    libcublas-12.4.2.65        |                0       308.8 MB  nvidia
    libcufft-11.2.0.44         |                0       190.5 MB  nvidia
    libcufile-1.11.1.6         |                0         1.0 MB  nvidia
    libcurand-10.3.7.68        |                0        51.7 MB  nvidia
    libcusolver-11.6.0.99      |                0       114.3 MB  nvidia
    libcusparse-12.3.0.142     |                0       179.6 MB  nvidia
    libdeflate-1.17            |       h5eee18b_1          64 KB
    libiconv-1.16              |       h5eee18b_3         759 KB
    libidn2-2.3.4              |       h5eee18b_0         146 KB
    libjpeg-turbo-2.0.0        |       h9bf148f_0         950 KB  pytorch
    libnpp-12.2.5.2            |                0       142.8 MB  nvidia
    libnvfatbin-12.6.68        |                0         888 KB  nvidia
    libnvjitlink-12.4.99       |                0        18.2 MB  nvidia
    libnvjpeg-12.3.1.89        |                0         3.0 MB  nvidia
    libpng-1.6.39              |       h5eee18b_0         304 KB
    libtasn1-4.19.0            |       h5eee18b_0          63 KB
    libtiff-4.5.1              |       h6a678d5_0         533 KB
    libunistring-0.9.10        |       h27cfd23_0         536 KB
    libwebp-base-1.3.2         |       h5eee18b_0         387 KB
    llvm-openmp-14.0.6         |       h9e868ea_0         4.4 MB
    markupsafe-2.1.3           |  py310h5eee18b_0          22 KB
    mkl-2023.1.0               |   h213fc3f_46344       171.5 MB
    mkl-service-2.4.0          |  py310h5eee18b_1          54 KB
    mkl_fft-1.3.10             |  py310h5eee18b_0         217 KB
    mkl_random-1.2.7           |  py310h1128e8f_0         352 KB
    mpc-1.1.0                  |       h10f8cd9_1          90 KB
    mpfr-4.0.2                 |       hb69a4c5_1         487 KB
    mpmath-1.3.0               |  py310h06a4308_0         834 KB
    nettle-3.7.3               |       hbbd107a_1         809 KB
    networkx-3.2.1             |  py310h06a4308_0         2.4 MB
    numpy-2.0.0                |  py310h5f9d8c6_1          11 KB
    numpy-base-2.0.0           |  py310hb5e798b_1         8.0 MB
    openh264-2.1.1             |       h4ff587b_0         711 KB
    openjpeg-2.5.2             |       he7f1fd0_0         371 KB
    pillow-10.4.0              |  py310h5eee18b_0         804 KB
    pysocks-1.7.1              |  py310h06a4308_0          28 KB
    pytorch-2.4.1              |py3.10_cuda12.4_cudnn9.1.0_0        1.34 GB  pytorch
    pytorch-cuda-12.4          |       hc786d27_6           7 KB  pytorch
    pytorch-mutex-1.0          |             cuda           3 KB  pytorch
    pyyaml-6.0.1               |  py310h5eee18b_0         180 KB
    requests-2.32.3            |  py310h06a4308_0         100 KB
    sympy-1.13.2               |  py310h06a4308_0        11.3 MB
    tbb-2021.8.0               |       hdb19cb5_0         1.6 MB
    torchaudio-2.4.1           |      py310_cu124         6.2 MB  pytorch
    torchtriton-3.0.0          |            py310       233.4 MB  pytorch
    torchvision-0.19.1         |      py310_cu124         8.2 MB  pytorch
    typing_extensions-4.11.0   |  py310h06a4308_0          59 KB
    urllib3-2.2.2              |  py310h06a4308_0         179 KB
    yaml-0.2.5                 |       h7b6447c_0          75 KB
    ------------------------------------------------------------
                                           Total:        2.84 GB
...
python -c "import torch; print(torch.__version__); print(torch.version.cuda); print(torch.cuda.is_available()); print(torch.randn(1).cuda())"
2.4.1
12.4
True
tensor([-0.0393], device='cuda:0')