0
0
PyTorchml~10 mins

CUDA availability check in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if CUDA is available in PyTorch.

PyTorch
import torch

cuda_available = torch.cuda.[1]()
print(f"CUDA available: {cuda_available}")
Drag options to blanks, or click blank then click option'
Acuda_available
Bavailable
Cis_available
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.cuda.available() which does not exist.
Trying to access a property instead of calling a function.
2fill in blank
medium

Complete the code to get the name of the current CUDA device.

PyTorch
import torch

if torch.cuda.is_available():
    device_name = torch.cuda.get_device_[1](torch.cuda.current_device())
    print(f"Current CUDA device: {device_name}")
Drag options to blanks, or click blank then click option'
Adevice
Binfo
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.cuda.get_device_id() which does not exist.
Using torch.cuda.get_device_info() which is incorrect.
3fill in blank
hard

Fix the error in the code to correctly check CUDA availability and print a message.

PyTorch
import torch

if torch.cuda.[1]:
    print("CUDA is available")
else:
    print("CUDA is not available")
Drag options to blanks, or click blank then click option'
Ais_available
Bis_available()
Cavailable()
Davailable
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after is_available.
Using a non-existent attribute like available.
4fill in blank
hard

Fill both blanks to create a tensor on the CUDA device if available, otherwise on CPU.

PyTorch
import torch

device = torch.device("cuda" if torch.cuda.[1]() else "[2]")
tensor = torch.tensor([1, 2, 3], device=device)
print(tensor.device)
Drag options to blanks, or click blank then click option'
Ais_available
Bcpu
Ccuda
Dis_ready
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cuda' instead of 'cpu' as fallback device.
Using a non-existent function like is_ready.
5fill in blank
hard

Fill all three blanks to print the number of CUDA devices and the name of the first device if available.

PyTorch
import torch

if torch.cuda.[1]():
    count = torch.cuda.[2]()
    name = torch.cuda.get_device_[3](0)
    print(f"CUDA devices: {count}, First device: {name}")
else:
    print("No CUDA devices found.")
Drag options to blanks, or click blank then click option'
Ais_available
Bdevice_count
Cname
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.cuda.count() which does not exist.
Using torch.cuda.get_device_id() instead of get_device_name().