{"record":{"id":"2d51ad451c4158bb","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"not-find-gpu-device-for-training-2d51ad","errorCode":null,"errorMessage":"not find GPU device for training.","messagePattern":"not find GPU device for training\\.","errorType":"exception","errorClass":"EnvironmentError","httpStatus":null,"severity":"critical","filePath":"pytorch_object_detection/yolov3_spp/train_multi_GPU.py","lineNumber":29,"sourceCode":"from models import *\nfrom build_utils.datasets import *\nfrom build_utils.utils import *\nfrom train_utils import train_eval_utils as train_util\nfrom train_utils import get_coco_api_from_dataset, init_distributed_mode, torch_distributed_zero_first\n\n\ndef main(opt, hyp):\n    # 初始化各进程\n    init_distributed_mode(opt)\n\n    if opt.rank in [-1, 0]:\n        print(opt)\n        print('Start Tensorboard with \"tensorboard --logdir=runs\", view at http://localhost:6006/')\n        tb_writer = SummaryWriter(comment=opt.name)\n\n    device = torch.device(opt.device)\n    if \"cuda\" not in device.type:\n        raise EnvironmentError(\"not find GPU device for training.\")\n\n    # 使用DDP后会对每个device上的gradients取均值，所以需要放大学习率\n    hyp[\"lr0\"] *= max(1., opt.world_size * opt.batch_size / 64)\n\n    wdir = \"weights\" + os.sep  # weights dir\n    best = wdir + \"best.pt\"\n    results_file = \"results{}.txt\".format(datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n\n    cfg = opt.cfg\n    data = opt.data\n    epochs = opt.epochs\n    batch_size = opt.batch_size\n    # accumulate n times before optimizer update (bs 64)\n    accumulate = max(round(64 / (opt.world_size * opt.batch_size)), 1)\n    weights = opt.weights  # initial training weights\n    imgsz_train = opt.img_size\n    imgsz_test = opt.img_size  # test image sizes\n    multi_scale = opt.multi_scale","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/yolov3_spp/train_multi_GPU.py#L11-L47","documentation":"train_multi_GPU.py requires an actual CUDA device: it builds device = torch.device(opt.device) and raises EnvironmentError('not find GPU device for training.') if 'cuda' is not in device.type. Multi-GPU DDP training cannot proceed on CPU.","triggerScenarios":"Running train_multi_GPU.py with --device cpu, or --device cuda:0 while torch.cuda.is_available() is False (no GPU, drivers/CUDA not installed, or a CPU-only torch build).","commonSituations":"Forgetting the --device flag defaults to cpu; running on a machine/VM without GPUs; installing the wrong torch wheel (cpu-only); CUDA_VISIBLE_DEVICES hiding all GPUs.","solutions":["Verify GPUs are visible: python -c \"import torch; print(torch.cuda.is_available(), torch.cuda.device_count())\"","Install the CUDA-enabled torch build matching your CUDA driver (see pytorch.org install matrix)","Pass --device cuda:0 (and correct --device per rank) or run on a GPU machine"],"exampleFix":"// before\npython train_multi_GPU.py --device cpu ...\n// after\npython train_multi_GPU.py --device cuda:0 ...  # after verifying torch.cuda.is_available() == True","handlingStrategy":"validation","validationCode":"import torch\nassert torch.cuda.is_available() and torch.cuda.device_count() > 0, \\\n    'No CUDA device: install CUDA-enabled torch or run on a GPU machine'","typeGuard":null,"tryCatchPattern":"try:\n    device = torch.device(opt.device)\n    if 'cuda' not in device.type:\n        raise EnvironmentError('not find GPU device for training.')\nexcept EnvironmentError:\n    raise SystemExit('Pass --device cuda:N on a machine with torch.cuda.is_available()==True')","preventionTips":["Smoke-test torch.cuda.is_available() before launching DDP runs","Install the CUDA build of torch matching your driver","Check CUDA_VISIBLE_DEVICES isn't masking GPUs; never pass --device cpu to train_multi_GPU.py"],"tags":["python","pytorch","gpu","cuda","environment"],"backgroundTag":"cuda-device-unavailable","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}