import torch


def detach(args, device=None):
    """
    Detach all the tensors in the tuple of data.

    Args:
        args (Tuple[Any]): tuple of data
        device (torch.device): device object

    Returns:
        Tuple[Any]: tuple of detached data.
    """
    if isinstance(args, tuple):
        output = []
        for arg in args:
            if not isinstance(arg, torch.Tensor):
                output.append(arg)
                continue

            requires_grad = arg.requires_grad

            if device is not None:
                x = arg.to(device=device)
            else:
                x = arg

            x = x.detach()
            x.requires_grad = requires_grad
            output.append(x)

        return tuple(output)
    else:
        raise RuntimeError(
            "Only tuple of tensors is supported. Got Unsupported input type: ",
            type(args).__name__,
        )