# RuntimeError: CUDA error: invalid device ordinal

- **ID:** `pytorch/cuda-error-invalid-device-ordinal`
- **Domain:** pytorch
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The requested GPU device index (e.g., cuda:0) does not exist on the system, or the CUDA_VISIBLE_DEVICES environment variable restricts available devices.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| pytorch>=2.0.0 | active | — | — |
| cuda>=11.7 | active | — | — |

## Workarounds

1. **Check available GPU devices with `torch.cuda.device_count()` and list them using `nvidia-smi`. Then set the device to a valid index, e.g., `torch.device('cuda:0')` if at least one GPU exists.** (90% success)
   ```
   Check available GPU devices with `torch.cuda.device_count()` and list them using `nvidia-smi`. Then set the device to a valid index, e.g., `torch.device('cuda:0')` if at least one GPU exists.
   ```
2. **Verify the CUDA_VISIBLE_DEVICES environment variable. In bash, run `echo $CUDA_VISIBLE_DEVICES`. If set, ensure it contains valid indices, or unset it: `unset CUDA_VISIBLE_DEVICES`.** (85% success)
   ```
   Verify the CUDA_VISIBLE_DEVICES environment variable. In bash, run `echo $CUDA_VISIBLE_DEVICES`. If set, ensure it contains valid indices, or unset it: `unset CUDA_VISIBLE_DEVICES`.
   ```

## Dead Ends

- **** — The issue is configuration (device index), not installation. Reinstalling does not fix the index mismatch. (70% fail)
- **** — The environment variable is still incorrect after the change; users may set it to a non-existent device. (50% fail)
- **** — This still fails if no GPU is available; the root cause is the ordinal, not the device type. (60% fail)
