Repository navigation
Opt-in activation checkpointing for EEGConformer - #1507
bruAristimunha merged 1 commit into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #1507 +/- ##
==========================================
+ Coverage 91.20% 91.28% +0.08%
==========================================
Files 169 170 +1
Lines 21916 22072 +156
==========================================
+ Hits 19989 20149 +160
+ Misses 1927 1923 -4 🚀 New features to boost your workflow:
|
antonsoo
left a comment
There was a problem hiding this comment.
Tested checkpointing commit 3a7132cf against its stack parent 30f278e7, using recorded PhysioNet S001R04 EEG, torch 2.7.0+cu128, MNE 1.13.2 and an RTX 5070.
Eight CPU/CUDA configurations cover classifier/features output, a frozen stem or transformer, FP32, FP16 with GradScaler, and BF16. Three AdamW updates with two accumulated microbatches per update, gradient clipping and a model/optimizer state restore after the first update give identical outputs, losses, input/parameter gradients, model/optimizer/scaler state, BatchNorm buffers and Python/NumPy/CPU-Torch/all-CUDA RNG states. Checkpointing on/off is exact, as is the default against the parent.
For [8, 22, 8192], embedding 64, depth 6 and 8 heads, three alternating fresh-process GPU pairs reduce peak allocated memory from 379.07 to 290.53 MiB in every pair (23.4%). The median paired steady-step time ratio is 1.216, with a noisy 1.16-1.47 range. All four updates in each pair preserve the same outputs, updated model/optimizer and RNG state hashes. This is GPU allocated memory; I did not reproduce the CPU RSS or batch-capacity figures.
The added checkpointing test file passes 59 tests with 1 skip. No additional defect found in this scope. This review covers the checkpointing commit, not the rest of the stack or a dataset accuracy benchmark.
Keyword-only activation_checkpointing=False. When enabled, the pure transformer blocks are recomputed during backward with non-reentrant, RNG-preserving checkpointing. Compiled/traced execution with checkpointing is rejected explicitly. Default unchanged; old pickles load. Part of braindecode#1489.
3a7132c to
3f0219b
Compare
Summary
Part of #1489.
EEGConformer(..., activation_checkpointing=True)recomputes the transformer blocks during backward instead of storing their activations. It trades extra step time for training memory, so it is off by default.Changes
braindecode/models/eegconformer.py: new keyword-only argumentactivation_checkpointing=False.torch.utils.checkpoint.checkpoint(..., use_reentrant=False, preserve_rng_state=True), so dropout draws are reproduced. The BatchNorm stem is never recomputed.activation_transfor(for examplenn.Tanh) can be checkpointed.model.compile(),torch.compile(model)orEEGClassifier(compile=True), raiseactivation_checkpointing requires eager execution. They do not silently fall back.compile(disable=True)keeps working.torch.compile(model)is caught: the eager-only boundary carries Dynamo's forbidden marker (_dynamo_forbidden = True, the attributetorch._dynamo.forbid_in_graphsets), set once in the class body. Dynamo checks it before tracing any call, so it fails instead of falling back to eager, for every route: construction, later flag changes, unpickling and deepcopy. There are no__setattr__/__setstate__hooks and no runtime registration, andimport braindecodeand models never importtorch._dynamo; a fresh-process test checks the import, and a unit test checks Dynamo's ownis_forbiddenon the boundary, so a rename in torch fails a test rather than a user.no_gradforwards run the usual code. Parameter names, buffers and state-dict keys are unchanged. Pickled models from before this change still load: the class-level default isFalse.test_integration.py: the activation-class test no longer treatsactivation_checkpointingas an activation constructor.whats_newentry under Enhancements.Defaults
Nothing changes with the default
activation_checkpointing=False. A byte-parity probe against the pre-change source shows identical initialized state, outputs, input and parameter gradients, SGD update, buffers and RNG.Evidence
Exactness, checkpointing on vs off (torch 2.14.1+cpu)
torch.equal).return_featureson and off, and at the measured shape[8, 22, 8192].Memory and time
[8, 22, 8192]; five alternating fresh-process pairs.Tests
test/unit_tests/models/test_eegconformer_checkpointing.pyis new and covers:nn.Tanh): outputs and gradients equal with checkpointing on and off;is_forbiddenon the checkpoint boundary;sys.executable) carry per-line# nosecmarkers with the reason, astest_compile_estimator.pydoes in PR 4.Branch tip
3f0219be9baf(treea4818c0efb54) on upstreammastere92940d, i.e. after #1504, #1505 and #1506. The tree is byte-identical to the verified tip90e3581996bf, which sat on a78824d plus the then-open #1504 to #1506; all three merged with those same trees. The default suite was re-run on this tree inbdw3-stack-verify-4-20261031t211836z-371a1219(6,376 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,376 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.