Repository navigation
Opt-in compile in EEGClassifier/EEGRegressor keeps canonical checkpoint keys and Parameter identity - #1506
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1506 +/- ##
==========================================
+ Coverage 91.20% 91.28% +0.07%
==========================================
Files 169 170 +1
Lines 21916 22042 +126
==========================================
+ Hits 19989 20120 +131
+ Misses 1927 1922 -5 🚀 New features to boost your workflow:
|
antonsoo
left a comment
There was a problem hiding this comment.
Tested 30f278e7 against its stack parent 49a003c0 on recorded PhysioNet S001R04 EEG with PyTorch 2.7.0+cu128, skorch 1.4.0, CPU and RTX 5070. This reviews the compile commit only.
With native Inductor execution confirmed, EEGClassifier/EEGNet and EEGRegressor/ShallowFBCSPNet now save canonical keys and load strictly into eager estimators. The parent emits _orig_mod. keys and rejects those eager loads in all four cases. After restoring parameters, SGD momentum, criterion and history, two further updates agree with uninterrupted compiled training. Four-update model/optimizer/gradient/prediction comparisons against eager references pass rtol=3e-4, atol=5e-5; the largest absolute difference on this head is 4.0e-5. The added test file also passes all 15 cases with --run-slow.
ShallowFBCSPNet pickle/deepcopy resume eagerly and preserve the continuation. EEGNet pickle and estimator deepcopy still raise PyTorch's parametrized-module serialization error in both eager and compiled modes; save_params works. I would qualify the whole-estimator serialization paragraph with this existing native limitation. One parent CPU regression-gradient comparison exceeded the stated tolerance; I am not claiming parent parity or a general numerical improvement here. No new compile defect found, and no speed or full-stack approval claim.
With compile=True the estimators compile modules in place (nn.Module.compile), so state dicts keep canonical keys (no `_orig_mod.`) and caller-supplied Parameters keep their identity. Default (compile=False) unchanged. Part of braindecode#1489.
30f278e to
8a99ac8
Compare
Summary
Part of #1489.
skorch's
compile=Truewraps the module intorch.compile(module), which saves state dicts with_orig_mod.-prefixed keys. For braindecode estimators, these checkpoints no longer load into an eager model.EEGClassifierandEEGRegressornow compile in place withnn.Module.compile. The module and the state dict keep their names, and caller-supplied modules keep their Parameter objects.Changes
_EEGNeuralNet.torch_compileuses native in-placeModule.compile(**compile__*)for the module and the criterion._compile_ownership_, which records only the compiled call the estimator installed (its identity is enough:Module.compilebinds it to that module):compileoff, orcompile__disable=True, clears only the compiled call that this estimator installed;__getstate__drops the ownership record. Pickle, joblib and deepcopy give an eager estimator, as native Module serialization already does. The next explicitinitialize()compiles again.TORCHDYNAMO_DISABLE=1is honoured: the estimator runs eagerly._orig_mod.wrapper checkpoints;whats_newentry under API changes.Defaults
compile=Falseremains the default, and nothing changes withoutcompile=True. Withcompile=True:_orig_mod.keys are not rewritten automatically on load;partial_fitafter unpickling run eagerly untilinitialize()is called.Evidence
Correctness, CPU (torch 2.14.1+cpu, skorch 1.4.0, Python 3.12)
save_params/load_paramswith SGD momentum, warm start,partial_fit, parameter gradients, and compiled chunked trial inference all hold within the same tolerance.Time per epoch, compiled ÷ eager (median of 5 paired fresh processes)
¹ Xeon Gold 6230R, 4 threads, FP32; synthetic 8 × 512 inputs, batch 16.
² A100-PCIE-40GB, torch 2.14.1+cu130, FP32 with TF32 off.
BackendCompilerFailed; it already happens on upstreammaster). It works on torch >= 2.7, with dropout 0, in eval mode and withaot_eager. The parity and timing cells above ran on torch 2.14.1.Tests
test/unit_tests/test_compile_estimator.pyis new and covers:slow.Branch tip
8a99ac8ef35d(tree2ce524053d21) on upstreammaster50d6d59, i.e. after #1504 and #1505. The tree is byte-identical to the verified tipbcb361ce08c1, which sat on a78824d plus the then-open #1504 and #1505; both merged with those same trees. The default suite was re-run on this tree inbdw3-stack-verify-4-20261031t211836z-371a1219(6,317 passed, 0 failed). Every check ran in one Voyager CPU Job,bdw3-stack-verify-3-20261031t201512z-cca0e0cc(its docs step was re-run inbdw3-stack-docs-1after a fix to the check script):pytest test/ -n 8from the repository root at this tip: 6,317 passed, 375 skipped, 1 xfailed, 0 failed. Upstreammasterunder the same settings: 5,995 passed, 375 skipped, 1 xfailed.pre-commit run --files <files changed by this PR>, with the repository's pinned hooks (Ruff, mypy, docstrfmt, blacken-docs, codespell, sphinx-lint): passed.