def grad_ignore_norm_(parameters: _tensor_or_tensors, max_norm: float, norm_type: float = 2.0) -> torch.Tensor:
    """
    **Overview**:
        Implementation of grad_ignore_norm <link https://github.com/opendilab/DI-engine/blob/2ab7c44a64329fb90fa877e6070bc76bb6fdb31e/ding/torch_utils/optimizer_helper.py#L56 link>
        Different from ``grad_clip_norm`` , ``grad_ignore_norm`` **ignore**: those gradients that have a norm exceeds the specified threshold, instead of cliping their norm to the threshold.
    """
    # Save the parameters with non-empty gradient into a list.
    if isinstance(parameters, torch.Tensor):
        parameters = [parameters]
    parameters = list(filter(lambda p: p.grad is not None, parameters))
    # Convert max_norm and norm_type to float.
    max_norm = float(max_norm)
    norm_type = float(norm_type)
    device = parameters[0].grad.device
    # The max norm of gradient: $$\mathrm{total\_norm}^{\infty} = \max_{\theta_i\in \Theta} |\mathrm{grad}(\theta_i)|$$
    if norm_type == inf:
        norms = [p.grad.detach().abs().max().to(device) for p in parameters]
        total_norm = norms[0] if len(norms) == 1 else torch.max(torch.stack(norms))
    # The p-norm of gradient: $$\begin{split}\mathrm{total\_norm} &= (\sum_{\theta\in\Theta}((\sum_{\theta_i}\mathrm{grad}(\theta_i)^p)^\frac{1}{p})^p)^\frac{1}{p}\\&=(\sum_{\theta\in\Theta}(\sum_{\theta_i}\mathrm{grad}(\theta_i)^p))^\frac{1}{p}\end{split}$$
    else:
        total_norm = torch.norm(
            torch.stack([torch.norm(p.grad.detach(), norm_type).to(device) for p in parameters]), norm_type
        )
    # The clip coefficient (the 1e-6 is used to avoid zero in the denominator): $$\mathrm{clip\_coef} = \frac{\mathrm{max\_norm}}{\mathrm{total\_norm}}$$
    clip_coef = max_norm / (total_norm + 1e-6)
    # If total_norm > max_norm, all the gradient is clipped to zero.
    if clip_coef < 1:
        for p in parameters:
            p.grad.zero_()
    return total_norm

    **Overview**:
        Implementation of grad_ignore_norm <link https://github.com/opendilab/DI-engine/blob/2ab7c44a64329fb90fa877e6070bc76bb6fdb31e/ding/torch_utils/optimizer_helper.py#L56 link>
        Different from ``grad_clip_norm`` , ``grad_ignore_norm`` **ignore**: those gradients that have a norm exceeds the specified threshold, instead of cliping their norm to the threshold.
    
chapter7_tricks/grad_ignore.py