Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,44 @@ 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. Components are identified by their dotted module paths;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also in other places) Clarify what "dotted" means in this context. Maybe easiest with a small example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotted path here refers to components such as forecaster.predictor.processor and such
also agreed, my bad, passed this very example in the docstrings all across so that it is clearer

for example, this 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 matching graph structure and features.
For graph-based models, Neural-LAM compares fingerprints of the registered graph
tensors and grid-node count; this does not verify broader dataset or feature
semantics. Checkpoints created before this metadata was introduced are rejected
by default. To use a trusted legacy checkpoint after verifying its graph, add
`--allow_missing_compatibility_metadata`; this emits a warning and makes the
caller responsible for compatibility. A stored fingerprint mismatch can never
be overridden. Cross-topology transfer is not supported.

The implemented models are:

### Graph-LAM
Expand Down
15 changes: 15 additions & 0 deletions docs/getting-started/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ 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 training from selected components of a trusted checkpoint, use
`--pretrained_checkpoint` together with one or more dotted module paths:

```{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. A
trusted older checkpoint can be used with
`--allow_missing_compatibility_metadata` after manually verifying its graph;
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
23 changes: 23 additions & 0 deletions neural_lam/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ 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,
graph: str = "multiscale",
hidden_dim: int = 64,
hidden_layers: int = 1,
Expand Down Expand Up @@ -95,6 +99,15 @@ 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 components for fresh training.
pretrained_components : list of str or None, optional
Dotted module paths to initialize from ``pretrained_checkpoint``.
freeze_components : list of str or None, optional
Dotted module paths whose parameters should remain frozen.
allow_missing_compatibility_metadata : bool, default False
Allow a legacy pretrained checkpoint without compatibility metadata.
The caller then assumes responsibility for graph compatibility.
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 +197,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 +239,12 @@ 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
),
eval=eval,
n_example_pred=n_example_pred,
create_gif=create_gif,
Expand Down
15 changes: 14 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,15 @@ 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 []
Dotted module paths to initialize from ``pretrained_checkpoint``.
freeze_components : list of str, default []
Dotted module paths to freeze during training.
allow_missing_compatibility_metadata : bool, default False
Whether selective initialization may use a legacy checkpoint without
compatibility metadata.
"""

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


@dataclasses.dataclass
Expand Down
Loading
Loading