Pytorch multiple GPUs: AttributeError: 'list' object has no attribute 'to

I have simply implemented DataParallel technique to utilize multiple GPUs on single machine. I am getting an error in fit function

from fastprogress.fastprogress import master_bar, progress_bar

In fit_one_epoch function:

    for images, targets in progress_bar(train_loader, parent=mb):

        images =
        targets =

In main func:

model =
    if device == 'cuda':
        model = nn.DataParallel(model)
        # model =
        cudnn.benchmark = True


Traceback (most recent call last):
  File "/home2/coremax/Documents/doctr/references/recognition/", line 481, in <module>
  File "/home2/coremax/Documents/doctr/references/recognition/", line 390, in main
    fit_one_epoch(model, train_loader, batch_transforms, optimizer, scheduler, mb, amp=args.amp)
  File "/home2/coremax/Documents/doctr/references/recognition/", line 122, in fit_one_epoch
    targets =
AttributeError: 'list' object has no attribute 'to

Based on the error message it seems your targets are passed as a list from the DataLoader. I don’t understand how nn.DataParallel is related to it as the data loading logic shouldn’t change. In any case, could you describe how you are loading the data and targets in your Dataset.__getitem__?

You are right! this is docTR library and they are using different logic for a single GPU. Due to the huge amount of training data, I have to utilize multiple data. targets variable is problem for me

train_set = RecognitionDataset(
                    T.Resize((args.input_size, 4 * args.input_size), preserve_aspect_ratio=True),
                    # Augmentations
                    T.RandomApply(T.ColorInversion(), 0.1),
                    ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.02),

train_loader = DataLoader(
        # sampler=train_data_sampler,

Could you point me to the code which shows a different usage for multi-GPU use cases in the Dataset, please? I still don’t understand how this could be the case since the Dataset is not aware if you are using nn.DataParallel or not.

I did not find multi-GPU usage in the Dataset Do you mean Data Parallel code? I am using simple example to implement DataParallel in my code like below link

# Load doctr model
    model = recognition.__dict__[args.arch](pretrained=args.pretrained, vocab=vocab)

    # Resume weights
    if isinstance(args.resume, str):
        print(f"Resuming {args.resume}")
        checkpoint = torch.load(args.resume, map_location="cpu")

    model =

    if device == 'cuda':
        model = nn.DataParallel(model)
        # model =
        cudnn.benchmark = True

    # Metrics
    val_metric = TextMatch()

    if args.test_only:
        print("Running evaluation")
        val_loss, exact_match, partial_match = evaluate(model, val_loader, batch_transforms, val_metric, amp=args.amp)
        print(f"Validation loss: {val_loss:.6} (Exact: {exact_match:.2%} | Partial: {partial_match:.2%})")

    st = time.time()

No, I did mean the Dataset since you’ve previously mentioned:

Could you point me to the code in docTR which apparently uses different logic for single- vs. multi-GPU usage, as I still doubt this is the case.

They are utilizing single GPU and i just would like to add DataParallel to utilize multiple GPUs
Line 240 to 252

docTR Code

 # GPU
    if isinstance(args.device, int):
        if not torch.cuda.is_available():
            raise AssertionError("PyTorch cannot access your GPU. Please investigate!")
        if args.device >= torch.cuda.device_count():
            raise ValueError("Invalid device index")
    # Silent default switch to GPU if available
    elif torch.cuda.is_available():
        args.device = 0
        logging.warning("No accessible GPU, targe device set to CPU.")
    if torch.cuda.is_available():
        model = model.cuda()