I haven’t used/tested this API that’s available in pytorch.
Can you try parallel_apply?

  
      github.com
  
  
    pytorch/pytorch/blob/master/torch/nn/parallel/parallel_apply.py 5
import threading
import torch
from torch.cuda._utils import _get_device_index


def get_a_var(obj):
    if isinstance(obj, torch.Tensor):
        return obj

    if isinstance(obj, list) or isinstance(obj, tuple):
        for result in map(get_a_var, obj):
            if isinstance(result, torch.Tensor):
                return result
    if isinstance(obj, dict):
        for result in map(get_a_var, obj.items()):
            if isinstance(result, torch.Tensor):
                return result
    return None




  This file has been truncated. show original