Callback Base Classes#

Callback Base Interface#

Defines the minimal callback protocol used for training, evaluation, and telemetry integration. Provides a unified interface for hook registration and event dispatch during iterative processes (e.g., epochs, steps).

Includes:

  • Callback: protocol defining the event hook signatures.

  • CallbackList: container that manages multiple callbacks and dispatches events sequentially.

class qmlhc.callbacks.base.Callback(*args, **kwargs)[source]#

Bases: Protocol

Minimal interface for runtime hooks used during training or evaluation.

A callback provides optional implementations for any of the event methods below. They can be combined to track progress, modify state, or log telemetry data across steps or epochs.

on_epoch_begin(epoch, context)[source]#

Called before each epoch begins.

Parameters:
  • epoch (int) – Current epoch number (0-based).

  • context (Mapping[str, Any]) – Mutable context dictionary shared among callbacks and the trainer.

Return type:

None

on_epoch_end(epoch, context)[source]#

Called after each epoch ends.

Parameters:
  • epoch (int) – Current epoch number (0-based).

  • context (Mapping[str, Any]) – Mutable context dictionary shared among callbacks and the trainer.

Return type:

None

on_error(error, context)[source]#

Called when an exception occurs within the training or evaluation loop.

Parameters:
  • error (Exception) – The raised exception object.

  • context (Mapping[str, Any]) – Context snapshot at the time of the error.

Return type:

None

on_step_begin(step, context)[source]#

Called before each optimization or update step.

Parameters:
  • step (int) – Index of the current step (0-based).

  • context (Mapping[str, Any]) – Mutable context dictionary shared among callbacks and the trainer.

Return type:

None

on_step_end(step, context)[source]#

Called after each optimization or update step.

Parameters:
  • step (int) – Index of the current step (0-based).

  • context (Mapping[str, Any]) – Mutable context dictionary shared among callbacks and the trainer.

Return type:

None

class qmlhc.callbacks.base.CallbackList(callbacks=None)[source]#

Bases: object

Manages multiple callbacks and dispatches their events sequentially.

Parameters:

callbacks (list[Callback] or None, optional) – Initial list of callbacks. Defaults to an empty list.

Notes

  • Callbacks are executed in the order they were added.

  • Each event propagates to all registered callbacks.

append(cb)[source]#

Add a new callback to the list.

Parameters:

cb (Callback) – Instance implementing the Callback protocol.

Return type:

None

on_epoch_begin(epoch, context)[source]#

Trigger on_epoch_begin for all registered callbacks.

Return type:

None

on_epoch_end(epoch, context)[source]#

Trigger on_epoch_end for all registered callbacks.

Return type:

None

on_error(error, context)[source]#

Trigger on_error for all registered callbacks.

Return type:

None

on_step_begin(step, context)[source]#

Trigger on_step_begin for all registered callbacks.

Return type:

None

on_step_end(step, context)[source]#

Trigger on_step_end for all registered callbacks.

Return type:

None