def move_to_device(obj, cuda_device: int):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    """
    if cuda_device < 0 or not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.cuda(cuda_device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, cuda_device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, cuda_device) for item in obj]
    elif isinstance(obj, tuple):
        return tuple([move_to_device(item, cuda_device) for item in obj])
    else:
        return obj
def move_to_device ( obj , cuda_device : int ) : if cuda_device < 0 or not has_tensor ( obj ) : return obj elif isinstance ( obj , torch . Tensor ) : return obj . cuda ( cuda_device ) elif isinstance ( obj , dict ) : return { key : move_to_device ( value , cuda_device ) for key , value in obj . items ( ) } elif isinstance ( obj , list ) : return [ move_to_device ( item , cuda_device ) for item in obj ] elif isinstance ( obj , tuple ) : return tuple ( [ move_to_device ( item , cuda_device ) for item in obj ] ) else : return obj
Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).