{"record":{"id":"b6bc5c2deab2c3ad","repo":"invoke-ai/InvokeAI","slug":"generation-devices-requested-device-str-but-o","errorCode":null,"errorMessage":"generation_devices requested '{device_str}', but only {torch.cuda.device_count()} CUDA device(s) are available (valid indices 0-{torch.cuda.device_count() - 1}).","messagePattern":"generation_devices requested '(.+?)', but only (.+?) CUDA device\\(s\\) are available \\(valid indices 0-(.+?)\\)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/util/devices.py","lineNumber":299,"sourceCode":"            else:\n                device_strs = [str(device) for device in cls._auto_generation_devices()]\n        elif not generation_devices:\n            return []\n        else:\n            device_strs = list(generation_devices)\n\n        devices: list[torch.device] = []\n        seen: set[str] = set()\n        for device_str in device_strs:\n            device = cls.normalize(device_str)\n            # Fail fast on a CUDA device that doesn't exist, rather than starting a worker pinned to\n            # it that only errors cryptically at the first tensor allocation. (\"auto\" only generates\n            # valid indices, so this just validates explicitly-configured devices.)\n            if device.type == \"cuda\":\n                if not torch.cuda.is_available():\n                    raise ValueError(f\"generation_devices requested '{device_str}', but no CUDA device is available.\")\n                if device.index is not None and device.index >= torch.cuda.device_count():\n                    raise ValueError(\n                        f\"generation_devices requested '{device_str}', but only {torch.cuda.device_count()} \"\n                        f\"CUDA device(s) are available (valid indices 0-{torch.cuda.device_count() - 1}).\"\n                    )\n            elif device.type == \"xpu\":\n                if not _xpu_is_available():\n                    raise ValueError(f\"generation_devices requested '{device_str}', but no XPU device is available.\")\n                if device.index is not None and device.index >= torch.xpu.device_count():\n                    raise ValueError(\n                        f\"generation_devices requested '{device_str}', but only {torch.xpu.device_count()} \"\n                        f\"XPU device(s) are available (valid indices 0-{torch.xpu.device_count() - 1}).\"\n                    )\n            elif device.type == \"mps\" and not torch.backends.mps.is_available():\n                raise ValueError(f\"generation_devices requested '{device_str}', but MPS is not available.\")\n            if str(device) not in seen:\n                seen.add(str(device))\n                devices.append(device)\n        return devices\n","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/util/devices.py#L281-L317","documentation":"Same fail-fast validation as the no-CUDA case, but CUDA exists and the requested index is out of range: the explicitly configured cuda:N has N >= torch.cuda.device_count(). Raised with a message listing the valid index range.","triggerScenarios":"Configuring generation_devices 'cuda:1' (or higher) on a single-GPU machine, or after removing a GPU, since torch.cuda.device_count() is smaller than the requested index.","commonSituations":"Copying configs between machines with different GPU counts; GPU removed/failed at boot; container given only a subset of GPUs (CUDA_VISIBLE_DEVICES=0) while config still names cuda:1.","solutions":["Set generation_devices to an in-range index (e.g. cuda:0) or 'auto'","Check torch.cuda.device_count() / nvidia-smi to see how many devices are visible","Fix CUDA_VISIBLE_DEVICES so the intended device index is exposed"],"exampleFix":"// before\ngeneration_devices: [\"cuda:1\"]  # single-GPU host\n// after\ngeneration_devices: [\"cuda:0\"]","handlingStrategy":"validation","validationCode":"import torch\nfor d in generation_devices:\n    if d.startswith(\"cuda:\"):\n        idx = int(d.split(\":\")[1])\n        if idx >= torch.cuda.device_count():\n            raise SystemExit(f\"{d} invalid: only {torch.cuda.device_count()} CUDA device(s)\")","typeGuard":"def cuda_index_in_range(device_str: str) -> bool:\n    import torch\n    if not device_str.startswith(\"cuda:\"):\n        return True\n    return int(device_str.split(\":\")[1]) < torch.cuda.device_count()","tryCatchPattern":"try:\n    devices = DeviceService.get_generation_devices(cfg.generation_devices)\nexcept ValueError as e:\n    if \"CUDA device(s) are available\" in str(e):\n        log.warning(\"Requested CUDA index out of range; using cuda:0/auto\")\n        devices = DeviceService.get_generation_devices([\"auto\"])\n    else:\n        raise","preventionTips":["Match device indices to the host's actual GPU count","Re-check config after hardware changes or GPU removal","Remember containers may expose a subset of GPUs via CUDA_VISIBLE_DEVICES","Prefer 'auto' in shared/portable configs"],"tags":["config","cuda","device-index","validation"],"backgroundTag":"invalid-cuda-device-index","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}