Repository navigation
FIX MEDFormer: size the patch position table by the largest patch count - #1868
Merged
bruAristimunha merged 3 commits intoDec 1, 2026
Merged
bruAristimunha merged 3 commits into
bruAristimunha merged 3 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MEDFormerbuilds, 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 isn_times >= 10000(40 s at 250 Hz).#1311 fixed the same crash in the channel table. v2.0.0 fails the same way.
Changes
MEDFormer.__init__computes the table size frompatch_num_list,max([5000, *patch_num_list]), and passes it to_ListPatchEmbeddingas an argument without a default.Testing
Run with
-n 2 -rfEs,--select-models=MEDFormer, on master (92cb203) and on this branch. Counts are passed / failed / skipped.pytest test/unit_tests/models/ -k "MEDFormer or completeness or summary_csv or categorization"pytest test/unit_tests/models/test_model_contract.py -k MEDFormerpytest test/unit_tests/models/test_pretrained_compat.py -k "MEDFormer or covers_every or no_strategy"pytest test/unit_tests/models/test_models.py -k medformer(no model file of its own)scripts/ci_plan.py --base upstream/master --head HEADSkips (same on both): pydantic/numpydantic not installed (
test_config.py),torch.compileskipped on Windows, fixed-length input contract (2), forward takes noch_mask, no CUDA/MPS, network tests, sphinx not installed, two EEGNet**kwargscases, EEGNet has no embedding parameter. The 5 failures are the same on master:test_huggingface.pysymlink 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
5000in_ListPatchEmbeddingfailstest_medformer_more_than_5000_patches; dropping the floor (max(patch_num_list)) failstest_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_times1, 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 noreturn_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.rstpasseddocs/whats_new.rstupdated (3 lines)Notes for reviewers
get_config()and pickles saved on v2.0.0 and v2.1.0 (n_times1000, 500 withsingle_channel=True, 9999) load withstrict=Trueand 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/MEDFormeron the Hub holds no weights._V2_MODELSintest_base.py, if you prefer.