WZMIAOMIAO/deep-learning-for-image-processing · critical · EnvironmentError

not find GPU device for training.

Error message

not find GPU device for training.

What it means

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.

Source

Thrown at pytorch_classification/mini_imagenet/train_multi_gpu_using_launch.py:19

import os
import math
import tempfile
import argparse

import torch
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms

from model import shufflenet_v2_x1_0
from my_dataset import MyDataSet
from multi_train_utils import train_one_epoch, evaluate, init_distributed_mode, dist, cleanup


def main(args):
    if torch.cuda.is_available() is False:
        raise EnvironmentError("not find GPU device for training.")

    # 初始化各进程环境
    init_distributed_mode(args=args)

    rank = args.rank
    device = torch.device(args.device)
    batch_size = args.batch_size
    num_classes = args.num_classes
    weights_path = args.weights
    args.lr *= args.world_size  # 学习率要根据并行GPU的数量进行倍增

    if rank == 0:  # 在第一个进程中打印信息,并实例化tensorboard
        print(args)
        print('Start Tensorboard with "tensorboard --logdir=runs", view at http://localhost:6006/')
        tb_writer = SummaryWriter()
        if os.path.exists("./weights") is False:
            os.makedirs("./weights")

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Verify nvidia-smi shows a GPU and the driver is loaded.
  2. Install a CUDA build of PyTorch (e.g. pip install torch --index-url https://download.pytorch.org/whl/cu121), not the CPU-only wheel.
  3. If using Docker, run with --gpus all and an NVIDIA/CUDA image.
  4. Fall back to the single-GPU or CPU training script (train_single_gpu.py) if no GPU is available.

Example fix

// before
raise EnvironmentError("not find GPU device for training.")
// after
if not torch.cuda.is_available():
    raise EnvironmentError("not find GPU device for training.")  # fix env: install CUDA-enabled torch / expose GPU in container
Defensive patterns

Strategy: validation

Validate before calling

if not torch.cuda.is_available():
    raise EnvironmentError("CUDA unavailable: check nvidia-smi, driver, and that torch is a CUDA build. Use train_single_gpu.py on CPU-only machines.")

Type guard

def has_training_gpu() -> bool:
    return torch.cuda.is_available() and torch.cuda.device_count() >= 1

Try / catch

try:
    main(args)
except EnvironmentError as e:
    print("GPU required for multi-GPU training:", e)
    print("Falling back to: python train_single_gpu.py")
    sys.exit(1)

Prevention

When it happens

Trigger: 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.

Common situations: CPU-only server or laptop, container without --gpus/nvidia runtime, PyTorch installed as the CPU wheel, driver/toolkit version mismatch making CUDA invisible.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/7017d44819f98846. Report an issue: GitHub.