The error occurs because device="0" is incorrect syntax. For PyTorch, you should use:
python
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
Corrected full code:
python
from transformers import pipeline
import torch
# Check if GPU is available
gpu_available = torch.cuda.is_available()
device_name = torch.cuda.get_device_name(0) if gpu_available else "No GPU found"
print(f"GPU Available: {gpu_available}")
print(f"GPU Name: {device_name}")
# Use the correct device specification
device = torch.device("cuda:0" if gpu_available else "cpu")
model = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
response = model("Your text to summarize here. Make sure it's long enough for the model to work with.")
print(response)
Additional troubleshooting if GPU still not detected:
Verify CUDA is properly installed:
Run nvcc --version in command prompt
If not found, install CUDA Toolkit from NVIDIA
Check for common issues:
Your virtual environment might not have access to system CUDA
Try installing PyTorch without --index-url (let pip find the right version)
Make sure you're not using WSL (Windows Subsystem for Linux) unless properly configured
(Adjust cu118 to match your driver's supported version)
User
I'm still getting the following error: GPU Available: False
GPU Name: No GPU found
Device set to use cpu
Traceback (most recent call last):
File "D:\Install\AI\Langchain-Transformers-Python\pythonProject1.venv\main.py", line 14, in
print(response)
^^^^^^^^
NameError: name 'response' is not defined
Process finished with exit code 1
Assistant
The error you're seeing now (NameError: name 'response' is not defined) is different from your original issue and occurs because the code failed before reaching the print(response) line. The root problem is still that PyTorch cannot detect your GPU (GPU Available: False).
Let's fix this step by step:
1. First, verify your GPU and CUDA setup:
Run these commands in order:
python
import torch
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"CUDA version: {torch.version.cuda}")
print(f"GPU name: {torch.cuda.get_device_name(0)}")
else:
print("CUDA not available")
2. Check your NVIDIA driver and CUDA compatibility:
Open Command Prompt and run:
code
nvidia-smi
Note the CUDA version at the top (this is the maximum version your driver supports)
Make sure your GPU is listed
Compare this with PyTorch's CUDA version from step 1
3. Reinstall PyTorch with the correct CUDA version:
Based on your nvidia-smi output, install the matching PyTorch version. For example, if your driver supports CUDA 11.8:
After running the PyTorch installation command for CUDA 12.8, I can confirm that the issue has been resolved. Thanks a lot for your help. Much appreciated.
Assistant
That's great to hear! 🎉 I'm glad the CUDA 12.8 installation fixed your issue and PyTorch is now detecting your GPU properly.
Final Notes for Future Reference:
Always match PyTorch's CUDA version with your NVIDIA driver's supported version (from nvidia-smi).
Use torch.cuda.is_available() to verify GPU detection before running GPU-dependent code.
Correct device specification in Hugging Face pipelines:
from transformers import pipeline
import torch
# Check GPU status
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"CUDA version: {torch.version.cuda}")
print(f"GPU name: {torch.cuda.get_device_name(0)}")
device = torch.device("cuda")
else:
print("Using CPU instead")
device = torch.device("cpu")
# Create pipeline (with shorter text for testing)
model = pipeline(
"summarization",
model="facebook/bart-large-cnn",
device=device
)
# Make sure to actually call the model and store the result
input_text = """Artificial intelligence is transforming many industries.
Machine learning, a subset of AI, enables systems to learn from data.
Deep learning uses neural networks for complex pattern recognition."""
result = model(input_text, max_length=130, min_length=30, do_sample=False)
print("\nSummary:")
print(result[0]['summary_text'])
If GPU still not detected:
Verify CUDA Toolkit installation:
Download and install CUDA Toolkit from NVIDIA's website
Make sure to select the version that matches your driver
Check environment variables:
Ensure CUDA_PATH is set in your system environment variables
Should point to something like C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8