flashcart.training¶
Train and evaluate potentials using configurable loss functions, optimizers, and
learning-rate schedules. TrainingTask runs
training, validation, and checkpointing, while
EvaluationTask evaluates a model over a loader
and combines batch statistics into the requested metrics.
Loss objects specify both the target property and the predictions needed to compute it. The task uses these requirements to request energies, forces, or stress and to retain the derivative graph when needed for training. Optimizer and scheduler functions read the configuration to create the update rules.
See Quickstart for the training workflow and Multi-GPU training for distributed execution.
flashcart.training.tasks¶
Run training, validation, checkpointing, and model evaluation.
The best checkpoint is selected by the validation metric. The log
checkpoint stores the state needed to resume training. Despite the argument name
early_stopping_loss, training continues to max_epochs even if the
validation metric stops improving.
- flashcart.training.tasks.preserve_grad_state(model)[source]¶
Freeze all parameters inside the block, restoring their flags on exit.
Used around evaluation so force/stress autograd tracks only the inputs, not the parameters.
- Parameters:
model (torch.nn.Module) – Model whose parameters are frozen.
- flashcart.training.tasks.predict_kwargs(losses)[source]¶
Derive the
predict()flags from what the given losses require.- Parameters:
losses (list[LossFunction]) – Losses that will consume the predictions.
- Returns:
compute_forcesandcompute_stressflags required by thelosses.
- Return type:
- flashcart.training.tasks.unwrap_model(model)[source]¶
Strip Fabric/DDP wrappers down to the underlying model.
Training forwards must go through the wrapper (gradient synchronization). Use this only for evaluation, checkpointing, and attribute access.
- Parameters:
model (torch.nn.Module) – Possibly wrapped model.
- Returns:
Underlying module after removing wrappers exposing
module.- Return type:
- flashcart.training.tasks.make_predict_padder(model, predict_compile, pad_kwargs)[source]¶
Build a batch padder for compiled prediction.
Padding reduces tensor-shape changes while batches fit within the allocated capacities. Eager prediction runs without padding.
- Parameters:
model (torch.nn.Module) – Model providing
r_maxfor fake edges.predict_compile (bool) – Whether predictions run compiled.
pad_kwargs (dict) – PadAtomicData rounding multiples.
- Returns:
- Padder for compiled prediction, or None for eager
prediction.
- Return type:
PadAtomicData | None
- flashcart.training.tasks.seed_padder_from_loader(padder, loader)[source]¶
Initialize padding capacities from the batch sampler’s configured limits.
These initial capacities reduce growth as batch sizes vary. Batches that exceed them may still increase the capacities and trigger recompilation. When sampler limits are unavailable, capacities are determined from observed batches.
- Parameters:
padder (PadAtomicData, optional) – Padder to initialize. Nothing is changed when None.
loader (DataLoader) – Loader whose batch sampler carries the maxima.
- flashcart.training.tasks.padded_predict(model, batch, padder, predict_kwargs, compile_kwargs, create_graph)[source]¶
Evaluate a batch with optional padding and remove padded output entries.
- Parameters:
model (torch.nn.Module) – Model exposing
predict.batch (Any) – Batched atomic graph.
padder (PadAtomicData, optional) – None predicts unpadded.
predict_kwargs (dict) – Property flags (see
predict_kwargs).compile_kwargs (dict) –
use_compile/mode kwargs, empty for eager.create_graph (bool) – Keep derivative graphs when training on energy derivatives.
- Returns:
Predictions for the real atoms and structures.
- Return type:
- class flashcart.training.tasks.TrainingTask(model, train_loss, early_stopping_loss, eval_losses, optimizer, scheduler=None, aux_optimizers=None, max_epochs=1000, validate_every=1, save_every=100, grad_clip_norm=None, log_significant_digits=4, log_weight_norms=False, ema_decay=None, predict_compile=False, predict_compile_mode='reduce-overhead', predict_compile_fullgraph=True, predict_compile_dynamic=False, predict_pad_atom_multiple=128, predict_pad_edge_multiple=128, predict_pad_graph_multiple=8)[source]¶
Bases:
objectTrain a potential with validation and checkpointing.
Optionally maintain exponential moving averages of the model parameters.
The
logcheckpoint stores the current weights and training state for resumption. Thebestcheckpoint stores the weights that minimize the selected validation metric, using averaged weights when enabled. Training continues tomax_epochs. The validation metric selects checkpoints without applying a patience-based stopping rule.- Parameters:
model (torch.nn.Module) – Model to train.
train_loss (LossFunction) – Loss backpropagated per step.
early_stopping_loss (LossFunction) – Validation metric selecting the best checkpoint.
eval_losses (list[LossFunction]) – Metrics logged for train and valid.
optimizer (torch.optim.Optimizer) – Main optimizer.
scheduler (LRScheduler, optional) – Learning-rate scheduler stepped after each epoch. Its first parameter group’s learning rate is copied to the auxiliary optimizers. Default: None.
aux_optimizers (list, optional) – Additional optimizers stepped after the main optimizer. Default: None.
max_epochs (int, optional) – Total epochs. Default: 1000.
validate_every (int, optional) – Epochs between validations. Default: 1.
save_every (int, optional) – Epochs between rolling checkpoints. Default: 100.
grad_clip_norm (float, optional) – Maximum global gradient norm, applied before all optimizer steps. This limits gradients rather than directly bounding the resulting parameter updates. Default: None.
log_significant_digits (int, optional) – Round CSV values to this many significant digits. None logs full precision. Default: 4.
log_weight_norms (bool, optional) – Include Muon weight and momentum diagnostics in
metrics.csv. Default: False.ema_decay (float, optional) – Decay factor for the exponential moving average used during validation and best-checkpoint selection. Default: None, disabling averaging.
predict_compile (bool, optional) – Run predictions through
torch.compile(enables batch padding). Default: False.predict_compile_mode (str, optional) – Compile mode. Default: “reduce-overhead”.
predict_compile_fullgraph (bool, optional) – Compile without graph breaks. Default: True.
predict_compile_dynamic (bool, optional) – Dynamic-shape compile. Default: False.
predict_pad_atom_multiple (int, optional) – Atom-budget rounding of the padder. Default: 128.
predict_pad_edge_multiple (int, optional) – Edge-budget rounding. Default: 128.
predict_pad_graph_multiple (int, optional) – Graph-budget rounding. Default: 8.
- run(train_loader, valid_loader, output_path, fabric=None)[source]¶
Train through
max_epochs, restoring a saved training state when present.If
output_path/log/training_state.ptexists, the model weights, optimizer states, scheduler, and averaging state are restored before training continues.- Parameters:
train_loader (DataLoader) – Training batches.
valid_loader (DataLoader) – Validation batches.
output_path (str | Path) – Run directory (checkpoints, metrics.csv).
fabric (Fabric, optional) – Launched Fabric. A single-process instance is created when omitted.
- class flashcart.training.tasks.EvaluationTask(model, eval_losses, predict_compile=False, predict_compile_mode='reduce-overhead', predict_compile_fullgraph=True, predict_compile_dynamic=False, predict_pad_atom_multiple=128, predict_pad_edge_multiple=128, predict_pad_graph_multiple=8)[source]¶
Bases:
objectEvaluate a model over a loader and aggregate the requested metrics.
- Parameters:
model (torch.nn.Module) – Model to evaluate.
eval_losses (list[LossFunction]) – Metrics to compute.
predict_compile (bool, optional) – Compiled predictions (enables batch padding). Default: False.
predict_compile_mode (str, optional) – Compile mode. Default: “reduce-overhead”.
predict_compile_fullgraph (bool, optional) – Compile without graph breaks. Default: True.
predict_compile_dynamic (bool, optional) – Dynamic-shape compile. Default: False.
predict_pad_atom_multiple (int, optional) – Atom-budget rounding of the padder. Default: 128.
predict_pad_edge_multiple (int, optional) – Edge-budget rounding. Default: 128.
predict_pad_graph_multiple (int, optional) – Graph-budget rounding. Default: 8.
- run(data_loader, fabric=None)[source]¶
Evaluate over the loader and return {metric name: value}.
- Parameters:
data_loader (DataLoader) – Batches to evaluate.
fabric (Fabric, optional) – Fabric instance used for batch placement and distributed metric reduction. Default: None, retaining any instance provided by an earlier call.
- Returns:
Metric values keyed by loss name.
- Return type:
flashcart.training.loss¶
Define property loss functions and combine their batch statistics.
- flashcart.training.loss.merge_compute_keys(losses)[source]¶
Combine the prediction requirements of several losses.
- Parameters:
losses (list[LossFunction]) – Losses to merge.
- Returns:
Combined force, stress, and virial requirements.
- Return type:
- class flashcart.training.loss.LossFunction[source]¶
Bases:
objectInterface for training losses and evaluation metrics.
reduce_batchcomputes the statistics needed from one batch, andreduce_overallcombines them using the total atom and structure counts. Means and square roots are applied after aggregation so that their normalization does not depend on batch size.- property name¶
Column name in logs and metrics.csv, e.g.
"forces_mae".
- property compute_keys¶
Prediction requirements used to select energy derivatives.
The mapping contains
compute_forces,compute_stress, andcompute_virials. Training tasks combine stress and virial requests into the model’scompute_stressflag.
- property is_extensive¶
True for sum-reduced losses, which scale with the batch size.
- __call__(results, batch)[source]¶
Evaluate the loss for one batch.
- Parameters:
results (dict[str, torch.Tensor]) – Model predictions.
batch (Data) – Batch containing reference targets.
- Returns:
Scalar loss or metric.
- Return type:
- training_loss(results, batch, world_size=1)[source]¶
Evaluate the per-rank loss for distributed gradient computation.
Extensive (sum-reduced) losses are multiplied by the world size: DDP averages gradients across ranks, and the rescale turns that average back into the global-batch sum.
- Parameters:
- Returns:
- Scalar loss, scaled for distributed gradient averaging when
the reduction is extensive.
- Return type:
- reduce_batch(results, batch)[source]¶
Compute batch statistics to be combined by
reduce_overall.- Parameters:
results (dict) – Model predictions.
batch (Data) – Batch with reference targets.
- Returns:
Batch statistics keyed by metric name.
- Return type:
- class flashcart.training.loss.PropertyLossFunction(key, reduce='mae', normalization='per_atom', delta=None)[source]¶
Bases:
LossFunctionEvaluate a loss function or metric for one predicted property.
The reduction modes
sae,sse,huber_sum, andnorm_sumare extensive sums.mae,mse,huber_mean, andnorm_meanare per-component or per-vector means.rmseis the root mean square error, andmaxeis the maximum absolute error. For vector-valued residuals,norm_sumandnorm_meanreducesqrt(sum(residual**2) + 1e-12)along the final dimension. For scalar energies, they reduce absolute residuals.- Parameters:
key (str) – Property: “energy”, “forces”, “stress”, or “virials”.
reduce (str, optional) – Reduction mode (see above). Default: “mae”.
normalization (str, optional) – Residual normalization, “per_atom”, “per_sqrt_atom”, or “none”. Only “none” is valid for the per-atom/already-normalized properties (forces, stress). Default: “per_atom”.
delta (float, optional) – Positive Huber transition point in the units of the normalized residual. Required for Huber reductions and rejected for other reductions. Default: None.
- property name¶
Column name in logs and metrics.csv, e.g.
"forces_mae".
- get_residuals(results, batch)[source]¶
Normalized prediction-minus-target residuals.
- Parameters:
results (dict) – Model predictions.
batch (Data) – Batch with reference targets.
- Returns:
- Normalized prediction-minus-target residuals with the
predicted property’s shape.
- Return type:
- property compute_keys¶
Prediction requirements used to select energy derivatives.
The mapping contains
compute_forces,compute_stress, andcompute_virials. Training tasks combine stress and virial requests into the model’scompute_stressflag.
- property is_extensive¶
True for sum-reduced losses, which scale with the batch size.
- __call__(results, batch)[source]¶
Evaluate the loss for one batch.
- Parameters:
results (dict[str, torch.Tensor]) – Model predictions.
batch (Data) – Batch containing reference targets.
- Returns:
Scalar loss or metric.
- Return type:
- reduce_batch(results, batch)[source]¶
Compute the batch statistic for this reduction, keyed by
name.- Parameters:
results (dict) – Model predictions.
batch (Data) – Batch with reference targets.
- Returns:
A scalar sum or maximum keyed by
name.- Return type:
- get_denominator(n_atoms, n_structures)[source]¶
Count the observations used to normalize mean reductions.
- class flashcart.training.loss.WeightedSumLoss(losses, weights=None)[source]¶
Bases:
LossFunctionCombine property loss functions with specified weights.
Component losses must have distinct names when batch statistics are accumulated. The name identifies the property, normalization, and reduction, but does not include the Huber threshold.
- Parameters:
losses (list[PropertyLossFunction]) – Component losses.
weights (list[float], optional) – One weight per component. Default: all 1.0.
- property name¶
Column name in logs and metrics.csv, e.g.
"forces_mae".
- property compute_keys¶
Prediction requirements used to select energy derivatives.
The mapping contains
compute_forces,compute_stress, andcompute_virials. Training tasks combine stress and virial requests into the model’scompute_stressflag.
- property is_extensive¶
True for sum-reduced losses, which scale with the batch size.
- __call__(results, batch)[source]¶
Evaluate the loss for one batch.
- Parameters:
results (dict[str, torch.Tensor]) – Model predictions.
batch (Data) – Batch containing reference targets.
- Returns:
Scalar loss or metric.
- Return type:
- training_loss(results, batch, world_size=1)[source]¶
Evaluate the per-rank loss for distributed gradient computation.
Extensive (sum-reduced) losses are multiplied by the world size: DDP averages gradients across ranks, and the rescale turns that average back into the global-batch sum.
- Parameters:
- Returns:
- Scalar loss, scaled for distributed gradient averaging when
the reduction is extensive.
- Return type:
- reduce_batch(results, batch)[source]¶
Compute batch statistics to be combined by
reduce_overall.- Parameters:
results (dict) – Model predictions.
batch (Data) – Batch with reference targets.
- Returns:
Batch statistics keyed by metric name.
- Return type:
- class flashcart.training.loss.LossAccumulator(loss)[source]¶
Bases:
objectAccumulate batch statistics and counts for a loss or metric.
updatestores detached statistics on CPU together with atom and structure counts.computecombines these statistics and, when a distributed Fabric instance is provided, communicates them across ranks.- Parameters:
loss (LossFunction) – Loss defining how batch statistics are combined.
- update(results, batch)[source]¶
Store one batch’s detached statistics and update the observation counts.
- Parameters:
results (dict) – Model predictions.
batch (Data) – Batch with reference targets.
- flashcart.training.loss.loss_from_config(spec)[source]¶
Build a property loss or weighted sum from a configuration mapping.
- Parameters:
spec (dict) – A property specification with
propertyand optionalreduce,normalization, anddeltaentries, or aweighted_sumlist of property specifications with optionalweightentries. Defaults arereduce="mae",normalization="none", andweight=1.0.- Returns:
Configured property loss or weighted sum.
- Return type:
flashcart.training.optimizers¶
Construct optimizers from model parameters and configuration settings.
- flashcart.training.optimizers.make_optimizer(model, optimizer='muon', lr=0.01, weight_decay=0.001, muon_alignment_damping=False, muon_damping_tau=2.0, muon_damping_ema_decay=0.9, muon_damping_min_scale=0.1, muon_ns_steps=None, muon_ns_polish_steps=None, muon_ns_rms_scale=None)[source]¶
Construct optimizers from the model’s parameter-selection hooks.
With
optimizer="muon", trainable matrix parameters not excluded bynon_muon_parametersare assigned toHybridMuon. Remaining parameters are assigned to AdamW, with weight decay disabled for those returned bynon_decayable_parameters. Withoptimizer="adamw", AdamW handles all parameter groups.The returned AdamW optimizer is the main optimizer.
TrainingTaskapplies its learning-rate schedule to any auxiliary Muon optimizer.- Parameters:
model (nn.Module) – Model providing the parameter-group hooks.
optimizer (str, optional) – “muon” or “adamw”. Default: “muon”.
lr (float, optional) – Learning rate for all groups. Default: 1e-2.
weight_decay (float, optional) – Decay for the decayable groups. Default: 1e-3.
muon_alignment_damping (bool, optional) – Magma alignment damping in HybridMuon. Default: False.
muon_damping_tau (float, optional) – Damping sigmoid temperature. Default: 2.0.
muon_damping_ema_decay (float, optional) – Damping score EMA decay. Default: 0.9.
muon_damping_min_scale (float, optional) – Damping floor. Default: 0.1.
muon_ns_steps (int, optional) – Number of Newton-Schulz iterations. Default: None, using
NS_STEPS(5).muon_ns_polish_steps (int, optional) – Number of polishing iterations after the Newton-Schulz steps. Default: None, using
NS_POLISH_STEPS(0).muon_ns_rms_scale (float, optional) – Rescaling factor for the normalized update. Default: None, using
NS_RMS_SCALE(0.2).
- Returns:
- Main AdamW optimizer and a list of auxiliary Muon optimizers, or None for
the auxiliary entry when
optimizer="adamw".
- Return type:
flashcart.training.schedulers¶
Construct learning-rate schedules with warmup and decay.
- flashcart.training.schedulers.make_scheduler(optimizer, start_factor=0.1, end_factor=0.001, warmup_epochs=0, max_epochs=100, decay_epochs=0, warmup_shape='linear', decay_shape='cosine', **kwargs)[source]¶
Construct an epoch-based warmup, constant, and decay schedule.
The learning-rate factor rises from
start_factorto one during warmup. It remains constant until the finaldecay_epochs, when it decreases toend_factor. A zero warmup or decay length disables that phase.When decay is enabled,
end_factoris reached at scheduler epochmax_epochs. If the scheduler is stepped after each training epoch, this occurs after the final optimizer step.- Parameters:
optimizer (torch.optim.Optimizer) – Optimizer to schedule.
start_factor (float, optional) – Initial LR fraction, in (0, 1]. Default: 0.1.
end_factor (float, optional) – Final LR fraction, in [0, 1]. Default: 1e-3.
warmup_epochs (int, optional) – Warmup length. Default: 0.
max_epochs (int, optional) – Total epochs. Default: 100.
decay_epochs (int, optional) – Decay length (counted from the end). Default: 0.
warmup_shape (str, optional) – “linear”, “cosine”, or “sqrt”. Default: “linear”.
decay_shape (str, optional) – “linear”, “cosine”, or “sqrt”. Default: “cosine”.
**kwargs – Additional keyword arguments, accepted but unused.
- Returns:
Epoch-based learning-rate schedule.
- Return type:
- flashcart.training.schedulers.scheduler_from_config(optimizer, cfg)[source]¶
Construct a learning-rate schedule from a run configuration.
This factory reads the supplied mapping without loading packaged defaults. Omitted warmup and decay lengths are zero.
- Parameters:
optimizer (torch.optim.Optimizer) – Optimizer to schedule.
cfg (dict) – Run config (keys as in
make_scheduler).
- Returns:
- Configured schedule, or None when both warmup and decay have
zero length.
- Return type:
LRScheduler | None
flashcart.training.muon¶
Hybrid Muon optimizer with batched Newton-Schulz orthogonalisation.
Parts of this module (the Newton-Schulz iteration and the learning-rate
adjustment) are adapted from torch.optim._muon in PyTorch,
Copyright (c) 2016- Facebook, Inc. and other contributors, BSD 3-Clause
License (see NOTICE).
- class flashcart.training.muon.HybridMuon(params, alignment_damping=False, damping_tau=2.0, damping_ema_decay=0.9, damping_min_scale=0.1, ns_polish_steps=0, ns_polish_coefficients=(2.0, -1.5, 0.5), ns_rms_scale=0.2, **muon_kwargs)[source]¶
Bases:
MuonBatched Muon with optional Magma alignment damping.
Follows the Muon optimizer used to train DPA4 (arXiv:2606.02419).
Parameters with the same shape, device, and dtype are grouped for batched Newton-Schulz updates. Optional alignment damping rescales each update using a moving average of the alignment between the gradient and momentum.
- Parameters:
params (iterable) – Iterable of parameters to optimize or dicts defining parameter groups.
alignment_damping (bool, optional) – Whether to apply alignment damping. Default: False.
damping_tau (float, optional) – Tau parameter for the alignment damping sigmoid. Default: 2.0.
damping_ema_decay (float, optional) – EMA decay for the alignment damping score. Default: 0.9.
damping_min_scale (float, optional) – Minimum scale for the alignment damping. Default: 0.1.
ns_polish_steps (int, optional) – Exact-Newton polish iterations after the fast ones. Default: 0 (torch behaviour).
ns_polish_coefficients (tuple, optional) – Coefficients for those iterations. Default: (2.0, -1.5, 0.5).
ns_rms_scale (float, optional) – Match-RMS rescale constant. Default: 0.2.
**muon_kwargs – Additional keyword arguments for the base Muon optimizer.
- load_state_dict(state_dict)[source]¶
Restore the optimizer state and reset the internal parameter grouping.
Missing parameter-group settings are filled from the optimizer defaults. Parameters are matched by their order within each group, not by name.
- Parameters:
state_dict (dict) – Optimizer state returned by
state_dict().
- add_param_group(param_group)[source]¶
Add a param group to the
Optimizers param_groups.This can be useful when fine tuning a pre-trained network as frozen layers can be made trainable and added to the
Optimizeras training progresses.- Parameters:
param_group (dict) – Specifies what Tensors should be optimized along with group specific optimization options.
- step(closure=None)[source]¶
Update parameters using the current gradients.
- Parameters:
closure (callable, optional) – Function that reevaluates the loss with gradient recording enabled. Default: None.
- Returns:
- Loss returned by the closure, or None when no closure
is provided.
- Return type:
torch.Tensor | None
flashcart.training.muon_norm¶
Estimate and measure the RMS magnitude of weights updated by Muon.
The forecast models the squared weight RMS, u, by
u_next = (1 - lr*wd)**2 * u + (A*lr)**2, starting from u=1. For constant
learning rate and small lr*wd, the stationary RMS is approximately
A * sqrt(lr / (2*wd)). The accumulated quantity 2 * wd * sum(lr) estimates the
relaxation of the squared RMS toward this stationary value.
The model is an empirical approximation. Its fitted coefficient does not account explicitly for gradient correlations, alignment damping, or changes to the Newton-Schulz update. The reports compare this forecast with observed weight and momentum norms.
- flashcart.training.muon_norm.epoch_learning_rates(cfg)[source]¶
Replay the configured lr schedule, one entry per epoch.
- flashcart.training.muon_norm.forecast_weight_norm(epoch_lrs, steps_per_epoch, weight_decay, constant=0.869)[source]¶
Integrate the norm recursion over a schedule.
- Parameters:
- Returns:
ceilingis the approximate stationary RMS at the largestlearning rate.
efoldingsis the accumulated decay measure.peak_rmsandfinal_rmsare estimated from the recurrence.effective_end_factoris the finallr / rms(W)divided by its largest value at the sampled epoch endpoints. The ceiling is infinite when weight decay is nonpositive.
- Return type:
- flashcart.training.muon_norm.format_forecast(forecast)[source]¶
One line summarizing a forecast, with any warnings appended.
- flashcart.training.muon_norm.weight_rms_report(model, muon_optimizers=())[source]¶
Report aggregate Muon weight RMS and the largest individual matrix RMS.
The aggregate includes trainable floating-point matrices owned by the provided optimizers. The maximum also considers other trainable floating-point matrices unless they are excluded from weight decay. The maximum helps identify individual matrices that grow while the aggregate RMS changes little.
- Parameters:
model (torch.nn.Module) – Unwrapped model.
muon_optimizers (Iterable[torch.optim.Optimizer], optional) – Optimizers whose parameters define the aggregate. Default: an empty iterable, giving an aggregate RMS of zero.
- Returns:
- Aggregate
weight_rms, largestweight_rms_max, and its parameter name in
weight_rms_max_tensor.
- Aggregate
- Return type:
- flashcart.training.muon_norm.momentum_norm_report(optimizers)[source]¶
Report the smallest momentum norm relative to the first available epsilon.
The epsilon is taken from the first optimizer group that defines it. Comparisons therefore assume that the provided optimizers use the same Newton-Schulz normalization epsilon.
- flashcart.training.muon_norm.muon_weight_decay(optimizers)[source]¶
Read weight decay from the first available optimizer group.
- Parameters:
optimizers (Iterable[torch.optim.Optimizer]) – Optimizers searched in order.
- Returns:
- First group’s weight decay, defaulting to zero when absent, or
None when no groups are available.
- Return type:
float | None