feat(crash): lazy Zarr loading for multi-GPU DDP training
Summary
- Add lazy Zarr loading (
lazy_load: trueby default) so mesh trajectories and point features materialize on first sample access instead of at dataset construction. - Update the crash datapipe to stream stats and defer per-sample tensors/graphs when lazy records are returned.
- Document DDP memory behavior (
DistributedSamplershards indices, not host RAM) in the crash README and log a hint intrain.pywhen multi-GPU + lazy Zarr are active.
closes #1550
Test plan
- [x]
pytest examples/structural_mechanics/crash/tests/test_zarr_reader.py examples/structural_mechanics/crash/tests/test_datapipe_lazy.py - [x] Eager-mode smoke test with
Reader(lazy_load=False)(all samples materialized at init) - [x] pre-commit on changed files
Notes
- This PR is scoped to #1550 (lazy Zarr + DDP docs). A few short docstrings on existing crash example symbols were added only as a temporary pre-commit workaround — the interrogate hook falsely flagged baseline-grandfathered items due to a path-parsing bug in
check_docstring_coverage.py(details in follow-up comment). - Root-cause fix tracked in #1704.
This pull request requires additional validation before any workflows can run on NVIDIA's runners.
Pull request vetters can view their responsibilities here.
Contributors can view more details about this message here.
Follow-up: interrogate hook false positives (separate PR)
What this PR is for: lazy Zarr loading and DDP memory documentation for #1550.
While getting this through pre-commit, the interrogate check failed even though the flagged symbols (SimSample.to, CrashGraphDataset.create_graph, Trainer.train, etc.) were already listed in test/ci_tests/interrogate_baseline.txt.
Root cause: test/ci_tests/check_docstring_coverage.py parses interrogate section headers with a regex that only matches lines surrounded by 4+ equals signs (={4,}). Interrogate 1.7 (pinned in pre-commit) prints headers like:
= Coverage for /abs/path/examples/structural_mechanics/crash/ =
Because that header is not matched, current_dir stays empty and file paths are resolved incorrectly (e.g. ../../../datapipe.py instead of examples/structural_mechanics/crash/datapipe.py). Baseline entries then look like "new" undocumented items whenever those example files are edited.
Temporary workaround in this PR: added small docstrings on those symbols so they are no longer reported as MISSED and the hook passes.
Actual fix: #1704 broadens the header parser so baseline matching works correctly — this means no docstring workarounds needed going forward even for the next person who touches an example file with baseline gaps.
Follow-up parser fix opened in #1704.
Greptile Summary
This PR adds lazy Zarr loading (lazy_load: true by default) to the crash example so that mesh trajectories and point features are materialized on first __getitem__ access instead of at dataset construction — reducing per-rank host RAM usage in multi-GPU DDP training. It also documents the DDP memory model in the README and adds a log hint in train.py.
zarr_reader.pygainsvalidate_zarr_store,load_zarr_edges, andmaterialize_zarr_recordhelpers;Readernow always returns a 4-tuple and accepts alazy_loadflag.datapipe.pysplits the per-sample build/normalize pipeline into lazy and eager branches, with new lazy stat-computation methods (_compute_autoreg_node_stats_lazy,_compute_feature_stats_lazy,_compute_edge_stats_lazy) and on-demand materialization helpers (_ensure_sample_loaded,_ensure_graph_loaded).- Two new test files cover lazy reader round-trips and deferred-materialization behaviour.
Important Files Changed
| Filename | Overview |
|---|---|
| examples/structural_mechanics/crash/datapipe.py | Adds lazy loading support with two correctness bugs: biased running-mean in _compute_feature_stats_lazy/_compute_edge_stats_lazy, and mismatched normalization state between edge stat computation (unnormalized) and actual graph construction (normalized). |
| examples/structural_mechanics/crash/zarr_reader.py | Cleanly adds lazy-load support: new validate_zarr_store, load_zarr_edges, and materialize_zarr_record helpers; Reader gains lazy_load flag and now always returns a 4-tuple with an empty global_features list. |
| examples/structural_mechanics/crash/tests/test_datapipe_lazy.py | New test verifies deferred materialization behavior; does not exercise multi-sample stat computation, so the biased-mean bugs are not caught. |
| examples/structural_mechanics/crash/tests/test_zarr_reader.py | Updates create_dataset to create_array (zarr v3 API), adds lazy-load reader tests and materialize_zarr_record round-trip test; changes are clean. |
| examples/structural_mechanics/crash/train.py | Minor: adds an info log for multi-GPU + lazy-load path and docstrings; no logic changes. |
| examples/structural_mechanics/crash/conf/reader/zarr.yaml | Adds lazy_load: true to the Zarr reader config; straightforward change. |
| examples/structural_mechanics/crash/README.md | Adds accurate documentation of DDP + Zarr memory behavior and lazy_load configuration; no issues. |
Comments Outside Diff (1)
-
examples/structural_mechanics/crash/datapipe.py, line 646-660 (link)Edge stats computed on unnormalized positions, but graphs are built from normalized positions
_compute_edge_stats_lazycalls_load_sample_tensors(i, retain=False, normalize=False), so the temporary graphs it creates have edge features (displacements and distances) derived from raw, unnormalized node positions. However,_ensure_graph_loadedcalls_ensure_sample_loadedfirst — which stores normalized positions inself.mesh_pos_seq— then usesself.mesh_pos_seq[batch_idx][0](the normalized position) to build the final graph. The resulting edge features are therefore on a completely different scale from the statistics used to normalize them. In the eager path, graphs are built from already-normalized positions (because_normalize_loaded_samplesruns inside the base-class__init__before graph construction), so its stats and features are consistent. The lazy path breaks that consistency and will produce garbage edge features at training time.
Reviews (1): Last reviewed commit: "feat(crash): add lazy Zarr loading for m..." | Re-trigger Greptile
Reviewed PR comments: triage:
- Greptile P1 (lazy feature/edge running mean): Valid. Fixed in 0b5e96a — lazy stat helpers now use the same
zeros+m/num_samplesaccumulation as the eager path (first sample was previously overweighted). - Greptile P1 (edge stats on raw vs normalized positions): Valid for graph + lazy path. Fixed in 0b5e96a —
_compute_edge_stats_lazynow materializes normalized positions before building temporary graphs, matching eager construction order. - Greptile P2 (eager double-normalize landmine): Valid defensive fix. Addressed in 0b5e96a — non-lazy
_load_sample_tensorsreturns already-normalized tensors without re-applying normalization.
Thanks @greptile-apps for the catch.
Thanks for the detailed triage and for addressing all three issues. The lazy stat accumulation, normalized-position edge stats, and eager double-normalize fixes in 0b5e96a all look correct.
One remaining issue worth flagging: the eager _compute_edge_stats still has the original first-sample overweighting bug that was fixed in the lazy path. The sentinel None pattern leaves m_0 with full weight while m_1…m_{N-1} are each divided by N:
# datapipe.py – _compute_edge_stats (eager)
edge_mean = m if edge_mean is None else edge_mean + m / self.num_samples
# ↑ full weight for i=0; all others get 1/N weight
This should use the same zeros + m/N pattern as _compute_edge_stats_lazy:
def _compute_edge_stats(self):
edge_mean = None
edge_meansqr = None
edge_dim = None
for i in range(self.num_samples):
x_e = self.graphs[i].edge_attr.to(torch.float32) # [E,De]
if edge_dim is None:
edge_dim = x_e.shape[1]
edge_mean = torch.zeros(edge_dim, dtype=torch.float32)
edge_meansqr = torch.zeros(edge_dim, dtype=torch.float32)
m = torch.mean(x_e, dim=0)
msq = torch.mean(x_e * x_e, dim=0)
edge_mean += m / self.num_samples
edge_meansqr += msq / self.num_samples
The same sentinel pattern also appears in _compute_feature_stats, but that one is fine — feat_mean is initialized as zeros(fdim) before the loop and accumulates uniformly. Only _compute_edge_stats has the stale None-sentinel issue.
Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.
Good catch, fixed in 349312a. _compute_edge_stats now uses the same zeros + m/num_samples accumulation as _compute_edge_stats_lazy.
Thanks for the detailed triage and for addressing all three issues. The lazy stat accumulation, normalized-position edge stats, and eager double-normalize fixes in
0b5e96aall look correct.One remaining issue worth flagging: the eager
_compute_edge_statsstill has the original first-sample overweighting bug that was fixed in the lazy path. The sentinelNonepattern leavesm_0with full weight whilem_1…m_{N-1}are each divided byN:# datapipe.py – _compute_edge_stats (eager) edge_mean = m if edge_mean is None else edge_mean + m / self.num_samples # ↑ full weight for i=0; all others get 1/N weightThis should use the same
zeros + m/Npattern as_compute_edge_stats_lazy:def _compute_edge_stats(self): edge_mean = None edge_meansqr = None edge_dim = None for i in range(self.num_samples): x_e = self.graphs[i].edge_attr.to(torch.float32) # [E,De] if edge_dim is None: edge_dim = x_e.shape[1] edge_mean = torch.zeros(edge_dim, dtype=torch.float32) edge_meansqr = torch.zeros(edge_dim, dtype=torch.float32) m = torch.mean(x_e, dim=0) msq = torch.mean(x_e * x_e, dim=0) edge_mean += m / self.num_samples edge_meansqr += msq / self.num_samplesThe same sentinel pattern also appears in
_compute_feature_stats, but that one is fine —feat_meanis initialized aszeros(fdim)before the loop and accumulates uniformly. Only_compute_edge_statshas the staleNone-sentinel issue.Tip: You can customize Greptile's behavior for this repo with
.greptile/rules.mdand.greptile/config.json.
This is a pre-existing bug (not introduced by lazy loading), but you are right @greptile-apps that it’s still in the file we’re editing.
Looking at the current PR branch, _compute_edge_stats has already been fixed — it initializes to zeros on first sample, just like the lazy path:
def _compute_edge_stats(self):
edge_mean = None
edge_meansqr = None
edge_dim = None
for i in range(self.num_samples):
x_e = self.graphs[i].edge_attr.to(torch.float32)
if edge_dim is None:
edge_dim = x_e.shape[1]
edge_mean = torch.zeros(edge_dim, dtype=torch.float32) # ✓ zeros, not m
edge_meansqr = torch.zeros(edge_dim, dtype=torch.float32)
m = torch.mean(x_e, dim=0)
msq = torch.mean(x_e * x_e, dim=0)
edge_mean += m / self.num_samples # ✓ uniform weight
edge_meansqr += msq / self.num_samples
The None-sentinel pattern (edge_mean = m if edge_mean is None else edge_mean + m / self.num_samples) is gone — this was corrected in the same commit you referenced. The eager and lazy paths are now consistent. No further changes needed here.
@coreyjadams gentle ping when you have a moment. Greptile items are addressed and replied on the threads. Closes #1550. Thanks!