Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add selective pretrained component initialization and freezing for GraphLAM
transfer learning [\#856](https://github.com/mllam/neural-lam/pull/856)
@Karvy-Singh

- Add deterministic and probabilistic forecaster/module interfaces with
shared autoregressive unrolling and forecaster-owned training objectives.
[\#700](https://github.com/mllam/neural-lam/pull/700) @Sir-Sloth-The-Lazy
Expand Down
3 changes: 3 additions & 0 deletions docs/getting-started/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ python -m neural_lam.train_model --config_path $CONFIG --model graph_lam --graph

See {doc}`../user-guide/training` for the available models and training options, and {doc}`../user-guide/datastores` for how to write a config for your own data.

To initialize selected GraphLAM components from a trusted checkpoint, see
{ref}`transfer-learning`.

## 3. Evaluate the model

```bash
Expand Down
72 changes: 72 additions & 0 deletions docs/user-guide/training.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,78 @@ A few of the key ones are outlined below:
* `--gradient_checkpointing`: Recompute the activations of each autoregressive step in the backward pass instead of storing them, lowers peak memory when `--ar_steps_train` > 1 at the cost of extra compute

Checkpoints of trained models are stored under `runs/<run-name>/checkpoints/`, alongside Lightning and logger outputs for the same run.

(transfer-learning)=
## Transfer learning
Comment thread
Karvy-Singh marked this conversation as resolved.
Resolved

Selected model components can initialize a fresh GraphLAM training run from a
trusted Lightning checkpoint. Configure the target datastore and graph as for
an ordinary training run, then provide the source checkpoint and components to
transfer:

```bash
python -m neural_lam.train_model \
--config_path <target-config> \
--model graph_lam \
--graph <target-graph> \
--pretrained_checkpoint <source-checkpoint> \
--pretrained_components \
forecaster.predictor.processor \
forecaster.predictor.mesh_embedder \
forecaster.predictor.m2m_embedder
```

The three component paths above are the tested setup for reusing the processor
across datastores with different state-variable counts. A dotted module path
such as `forecaster.predictor.processor` identifies the `processor` module
inside `predictor`, which is inside `forecaster`. Dataset-dependent input and
output layers remain newly initialized.

Transferred components are fine-tuned by default. Add
`--freeze_components forecaster.predictor.processor` to keep the processor
fixed instead. Freezing is a run setting rather than checkpoint state, so repeat
it when resuming a run that should remain frozen.

The same workflow is available through the Python API:

```python
from neural_lam.api import train

run = train(
config_path="<target-config>",
model="graph_lam",
graph="<target-graph>",
pretrained_checkpoint="<source-checkpoint>",
pretrained_components=[
"forecaster.predictor.processor",
"forecaster.predictor.mesh_embedder",
"forecaster.predictor.m2m_embedder",
],
freeze_components=["forecaster.predictor.processor"],
)
```

`--pretrained_checkpoint` starts a fresh run with a new optimizer, epoch and
trainer state. It cannot be combined with `--load`, which resumes a complete
training checkpoint. Every selected component must exist in both models with
exactly matching state keys and tensor shapes.

Selective transfer also requires the same normalized graph structure, node
ordering and graph features. Neural-LAM records graph identity before runtime
precision conversion, so training precision does not affect this check.
Matching graph identity does not verify dataset-variable meaning, ordering,
units, normalization statistics or scientific suitability.

Known GraphLAM execution settings, including mesh aggregation, must also match.
Use `--allow_incompatible_pretrained_behavior` only for an intentional
behavior-changing experiment; it warns with the accepted differences. A graph
mismatch cannot be overridden, and cross-topology transfer is unsupported.

Checkpoints created before compatibility metadata was introduced are rejected
by default. To use a trusted legacy checkpoint after verifying its graph and
model settings, add `--allow_missing_compatibility_metadata`; this emits a
warning and makes the caller responsible for both forms of compatibility.

The implemented models are:

### Graph-LAM
Expand Down
35 changes: 35 additions & 0 deletions neural_lam/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def train(
precision: str | int = 32,
load: str | None = None,
restore_opt: bool = False,
pretrained_checkpoint: str | None = None,
pretrained_components: list[str] | None = None,
freeze_components: list[str] | None = None,
allow_missing_compatibility_metadata: bool = False,
allow_incompatible_pretrained_behavior: bool = False,
graph: str = "multiscale",
hidden_dim: int = 64,
hidden_layers: int = 1,
Expand Down Expand Up @@ -96,6 +101,23 @@ def train(
Path to checkpoint to resume training from.
restore_opt : bool, default False
Whether to restore optimizer state when loading checkpoint.
pretrained_checkpoint : str or None, optional
Checkpoint used to initialize selected GraphLAM components for fresh
training. Cannot be combined with ``load``, evaluation, or
``restore_opt``.
pretrained_components : list of str or None, optional
Nested module paths, such as ``forecaster.predictor.processor``, to
initialize from ``pretrained_checkpoint``.
freeze_components : list of str or None, optional
Nested module paths whose parameters should remain frozen during
training. This setting is not available during evaluation.
allow_missing_compatibility_metadata : bool, default False
Allow a legacy pretrained checkpoint without compatibility metadata.
The caller then assumes responsibility for graph and model-behavior
compatibility.
allow_incompatible_pretrained_behavior : bool, default False
Allow known model execution-setting differences during selective
initialization. A warning identifies the accepted differences.
graph : str, default "multiscale"
Name of the graph to load from the datastore graph directory.
hidden_dim : int, default 64
Expand Down Expand Up @@ -189,6 +211,10 @@ def train(
train_steps_to_log = []
if metrics_watch is None:
metrics_watch = []
if pretrained_components is None:
pretrained_components = []
if freeze_components is None:
freeze_components = []

model_config = ModelConfig(
model=model,
Expand Down Expand Up @@ -227,6 +253,15 @@ def train(
var_leads_metrics_watch=var_leads,
load=load,
restore_opt=restore_opt,
pretrained_checkpoint=pretrained_checkpoint,
pretrained_components=pretrained_components,
freeze_components=freeze_components,
allow_missing_compatibility_metadata=(
allow_missing_compatibility_metadata
),
allow_incompatible_pretrained_behavior=(
allow_incompatible_pretrained_behavior
),
eval=eval,
n_example_pred=n_example_pred,
create_gif=create_gif,
Expand Down
20 changes: 19 additions & 1 deletion neural_lam/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class TrainRunConfig:
var_leads_metrics_watch : dict of {int: list of int}, default {}
Mapping from variable index to list of watched rollout steps.
load : str or None, default None
Checkpoint path to load.
Checkpoint path used for full training resume or evaluation.
restore_opt : bool, default False
Whether to restore optimizer state from checkpoint.
eval : str or None, default None
Expand All @@ -351,6 +351,19 @@ class TrainRunConfig:
Number of example predictions to plot during testing.
create_gif : bool, default False
Whether to create animation GIFs during evaluation.
pretrained_checkpoint : str or None, default None
Checkpoint used to initialize selected components for a fresh run.
pretrained_components : list of str, default []
Nested module paths, such as ``forecaster.predictor.processor``, to
initialize from ``pretrained_checkpoint``.
freeze_components : list of str, default []
Nested module paths whose parameters should be frozen during training.
allow_missing_compatibility_metadata : bool, default False
Whether selective initialization may use a legacy checkpoint without
compatibility metadata.
allow_incompatible_pretrained_behavior : bool, default False
Whether selective initialization may use weights with known model
execution-setting differences.
gradient_checkpointing : bool, default False
Whether to recompute the activations of each autoregressive training
step in the backward pass instead of storing them. Lowers peak memory
Expand Down Expand Up @@ -378,6 +391,11 @@ class TrainRunConfig:
eval: str | None = None
n_example_pred: int = 1
create_gif: bool = False
pretrained_checkpoint: str | None = None
Comment thread
Karvy-Singh marked this conversation as resolved.
Resolved
pretrained_components: list[str] = dataclasses.field(default_factory=list)
freeze_components: list[str] = dataclasses.field(default_factory=list)
allow_missing_compatibility_metadata: bool = False
allow_incompatible_pretrained_behavior: bool = False
gradient_checkpointing: bool = False


Expand Down
Loading
Loading