Repository navigation
CBraMod: accept ch_mask with the encoder output - #1544
Merged
bruAristimunha merged 35 commits intoNov 5, 2026
Merged
Conversation
Padded channels are zero-filled, never attended to (key mask before the key softmax of the linear attention) and not pooled (masked_mean).
Masked GroupNorm, zero padded rows before the positional convolution, key padding mask in the spatial half of the criss-cross attention, zero padded tokens. The flattened head refuses ch_mask.
Masked GroupNorm in the patch embedding, location range over real channels, key padding mask in the channel cross-attention. A class-level annotation lets LUNA script with an empty location cache.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the CBraMod channel-mask changes.
No open issues 🎉
What changed in this PR
Adds channel-mask support to CBraMod’s encoder output, allowing batches of recordings with different channel counts while excluding padded channels from attention and normalization.
Changes:
- Masks padded channels in spatial attention and GroupNorm, and zeros their output tokens.
- Rejects channel masks when using the channel-dependent classification head.
- Adds tests for per-recording equivalence, zero padded outputs and gradients, feature output, and TorchScript support.
| File | Description |
|---|---|
test/unit_tests/models/test_ch_mask.py |
Adds CBraMod masking and classification-head refusal tests. |
docs/whats_new.rst |
Documents the new CBraMod channel-mask support. |
braindecode/modules/attention.py |
Expands channel masks across patches for spatial attention only. |
braindecode/models/cbramod.py |
Implements masked normalization, encoder masking, and zeroed padded tokens. |
Review effort: Balanced
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1544 +/- ##
==========================================
+ Coverage 91.39% 91.47% +0.07%
==========================================
Files 170 170
Lines 22155 22249 +94
==========================================
+ Hits 20249 20352 +103
+ Misses 1906 1897 -9 🚀 New features to boost your workflow:
|
This was referenced Nov 5, 2026
…-mask # Conflicts: # test/unit_tests/models/test_ch_mask.py
…fail (no ch_names routing)
…a local position bank
bruAristimunha
added a commit
to bruAristimunha/braindecode
that referenced
this pull request
Nov 9, 2026
Brings in the ch_mask series (braindecode#1545, braindecode#1548, braindecode#1540, braindecode#1542-braindecode#1544, braindecode#1549, braindecode#1550) and braindecode#1557/braindecode#1559. No conflicts.
bruAristimunha
added a commit
to bruAristimunha/braindecode
that referenced
this pull request
Nov 9, 2026
Brings in the ch_mask series (braindecode#1545 braindecode#1548 braindecode#1540 braindecode#1549 braindecode#1542 braindecode#1543 braindecode#1544 braindecode#1550), braindecode#1557 and braindecode#1559 (master dba9d42). No conflicts; NOTICE.txt and docs/whats_new.rst carry only this PR's additions over master.
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.
CBraModacceptsch_maskfor its encoder output (return_encoder_output=True): padded channels are masked out of the spatial attention and the GroupNorm statistics, and their tokens are zero. Its classification head flattens all channels, so it is tied to them and refuses the mask (ValueError("ch_mask needs ..."), as in #1539).CrissCrossTransformerEncoderLayer(braindecode/modules/attention.py) takes an optional key mask for its spatial attention. CSBrain shares the module, passes nothing and is unchanged.pad_channels_collateproduces it. This is documented.ch_mask=Nonenothing changes: logits, encoder output, features and the pretraining patch mask are bit-identical to master. No parameter or state-dict key changes.The refusal test uses the
"ch_mask needs"message of #1539, so please merge after #1539 and #1540.Testing
test_ch_mask.py: CBraMod's flattened head is added to_FIXED_HEADS, and a dedicated test covers the encoder output (each recording matches it alone, padded tokens are zero, zero input gradient, scripted).