Skip to content

BIOT: accept ch_mask - #1542

Merged
bruAristimunha merged 20 commits into
braindecode:masterfrom
bruAristimunha:chmask-biot
Nov 5, 2026
Merged

bruAristimunha merged 20 commits into
braindecode:masterfrom
bruAristimunha:chmask-biot

Conversation

@bruAristimunha

Copy link
Copy Markdown
Collaborator

BIOT accepts ch_mask, the (batch, n_chans) mask of real channels that the iEEG foundation models of #1487 take. A batch can then mix recordings with different channel counts, and each recording gets the output it gets when run alone.

  • LinearAttentionTransformer (braindecode/modules/linear_attention.py) takes an optional key mask: padded keys are left out of the key softmax. TFMTokenizer, which shares the module, passes nothing and is unchanged.
  • The token mean before the head is a masked_mean.
  • With ch_mask=None nothing changes: in a Voyager job, 1,157 of 1,157 tensors were torch.equal between master and this change. That covered BIOT, LUNA, CBraMod, CSBrain and TFMTokenizer (outputs, input and parameter gradients, scripted outputs). No parameter or state-dict key changes, so the Hub weights load as before.

Part of making ch_mask work for the foundation models where masking can be exact (BIOT, LUNA and CBraMod first). Please merge after #1539 and #1540, which settle the refusal message and the tokens output.

Testing

  • test_ch_mask.py: BIOT added to _CASES (each recording matches it alone, eager and scripted; no gradient into padding). Together with the LUNA and CBraMod PRs: 29 passed.
  • Existing tests for BIOT, LUNA, CBraMod, CSBrain, TFMTokenizer, criss-cross and linear attention: master 316 passed, branch 321 passed, 0 failed.

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

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.

🟡 Changes recommended

BIOT’s missing tokens output will break the parametrized test after the required merge, while non-native channel adaptation leaves masks misaligned and can spread NaN padding into real channels.

2 open issues
What changed in this PR

This PR adds channel-mask support to BIOT, extending the foundation models’ support for batching recordings with different channel counts while preserving the unmasked execution path.

Changes:

  • Passes optional masks through linear attention to exclude padded keys.
  • Clears padded BIOT inputs before spectral processing and excludes padded tokens from pooling.
  • Adds BIOT to shared channel-mask regression tests and documents the enhancement.
File Description
test/​unit_tests/​models/​test_ch_mask.py Adds BIOT to shared masking tests.
docs/​whats_new.rst Announces BIOT channel-mask support.
braindecode/​modules/​linear_attention.py Adds optional attention key masking.
braindecode/​models/​biot.py Accepts channel masks and applies masked attention and pooling.

Review effort: Balanced

Comment thread test/unit_tests/models/test_ch_mask.py Resolved
``(out, emb)`` tuple (ignored when ``return_features=True``).
"""
emb = self.encoder(x)
emb = self.encoder(x, ch_mask=check_ch_mask(ch_mask, x))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right, and this applies to every model, not just BIOT: the channel layer in EEGModuleMixin._channel_forward mixes every input channel, padded ones included, into each target channel. Fixed once there (bbd1a76): any model built with a non-native channel_strategy now raises ValueError("ch_mask needs channel_strategy='native' ..."). Test: test_ch_mask_is_refused_under_a_channel_strategy.

@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.40%. Comparing base (b40e1cc) to head (55d5c57).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1542   +/-   ##
=======================================
  Coverage   91.39%   91.40%           
=======================================
  Files         170      170           
  Lines       22155    22204   +49     
=======================================
+ Hits        20249    20295   +46     
- Misses       1906     1909    +3     
🚀 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 15:11

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.

🟡 Changes recommended

The mask guard can be bypassed by positional arguments, risking NaN contamination or montage-mask mismatches, and the refusal test fails during construction before exercising the guard.

4 open issues

Review effort: Balanced

Comment thread braindecode/models/base.py Resolved
Comment thread test/unit_tests/models/test_ch_mask.py Resolved
Copilot AI balanced review requested due to automatic review settings November 5, 2026 15:15

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.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 15:37

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 for the BIOT channel-mask support changes.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 5, 2026 17:57

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 BIOT channel-mask support changes.

1 open issue
1 resolved since last review

Review effort: Balanced

@bruAristimunha
bruAristimunha merged commit 6bf1f38 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