unslothai/unsloth · error · ValueError

GPU selection is unavailable on this host: {exc}

Error message

GPU selection is unavailable on this host: {exc}

What it means

Translating requested physical GPU ids into torch ordinals requires utils.hardware.hardware (get_parent_visible_gpu_ids / resolve_requested_gpu_ids), and importing it raised. Without the hardware layer the CUDA_VISIBLE_DEVICES mask is unknowable, so the load is refused with a reason instead of guessing a device.

Source

Thrown at studio/backend/core/inference/diffusion_device.py:188

    Raises ValueError for a selection this host cannot honour, so the load is refused with a
    reason rather than quietly running somewhere the user did not choose.

    ``allow_ranking = False`` drops only the free-VRAM probe, for a caller that must not open a
    CUDA context (the plan routes while a trainer holds the cards). Validation and translation
    still run -- they read the mask and nvidia-smi -- so the single card the UI sends resolves and
    only a multi-card pick comes back None.
    """
    wanted = sorted({int(gpu_id) for gpu_id in gpu_ids or ()})
    if not wanted:
        return None
    try:
        from utils.hardware.hardware import (
            get_parent_visible_gpu_ids,
            resolve_requested_gpu_ids,
        )
    except Exception as exc:  # noqa: BLE001 -- without the hardware layer the mask is unknowable
        raise ValueError(f"GPU selection is unavailable on this host: {exc}") from exc
    allowed = resolve_requested_gpu_ids(wanted)
    visible = get_parent_visible_gpu_ids()
    # Torch enumerates the parent-visible list in order, so its ordinal for a physical id is that
    # id's position in the mask. Unmasked, the layer reports range(physical count) and this is
    # the identity mapping.
    ordinals = [visible.index(gpu_id) for gpu_id in allowed if gpu_id in visible]
    if not ordinals:
        raise ValueError(
            f"Requested GPU {wanted} but none of them are visible to this process "
            f"(visible: {visible}). Clear the GPU selection to use the default device."
        )
    if len(ordinals) == 1:
        return ordinals[0]
    if not allow_ranking:
        return None

    def _free_vram(ordinal: int) -> int:
        try:

View on GitHub (pinned to 203007d190)

Solutions

  1. Run the backend from the repository root (or otherwise ensure utils.hardware.hardware is importable) and repair the install
  2. python -c 'from utils.hardware.hardware import get_parent_visible_gpu_ids' to reproduce and see the underlying error
  3. As a workaround, clear the GPU selection (empty gpu_ids) so the default device resolution runs without the hardware layer

Example fix

# before
ordinal = resolve_gpu_ordinal(gpu_ids=[0])  # utils.hardware not importable

# after
# fix the environment first:
#   cd <repo root> && python -c "from utils.hardware.hardware import get_parent_visible_gpu_ids"
ordinal = resolve_gpu_ordinal(gpu_ids=[0])
# or bypass selection:
ordinal = resolve_gpu_ordinal(gpu_ids=None)
Defensive patterns

Strategy: try-catch

Validate before calling

def gpu_selection_available() -> bool:
    try:
        from utils.hardware.hardware import get_parent_visible_gpu_ids  # noqa: F401
        return True
    except Exception:
        return False

Try / catch

try:
    ordinal = resolve_gpu_ordinal(gpu_ids, allow_ranking=True)
except ValueError as e:
    if "unavailable on this host" in str(e):
        fix_environment_or_run_without_gpu_selection()  # or pass gpu_ids=None
    raise

Prevention

When it happens

Trigger: Calling the GPU-selection resolve with non-empty gpu_ids on a host where importing utils.hardware.hardware raises — missing module, missing dependency of that module, broken install, or a sys.path/CWD that does not include the utils package.

Common situations: Running the backend from a different working directory or as a packaged binary that omits the utils package; a broken venv after a partial upgrade; utils.hardware itself raising on import due to a missing helper dependency (e.g. nvidia-smi wrapper deps) on an unusual host.

Related errors


AI-assisted analysis of unslothai/unsloth@203007d190 (2026-08-15). Data as JSON: /api/errors/49a70ba82429be3d. Report an issue: GitHub.