Skip to content

Fix CPU-only import - #4504

Open
jloftin-nv wants to merge 1 commit into
pytorch:mainfrom
jloftin-nv:dev-jloftin-cpu-only-import
Open

Fix CPU-only import#4504
jloftin-nv wants to merge 1 commit into
pytorch:mainfrom
jloftin-nv:dev-jloftin-cpu-only-import

Conversation

@jloftin-nv

Copy link
Copy Markdown
Contributor

Description

import torch_tensorrt currently fails on hosts without an NVIDIA driver because several CUDA queries run at import / def time with no torch.cuda.is_available() guard.

Guard is_tegra_platform(), default_device(), and Device._current_device() so the package imports cleanly on CPU-only machines. GPU paths are unchanged once a driver is present.

Type of change

Please delete options that are not relevant and/or add your own.

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • [ X] My code follows the style guidelines of this project (You can use the linters)
  • [ X] I have performed a self-review of my own code
  • [ X] I have commented my code, particularly in hard-to-understand areas and hacks
  • [ X] I have made corresponding changes to the documentation
  • [X ] I have added tests to verify my fix or my feature
  • [ X] New and existing unit tests pass locally with my changes
  • [ X] I have added the relevant labels to my PR in so that relevant reviewers are notified

@meta-cla meta-cla Bot added the cla signed label Aug 18, 2026
@github-actions github-actions Bot added component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Aug 18, 2026
@github-actions
github-actions Bot requested a review from cehongwang August 18, 2026 21:47
@@ -96,4 +96,6 @@


def default_device() -> Device:
if not torch.cuda.is_available():
return Device(gpu_id=0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it gpu_id=0 instead of cpu?

@@ -145,7 +145,7 @@ def _from_torch_device(cls, torch_dev: torch.device) -> Device:

@classmethod
def _current_device(cls) -> Device:
dev_id = torch.cuda.current_device()
dev_id = torch.cuda.current_device() if torch.cuda.is_available() else 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@jloftin-nv
jloftin-nv force-pushed the dev-jloftin-cpu-only-import branch from 7c62c0a to 625d7a2 Compare August 20, 2026 23:51
@lanluo-nvidia lanluo-nvidia added this to the v2.15.0 milestone Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants