Conversation
| image = latents | ||
| else: | ||
| latents = latents.to(self.vae.dtype) | ||
| torch_accelerator_module = getattr(torch, get_device(), torch.cuda) |
There was a problem hiding this comment.
Deadcode since min supported torch version is 2.6
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
| raise ValueError("Using streams for data transfer requires a CUDA device, or an Intel XPU device.") | ||
| if use_stream and onload_device.type == "cpu": | ||
| raise ValueError("Using streams for data transfer requires an accelerator onload device, got `cpu`.") | ||
| stream = TorchDeviceBackend(onload_device).Stream() if use_stream else None |
There was a problem hiding this comment.
What if the underlying device doesn't support streams?
| torch_accelerator_module = getattr(torch, get_device(), torch.cuda) | ||
| oom_error = ( | ||
| torch.OutOfMemoryError | ||
| if is_torch_version(">=", "2.5.0") | ||
| else torch_accelerator_module.OutOfMemoryError | ||
| ) |
There was a problem hiding this comment.
Safe because we pin on >=2.6.
| def empty_cache(self) -> None: | ||
| # Backends without a caching allocator (cpu, neuron) have nothing to clear. | ||
| empty_cache = getattr(self.module, "empty_cache", None) | ||
| if empty_cache is not None: | ||
| empty_cache() |
There was a problem hiding this comment.
Should we also warn in case empty_cache is None?
| """Tests for :class:`diffusers.utils.torch_utils.TorchDeviceBackend` on the CPU backend.""" | ||
|
|
||
| def test_module_resolves_from_str_and_torch_device(self): | ||
| import torch |
There was a problem hiding this comment.
I think we can keep all the imports at the top. They seem harmless.
| else torch.cuda.current_device() | ||
| ) | ||
| model.to(device) | ||
| model.to(get_device()) |
There was a problem hiding this comment.
Are all the sites of .to(device) being changed to .to(get_device() in this PR?
There was a problem hiding this comment.
Not everywhere. Just the places where we were inlining the device fallback. Can do a follow up pass to find the other places where this is applicable.
What does this PR do?
Support for torch device backends keeps growing, and each new backend adds a branch at every site that touches the device module. e.g. Adding NPU streams to group offloading (#14785) looks like this:
This PR introduces a
TorchDeviceBackendobject that automatically routes to the appropriate backend module for a given device type. e.g.Fixes # (issue)
Before submitting
self-reviewskill on the diff?documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.