import re


def fetch_layers(model):
    """Fetch all parameters as tuples according to `torch.nn.prune` format
    Args
        model: nn.Module
    Returns:
        params: list of tuples with layer and name of parameter
        (e.g. (nn.Linear(...), 'weight'))
    """

    loc = locals()
    params = [name for name, _ in model.named_parameters()]
    params = [re.sub(r"(\.)([0-9]+)", r"[\2]", name.split("_")[0]) for name in params]
    params = [
        (eval("model." + attr, loc), param_type)
        for (attr, _, param_type, _) in [
            re.split(r"(\.)(\w+$)", name) for name in params
        ]
    ]

    return params