Repository navigation
PERF MSCFormer: temporal and spatial convolutions of the patch embedding as one convolution (train step 0.63x Gaudi eager, 0.66x lazy, 0.63x CPU at 21 x 6000) - #1381
Draft
bruAristimunha wants to merge 6 commits into
Conversation
… the multi-scale patch embedding
…patch embedding equals its branches run in order (float64, values and grads)
…the spatial-first form was 1.17x slower on CPU at the registry input)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1381 +/- ##
=======================================
Coverage 89.95% 89.96%
=======================================
Files 164 164
Lines 21067 21082 +15
=======================================
+ Hits 18951 18966 +15
Misses 2116 2116 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft: one gate cell needs a decision (see "Open cell" below).
Each of MSCFormer's three patch-embedding branches runs a temporal convolution (1 -> 16 filters, kernel 85 / 65 / 45, bias) and then a depthwise spatial convolution (16 -> 16, all channels, bias) with nothing in between. Both are linear, so this PR runs them as one convolution over the channels with the composed weight
spatial.weight.squeeze(1) * temporal.weight.squeeze(1)(filters, n_chans, kernel) and the composed bias (the algebra of the EEGConformer #1373 and MIRepNet #1377 stems). The rest of each branch (batch norm, ELU, pooling, dropout) is unchanged. Same parameters, names, state dict and init; gradients reach both original weights through the composition. The (batch, 16, n_chans, n_times) intermediate goes away (516 MB per branch at 21 x 6000, batch 64).A spatial-first form (project the channels, then a depthwise temporal convolution) was faster still at 21 x 6000 (Gaudi eager 0.58x, CPU 0.38x) but 1.18x slower on CPU at the registry input (CPU depthwise long convolutions at batch 2); the composed form is not.
Tier: numerical only (Tier C numerical-only cells disclosed below, accepted by Bruno on 2026-10-11 at 16:28). Same initial state dict (0.0), same dropout draws, argmax identical, TorchScript scripted == eager (0.0).
Speed (train step = forward + cross-entropy + backward + AdamW, paired in one process, master and PR alternated, median of per-round ratios PR / master)
Gaudi small-input cells: 60 rounds; at these inputs the Gaudi steps are host-bound (host 31-55 ms of 34-72 ms) and vary by +-10-15 % between rounds (master -> master controls in earlier runs: 0.987-1.053).
Open cell
At the registry input (batch 2, 3 channels) in Gaudi lazy mode the train step is 1.05-1.10x master in four independent runs (1.052, 1.088, 1.086, 1.102), and Gaudi eager evaluation at batch 2-32 is 1.06-1.11x: the weight and bias composition adds about five small ops per branch per call, which costs host time when the step is host-bound. Everywhere else the PR is faster or within noise. Per the speed rule this needs a decision before merging.
Numerics (max abs vs master, same weights and input; yardstick = master fp32 vs master fp64)
No released checkpoints for MSCFormer.
Tests (Voyager, CPU)
pytest test/unit_tests/models -k MSCFormer: 31 passed, 2 skipped (newtest_mscformer.py: the patch embedding equals its branches run in order, float64 values and grads).pytest test/unit_tests/models/test_return_features.py: 137 passed, 5 skipped.Files:
braindecode/models/mscformer.py,test/unit_tests/models/test_mscformer.py,docs/whats_new.rst.