Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions neural_lam/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .step_predictors.graph.hi_lam_parallel import HiLAMParallel
from .step_predictors.graph.hierarchical import BaseHiGraphModel
from .step_predictors.persistence import PersistencePredictor
from .transfer import initialize_graph_lam_from_checkpoint

# NOTE: GraphEFM/GraphEFMMultiScale are intentionally NOT registered in
# MODELS yet.
Expand Down
133 changes: 133 additions & 0 deletions neural_lam/models/transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"""Selective initialization of model components from checkpoints."""

# Standard library
from collections.abc import Mapping, Sequence
from pathlib import Path

# Third-party
import torch

# Local
from .step_predictors.graph.graph_lam import GraphLAM

SUPPORTED_GRAPH_LAM_TRANSFER_MODULES = frozenset({"processor"})


def initialize_graph_lam_from_checkpoint(
target_model: GraphLAM,
checkpoint_path: str | Path,
modules: Sequence[str],
) -> None:
"""Initialize selected GraphLAM modules from a Lightning checkpoint.

The complete state of every selected module must be present and have the
same parameter shapes as the target module. Validation finishes before any
target weights are changed.

Parameters
----------
target_model : GraphLAM
Newly initialized model that receives the pretrained weights.
checkpoint_path : str or pathlib.Path
Path to a trusted Neural-LAM Lightning checkpoint.
modules : sequence of str
GraphLAM modules to initialize. Currently only ``"processor"`` is
supported.

Raises
------
TypeError
If ``target_model`` is not a GraphLAM model.
ValueError
If the module selection or checkpoint is incompatible with the target
model.
"""
if not isinstance(target_model, GraphLAM):
raise TypeError("Selective initialization currently requires GraphLAM.")

selected_modules = tuple(modules)
if not selected_modules:
raise ValueError("At least one GraphLAM module must be selected.")
if len(set(selected_modules)) != len(selected_modules):
raise ValueError(
"GraphLAM transfer modules must not contain duplicates."
)

unsupported = sorted(
set(selected_modules) - SUPPORTED_GRAPH_LAM_TRANSFER_MODULES
)
if unsupported:
supported = ", ".join(sorted(SUPPORTED_GRAPH_LAM_TRANSFER_MODULES))
raise ValueError(
f"Unsupported GraphLAM transfer modules: {unsupported}. "
f"Supported modules: {supported}."
)

checkpoint = torch.load(
checkpoint_path,
map_location="cpu",
weights_only=False,
)
if not isinstance(checkpoint, Mapping):
raise ValueError("Checkpoint must contain a mapping.")

source_state = checkpoint.get("state_dict")
if not isinstance(source_state, Mapping):
raise ValueError("Checkpoint does not contain a valid 'state_dict'.")

validated_states: dict[str, dict[str, torch.Tensor]] = {}
for module_name in selected_modules:
prefix = f"forecaster.predictor.{module_name}."
raw_module_state = {
key.removeprefix(prefix): value
for key, value in source_state.items()
if isinstance(key, str) and key.startswith(prefix)
}
if not raw_module_state:
raise ValueError(
f"Checkpoint does not contain GraphLAM module {module_name!r}."
)

target_state = getattr(target_model, module_name).state_dict()
missing = sorted(target_state.keys() - raw_module_state.keys())
unexpected = sorted(raw_module_state.keys() - target_state.keys())
if missing or unexpected:
raise ValueError(
f"Incompatible state keys for GraphLAM module {module_name!r}: "
f"missing={missing}, unexpected={unexpected}."
)

non_tensors = sorted(
key
for key, value in raw_module_state.items()
if not isinstance(value, torch.Tensor)
)
if non_tensors:
raise ValueError(
f"Non-tensor state values for GraphLAM module {module_name!r}: "
f"{non_tensors}."
)

module_state = {
key: value
for key, value in raw_module_state.items()
if isinstance(value, torch.Tensor)
}
shape_mismatches = [
f"{key}: checkpoint {tuple(module_state[key].shape)} != "
f"target {tuple(target_state[key].shape)}"
for key in target_state
if module_state[key].shape != target_state[key].shape
]
if shape_mismatches:
raise ValueError(
f"Incompatible parameter shapes for GraphLAM module "
f"{module_name!r}: {'; '.join(shape_mismatches)}."
)

validated_states[module_name] = module_state

for module_name, module_state in validated_states.items():
getattr(target_model, module_name).load_state_dict(
module_state, strict=True
)
225 changes: 225 additions & 0 deletions tests/test_transfer_learning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
"""Tests for selective GraphLAM checkpoint initialization.

These tests use minimal Lightning-shaped checkpoints with synthetic model
weights. They verify transfer mechanics, not full Lightning serialization or
whether pretrained representations improve forecast skill.
"""

# Standard library
from pathlib import Path

# Third-party
import pytest
import torch

# First-party
from neural_lam.create_graph import create_graph_from_datastore
from neural_lam.models import GraphLAM, initialize_graph_lam_from_checkpoint
from tests.dummy_datastore import DummyDatastore


class ReducedStateDummyDatastore(DummyDatastore):
"""Dummy datastore with fewer target state variables."""

N_FEATURES = dict(DummyDatastore.N_FEATURES, state=3)


def create_flat_graph(datastore):
"""Create the one-level graph required by the test GraphLAM models."""
graph_dir = Path(datastore.root_path) / "graph" / "1level"
create_graph_from_datastore(
datastore=datastore,
output_root_path=str(graph_dir),
n_max_levels=1,
)


@pytest.fixture(scope="module")
def graph_datastore():
"""Create a small datastore and flat graph shared by transfer tests."""
datastore = DummyDatastore(n_grid_points=81)
create_flat_graph(datastore)
return datastore


@pytest.fixture(scope="module")
def different_feature_datastores():
"""Create source and target datastores with different state variables."""
source_datastore = DummyDatastore(n_grid_points=81)
target_datastore = ReducedStateDummyDatastore(n_grid_points=81)
create_flat_graph(source_datastore)
create_flat_graph(target_datastore)
return source_datastore, target_datastore


def build_graph_lam(datastore, hidden_dim=4):
"""Build the small GraphLAM configuration used by transfer tests."""
return GraphLAM(
datastore=datastore,
graph_name="1level",
hidden_dim=hidden_dim,
hidden_layers=1,
processor_layers=1,
num_past_forcing_steps=0,
num_future_forcing_steps=0,
)


def save_graph_lam_checkpoint(model, path):
"""Save a minimal synthetic checkpoint consumed by the transfer utility."""
state_dict = {
f"forecaster.predictor.{key}": value.detach().clone()
for key, value in model.state_dict().items()
}
torch.save(
{"state_dict": state_dict},
path,
)


def clone_state(module):
"""Clone a module state for before/after comparisons."""
return {key: value.clone() for key, value in module.state_dict().items()}


def assert_state_equal(actual, expected):
"""Assert that a module state exactly matches a saved state."""
assert actual.keys() == expected.keys()
for key, value in actual.items():
assert torch.equal(value, expected[key]), key


def test_transfer_supports_different_state_variable_counts(
different_feature_datastores, tmp_path
):
"""Keep processor transfer working across different feature dimensions."""
source_datastore, target_datastore = different_feature_datastores
source = build_graph_lam(source_datastore)
target = build_graph_lam(target_datastore)

assert source.grid_input_dim != target.grid_input_dim
assert source.grid_output_dim != target.grid_output_dim
graph_tensors = (
"m2m_edge_index",
"m2m_features",
"mesh_static_features",
"g2m_edge_index",
"m2g_edge_index",
"g2m_features",
"m2g_features",
)
for name in graph_tensors:
assert torch.equal(getattr(source, name), getattr(target, name)), name

grid_embedder_before = clone_state(target.grid_embedder)
output_map_before = clone_state(target.output_map)

checkpoint_path = tmp_path / "different_features.ckpt"
save_graph_lam_checkpoint(source, checkpoint_path)
initialize_graph_lam_from_checkpoint(
target,
checkpoint_path,
modules=["processor"],
)

assert_state_equal(
target.processor.state_dict(), source.processor.state_dict()
)
assert_state_equal(target.grid_embedder.state_dict(), grid_embedder_before)
assert_state_equal(target.output_map.state_dict(), output_map_before)

batch_size = 2
num_grid_nodes = target.num_grid_nodes
num_state_vars = target_datastore.get_num_data_vars("state")
num_forcing_vars = target_datastore.get_num_data_vars("forcing")
prev_state = torch.randn(batch_size, num_grid_nodes, num_state_vars)
prev_prev_state = torch.randn(batch_size, num_grid_nodes, num_state_vars)
forcing = torch.randn(batch_size, num_grid_nodes, num_forcing_vars)

prediction, prediction_std = target(
prev_state,
prev_prev_state,
forcing,
)
assert prediction.shape == prev_state.shape
assert prediction_std is None

prediction.square().mean().backward()
processor_gradients = [
parameter.grad for parameter in target.processor.parameters()
]
assert all(gradient is not None for gradient in processor_gradients)
assert all(
torch.isfinite(gradient).all() for gradient in processor_gradients
)


def test_incompatible_processor_shapes_do_not_modify_target(
graph_datastore, tmp_path
):
"""Reject incompatible shapes before changing any target parameters."""
source = build_graph_lam(graph_datastore, hidden_dim=4)
target = build_graph_lam(graph_datastore, hidden_dim=8)
target_before = clone_state(target.processor)

checkpoint_path = tmp_path / "incompatible.ckpt"
save_graph_lam_checkpoint(source, checkpoint_path)

with pytest.raises(ValueError, match="Incompatible parameter shapes"):
initialize_graph_lam_from_checkpoint(
target,
checkpoint_path,
modules=["processor"],
)

assert_state_equal(target.processor.state_dict(), target_before)


def test_incomplete_processor_state_does_not_modify_target(
graph_datastore, tmp_path
):
"""Reject a missing processor parameter before changing the target."""
source = build_graph_lam(graph_datastore)
target = build_graph_lam(graph_datastore)
target_before = clone_state(target.processor)

checkpoint_path = tmp_path / "incomplete.ckpt"
save_graph_lam_checkpoint(source, checkpoint_path)
checkpoint = torch.load(checkpoint_path, weights_only=False)
missing_key = next(
key
for key in checkpoint["state_dict"]
if key.startswith("forecaster.predictor.processor.")
)
del checkpoint["state_dict"][missing_key]
torch.save(checkpoint, checkpoint_path)

with pytest.raises(ValueError, match="Incompatible state keys"):
initialize_graph_lam_from_checkpoint(
target,
checkpoint_path,
modules=["processor"],
)

assert_state_equal(target.processor.state_dict(), target_before)


@pytest.mark.parametrize(
("modules", "match"),
[
([], "At least one GraphLAM module"),
(["grid_embedder"], "Unsupported GraphLAM transfer modules"),
],
)
def test_rejects_invalid_module_selection(
graph_datastore, tmp_path, modules, match
):
"""Reject empty or unsupported module selections before loading a file."""
target = build_graph_lam(graph_datastore)

with pytest.raises(ValueError, match=match):
initialize_graph_lam_from_checkpoint(
target,
tmp_path / "unused.ckpt",
modules=modules,
)
Loading