Skip to content

FIX MEDFormer: size the patch position table by the largest patch count - #1868

Merged
bruAristimunha merged 3 commits into
braindecode:masterfrom
AxelNoun:fix/bd-t10-3-medformer-patch-positions
Dec 1, 2026
Merged

bruAristimunha merged 3 commits into
braindecode:masterfrom
AxelNoun:fix/bd-t10-3-medformer-patch-positions

Conversation

@AxelNoun

@AxelNoun AxelNoun commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

MEDFormer builds, then raises on every call, when a granularity has more than 5000 patches: its patch position table has a fixed 5000 rows. With patch length 2 that is n_times >= 10000 (40 s at 250 Hz).

import torch
from braindecode.models import MEDFormer

for n_times in (9999, 10000, 15000):
    model = MEDFormer(n_chans=4, n_outputs=2, n_times=n_times, num_layers=1,
                      embed_dim=32, num_heads=4).eval()
    try:
        print(n_times, tuple(model(torch.randn(1, 4, n_times)).shape))
    except RuntimeError as e:
        print(n_times, str(e)[:75])
master:
9999 (1, 2)
10000 The size of tensor a (5001) must match the size of tensor b (5000) at non-s
15000 The size of tensor a (7501) must match the size of tensor b (5000) at non-s
this branch:
9999 (1, 2)
10000 (1, 2)
15000 (1, 2)

#1311 fixed the same crash in the channel table. v2.0.0 fails the same way.

Changes

  • MEDFormer.__init__ computes the table size from patch_num_list, max([5000, *patch_num_list]), and passes it to _ListPatchEmbedding as an argument without a default.
  • Two tests: 5001 patches run; a short window keeps the 5000-row table.

Testing

Run with -n 2 -rfEs, --select-models=MEDFormer, on master (92cb203) and on this branch. Counts are passed / failed / skipped.

Command master branch
pytest test/unit_tests/models/ -k "MEDFormer or completeness or summary_csv or categorization" 76 / 0 / 5 78 / 0 / 5
pytest test/unit_tests/models/test_model_contract.py -k MEDFormer 13 / 0 / 1 13 / 0 / 1
pytest test/unit_tests/models/test_pretrained_compat.py -k "MEDFormer or covers_every or no_strategy" 3 / 0 / 0 3 / 0 / 0
pytest test/unit_tests/models/test_models.py -k medformer (no model file of its own) 22 / 0 / 0 24 / 0 / 0
the 22 test files listed by scripts/ci_plan.py --base upstream/master --head HEAD 1446 / 5 / 19 1448 / 5 / 19
  • Skips (same on both): pydantic/numpydantic not installed (test_config.py), torch.compile skipped on Windows, fixed-length input contract (2), forward takes no ch_mask, no CUDA/MPS, network tests, sphinx not installed, two EEGNet **kwargs cases, EEGNet has no embedding parameter. The 5 failures are the same on master: test_huggingface.py symlink tests, WinError 1314 (Windows symlink privilege).

  • test_config.py --select-models=MEDFormer, run again with numpydantic 2.0.1 installed: 19 passed on master and here.

  • torch 2.6.0 (the floor), test_integration.py --select-models=MEDFormer -k "MEDFormer and (exported or torch_script)": 2 passed on master and here; the 2 new tests pass.

  • Mutation: restoring 5000 in _ListPatchEmbedding fails test_medformer_more_than_5000_patches; dropping the floor (max(patch_num_list)) fails test_medformer_short_window_keeps_v2_patch_position_table. Both tests pass/fail as expected on master.

  • Parity, master vs branch, float64, seed 0, OMP_NUM_THREADS=1, 10 configurations (default; single_channel; no_inter_attn; patches [14, 44, 45]; n_times 1, 6000, 9998, 9999; patch longer than the window; patch 1 with 4999 samples): state dict keys, shapes and values, outputs, input and parameter gradients, from_config(get_config()) outputs are bitwise equal (10/10). MEDFormer has no return_features.

  • New or changed behavior is covered by relevant regression tests

  • Style checks recorded: pre-commit run --files braindecode/models/medformer.py test/unit_tests/models/test_models.py docs/whats_new.rst passed

  • docs/whats_new.rst updated (3 lines)

Notes for reviewers

  • Up to 5000 patches the table, state dict and outputs are unchanged (parity above). State dicts, get_config() and pickles saved on v2.0.0 and v2.1.0 (n_times 1000, 500 with single_channel=True, 9999) load with strict=True and give bitwise equal outputs. A v2 state dict of a model with more than 5000 patches, which never ran, no longer loads strictly (5000 rows against 5001). braindecode/MEDFormer on the Hub holds no weights.
  • The 5000 floor stays a commented literal rather than a parameter: it sets no output, only the stored shape that v2 state dicts fix. I can make it a keyword argument, or add MEDFormer to _V2_MODELS in test_base.py, if you prefer.

AxelNoun and others added 3 commits December 1, 2026 02:02
MEDFormer built for windows with more than 5000 patches (10000 samples with patch length 2) and then raised a RuntimeError on every forward, because the patch position table had a fixed 5000 rows. The table now has max(5000, largest patch count) rows, so shorter windows keep the shape that v2.0 state dicts hold.
@bruAristimunha
bruAristimunha merged commit 339e362 into braindecode:master Dec 1, 2026
4 of 5 checks passed
@AxelNoun
AxelNoun deleted the fix/bd-t10-3-medformer-patch-positions branch December 1, 2026 12:08
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