Skip to content

Fix CUDA availability check in get_device_properties()#45515

Open
Jah-yee wants to merge 1 commit intohuggingface:mainfrom
Jah-yee:fix/testing-utils-cuda-check
Open

Fix CUDA availability check in get_device_properties()#45515
Jah-yee wants to merge 1 commit intohuggingface:mainfrom
Jah-yee:fix/testing-utils-cuda-check

Conversation

@Jah-yee
Copy link
Copy Markdown

@Jah-yee Jah-yee commented Apr 19, 2026

Good day

Summary

This PR fixes a bug in src/transformers/testing_utils.py where get_device_properties() calls torch.cuda.get_device_capability() without first checking if a GPU is actually available.

Bug Description

When CUDA is installed on a system (e.g., torch.version.cuda is not None) but no GPU is present, calling torch.cuda.get_device_capability() raises a RuntimeError.

Fix

Changed the condition from:

if IS_CUDA_SYSTEM or IS_ROCM_SYSTEM:
    import torch
    major, minor = torch.cuda.get_device_capability()

to:

if (IS_CUDA_SYSTEM or IS_ROCM_SYSTEM) and torch.cuda.is_available():
    major, minor = torch.cuda.get_device_capability()

This ensures the GPU capability call only happens when a GPU is actually present, and also removes the redundant import torch statement that was inside the conditional block (the import is already at the top of the file).

Reference

Fixes #45341


Thank you for your attention. If there are any issues or suggestions, please leave a comment and I will address them promptly.

Warmly,
RoomWithOutRoof

Fixes a RuntimeError on systems where CUDA is installed but no GPU is available.
The function now checks torch.cuda.is_available() before calling
torch.cuda.get_device_capability(), preventing crashes when running
on CPU-only machines with CUDA-capable drivers.

Also removes redundant 'import torch' statement that was inside
the conditional block.
@github-actions
Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45515&sha=b5c6f9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A little bug in testing_utils.py

1 participant