(grad_tree, max_norm)
return fastmath.nested_map(grad_tree, normalize)
Proportionally reduces each gradient value to respect an aggregate limit.

  Args:
    grad_tree: Gradient values structured as a tree of tensors matching the
        model's layer structure.
    max_norm: The aggregate limit on gradient values. All gradient elements in
        `grad_tree` are treated as if they belonged to a single vector and
        that vector is shortened if needed so that its L2 norm does not exceed
        `clip_grad_norm`.

  Returns:
    A new tree of tensors matching the structure of `grad_tree`, but with
    element values proportionally rescaled as needed to respect the `max_norm`
    limit.
Proportionally reduces each gradient value to respect an aggregate limit.
Proportionally reduces each gradient value to respect an aggregate limit .
def clip_grads(grad_tree, max_norm):
  """Proportionally reduces each gradient value to respect an aggregate limit.

  Args:
    grad_tree: Gradient values structured as a tree of tensors matching the
        model's layer structure.
    max_norm: The aggregate limit on gradient values. All gradient elements in
        `grad_tree` are treated as if they belonged to a single vector and
        that vector is shortened if needed so that its L2 norm does not exceed
        `clip_grad_norm`.

  Returns:
    A new tree of tensors matching the structure of `grad_tree`, but with
    element values proportionally rescaled as needed to respect the `max_norm`
    limit.
  """
  norm = l2_norm(grad_tree)
  normalize = lambda g: jnp.where(norm < max_norm, g, g * (max_norm / norm))
  return fastmath.nested_map(grad_tree, normalize)
def clip_grads ( grad_tree , max_norm ) : norm = l2_norm ( grad_tree ) normalize = lambda g : jnp . where ( norm < max_norm , g , g * ( max_norm / norm ) ) return fastmath . nested_map ( grad_tree , normalize )