import torch


def calc_parameter_sparsity(p):
    """Calculates the sparsity percentage of a torch parameter.

    Args:
      p: torch parameter
    Returns: sparsity percentage (as float) with range [0.0, 1.0]
    """
    x = torch.sum((torch.flatten(p) == 0).float())
    return float(x) / p.numel()