Validation#
ValidationConfig configures strategy-owned
validation passes; ValidationLoop is the reusable
loop that the strategy drives, and that you can also run standalone. Neither
performs a backward pass or optimizer step — validation runs the forward and
loss only, then reduces per-batch results across ranks.
See also
Training update hooks —
AFTER_VALIDATIONstage and update hooks.Training — validation configuration patterns and customization.
Training vs validation#
Aspect |
Training step |
Validation pass |
|---|---|---|
Backward / optimizer step |
Yes |
No — forward + loss only |
Module mode |
|
|
Autograd |
Always on |
Driven by |
Weights |
Live training weights |
Live, or the EMA / inference slot ( |
Per-batch output |
Loss for the update |
Accumulated into a reduced summary |
Gradient buffers |
Updated in place |
Snapshotted, cleared, restored |
ValidationConfig#
Field |
Type |
Description |
|---|---|---|
|
|
Re-iterable container (e.g. |
|
|
Validation forward callable. |
|
|
Validation loss function. |
|
|
Run validation after every n-th completed epoch. Mutually exclusive with |
|
|
Run validation after every n-th completed optimizer step. Mutually exclusive with |
|
|
Autograd policy during validation. |
|
|
If |
|
|
Whether the strategy’s |
|
|
Whether to reuse a registered |
|
|
Optional user-supplied callable invoked once per validation batch with the batch, predictions, and per-batch loss output. Use it to stream per-sample diagnostics to a custom logging or storage backend. |
|
|
Name stored in the validation summary dictionary. |
Assign to strategy.validation_config to enable strategy-owned validation:
from nvalchemi.training import TrainingStrategy, ValidationConfig
strategy = TrainingStrategy(...)
strategy.validation_config = ValidationConfig(
validation_data=val_data,
every_n_epochs=1,
)
strategy.run(train_loader)
validation_data must be a re-iterable container (list, DataLoader,
Dataset); one-shot generators are rejected at construction time.
Standalone validation#
ValidationLoop is a context manager — call
execute() inside the with block; training modes and gradient buffers
are snapshotted and restored on exit, even on exception:
from nvalchemi.training import ValidationConfig, ValidationLoop
config = ValidationConfig(validation_data=val_data, loss_fn=loss_fn)
loop = ValidationLoop(
validation_data=val_data,
config=config,
device=device,
model=model,
validation_fn=validation_fn,
)
with loop as active:
summary = active.execute()
The returned summary matches ctx.validation / strategy.last_validation
during integrated training: total_loss, per-component totals, batch and
sample counts, model_source, precision, and distributed_reduced.
API reference#
Configuration for strategy-owned validation passes. |
|
Context-manager orchestrator for a single validation pass. |
|
Protocol for an optional per-batch validation callback. |