mlx.nn.utils.clip_grad_norm_sharded

mlx.nn.utils.clip_grad_norm_sharded#

clip_grad_norm_sharded(gradients: Any, max_norm: float, group: Group | None = None)#

Clip the global norm of gradients that are sharded across a group.

This is the sharded equivalent of mlx.optimizers.clip_grad_norm(). Each member of the group holds only a shard of the gradients, so the global norm is computed by summing the local squared norms across the group before rescaling. It is useful for clipping the gradients of a module wrapped with mlx.nn.layers.distributed.fully_shard().

Parameters:
  • gradients (Any) – A Python tree containing the local shard of the gradient arrays.

  • max_norm (float) – The maximum allowed global norm of the gradients.

  • group (Optional[Group]) – The group across which the gradients are sharded. If set to None the global group is used. Default: None.

Returns:

The possibly rescaled local shard of the gradients and the global gradient norm.

Return type:

(Any, array)