Trainer¶
Trainer runs everything that learns. A script calls set_seed, train,
then optionally train_out_proj, train_finetune and prune, and
infer to predict (see Training and
Heads and fine-tuning). The other
methods are the steps train runs and the checkpoint helpers.
Trainer
¶
Trainer(config: Any, batch_callback: Callable[[Any], tuple[Tensor, Tensor]] | None = None, feature_names: list[str] | None = None, class_weights: Tensor | None = None)
Grows and trains a SONN, layer by layer.
train runs the structural search: it creates a layer of candidate
neurons, calibrates them on the layer's inputs, fits all candidates in
parallel and keeps the best ones (train_layer), and adds layers until
the growth rule (GrowthCriterion) stops it. The layers after the best
one are dropped and the model is saved. After
that, train_out_proj fits the optional output head, train_finetune
trains every parameter end to end, prune removes the neurons that do
not reach the output and infer predicts over a loader.
Every train call writes to a run folder of its own, run_dir:
<train.checkpoint_dir>/<YYYY-MM-DD-HH-MM-SS>/. It holds the step
checkpoints written as training goes, model_last.ckpt and the run's
train.log. load_model_checkpoint restores a saved model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SONNConfig - like
|
The full configuration ( |
required |
batch_callback
|
callable
|
Maps a loader batch to an |
None
|
feature_names
|
list of str
|
Feature names, used in logs and model descriptions. |
None
|
class_weights
|
tensor
|
Per-class loss weights, passed to the model at the start of
|
None
|
run_dir
property
¶
The current run folder, <checkpoint_root>/<YYYY-MM-DD-HH-MM-SS>.
train creates a new one at the start of every run, or reopens the
run it resumes, and load_model_checkpoint sets it to the run it
loads from. The run's step checkpoints, model_last.ckpt and
train.log live there. None before the first run or load.
checkpoint_root
property
¶
The folder that holds the run folders: train.checkpoint_dir of the trainer's configuration, checkpoints in the working directory by default.
set_seed
classmethod
¶
Seed random, NumPy and PyTorch (CPU and every GPU), and make cuDNN deterministic.
run_folders
classmethod
¶
List the run folders in root, oldest first.
A run folder's name is its start time, YYYY-MM-DD-HH-MM-SS, with a
-<n> suffix for runs started in the same second. They sort by the
time, then by the suffix as a number. Other entries are left out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str or Path
|
The folder to look in, usually |
required |
Returns:
| Type | Description |
|---|---|
list of Path
|
The run folders; empty when |
parse_checkpoint_step
classmethod
¶
Parse a step checkpoint file name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
str
|
File name of the form
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
key |
tuple of int
|
|
from_checkpoint
¶
Load the latest step checkpoint of a run, for resuming train.
The latest one is the highest (layer, neuron_model, step) among the
run folder's step checkpoints, _last copies included. Other files
in the folder, such as train.log and model_last.ckpt, are
ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_dir
|
str or Path
|
The run folder. Default: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
checkpoint |
dict or None
|
The checkpoint contents, or None if there is no run folder or it holds no step checkpoint. |
save_checkpoint
¶
Save a step checkpoint in the run folder and delete the older ones.
Writes model_layer_<L>_neuron_<N>_step_<S>[_<suffix>].ckpt with the
snapshot and the model config, then keeps the train.keep_last_n
most recent step checkpoints of the run folder (see
cleanup_checkpoints).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ckpt
|
StepCheckpoint
|
The training state to save. |
required |
suffix
|
str
|
Optional suffix for the file name, such as "last". Default "". |
''
|
load_model_checkpoint
¶
load_model_checkpoint(model: SONN, device: device | str = 'cpu', run: str | Path | None = None) -> None
Restore a model saved as model_last.ckpt.
Reads it from a run folder, rebuilds the layer stack, loads the
weights and moves the model to device. The run folder becomes
run_dir, so a later save_model_checkpoint writes to the same run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SONN
|
The model to restore into, built with the same config. |
required |
device
|
device or str
|
Device to move the restored model to. Default "cpu". |
'cpu'
|
run
|
str or Path
|
The run folder, by its name in |
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If there is no such run folder, or no run folder holds a
|
save_model_checkpoint
¶
Save the model's state dict as model_last.ckpt in the run folder (a new one if the trainer has none yet).
cleanup_checkpoints
¶
Delete a run folder's oldest step checkpoints, keeping the keep_last_n most recent.
Only the files named model_layer_<L>_neuron_<N>_step_<S>.ckpt
count, ordered by parse_checkpoint_step. Everything else in the
folder (model_last.ckpt, the _last copies, train.log, other
files and subfolders) is left alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint_dir
|
str or Path
|
The run folder. |
required |
keep_last_n
|
int
|
Number of most recent step checkpoints to keep. Default 10. |
10
|
cleanup_layer_checkpoints
¶
Delete every step checkpoint of one layer from the run folder, _last copies included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_idx
|
int
|
The layer whose checkpoints are deleted. |
required |
init_distributed
classmethod
¶
Initialize torch.distributed for multi-GPU / multi-node training.
Call once before Trainer.train(), then launch with torchrun:
torchrun --nproc_per_node=
Sets LOCAL_RANK as the active CUDA device so each rank uses a distinct GPU within the node.
train
¶
train(model: SONN, train_dl: DataLoader, dev_dl: DataLoader, test_dl: DataLoader, verbose: bool = True, resume: bool | str = False, val_dl: DataLoader | None = None) -> SONN
Grow and train the network, one layer at a time.
Each call is a run with a folder of its own, run_dir: a new
<train.checkpoint_dir>/<YYYY-MM-DD-HH-MM-SS>/, or the folder of the
run it resumes. The run's step checkpoints, model_last.ckpt and
train.log (every record of the root logger from the start of the
run until the next run starts) are written there.
Each iteration creates a layer of candidate neurons, calibrates them
on the layer's inputs, trains them and keeps the best ones
(train_layer). Growth stops when GrowthCriterion sees no
improvement for train.criterion_minimum_width layers, or at
train.max_layer_count. The layers after the one with the smallest
error are then dropped, and the model is saved as model_last.ckpt
in the run folder. The output head and the end-to-end fine-tune are
separate steps (train_out_proj, train_finetune).
With model.selection_mode: joint each layer's survivors are
selected jointly instead, by train_joint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SONN
|
The model to train. Its existing layers are discarded. |
required |
train_dl
|
DataLoader
|
Training split: fits the neuron coefficients. |
required |
dev_dl
|
DataLoader
|
Dev split: scores and selects the neurons, and drives the growth
rule unless |
required |
test_dl
|
DataLoader
|
Not used during training. |
required |
verbose
|
bool
|
Log the time each layer takes. Default True. |
True
|
resume
|
bool or str
|
False starts a new run. True continues the newest run folder in
|
False
|
val_dl
|
DataLoader
|
Validation split that selects nothing. Its error is logged per
layer, and it drives the growth rule when |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
model |
SONN
|
The trained model (the same object). |
train_layer
¶
train_layer(model: SONN, layer: SONNLayer, train_dl: DataLoader, dev_dl: DataLoader, checkpoint_data: dict[str, Any] | None, nbest: int | None = None, selection_method: str | None = None, joint_pool: bool = False) -> None
Calculate neuron weights
nbest and selection_method override model.nbest_neurons and
train.neuron_selection_method for this layer's selection; the
joint selection mode uses them to keep its pool. joint_pool skips
the per-layer fine-tune and the readout head (layer_finetune,
layer_err_source: readout): the joint pass takes their place.
train_model_ensemble
¶
train_model_ensemble(model: SONN, neuron_model: BasePolynomNeuron, neuron_model_idx: int, layer_idx: int, train_dl: DataLoader, dev_dl: DataLoader, checkpoint_data: dict[str, Any] | None, accumulator: LayerAccumulator, loss_fn_vmapped: Callable, eval_loss_fn_vmapped: Callable, params_batch: dict[str, Tensor], buffers_batch: dict[str, Tensor], shared_param_names: list[str], features_precomputed: bool = False) -> tuple[LayerAccumulator, dict[str, torch.Tensor], StepCheckpoint | None]
Train one neuron module's candidate ensemble in parallel.
Runs the configured batched optimizer over the stacked parameters of
all candidates, with per-candidate early stopping and learning-rate
drops (on the dev loss, or on the training loss when
train.early_stop_source is 'train'). Step checkpoints are saved
every train.save_interval steps and at the end, and the trained
parameters are written back to the module. Under distributed
training each rank trains its own slice of the ensemble.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SONN
|
The model being grown. |
required |
neuron_model
|
BasePolynomNeuron
|
The neuron module whose candidates are trained. |
required |
neuron_model_idx
|
int
|
Index of that module in the layer. |
required |
layer_idx
|
int
|
Index of the layer. |
required |
train_dl
|
DataLoader
|
Training and dev splits. |
required |
dev_dl
|
DataLoader
|
Training and dev splits. |
required |
checkpoint_data
|
dict or None
|
Step checkpoint to resume from, or None to start fresh. |
required |
accumulator
|
LayerAccumulator
|
The layer's running state, stored in the checkpoints. |
required |
loss_fn_vmapped
|
callable
|
Gradient and evaluation functions from |
required |
eval_loss_fn_vmapped
|
callable
|
Gradient and evaluation functions from |
required |
params_batch
|
dict of str to tensor
|
Stacked candidate parameters and buffers. |
required |
buffers_batch
|
dict of str to tensor
|
Stacked candidate parameters and buffers. |
required |
shared_param_names
|
list of str
|
Parameters shared by all candidates (the shared projection). |
required |
features_precomputed
|
bool
|
The loaders already yield the layer's input features, so the model forward is skipped. Default False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
accumulator |
LayerAccumulator
|
The accumulator passed in. |
params_batch |
dict of str to tensor
|
The trained candidate parameters. |
last_checkpoint |
StepCheckpoint
|
The snapshot of the finished ensemble, as saved. |
train_out_proj
¶
Train model.out_proj with all other parameters frozen.
Runs a standard PyTorch training loop — no vmapped ensemble, no custom optimizer — because out_proj is a single shared nn.Linear whose gradient comes from the full batch in one shot.
Stops at cfg.max_steps, or after early_stop_patience evaluations in a
row without an improvement of the dev loss. ReduceLROnPlateau lowers
the learning rate when the dev loss stalls, down to lr_min. With
optimizer: lbfgs the head is fitted full-batch instead, without the
plateau schedule.
The head ends on its last step, or with cfg.keep_best_weights on
the weights of its lowest dev loss (a last step between two
evaluations is evaluated too), copied back in place from where
cfg.best_weights_copy keeps them. The model is then saved to the
run folder's model_last.ckpt.
train_finetune
¶
train_finetune(model: SONN, train_dl: DataLoader, dev_dl: DataLoader, cfg: Any = None, val_dl: DataLoader | None = None) -> None
End-to-end fine-tuning of all SONN parameters.
After SONN's structural search is done every layer is a normal differentiable module — gradients flow through BasePolynomNeuron.weight, shared_proj / soft_binner / neuron_proj, and out_proj all at once.
The step is launch-bound on these models (hundreds of tiny kernels),
so on a CUDA device with adam / adamw it is captured once as a CUDA
graph over a static buffer of the loader's batch shape and replayed
per step, with the next batch copied into the buffer; see
training.finetune.FinetuneStep and OutProjTrainConfig.cuda_graph /
data_on_device. Evaluation, the plateau schedule and the early
stop run outside the graph every eval_interval steps.
cfg is an OutProjTrainConfig-shaped block of LR / scheduler /
early-stop hyperparameters; it defaults to
model.param.train.finetune_train, which exists so an end-to-end pass
can use a different (usually much smaller) learning rate than the
out_proj / layer_finetune passes that share out_proj_train.
The model ends on its last step, or with cfg.keep_best_weights on
the parameters of its lowest loss on the split the early stop reads
(a last step between two evaluations is evaluated too), copied back
in place, so a captured graph stays valid, from where
cfg.best_weights_copy keeps them. The model is then saved to the
run folder's model_last.ckpt.
train_joint
¶
train_joint(model: SONN, train_dl: DataLoader, dev_dl: DataLoader, verbose: bool = True, val_dl: DataLoader | None = None) -> SONN
Grow the network with neurons selected jointly at every layer.
The joint selection mode (model.selection_mode: joint, called by
train). A greedy layer keeps the candidates that score best on
their own; here each layer keeps the ones a sparsity penalty leaves
when a pool of candidates is trained together with a head
(_train_joint_layer):
- The layer's candidates are created, calibrated and fitted on
their own, as
train_layerdoes for a greedy layer. Those fits are the pool's starting weights;train.joint_sparsity.prefilterdecides how many enter the pool (all finite ones, or prefilter_mult xnbest_neuronsby a selection method). - A temporary head over the whole pool is fitted with the neurons
frozen (
train_out_proj). - The layer, the head and, under 'gates', the gates are trained
together on the training split with loss = data loss + lam *
penalty (
train.joint_sparsity); the earlier layers are frozen and the layer's input is computed once. Everyeval_intervalsteps the log shows lam, both terms, the loss on dev (or the validation split undertrain.stop_source: val) and the number of active neurons. Underlam_control'anneal' / 'adaptive' lam is steered towardnbest_neurons: the pass stops once the count holds just above it, and rolls back to the closest state from above if the count falls below it (training.joint_selection.CountControl). - The layer is cut to
nbest_neuronsof the active neurons (if more remain, the ones contributing most to the prediction: the norm of their head column times the standard deviation of their gated, scaled output on dev). Gates and head are discarded: the next layer reads the survivors' own outputs. Withtrain.layer_finetunethe survivors are then trained together with a temporary head (training.finetune.train_layer_finetune), as a greedy layer's are. The survivors'err_valuesbecome their own dev errors after the pass, before the fine-tune, as in a greedy layer.
Each layer then gets an error for the growth rule, as greedy layers
do (GrowthCriterion, train.criterion_minimum_width,
stop_train_min_delta, max_layer_count), by
train.layer_err_source: 'neuron' scores its survivors' own dev
errors after the pass (reduced by layer_err_criterion), 'readout'
the dev loss of a head refitted over them. Both are logged. When
growth stops, the layers after the best one are dropped, and the
model's head (num_out_neurons wide) is fitted over the last layer
(train_out_proj). The model is saved as model_last.ckpt;
train_finetune polishes it like any other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SONN
|
The model to train. Its existing layers are discarded. |
required |
train_dl
|
DataLoader
|
Training split (all fits) and dev split (candidate selection, the heads' early stop, the pass's log and the layer error). |
required |
dev_dl
|
DataLoader
|
Training split (all fits) and dev split (candidate selection, the heads' early stop, the pass's log and the layer error). |
required |
verbose
|
bool
|
Log the time each layer takes, and show progress bars. |
True
|
val_dl
|
DataLoader
|
Validation split: its layer error is logged, and under
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
model |
SONN
|
The trained model (the same object). |
train_calibration
¶
Fit train.calibration on the dev split and store it on the model.
The training passes optimize the training loss (class weighted when
class weights are set) while a multi-class model is scored on the
plain log loss, so its probabilities can come out over- or
underconfident. This pass fixes that without changing a weight: it
collects the dev split's log-probabilities and fits 'temperature'
(one scalar, argmax unchanged) or 'vector' (a scale and a shift per
class) with LBFGS against the dev log loss. The result lands in the
model's calib_* buffers, applies inside SONN.infer and is saved
to the run folder's model_last.ckpt.
The buffers are reset to identity before the dev pass, so a refit
replaces an earlier calibration instead of composing with it. A fit
that ends worse than identity on dev is not kept. With
train.calibration null the call does nothing.
infer
¶
infer(model: SONN, test_dl: DataLoader, verbose: bool = True, use_compile: bool = False) -> tuple[torch.Tensor, torch.Tensor]
Predict over test_dl and return (predictions, targets), both in loader order.
The predictions are one value per row for regression, a logit per
row for binary models and a row of log-probabilities per sample for
multi-class models (see SONN.infer). Each batch must be an
(x, y) pair: batch_callback is not applied here.
use_compile=True wraps model.infer with torch.compile before the loop, fusing the sequential per-layer kernel launches into a single optimized graph. First batch is slower (compilation), subsequent batches are faster — worthwhile when test_dl has many batches or is called repeatedly.
prune
¶
Strip the network down to the neurons that actually reach the output.
The last layer is cut to the columns the model reads, and every earlier
layer is then cut to whatever the layers reading it still reference —
its successor, and with shortcut.prev_layers later layers too. A
layer nobody reads any more is deleted.
How many columns the model reads is SONN._readout_width. Without a
head, SONN.infer returns the best-error column and a single neuron
suffices, except for a multi-class model with per-neuron projections
(use_neuron_proj): its prediction sums every last-layer neuron's
projected output, so the whole last layer stays. With out_proj it
reads in_features columns, chosen by _best_neuron_columns — so
collapsing the layer to one neuron would leave the head consuming
zero-padding on every other input and silently destroy the model
rather than raising. Keep exactly the columns the head consumes
instead.
The head needs no reindexing: the retained neurons keep their
err_values, so re-running _best_neuron_columns on the pruned layer
re-derives the same neurons in the same ascending-error order, which is
the order the head's inputs were fitted in. Every kept layer's
d_model is set to its new width.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the model was built with |
GrowthCriterion
dataclass
¶
GrowthCriterion(width: int, epsilon: float, min_delta: float = 0.0, min_error: float = sys.float_info.max, ref_error: float = sys.float_info.max, best_index: int = 0, last_improved_index: int = 0, last_gain: float = 0.0, last_margin: float = 0.0, last_since: int = 0)
The layer-growth stop rule of Trainer.train, kept out of the loop so
it can be tested on a scripted sequence of layer errors.
A layer improves when it lowers the error of the last accepted
improvement (ref_error) by at least max(min_delta, epsilon * best),
so several small steps down add up instead of each failing the margin
on its own; the search stops after width consecutive layers without
an improvement. best_index (the layer with the lowest error, margin or
not) is what the trainer keeps; the window is counted from
last_improved_index. Layer 0 always improves.
update
¶
Record a layer's error; returns True when the search should stop.
kept_index
¶
Return the index of the last layer the model keeps once the search ends.
train.layer_keep 'best' keeps the layers up to best_index, 'all'
every layer built (layer_count of them).
describe
¶
Return a one-line log message on the last update: error, best layer, gain and margin, layers without improvement.
setup_logger
¶
Configure the root logger for a training script.
Replaces every handler on the root logger (closing them, a run's
train.log handler included) with a tqdm-aware console handler at INFO
level, plus a file handler when log_path is given. Each training run
also writes its own train.log in its run folder (see
Trainer.run_dir), so a file here is needed only for lines outside
the runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_path
|
(str, Path or None)
|
A log file to write as well, overwritten on each call. Default None: the console only. |
None
|
Returns:
| Type | Description |
|---|---|
Logger
|
The configured root logger. |