# RuntimeError: CUDA error: invalid device function

- **ID:** `pytorch/cuda-error-invalid-device-function`
- **Domain:** pytorch
- **Category:** runtime_error
- **Error Code:** `CUDA_ERROR_INVALID_DEVICE_FUNCTION`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Compiled CUDA code contains a kernel call to a function that does not exist on the current GPU architecture, typically due to mismatched compute capabilities between compilation and runtime devices.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| torch>=1.9.0 | active | — | — |
| CUDA 11.x | active | — | — |
| CUDA 12.x | active | — | — |

## Workarounds

1. **export TORCH_CUDA_ARCH_LIST="7.5;8.0;8.6"  # Adjust to your GPU
pip install torch --no-binary torch
Or use a precompiled wheel for your architecture.** (90% success)
   ```
   export TORCH_CUDA_ARCH_LIST="7.5;8.0;8.6"  # Adjust to your GPU
pip install torch --no-binary torch
Or use a precompiled wheel for your architecture.
   ```
2. **import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'  # Ensure only one GPU is visible
# Then run your code** (70% success)
   ```
   import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'  # Ensure only one GPU is visible
# Then run your code
   ```
3. **torch.cuda.set_device(0)
# Ensure your model and data are on the same device** (65% success)
   ```
   torch.cuda.set_device(0)
# Ensure your model and data are on the same device
   ```

## Dead Ends

- **Reinstalling PyTorch with the same CUDA version** — The issue is not the CUDA toolkit version but the PTX/JIT compilation target architecture. Reinstalling without specifying TORCH_CUDA_ARCH_LIST does not change the compiled kernels. (60% fail)
- **Setting torch.backends.cudnn.enabled = False** — This disables cuDNN but does not affect the CUDA kernel dispatch that triggers the invalid function error. The error originates from a different layer. (80% fail)
- **Upgrading to the latest PyTorch version** — The error is architecture-specific; a newer PyTorch may still compile kernels for the same set of architectures. The root cause is the GPU not supporting the compiled kernel. (50% fail)
