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.