pytorch - 💡(How to fix) Fix Add a lazy import hook for autoloading? [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#180712Fetched 2026-04-18 05:51:25
View on GitHub
Comments
0
Participants
1
Timeline
11
Reactions
0
Participants
Timeline (top)
labeled ×5mentioned ×3subscribed ×3

Error Message

RuntimeError: Expected one of cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip, ve, fpga, maia, xla, lazy, vulkan, mps, meta, hpu, mtia, privateuseone device type at start of device string: tpu

Root Cause

This doesn't make it clear that the problem is that torch_tpu is deliberately choosing not to load the backend because the TPU device is not available. We could throw an error instead of choosing not to load, but that means any time you install a backend on machine where it can't load, you would be unable to import torch at all.

Code Example

RuntimeError: Expected one of cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip,
              ve, fpga, maia, xla, lazy, vulkan, mps, meta, hpu, mtia, privateuseone
              device type at start of device string: tpu
RAW_BUFFERClick to expand / collapse

🚀 The feature, motivation and pitch

This probably lands somewhere between #180343 and #180345, not sure if it is justified in having its own issue, but in TorchTPU we have set up autoloading of our tpu module, but we only load it if a TPU is present, since otherwise it won't work anyway. The problem is that this leads to a pretty bad error message, since what ends up happening is that when you do torch.device("tpu") it gives this error:

RuntimeError: Expected one of cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip,
              ve, fpga, maia, xla, lazy, vulkan, mps, meta, hpu, mtia, privateuseone
              device type at start of device string: tpu

This doesn't make it clear that the problem is that torch_tpu is deliberately choosing not to load the backend because the TPU device is not available. We could throw an error instead of choosing not to load, but that means any time you install a backend on machine where it can't load, you would be unable to import torch at all.

Ideally, we would be able to register a hook that would be called the first time someone tries to actually use our device or module, so that we have a stronger signal that an error should be thrown than "User has our package installed" (which may just be because it ended up in a requirements.txt that doesn't sufficiently distinguish between environments with and without a TPU).

This would have also helped us with an earlier problem we had where we wanted to defer full initialization until first use. We've since basically moved that logic to the C++ layer, but it would have been very convenient if instead of registering our backend, we could register lazy initialization hooks.

Alternatives

With unlimited backends like #180345, I would be a lot more comfortable writing a generic backend that throws an error whenever you try to load something from it to explain that no TPU is available.

Alternatively, if there were a simple API to explicitly register "This name is valid but an error should be thrown when you try to use it", that would solve our immediate use case as well.

Additional context

No response

cc @bdhirsh

extent analysis

TL;DR

Implement a lazy initialization hook to register the TPU backend only when it is actually used, or provide a clear error message when the TPU device is not available.

Guidance

  • Consider implementing a custom error message when torch.device("tpu") is called without a TPU device present, to clearly indicate the issue.
  • Explore the possibility of registering a hook that is called when the TPU device or module is first used, to provide a stronger signal for error handling.
  • Investigate the use of a generic backend that throws an error when trying to load something from it, as an alternative solution.
  • Look into the possibility of explicitly registering a valid backend name that throws an error when used, as a potential solution.

Example

No code snippet is provided due to the lack of specific implementation details in the issue.

Notes

The ideal solution would require changes to the underlying PyTorch framework to support lazy initialization hooks or explicit registration of error-throwing backends.

Recommendation

Apply a workaround, such as implementing a custom error message or using a generic backend that throws an error, as a temporary solution until a more robust fix can be implemented.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix Add a lazy import hook for autoloading? [1 participants]