import torch


def state_requires_grad(x):
    """Recursively turn requires_grad to True to the elements of an object x.
    Args:
        x: object of any type
    Returns:
        x: object where the elements require grad"""
    if isinstance(x, torch.Tensor):
        x.requires_grad = True
    elif isinstance(x, list):
        for i in range(len(x)):
            x[i] = state_requires_grad(x[i])
    return x