# cv::error: (-2:Unspecified error) Failed to allocate memory for DNN forward pass with CUDA backend

- **ID:** `opencv/dnn-forward-cuda-memory`
- **Domain:** opencv
- **Category:** resource_error
- **Error Code:** `-2`
- **Verification:** ai_generated
- **Fix Rate:** 75%

## Root Cause

CUDA GPU ran out of memory during DNN forward pass, often due to large model or batch size exceeding GPU VRAM capacity.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |
| 4.10.0 | active | — | — |

## Workarounds

1. **Reduce batch size in the DNN forward pass: `net.setInput(blob); output = net.forward()` with a smaller blob (e.g., half the batch size).** (80% success)
   ```
   Reduce batch size in the DNN forward pass: `net.setInput(blob); output = net.forward()` with a smaller blob (e.g., half the batch size).
   ```
2. **Use model optimization techniques like quantization or pruning before inference, or switch to a smaller model variant.** (70% success)
   ```
   Use model optimization techniques like quantization or pruning before inference, or switch to a smaller model variant.
   ```
3. **Free unused GPU memory by calling `torch.cuda.empty_cache()` or `cv2.cuda.resetDevice()` before the forward pass.** (75% success)
   ```
   Free unused GPU memory by calling `torch.cuda.empty_cache()` or `cv2.cuda.resetDevice()` before the forward pass.
   ```

## Dead Ends

- **** — CPU may also run out of memory or be too slow; the issue is the model/batch size, not the backend alone. (60% fail)
- **** — Environment variables don't change available VRAM; they only select GPU devices. (85% fail)
- **** — CUDA memory is GPU VRAM, not system RAM; adding RAM does not help GPU memory. (95% fail)
