Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(devices): leave integrated GPUs out of generation_devices: auto on CUDA/ROCm

Alexander Eichhorn2cee2e7ad422
diff --git a/docs/src/content/docs/start-here/system-requirements.mdx b/docs/src/content/docs/start-here/system-requirements.mdx
index 3199b1c5e8..08845c6d1f 100644
--- a/docs/src/content/docs/start-here/system-requirements.mdx
+++ b/docs/src/content/docs/start-here/system-requirements.mdx
@@ -177,6 +177,12 @@ systems ROCm 10 supports. AMD lists Windows 11; the Invoke team has run it on Wi
come out wrong or generation fails. The startup log says whether they are on.
:::
+:::note[Integrated graphics]
+ ROCm lists the Radeon graphics built into Ryzen CPUs as a GPU of its own. When a discrete GPU is
+ present, Invoke leaves the integrated one out of `generation_devices: auto`; list it in
+ `generation_devices` to generate on it anyway.
+:::
+
:::note[Wide-head attention runs on the math kernel]
On ROCm builds Invoke routes attention whose head dimension exceeds 256 to PyTorch's math kernel.
The fused kernels return wrong output there on torch 2.13 / ROCm 7.2 (measured on a W7900), which
diff --git a/docs/src/generated/settings.json b/docs/src/generated/settings.json
index bc0598066c..99cc98557b 100644
--- a/docs/src/generated/settings.json
+++ b/docs/src/generated/settings.json
@@ -606,7 +606,7 @@
{
"category": "DEVICE",
"default": "auto",
- "description": "Devices to use for parallel generation. `auto` (the default) uses every available GPU, running one generation session per GPU concurrently and distributing jobs fairly across users \u2014 unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)",
+ "description": "Devices to use for parallel generation. `auto` (the default) uses every available GPU, except an integrated GPU next to a discrete one, running one generation session per GPU concurrently and distributing jobs fairly across users \u2014 unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)",
"env_var": "INVOKEAI_GENERATION_DEVICES",
"literal_values": [],
"name": "generation_devices",
diff --git a/invokeai/app/services/config/config_default.py b/invokeai/app/services/config/config_default.py
index 434b9f5ee1..4f632e9a6a 100644
--- a/invokeai/app/services/config/config_default.py
+++ b/invokeai/app/services/config/config_default.py
@@ -243,7 +243,7 @@ class InvokeAIAppConfig(BaseSettings):
# DEVICE
device: str = Field(default="auto", description="Preferred execution device. `auto` will choose the device depending on the hardware platform and the installed torch capabilities.<br>Valid values: `auto`, `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, `xpu:N` (where N is a device number)", pattern=r"^(auto|cpu|mps|xpu(:\d+)?|cuda(:\d+)?)$")
- generation_devices: Union[Literal["auto"], list[str]] = Field(default="auto", description="Devices to use for parallel generation. `auto` (the default) uses every available GPU, running one generation session per GPU concurrently and distributing jobs fairly across users — unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)")
+ generation_devices: Union[Literal["auto"], list[str]] = Field(default="auto", description="Devices to use for parallel generation. `auto` (the default) uses every available GPU, except an integrated GPU next to a discrete one, running one generation session per GPU concurrently and distributing jobs fairly across users — unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)")
offload_text_encoders_to_idle_gpus: bool = Field(default=True, description="When running on multiple GPUs, load text encoders onto a currently-idle GPU instead of the one running the denoise pipeline. This avoids churning the denoise model in and out of VRAM to make room for the encoder, and lets a cached encoder be reused across generations. Has no effect unless at least two `generation_devices` are configured and a GPU is idle; under full load encoders run on the session's own GPU as before.")
precision: PRECISION = Field(default="auto", description="Floating point precision. `float16` will consume half the memory of `float32` but produce slightly lower-quality images. The `auto` setting will guess the proper precision based on your video card and operating system.")
rocm_aotriton_experimental: ROCM_AOTRITON = Field(default="auto", description="Use AOTriton's fused (flash and memory-efficient) attention kernels on AMD GPUs that PyTorch marks experimental for them, such as the RX 7600/7700/7800 series, Ryzen AI 300/Max and the RX 9060 series, by setting TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL. Without them, attention on those GPUs runs on the slower math kernel, which also needs far more memory. `auto` turns them on only with a ROCm 10 build of PyTorch, and only when every GPU used for generation is one they were measured correct on (so far gfx1200, the RX 9060 series); `on` and `off` decide for any build and GPU. A TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL value already set in the environment takes precedence. Has no effect on other GPUs.")
diff --git a/invokeai/backend/util/devices.py b/invokeai/backend/util/devices.py
index 84c6208594..7d7abb5346 100644
--- a/invokeai/backend/util/devices.py
+++ b/invokeai/backend/util/devices.py
@@ -28,6 +28,42 @@ def _xpu_is_available() -> bool:
return hasattr(torch, "xpu") and torch.xpu.is_available()
+def device_is_integrated(device: torch.device) -> Optional[bool]:
+ """Return True/False if ``device`` is an integrated GPU, or None if it cannot be determined.
+
+ XPU asks Level Zero. A CUDA device answers through its properties, which on ROCm also report AMD's integrated
+ graphics: HIP enumerates the Radeon iGPU of a Ryzen desktop CPU next to a discrete card (measured: gfx1036 beside
+ an RX 9060 XT on Windows, ``is_integrated=1``). CPU and MPS are not GPUs that could be paired with a discrete
+ card, so they answer False.
+ """
+ if device.type == "xpu":
+ return xpu_device_is_integrated(device)
+ if device.type != "cuda":
+ return False
+ try:
+ index = device.index if device.index is not None else torch.cuda.current_device()
+ flag = getattr(torch.cuda.get_device_properties(index), "is_integrated", None)
+ except Exception:
+ return None
+ return None if flag is None else bool(flag)
+
+
+def _default_cuda_device() -> torch.device:
+ """The CUDA device ``device: auto`` means: the current one, unless it is an integrated GPU and a discrete one exists.
+
+ HIP may enumerate an APU's integrated graphics before the discrete card, and the current device is the first one
+ until something pins another, so without this an iGPU could become the default device.
+ """
+ current = torch.device("cuda", torch.cuda.current_device())
+ if device_is_integrated(current) is not True:
+ return CUDA_DEVICE
+ for index in range(torch.cuda.device_count()):
+ candidate = torch.device("cuda", index)
+ if device_is_integrated(candidate) is False:
+ return candidate
+ return CUDA_DEVICE
+
+
@deprecated("Use TorchDevice.choose_torch_dtype() instead.") # type: ignore
def choose_precision(device: torch.device) -> TorchPrecisionNames:
"""Return the string representation of the recommended torch device."""
@@ -119,7 +155,7 @@ class TorchDevice:
if app_config.device != "auto":
device = torch.device(app_config.device)
elif torch.cuda.is_available():
- device = CUDA_DEVICE
+ device = _default_cuda_device()
elif _xpu_is_available():
device = XPU_DEVICE
elif torch.backends.mps.is_available():
@@ -260,19 +296,20 @@ class TorchDevice:
def _auto_generation_devices(cls) -> list[torch.device]:
"""The device list `generation_devices: auto` expands to.
- Unlike CUDA, Level Zero enumerates the CPU's integrated GPU alongside any discrete card,
- so on the mainstream Arc configuration (iGPU + discrete Arc) `auto` would dispatch half
- the queue to the iGPU and make it a text-encoder borrow target. Drop integrated GPUs here.
+ Level Zero and ROCm enumerate the CPU's integrated GPU alongside any discrete card, so on
+ the mainstream configurations (iGPU + discrete Arc, Ryzen iGPU + discrete Radeon) `auto`
+ would dispatch half the queue to the iGPU and make it a text-encoder borrow target. Drop
+ integrated GPUs here.
- Two deliberate limits: a device whose type cannot be determined is kept (the Level Zero
- probe returns None, and narrowing on a guess is worse than the status quo), and a machine
- whose only GPU is integrated keeps it -- otherwise there would be nothing to generate on.
- An explicit `generation_devices` list is unaffected, so an iGPU can still be opted into.
+ Two deliberate limits: a device whose type cannot be determined is kept (the probe returns
+ None, and narrowing on a guess is worse than the status quo), and a machine whose only GPU
+ is integrated keeps it -- otherwise there would be nothing to generate on. An explicit
+ `generation_devices` list is unaffected, so an iGPU can still be opted into.
"""
integrated: list[torch.device] = []
remaining: list[torch.device] = []
for device in cls._all_available_devices():
- (integrated if xpu_device_is_integrated(device) is True else remaining).append(device)
+ (integrated if device_is_integrated(device) is True else remaining).append(device)
if not integrated or not remaining:
return integrated + remaining
InvokeAILogger.get_logger(__name__).info(
@@ -288,7 +325,8 @@ class TorchDevice:
- ``"auto"`` (the default) defers to an explicitly pinned legacy ``device:`` setting (an
upgraded install that pinned e.g. ``device: cuda:1`` to avoid its display GPU must not
silently start generating on every GPU); otherwise it expands to every visible CUDA
- device, or the single best available device (mps/cpu) when CUDA is unavailable.
+ device except integrated GPUs next to a discrete one (see `_auto_generation_devices`),
+ or the single best available device (mps/cpu) when CUDA is unavailable.
- An explicit list is normalized and deduplicated, with order preserved, and overrides the
legacy ``device:`` setting.
- ``None`` or an empty list yields an empty list; the caller decides the single-device fallback.
diff --git a/invokeai/frontend/api/openapi.json b/invokeai/frontend/api/openapi.json
index 708ca35e81..d625a6c165 100644
--- a/invokeai/frontend/api/openapi.json
+++ b/invokeai/frontend/api/openapi.json
@@ -58151,7 +58151,7 @@
}
],
"title": "Generation Devices",
- "description": "Devices to use for parallel generation. `auto` (the default) uses every available GPU, running one generation session per GPU concurrently and distributing jobs fairly across users \u2014 unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)",
+ "description": "Devices to use for parallel generation. `auto` (the default) uses every available GPU, except an integrated GPU next to a discrete one, running one generation session per GPU concurrently and distributing jobs fairly across users \u2014 unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)",
"default": "auto"
},
"offload_text_encoders_to_idle_gpus": {
diff --git a/invokeai/frontend/api/schema.ts b/invokeai/frontend/api/schema.ts
index a14f0c93d9..9e4687ed18 100644
--- a/invokeai/frontend/api/schema.ts
+++ b/invokeai/frontend/api/schema.ts
@@ -22586,7 +22586,7 @@ export type components = {
device?: string;
/**
* Generation Devices
- * @description Devices to use for parallel generation. `auto` (the default) uses every available GPU, running one generation session per GPU concurrently and distributing jobs fairly across users — unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)
+ * @description Devices to use for parallel generation. `auto` (the default) uses every available GPU, except an integrated GPU next to a discrete one, running one generation session per GPU concurrently and distributing jobs fairly across users — unless the legacy `device` setting is pinned to a specific device, in which case `auto` uses only that device (preserving configs that pinned `device` before multi-GPU support existed). Provide an explicit list (e.g. `[cuda:0, cuda:1]`) to use specific devices regardless of `device`, or a single-device list (e.g. `[cuda:0]`) to run serially. On systems without a GPU, `auto` resolves to the single `cpu`/`mps` device.<br>Valid values: `auto`, or a list whose entries are each `cpu`, `cuda`, `mps`, `xpu`, `cuda:N`, or `xpu:N` (where N is a device number)
* @default auto
*/
generation_devices?: "auto" | string[];
diff --git a/tests/backend/util/test_devices.py b/tests/backend/util/test_devices.py
index 1f01eb2df6..2b6a1468e4 100644
--- a/tests/backend/util/test_devices.py
+++ b/tests/backend/util/test_devices.py
@@ -11,7 +11,13 @@ import pytest
import torch
from invokeai.app.services.config import get_config
-from invokeai.backend.util.devices import TorchDevice, choose_precision, choose_torch_device, torch_dtype
+from invokeai.backend.util.devices import (
+ TorchDevice,
+ choose_precision,
+ choose_torch_device,
+ device_is_integrated,
+ torch_dtype,
+)
devices = ["cpu", "cuda:0", "cuda:1", "cuda:2", "mps"]
device_types_cpu = [("cpu", torch.float32), ("cuda:0", torch.float32), ("mps", torch.float32)]
@@ -81,6 +87,11 @@ def test_get_generation_devices_auto_expands_to_all_cuda():
with (
patch("invokeai.backend.util.devices.torch.cuda.is_available", return_value=True),
patch("invokeai.backend.util.devices.torch.cuda.device_count", return_value=3),
+ # Discrete GPUs, whatever the machine running the test has (its iGPU would be left out).
+ patch(
+ "invokeai.backend.util.devices.torch.cuda.get_device_properties",
+ _cuda_properties({0: False, 1: False, 2: False}),
+ ),
):
assert TorchDevice.get_generation_devices("auto") == [
torch.device("cuda:0"),
@@ -912,3 +923,69 @@ def test_disable_conv_benchmark_empty_cache_flips_torch_flag():
assert getter() is False
finally:
setter(original)
+
+
+# ===== integrated GPUs on CUDA/ROCm =========================================
+
+
+def _cuda_properties(integrated_map: dict[int, bool | None]):
+ """A `torch.cuda.get_device_properties` stand-in; None leaves `is_integrated` off the properties."""
+
+ def get_device_properties(index):
+ index = index.index if isinstance(index, torch.device) else index
+ flag = integrated_map[index]
+ return SimpleNamespace() if flag is None else SimpleNamespace(is_integrated=int(flag))
+
+ return get_device_properties
+
+
+def _auto_cuda_devices(integrated_map: dict[int, bool | None]):
+ config = get_config()
+ config.device = "auto"
+ with (
+ patch("invokeai.backend.util.devices.torch.cuda.is_available", return_value=True),
+ patch("invokeai.backend.util.devices.torch.cuda.device_count", return_value=len(integrated_map)),
+ patch("invokeai.backend.util.devices.torch.cuda.get_device_properties", _cuda_properties(integrated_map)),
+ ):
+ return TorchDevice.get_generation_devices("auto")
+
+
+def test_auto_excludes_the_ryzen_igpu_that_rocm_enumerates_next_to_a_radeon():
+ """Measured on Windows ROCm: an RX 9060 XT at index 0 and the Ryzen iGPU (gfx1036, `is_integrated=1`) at index 1.
+ `auto` must not give the iGPU half of the queue."""
+ assert _auto_cuda_devices({0: False, 1: True}) == [torch.device("cuda:0")]
+
+
+def test_auto_keeps_a_cuda_igpu_that_is_the_only_gpu():
+ assert _auto_cuda_devices({0: True}) == [torch.device("cuda:0")]
+
+
+def test_auto_keeps_cuda_devices_whose_properties_do_not_say():
+ assert _auto_cuda_devices({0: None, 1: None}) == [torch.device("cuda:0"), torch.device("cuda:1")]
+
+
+def test_device_is_integrated_is_unknown_when_the_properties_cannot_be_read():
+ with patch("invokeai.backend.util.devices.torch.cuda.get_device_properties", side_effect=RuntimeError("no GPU")):
+ assert device_is_integrated(torch.device("cuda", 0)) is None
+
+
+ ("integrated_map", "current", "expected"),
+ [
+ ({0: True, 1: False}, 0, torch.device("cuda", 1)),
+ ({0: False, 1: True}, 0, torch.device("cuda", 0)),
+ ({0: True}, 0, torch.device("cuda", 0)),
+ ({0: None, 1: False}, 0, torch.device("cuda", 0)),
+ ],
+ ids=["igpu-first", "discrete-first", "igpu-only", "unknown-stays"],
+)
+def test_auto_device_skips_an_igpu_enumerated_before_the_discrete_card(integrated_map, current, expected):
+ config = get_config()
+ config.device = "auto"
+ with (
+ patch("torch.cuda.is_available", return_value=True),
+ patch("torch.cuda.current_device", return_value=current),
+ patch("torch.cuda.device_count", return_value=len(integrated_map)),
+ patch("torch.cuda.get_device_properties", _cuda_properties(integrated_map)),
+ ):
+ assert TorchDevice.choose_torch_device() == expected