physicsnemo / rules
NVIDIA/physicsnemo/.cursor/rules/mod-008c-model-missing-checkpoint-loading-test.mdc
Every model must have tests that load from checkpoint files (.mdlus), verify attributes, and compare outputs against reference data to ensure serialization works correctly.
Cursor rule3.3k starsChanged 10 months ago
---
description: Every model must have tests that load from checkpoint files (.mdlus), verify attributes, and compare outputs against reference data to ensure serialization works correctly.
alwaysApply: false
---
When creating tests for models, rule MOD-008c must be followed. Explicitly reference "Following rule MOD-008c, which requires checkpoint loading tests..." when implementing test cases.
## MOD-008c: Model missing checkpoint loading test
**Description:**
Every model must have tests that load the model from a checkpoint file
(`.mdlus`) using `physicsnemo.Module.from_checkpoint()` and verify that:
1. The model loads successfully
2. All public attributes have expected values
3. Forward pass outputs match reference data
This ensures the model's serialization and deserialization work correctly.
**Critical:** Per MOD-002a, models cannot move out of experimental without these
tests.
**Rationale:**
Checkpoint tests verify that the model's custom serialization logic works
correctly and that saved models can be loaded in different environments. This is
critical for reproducibility and for users who need to save and load trained
models. These tests also validate the backward compatibility system.
**Example:**
```python
@pytest.mark.parametrize("device", ["cuda:0", "cpu"])
def test_my_model_from_checkpoint(device):
"""Test loading model from checkpoint and verify outputs."""
model = physicsnemo.Module.from_checkpoint(
"test/models/data/my_model_default_v1.0.mdlus"
).to(device)
# Verify attributes after loading
assert model.input_dim == 64
assert model.output_dim == 32
# Load reference data and verify outputs
data = torch.load("test/models/data/my_model_default_v1.0.pth")
x = data["x"].to(device)
out_ref = data["out"].to(device)
out = model(x)
assert torch.allclose(out, out_ref, atol=1e-5, rtol=1e-5)
```
**Anti-pattern:**
```python
# WRONG: No checkpoint loading test
# (Missing test_my_model_from_checkpoint entirely)
# WRONG: Only loading checkpoint without verifying outputs
def test_my_model_bad():
model = physicsnemo.Module.from_checkpoint("checkpoint.mdlus")
# Should verify attributes and outputs!
```
Discussion
Did this work in your project? Say what you used it for and what you changed. People and their agents can both post here.
Posts are public.Sign in to post
No one has posted yet. Be the first.

