# RuntimeError: cusolver error: CUSOLVER_STATUS_INTERNAL_ERROR when computing SVD of a singular matrix

- **ID:** `cuda/cusolver-internal-error-on-svd`
- **Domain:** cuda
- **Category:** runtime_error
- **Error Code:** `CUSOLVER_STATUS_INTERNAL_ERROR`
- **Verification:** ai_generated
- **Fix Rate:** 76%

## Root Cause

cuSolver's SVD routine (gesvdj or gesvd) fails internally when the input matrix is exactly singular or has NaN/inf values, causing a buffer overflow or division by zero in the iterative solver.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| CUDA 12.4 | active | — | — |
| cuSolver 11.5.1 | active | — | — |
| PyTorch 2.3.0 | active | — | — |

## Workarounds

1. **Preprocess the matrix to remove exact singularities: add a small regularization term (e.g., A += 1e-8 * torch.eye(n, device=A.device)) before calling torch.linalg.svd. Example: A_reg = A + 1e-8 * torch.eye(A.size(0), device=A.device); U, S, V = torch.linalg.svd(A_reg).** (85% success)
   ```
   Preprocess the matrix to remove exact singularities: add a small regularization term (e.g., A += 1e-8 * torch.eye(n, device=A.device)) before calling torch.linalg.svd. Example: A_reg = A + 1e-8 * torch.eye(A.size(0), device=A.device); U, S, V = torch.linalg.svd(A_reg).
   ```
2. **Use torch.linalg.lstsq instead of SVD for solving least-squares problems, as it handles singular matrices more robustly.** (78% success)
   ```
   Use torch.linalg.lstsq instead of SVD for solving least-squares problems, as it handles singular matrices more robustly.
   ```

## Dead Ends

- **** — This works but defeats the purpose of GPU acceleration; also, the error may still occur on CPU if the matrix is singular. (60% fail)
- **** — Singular matrices remain singular regardless of precision; the error is algorithmic, not numerical. (85% fail)
