Skip to content

CSBrain: reuse CBraMod's patch embedding; fix CBraMod patch_size != 200 - #1240

Merged
bruAristimunha merged 6 commits into
braindecode:masterfrom
bruAristimunha:w41/fu-csbrain-patch-embedding
Oct 7, 2026
Merged

bruAristimunha merged 6 commits into
braindecode:masterfrom
bruAristimunha:w41/fu-csbrain-patch-embedding

Conversation

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Follow-up to #1196. It contains no new behaviour, plus one CBraMod fix.

Changes

  • csbrain.py: drop CSBrain's private copy of the patch embedding (conv stem + rFFT magnitude + depthwise-conv positional encoding) and build CBraMod's _PatchEmbedding with the same conv spec (−90 lines).
  • CBraMod creates the positional-encoding conv before the conv stem, and CSBrain's copy created them the other way round. CSBrain therefore saves the RNG state, builds the shared module, and redraws the initial weights in the stem-first order. This keeps the same-seed initial weights identical to master.
  • cbramod.py: the spectral reshape hard-coded p=101, so CBraMod(patch_size=400) failed in forward (einops shape error on master). It now uses patch_size // 2 + 1.
  • New test test_cbramod_patch_embedding_patch_size_not_200[CBraMod|CSBrain] in test_models.py. On master the CBraMod case fails and the CSBrain case passes. Both pass on this branch.
  • whats_new: one Enhancements entry and one Bug fixes entry.

Parity against master c282258 (CPU, fp32, torch.manual_seed(42), fixed input batch of 4, eval mode):

model patch_size state-dict load from master (strict) max-abs output diff same-seed initial weights
CSBrain (19 ch, 800 samples) 200 OK, same key set 0.0 identical
CSBrain (19 ch, 1600 samples) 400 OK, same key set 0.0 identical
CBraMod (19 ch, 800 samples) 200 OK, same key set and order 0.0 identical

The key set is unchanged. Inside patch_embedding, the state_dict() iteration order now follows CBraMod's module order, which has no effect on load_state_dict.

Tests (-k "(CSBrain or csbrain or CBraMod or cbramod) and not compiled"): test_models 27 passed, test_integration 20 passed / 2 skipped, test_return_features 8 passed, test_foundation_models 3 passed / 1 skipped, test_pretrained_compat 8 passed. pre-commit run --files on the four touched files: rc=0.

Copilot AI balanced review requested due to automatic review settings October 6, 2026 18:22

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 review overview

🟡 Changes recommended

The RNG rewind only handles CPU state, breaking initialization parity when models are constructed directly on non-CPU devices.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Refactors CSBrain to reuse CBraMod’s patch embedding and fixes non-default patch sizes.

Changes:

  • Shares patch-embedding implementation between CSBrain and CBraMod.
  • Derives spectral bins from patch_size.
  • Adds regression coverage and release notes.
File Description
braindecode/​models/​csbrain.py Reuses CBraMod’s patch embedding while preserving initialization order.
braindecode/​models/​cbramod.py Fixes dynamic rFFT-bin reshaping.
test/​unit_tests/​models/​test_models.py Tests 400-sample patches for both models.
docs/​whats_new.rst Documents the refactor and fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread braindecode/models/csbrain.py Outdated
@codecov

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.51%. Comparing base (03d73b5) to head (23253d7).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1240      +/-   ##
==========================================
- Coverage   88.52%   88.51%   -0.01%     
==========================================
  Files         158      158              
  Lines       19332    19306      -26     
==========================================
- Hits        17113    17089      -24     
+ Misses       2219     2217       -2     
🚀 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 October 6, 2026 21:53

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 review overview

🟢 Approval recommended

The shared implementation preserves compatibility and the patch-size fix is covered by focused regression tests.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@bruAristimunha
bruAristimunha merged commit ea5bc7d into braindecode:master Oct 7, 2026
27 of 29 checks passed
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.

2 participants