GradientClipCallback#

class braintools.trainer.GradientClipCallback(clip_val=None, clip_algorithm='norm', log_grad_norm=False)#

Placeholder callback for gradient clipping configuration.

Deprecated since version 0.4.0: GradientClipCallback has never clipped gradients and will be removed in 1.0.0. Use Trainer(gradient_clip_val=...) instead. Constructing it emits a BraintoolsDeprecationWarning.

Warning

This callback does not itself clip gradients or log gradient norms. Gradient clipping is performed inside the Trainer’s JIT-compiled apply step (where a Python callback cannot intercept the traced gradients), so a callback hook cannot see or modify them. To clip gradients, configure the Trainer directly:

Trainer(gradient_clip_val=1.0, gradient_clip_algorithm='norm')

The parameters below are validated and stored for forward compatibility but currently have no runtime effect.

Parameters:
  • clip_val (float | None) – Maximum gradient norm or value. Stored only; not applied.

  • clip_algorithm (str) – Clipping algorithm. One of ‘norm’ (global norm) or ‘value’ (element-wise). Validated only; not applied.

  • log_grad_norm (bool) – Reserved for gradient-norm logging, which is not yet implemented.

Raises:

ValueError – If clip_algorithm is not 'norm' or 'value'.

Warns:

BraintoolsDeprecationWarning – Always, because the callback is deprecated.

See also

Trainer

Set gradient_clip_val / gradient_clip_algorithm there to actually clip gradients.

Examples

Real clipping is configured on the Trainer, not via this callback:

>>> import braintools
>>> trainer = braintools.trainer.Trainer(
...     gradient_clip_val=1.0, gradient_clip_algorithm='norm',
...     logger=False, enable_progress_bar=False, enable_checkpointing=False)
>>> trainer.gradient_clip_val, trainer.gradient_clip_algorithm
(1.0, 'norm')
on_before_optimizer_step(trainer, module, optimizer)[source]#

Clip gradients before optimizer step.