Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ Use the environment variable `MLFLOW_TRACKING_URI` to set the URI of the MLFlow

## Train Models
Models can be trained using `python -m neural_lam.train_model --config_path <config_path>`.
Run `python neural_lam.train_model --help` for a full list of training options.
Run `python -m neural_lam.train_model --help` for a full list of training options.
A few of the key ones are outlined below:

* `--config_path`: Path to the configuration for neural-lam (for example in `data/myexperiment/config.yaml`).
Expand All @@ -515,6 +515,54 @@ A few of the key ones are outlined below:
* `--loss`: Which of the loss functions in `neural_lam/metrics.py` to train on

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

### Transfer learning

Selected model components can initialize a fresh training run from a trusted
Lightning checkpoint. This workflow currently supports GraphLAM. A dotted
module path identifies a nested module by
joining its attribute names with periods. For example,
`forecaster.predictor.processor` means the `processor` module inside
`predictor`, which is inside `forecaster`. This command transfers GraphLAM's
processor and mesh embedders while leaving dataset-dependent input and output
layers newly initialized:

```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
```

Add `--freeze_components forecaster.predictor.processor` to keep the
transferred processor parameters fixed during training. Freezing is a run
setting rather than checkpoint state, so repeat it when resuming a run that
should remain frozen.

`--pretrained_checkpoint` starts a fresh run with a new optimizer, epoch and
trainer state. It is therefore distinct from, and 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
20 changes: 20 additions & 0 deletions docs/getting-started/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ python -m neural_lam.train_model --config_path <path-to-config> --model graph_la

Neural-LAM supports several models like `graph_lam`, `hi_lam`, and `hi_lam_parallel`.

To start fresh GraphLAM training from selected components of a trusted
checkpoint, use
`--pretrained_checkpoint` with nested module paths such as
`forecaster.predictor.processor` (the `processor` inside `predictor`, inside
`forecaster`):

```{code-block} bash
python -m neural_lam.train_model --config_path <path-to-config> --model graph_lam --graph <graph-name> --pretrained_checkpoint <checkpoint-path> --pretrained_components forecaster.predictor.processor
```

This differs from `--load`, which resumes the complete training state. Selected
components must have matching state keys and tensor shapes in the source and
target models. Graph-model checkpoints must also contain the graph fingerprint
used to reject incompatible topology, node ordering and graph features. Known
GraphLAM execution settings such as mesh aggregation must also match unless an
intentional experiment uses `--allow_incompatible_pretrained_behavior`, which
emits a warning with the differences. A trusted older checkpoint can be used with
`--allow_missing_compatibility_metadata` after manually verifying its graph and
model behavior; known fingerprint mismatches are always rejected.

## Step 4: Evaluate

After training, you can evaluate the model on the test set by loading the saved checkpoint.
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 @@ -95,6 +100,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 @@ -184,6 +206,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 @@ -222,6 +248,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.
"""

epochs: int = 200
Expand All @@ -374,6 +387,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


@dataclasses.dataclass
Expand Down
Loading
Loading