import torch


def get_grad_norm_from_optimizer(optimizer, norm_type=2):
    """
    Get the gradient norm for some parameters contained in an optimizer.
    Arguments:
        optimizer (torch.optim.Optimizer)
        norm_type (int): Type of norm. Default value is 2.
    Returns:
        norm (float)
    """
    total_norm = 0
    if optimizer is not None:
        for param_group in optimizer.param_groups:
            for p in param_group['params']:
                if p.grad is not None:
                    with torch.no_grad():
                        param_norm = p.grad.data.norm(norm_type)
                    total_norm += param_norm ** norm_type
    total_norm = total_norm ** (1. / norm_type)
    return total_norm.item()