{"record":{"id":"7017d44819f98846","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"not-find-gpu-device-for-training","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_classification/mini_imagenet/train_multi_gpu_using_launch.py","lineNumber":19,"sourceCode":"import os\nimport math\nimport tempfile\nimport argparse\n\nimport torch\nimport torch.optim as optim\nimport torch.optim.lr_scheduler as lr_scheduler\nfrom torch.utils.tensorboard import SummaryWriter\nfrom torchvision import transforms\n\nfrom model import shufflenet_v2_x1_0\nfrom my_dataset import MyDataSet\nfrom multi_train_utils import train_one_epoch, evaluate, init_distributed_mode, dist, cleanup\n\n\ndef main(args):\n    if torch.cuda.is_available() is False:\n        raise EnvironmentError(\"not find GPU device for training.\")\n\n    # 初始化各进程环境\n    init_distributed_mode(args=args)\n\n    rank = args.rank\n    device = torch.device(args.device)\n    batch_size = args.batch_size\n    num_classes = args.num_classes\n    weights_path = args.weights\n    args.lr *= args.world_size  # 学习率要根据并行GPU的数量进行倍增\n\n    if rank == 0:  # 在第一个进程中打印信息，并实例化tensorboard\n        print(args)\n        print('Start Tensorboard with \"tensorboard --logdir=runs\", view at http://localhost:6006/')\n        tb_writer = SummaryWriter()\n        if os.path.exists(\"./weights\") is False:\n            os.makedirs(\"./weights\")\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/mini_imagenet/train_multi_gpu_using_launch.py#L1-L37","documentation":"main() in the multi-GPU training script hard-requires a CUDA device before initializing distributed training; if torch.cuda.is_available() returns False it raises EnvironmentError. DDP with NCCL cannot run without GPUs, so the script aborts early with a clear message.","triggerScenarios":"Running python -m torch.distributed.launch ... train_multi_gpu_using_launch.py on a machine with no NVIDIA GPU, no driver, or a CUDA-unavailable PyTorch build.","commonSituations":"CPU-only server or laptop, container without --gpus/nvidia runtime, PyTorch installed as the CPU wheel, driver/toolkit version mismatch making CUDA invisible.","solutions":["Verify nvidia-smi shows a GPU and the driver is loaded.","Install a CUDA build of PyTorch (e.g. pip install torch --index-url https://download.pytorch.org/whl/cu121), not the CPU-only wheel.","If using Docker, run with --gpus all and an NVIDIA/CUDA image.","Fall back to the single-GPU or CPU training script (train_single_gpu.py) if no GPU is available."],"exampleFix":"// before\nraise EnvironmentError(\"not find GPU device for training.\")\n// after\nif not torch.cuda.is_available():\n    raise EnvironmentError(\"not find GPU device for training.\")  # fix env: install CUDA-enabled torch / expose GPU in container","handlingStrategy":"validation","validationCode":"if not torch.cuda.is_available():\n    raise EnvironmentError(\"CUDA unavailable: check nvidia-smi, driver, and that torch is a CUDA build. Use train_single_gpu.py on CPU-only machines.\")","typeGuard":"def has_training_gpu() -> bool:\n    return torch.cuda.is_available() and torch.cuda.device_count() >= 1","tryCatchPattern":"try:\n    main(args)\nexcept EnvironmentError as e:\n    print(\"GPU required for multi-GPU training:\", e)\n    print(\"Falling back to: python train_single_gpu.py\")\n    sys.exit(1)","preventionTips":["Run nvidia-smi before launching DDP jobs","Install the CUDA-enabled torch wheel (check torch.cuda.is_available() in a REPL)","In Docker, launch with --gpus all and a CUDA base image","Use the CPU/single-GPU script when no GPU is present"],"tags":["python","pytorch","cuda","environment","gpu"],"backgroundTag":"cuda-unavailable","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}