{
  "id": "pytorch/cuda-error-invalid-device-function",
  "signature": "RuntimeError: CUDA error: invalid device function",
  "signature_zh": "运行时错误：CUDA错误：无效的设备函数",
  "regex": "RuntimeError: CUDA error: invalid device function",
  "domain": "pytorch",
  "category": "runtime_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "编译的CUDA代码中包含对当前GPU架构不存在的内核函数调用，通常是由于编译时与运行时的计算能力不匹配。",
  "versions": [
    {
      "version": "torch>=1.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "CUDA 11.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "CUDA 12.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Reinstalling PyTorch with the same CUDA version",
      "why_fails": "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.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting torch.backends.cudnn.enabled = False",
      "why_fails": "This disables cuDNN but does not affect the CUDA kernel dispatch that triggers the invalid function error. The error originates from a different layer.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Upgrading to the latest PyTorch version",
      "why_fails": "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.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "export TORCH_CUDA_ARCH_LIST=\"7.5;8.0;8.6\"  # Adjust to your GPU\npip install torch --no-binary torch\nOr use a precompiled wheel for your architecture.",
      "success_rate": 0.9,
      "how": "export TORCH_CUDA_ARCH_LIST=\"7.5;8.0;8.6\"  # Adjust to your GPU\npip install torch --no-binary torch\nOr use a precompiled wheel for your architecture.",
      "condition": "",
      "sources": []
    },
    {
      "action": "import os\nos.environ['CUDA_VISIBLE_DEVICES'] = '0'  # Ensure only one GPU is visible\n# Then run your code",
      "success_rate": 0.7,
      "how": "import os\nos.environ['CUDA_VISIBLE_DEVICES'] = '0'  # Ensure only one GPU is visible\n# Then run your code",
      "condition": "",
      "sources": []
    },
    {
      "action": "torch.cuda.set_device(0)\n# Ensure your model and data are on the same device",
      "success_rate": 0.65,
      "how": "torch.cuda.set_device(0)\n# Ensure your model and data are on the same device",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "export TORCH_CUDA_ARCH_LIST=\"7.5;8.0;8.6\"  # Adjust to your GPU\npip install torch --no-binary torch\nOr use a precompiled wheel for your architecture.",
    "import os\nos.environ['CUDA_VISIBLE_DEVICES'] = '0'  # Ensure only one GPU is visible\n# Then run your code",
    "torch.cuda.set_device(0)\n# Ensure your model and data are on the same device"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html",
  "official_doc_section": null,
  "error_code": "CUDA_ERROR_INVALID_DEVICE_FUNCTION",
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2023-09-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}