import torch


def detach(x):
    """Recursively detach the grads of elements in x.
    Args:
        x: object of any type
    Returns:
        x_detach: object where all elements have been detached"""
    if isinstance(x, torch.Tensor):
        return x.detach()
    elif isinstance(x, list):
        for i in range(len(x)):
            x[i] = detach(x[i])
        return x