Bird
Raised Fist0
PyTorchml~10 mins

CUDA availability check in PyTorch - Interactive Code Practice

Choose your learning style10 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().

Practice

(1/5)
1. What does the function torch.cuda.is_available() check in PyTorch?
easy
A. If the CPU supports multi-threading
B. If a CUDA-capable GPU is available for PyTorch to use
C. If the PyTorch version is up to date
D. If the current script has internet access

Solution

  1. Step 1: Understand the purpose of torch.cuda.is_available()

    This function checks if PyTorch can access a CUDA-enabled GPU on the machine.
  2. Step 2: Differentiate from other hardware checks

    It does not check CPU features, PyTorch version, or internet access.
  3. Final Answer:

    If a CUDA-capable GPU is available for PyTorch to use -> Option B
  4. Quick Check:

    torch.cuda.is_available() = GPU availability [OK]
Hint: Check GPU presence with torch.cuda.is_available() [OK]
Common Mistakes:
  • Confusing CUDA availability with CPU features
  • Assuming it checks PyTorch version
  • Thinking it checks internet connection
2. Which of the following is the correct syntax to check CUDA availability in PyTorch?
easy
A. torch.cuda.is_available()
B. torch.cuda.available()
C. torch.is_cuda_available()
D. torch.cuda.check_available()

Solution

  1. Step 1: Recall the exact function name

    The correct function is torch.cuda.is_available() with this exact spelling and structure.
  2. Step 2: Identify incorrect options

    Other options have wrong function names or missing parts.
  3. Final Answer:

    torch.cuda.is_available() -> Option A
  4. Quick Check:

    Correct function name = torch.cuda.is_available() [OK]
Hint: Remember exact function: torch.cuda.is_available() [OK]
Common Mistakes:
  • Misspelling the function name
  • Omitting 'is_' prefix
  • Using incorrect method names
3. What will be the output of this code snippet if the machine has no CUDA GPU?
import torch
print(torch.cuda.is_available())
medium
A. False
B. Raises an error
C. null
D. True

Solution

  1. Step 1: Understand torch.cuda.is_available() output

    This function returns a boolean: true if CUDA GPU is available, false otherwise.
  2. Step 2: Consider no CUDA GPU present

    If no CUDA GPU exists, the function returns false without error.
  3. Final Answer:

    False -> Option A
  4. Quick Check:

    No GPU means torch.cuda.is_available() = False [OK]
Hint: No GPU means torch.cuda.is_available() returns False [OK]
Common Mistakes:
  • Expecting null instead of False
  • Thinking it raises an error without GPU
  • Confusing output with string 'False'
4. Identify the error in this code snippet that checks for CUDA availability:
import torch
if torch.cuda.is_available:
    device = torch.device('cuda')
else:
    device = torch.device('cpu')
medium
A. Should import 'torch.cuda' separately
B. Wrong device string 'cuda' instead of 'gpu'
C. Missing parentheses after 'is_available' function call
D. No error, code is correct

Solution

  1. Step 1: Check function call syntax

    torch.cuda.is_available is a function and must be called with parentheses: torch.cuda.is_available()
  2. Step 2: Verify device string correctness

    'cuda' is the correct device string, so no error there.
  3. Final Answer:

    Missing parentheses after 'is_available' function call -> Option C
  4. Quick Check:

    Function call needs () = Missing parentheses [OK]
Hint: Always add () when calling torch.cuda.is_available [OK]
Common Mistakes:
  • Forgetting parentheses on is_available
  • Using 'gpu' instead of 'cuda' as device string
  • Thinking torch.cuda needs separate import
5. You want your PyTorch model to run on GPU if available, otherwise on CPU. Which code snippet correctly sets the device?
hard
A. device = torch.device('gpu' if torch.cuda.is_available() else 'cpu')
B. device = torch.device('cpu' if torch.cuda.is_available() else 'cuda')
C. device = torch.device('cuda') if torch.cuda.is_available else torch.device('cpu')
D. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

Solution

  1. Step 1: Use torch.cuda.is_available() with parentheses

    The function must be called with parentheses to get a boolean result.
  2. Step 2: Correct device string and conditional expression

    Use 'cuda' when GPU is available, else 'cpu'. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') uses correct syntax and logic.
  3. Final Answer:

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') -> Option D
  4. Quick Check:

    Correct device setting = device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') [OK]
Hint: Use device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') [OK]
Common Mistakes:
  • Swapping 'cpu' and 'cuda' in conditional
  • Missing parentheses on is_available
  • Using 'gpu' instead of 'cuda' as device string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard