def calc_sparsity(optimizer, total_params, total_quant_params):
    """
    Returns the sparsity of the overall network and the sparsity of quantized layers only.

    Parameters:
    -----------
        optimizer:
            An optimizer containing quantized model layers in param_groups[1]['params'] and non-quantized layers,
            such as BatchNorm, Bias, etc., in param_groups[1]['params'].
        total_params:
            Total number of parameters.
        total_quant_params:
            Number of quantized parameters.

    Returns:
    --------
        sparsity_total:
            Sparsity of the overall network.
        sparsity_quant:
            Sparsity of quantized layers of the network.
    """

    nonzero_elements_quant = 0
    for layer in optimizer.param_groups[1]['params']:
        nonzero_elements_quant += layer[layer != 0].numel()

    nonzero_elements_total = 0
    for layer in optimizer.param_groups[0]['params']:
        nonzero_elements_total += layer[layer != 0].numel()
    nonzero_elements_total += nonzero_elements_quant

    sparsity_total = (total_params - nonzero_elements_total) / total_params
    sparsity_quant = (total_quant_params - nonzero_elements_quant) / total_quant_params

    return sparsity_total, sparsity_quant