Skip to content

iEEG foundation models: return the per-token output, padded channels zeroed - #1540

Merged
bruAristimunha merged 4 commits into
masterfrom
feat/ch-mask-token-features
Nov 5, 2026
Merged

bruAristimunha merged 4 commits into
masterfrom
feat/ch-mask-token-features

Conversation

@julien-gadonneix

@julien-gadonneix julien-gadonneix commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

return_features=True also returns "tokens", the per-token encoder output before pooling, (batch, n_chans, n_tokens, dim) (PopT: one token per channel), with the tokens of padded channels (ch_mask) exactly zero, for the iEEG foundation models of #1487 (BrainBERT, EpiNT, Brant, PopulationTransformer, BaRISTA, DIVER1, MVPFormer). Register and class tokens are left out of "tokens". DIVER1 gains return_features.

This lets a downstream probe pool or attend over the tokens of real channels only, by skipping all-zero tokens, without the mask.

  • BrainBERT and EpiNT encode each real channel alone (pack_channels), so unpack_channels already returns zeros for the padded ones; Brant, PopT, BaRISTA, DIVER1 and MVPFormer attend across the padded grid, so their padded tokens are zeroed explicitly.
  • "features" stays the pooled embedding: returning the tokens there instead, as REVE and Labram do, would change it right after 2.0. REVE and Labram also return "tokens", same layout, in REVE, Labram: accept ch_mask #1539.

Testing

  • test_ch_mask_tokens_match_each_recording_alone (fails without the change): each recording's tokens match it alone, padded tokens are zero, scripted where return_features is honoured (DIVER1, MVPFormer); pytest test/unit_tests/models/test_ch_mask.py: 33 passed
  • pytest test/unit_tests/models -k "<the seven models>": 409 passed, 16 skipped
  • pre-commit run --files <changed files>: passed

Copilot AI balanced review requested due to automatic review settings November 5, 2026 03:43

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 03:43

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@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.39%. Comparing base (619e703) to head (187b2b1).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1540      +/-   ##
==========================================
+ Coverage   91.38%   91.39%   +0.01%     
==========================================
  Files         170      170              
  Lines       22137    22155      +18     
==========================================
+ Hits        20229    20249      +20     
+ Misses       1908     1906       -2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bruAristimunha bruAristimunha mentioned this pull request Nov 5, 2026
3 tasks done

@bruAristimunha bruAristimunha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The change is small and well tested: each recording's tokens match it run alone, padded tokens are zero, and the scripted paths are covered. I checked the reshapes:

  • the BaRISTA patch-major unflatten;
  • the MVPFormer (batch, segment, channel, d) transpose;
  • the Brant emb layout;
  • the BrainBERT unpack-then-crop, which gives the same pooled value as before.

P2: one layout for tokens
PopulationTransformer returns (batch, n_chans, hidden), while the six others return (batch, n_chans, n_tokens, dim). A probe written once for all models has to special-case PopT. tokens.unsqueeze(2) (one token per channel) keeps the contract uniform.

P2: the contract should cover REVE and Labram too
On the alternative you mention: keeping "features" as the pooled embedding and adding "tokens" is the better choice. REVE and Labram currently return token-level "features", and changing that right after 2.0 would be breaking. Instead, REVE and Labram (#1539) can also return "tokens" in the same (batch, n_chans, n_patches, dim) layout, with padded tokens zeroed. Then "tokens" means the same thing in every foundation model, and "features" can converge later with a deprecation.

P3

  • BrainBERT now unpacks every frame before cropping to pool_n_frames, so the zero padding is allocated for all frames. That's fine; just noting it is intentional.
  • Codacy reports 1 new minor issue. It is probably one of the long return {...} # nosec B105 lines; wrapping the dict over several lines clears it.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 14:35

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@julien-gadonneix

Copy link
Copy Markdown
Collaborator Author

Thanks!

  • PopT returns (batch, n_chans, 1, hidden_dim), so every model gives (batch, n_chans, n_tokens, dim).
  • Agreed on keeping "features" pooled; REVE and Labram return "tokens" too in REVE, Labram: accept ch_mask #1539, same layout, padded tokens zeroed.
  • BrainBERT: intentional, the tokens cover every frame.
  • Codacy: the two long return {...} lines are wrapped.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Thanks for the updates, they address the review. Two things before merging:

Copilot AI balanced review requested due to automatic review settings November 5, 2026 16:27

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@julien-gadonneix

Copy link
Copy Markdown
Collaborator Author

Merged master: took its _assert_matches_alone / _padded_batch / _padded_case and kept the tokens test on top. Dropped the # nosec B105 on the three returns this PR touches (BaRISTA, BrainBERT, Brant).

@bruAristimunha
bruAristimunha merged commit b40e1cc into 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.
@bruAristimunha
bruAristimunha deleted the feat/ch-mask-token-features branch November 22, 2026 23:01
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