# InternalError: CUDA_ERROR_INVALID_DEVICE: invalid device ordinal

- **ID:** `tensorflow/gpu-visible-devices-invalid-id`
- **Domain:** tensorflow
- **Category:** config_error
- **Error Code:** `GID`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

CUDA_VISIBLE_DEVICES environment variable references a GPU index that does not exist on the system.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| tensorflow 2.12 | active | — | — |
| tensorflow 2.13 | active | — | — |
| tensorflow 2.14 | active | — | — |
| cuda 11.8 | active | — | — |
| cuda 12.0 | active | — | — |

## Workarounds

1. **List available GPUs with nvidia-smi, then set CUDA_VISIBLE_DEVICES to a valid index. For example: export CUDA_VISIBLE_DEVICES=0 (if only one GPU exists). In Python: import os; os.environ['CUDA_VISIBLE_DEVICES'] = '0'; import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))** (95% success)
   ```
   List available GPUs with nvidia-smi, then set CUDA_VISIBLE_DEVICES to a valid index. For example: export CUDA_VISIBLE_DEVICES=0 (if only one GPU exists). In Python: import os; os.environ['CUDA_VISIBLE_DEVICES'] = '0'; import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))
   ```
2. **Remove CUDA_VISIBLE_DEVICES entirely to let TensorFlow auto-detect all GPUs: unset CUDA_VISIBLE_DEVICES** (85% success)
   ```
   Remove CUDA_VISIBLE_DEVICES entirely to let TensorFlow auto-detect all GPUs: unset CUDA_VISIBLE_DEVICES
   ```

## Dead Ends

- **Reinstalling CUDA drivers** — The issue is not driver installation but environment variable misconfiguration; reinstalling drivers does not fix the ordinal mapping. (95% fail)
- **Setting CUDA_VISIBLE_DEVICES to all GPUs (e.g., '0,1,2,3') blindly** — If the system has fewer GPUs than specified, the error persists; the correct approach is to query available devices first. (70% fail)
