{
  "id": "opencv/dnn-forward-cuda-memory",
  "signature": "cv::error: (-2:Unspecified error) Failed to allocate memory for DNN forward pass with CUDA backend",
  "signature_zh": "cv::error: (-2:未指定错误) 使用 CUDA 后端进行 DNN 前向传播时分配内存失败",
  "regex": "Failed to allocate memory for DNN forward pass with CUDA backend",
  "domain": "opencv",
  "category": "resource_error",
  "subcategory": null,
  "root_cause": "CUDA GPU ran out of memory during DNN forward pass, often due to large model or batch size exceeding GPU VRAM capacity.",
  "root_cause_type": "generic",
  "root_cause_zh": "CUDA GPU 在 DNN 前向传播期间内存不足，通常是因为模型过大或批次大小超过 GPU VRAM 容量。",
  "versions": [
    {
      "version": "4.6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "CPU may also run out of memory or be too slow; the issue is the model/batch size, not the backend alone.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Environment variables don't change available VRAM; they only select GPU devices.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "CUDA memory is GPU VRAM, not system RAM; adding RAM does not help GPU memory.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Reduce batch size in the DNN forward pass: `net.setInput(blob); output = net.forward()` with a smaller blob (e.g., half the batch size).",
      "success_rate": 0.8,
      "how": "Reduce batch size in the DNN forward pass: `net.setInput(blob); output = net.forward()` with a smaller blob (e.g., half the batch size).",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use model optimization techniques like quantization or pruning before inference, or switch to a smaller model variant.",
      "success_rate": 0.7,
      "how": "Use model optimization techniques like quantization or pruning before inference, or switch to a smaller model variant.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Free unused GPU memory by calling `torch.cuda.empty_cache()` or `cv2.cuda.resetDevice()` before the forward pass.",
      "success_rate": 0.75,
      "how": "Free unused GPU memory by calling `torch.cuda.empty_cache()` or `cv2.cuda.resetDevice()` before the forward pass.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Reduce batch size in the DNN forward pass: `net.setInput(blob); output = net.forward()` with a smaller blob (e.g., half the batch size).",
    "Use model optimization techniques like quantization or pruning before inference, or switch to a smaller model variant.",
    "Free unused GPU memory by calling `torch.cuda.empty_cache()` or `cv2.cuda.resetDevice()` before the forward pass."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.opencv.org/4.x/d6/d0f/group__dnn.html#ga29f34df9376379a603acd8df581ac8d7",
  "official_doc_section": null,
  "error_code": "-2",
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.75,
  "resolvable": "partial",
  "first_seen": "2023-07-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}