Skip to content

CBraMod: accept ch_mask with the encoder output - #1544

Merged
bruAristimunha merged 35 commits into
braindecode:masterfrom
bruAristimunha:chmask-cbramod
Nov 5, 2026
Merged

bruAristimunha merged 35 commits into
braindecode:masterfrom
bruAristimunha:chmask-cbramod

Conversation

@bruAristimunha

Copy link
Copy Markdown
Collaborator

CBraMod accepts ch_mask for 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.
  • The depthwise convolution over channels is exact only when padding is at the end, as pad_channels_collate produces it. This is documented.
  • With ch_mask=None nothing 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).
  • Existing CBraMod and CSBrain tests pass, with no new failures.

julien-gadonneix and others added 5 commits November 5, 2026 00:35
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.
Copilot AI balanced review requested due to automatic review settings November 5, 2026 14:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.47%. Comparing base (b40e1cc) to head (ef7c34c).
⚠️ Report is 3 commits behind head on master.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 18:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified in the channel-mask support changes.

No open issues 🎉

Review effort: Balanced

@bruAristimunha
bruAristimunha merged commit 5f609ad into braindecode:master Nov 5, 2026
10 checks passed
bruAristimunha added a commit to bruAristimunha/braindecode that referenced this pull request Nov 9, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants