kempnerforge.metrics.tracker

Metrics collection, accumulation, and reporting.

MetricsTracker aggregates per-step metrics (loss, grad norm, throughput, MFU, memory) and dispatches them to configured logging backends (stdout, WandB, TensorBoard, MLflow) at a configurable interval.

Classes

MetricsTracker

Collects, smooths, and reports training metrics.

StepMetrics

Metrics for a single training step.

TensorBoardBackend

TensorBoard logging backend.

WandBBackend

Weights & Biases logging backend.

class kempnerforge.metrics.tracker.StepMetrics[source]

Bases: object

Metrics for a single training step.

loss: float = 0.0
grad_norm: float = 0.0
lr: float = 0.0
tokens_per_sec: float = 0.0
mfu: float = 0.0
step_time_sec: float = 0.0
allocated_gb: float = 0.0
peak_gb: float = 0.0
reserved_gb: float = 0.0
total_gb: float = 0.0
mem_utilization: float = 0.0
__init__(loss=0.0, grad_norm=0.0, lr=0.0, tokens_per_sec=0.0, mfu=0.0, step_time_sec=0.0, allocated_gb=0.0, peak_gb=0.0, reserved_gb=0.0, total_gb=0.0, mem_utilization=0.0)
Parameters:
Return type:

None

class kempnerforge.metrics.tracker.MetricsTracker[source]

Bases: object

Collects, smooths, and reports training metrics.

Timing is handled internally — call start_step() before and end_step() after each training step. Metrics are logged to all configured backends at the configured interval.

Parameters:
  • config – Full job config (used for MFU calculation and backend selection).

  • num_gpus – Number of GPUs for MFU denominator.

  • gpu_peak_tflops – Per-GPU peak TFLOPS. If None, auto-detected.

__init__(config, num_gpus=1, gpu_peak_tflops=None)[source]
Parameters:
Return type:

None

start_step()[source]

Mark the beginning of a training step.

Return type:

None

end_step(step, loss, grad_norm, lr, tokens_in_step)[source]

Mark the end of a training step and optionally log metrics.

A logging step reads GPU memory once, from the current device. The tracker never resets the peak, so peak_gb is the most allocated since the process started, or since a reset_peak_memory elsewhere (DeviceMemoryMonitor.report makes one).

Parameters:
  • step (int) – Current training step number.

  • loss (float) – Loss value for this step.

  • grad_norm (float) – Total gradient norm before clipping (what clip_grad_norm_ returns); under pipeline parallelism, the whole model’s norm.

  • lr (float) – Current learning rate.

  • tokens_in_step (int) – Total tokens processed in this step (across all GPUs).

Returns:

StepMetrics if this step was a logging step, None otherwise.

Return type:

StepMetrics | None

log_eval(metrics, step)[source]

Log eval metrics to all backends and stdout.

Parameters:
Return type:

None

init_backends(config)[source]

Initialize logging backends (call after distributed setup).

Parameters:

config (JobConfig)

Return type:

None

tracking_entries()[source]

One record per backend with a live run, for a checkpoint’s metadata.json.

Empty once close() has begun. Reads only what the backends already hold, so it never waits on a server. Each entry is copied once into a plain dict; a backend that cannot describe itself, or whose entry is not strings under a named backend, is skipped with a warning and costs no other backend its entry.

Return type:

list[dict[str, str]]

checkpoint_fields(step)[source]

What each backend with a live run adds to its tracking entry of the save at step.

Keyed by backend name; this tracker is what run_training hands to CheckpointManager.set_checkpoint_registrar. Reads only what the backends hold, so it never waits on a server; a backend that fails costs only its own fields. Empty once close() has begun.

Parameters:

step (int)

Return type:

dict[str, dict[str, str]]

register_checkpoint(step, directory, record)[source]

Hand a committed checkpoint to each backend with a live run; never blocks.

Each backend gets record bound to its own name, so it can amend only its own tracking entry, then or later from another thread. A backend that fails costs only its own registration. Nothing is registered once close() has begun.

Parameters:
Return type:

None

close()[source]

Flush and close all logging backends.

Return type:

None

class kempnerforge.metrics.tracker.WandBBackend[source]

Bases: _LoggingBackend

Weights & Biases logging backend.

Initializes a WandB run on first log call.

__init__(config)[source]
Parameters:

config (MetricsConfig)

Return type:

None

log(metrics, step)[source]
Parameters:
Return type:

None

tracking_entry()[source]

This backend’s record of its live run for a checkpoint.

None until its run starts, after a failed init, and once it is closed.

Return type:

dict[str, str] | None

close()[source]
Return type:

None

class kempnerforge.metrics.tracker.TensorBoardBackend[source]

Bases: _LoggingBackend

TensorBoard logging backend.

__init__(config)[source]
Parameters:

config (MetricsConfig)

Return type:

None

log(metrics, step)[source]
Parameters:
Return type:

None

tracking_entry()[source]

This backend’s record of its live run for a checkpoint.

None until its run starts, after a failed init, and once it is closed.

Return type:

dict[str, str] | None

close()[source]
Return type:

None