Skip to content

FIX AttentionBaseNet: size fca and ge attention for even temporal kernels - #1867

Merged
bruAristimunha merged 3 commits into
braindecode:masterfrom
AxelNoun:fix/bd-t07-6-attention-seq-len
Dec 1, 2026
Merged

bruAristimunha merged 3 commits into
braindecode:masterfrom
AxelNoun:fix/bd-t07-6-attention-seq-len

Conversation

@AxelNoun

@AxelNoun AxelNoun commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

AttentionBaseNet with attention_mode="fca", or "ge" with extra_params=True, fails in forward whenever the attention block's temporal kernel is even:

AttentionBaseNet(n_chans=8, n_outputs=4, n_times=1000, attention_mode="fca", temp_filter_length=16)(torch.randn(2, 8, 1000))
# RuntimeError: The size of tensor a (63) must match the size of tensor b (62) at non-singleton dimension 2

With the default parameters this also happens for short windows: below n_times=180 the model shrinks temp_filter_length, often to an even value (10 at n_times 125 and 128), so 84 of the lengths 1 to 180 fail, "se" none.

Cause: FCA and GatherExcite(extra_params=True) are sized with seq_lengths[0], the length at the input of the block's convolution. That convolution pads by k // 2 and returns n + 1 - k % 2 steps, one more when k is even.

Changes

  • braindecode/models/attentionbasenet.py: pass seq_lengths[0] + 1 - temp_filter_length % 2 as seq_len.
  • test/unit_tests/models/test_models.py: test_attentionbasenet_even_attention_kernel, for "fca" and "ge" with extra_params=True, with temp_filter_length=16, and with n_times=128 (kernel shrunk from 15 to 10).
  • docs/whats_new.rst: entry under Bug fixes.

Testing

All runs with -p no:cacheprovider -n 2 -q -rfEs, on master (92cb203) and on this branch.

Command master branch
pytest test/unit_tests/models/ --select-models=AttentionBaseNet -k "AttentionBaseNet or completeness or summary_csv or categorization" 70 passed, 6 skipped 74 passed, 6 skipped
pytest test/unit_tests/models/test_model_contract.py --select-models=AttentionBaseNet -k AttentionBaseNet 12 passed, 1 skipped 12 passed, 1 skipped
pytest test/unit_tests/models/test_pretrained_compat.py --select-models=AttentionBaseNet -k "AttentionBaseNet or covers_every or no_strategy" 2 passed 2 passed
pytest test/unit_tests/models/test_models.py -k attentionbasenet (no own test file) 20 passed 24 passed
pytest --select-models=AttentionBaseNet on the 20 test files listed by scripts/ci_plan.py --base upstream/master --head HEAD 1398 passed, 14 failed, 18 skipped 1403 passed, 13 failed, 18 skipped

Skips printed by -rfEs (same on both): test_config.py (pydantic or numpydantic not installed), test_integration.py:360 (torch.compile skipped on Windows), test_integration.py:649 (no embedding parameter), test_model_contract.py:651 (forward takes no ch_mask), test_input_contract.py:142 and :1147 (fixed length). The ci_plan files add: network tests (--run-network), MPS, CUDA, sphinx not installed, EEGNet **kwargs and no embedding parameter.

test_config.py --select-models=AttentionBaseNet, 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=AttentionBaseNet -k "AttentionBaseNet and (exported or torch_script)": 2 passed on master and here; the 4 new cases pass.

The ci_plan failures are the same on master and on this branch and come from my Windows setup: 8 pass with PYTHONUTF8=1 (test_model_rules.py, test_no_new_private_blocks, test_dependencies.py: files read with the cp1252 codec), and 5 test_huggingface.py symlink tests need a privilege this account lacks (WinError 1314). The 14th on master, test_eegclip_one_empty_rank_nonempty_global_matches_ddp_oracle, passed when run again alone.

pre-commit run --files on the three changed files: all hooks pass. Not run locally: the full suite, the docs build, and torch.compile (skipped on Windows).

Mutation: restoring seq_len=seq_lengths[0] fails the four cases of the new test.

Parity, float64, seed 0, OMP_NUM_THREADS=1, over 4446 configurations (every attention mode, n_times 1 to 200, temp_filter_length 1 to 20 at seven usual lengths, even and odd input kernels): the 4138 that run on master give bitwise equal outputs (eval and train), input and parameter gradients, state_dict keys and shapes, and from_config(get_config()) outputs; the 308 that fail on master all run. v2.0.0 checkpoints of the default, "fca", "ge" with extra_params=True and "se" with temp_filter_length=16 models load with strict=True and give bitwise equal outputs.

  • New or changed behavior is covered by relevant regression tests (or N/A with reason)
  • Style checks recorded, e.g. pre-commit run --files <changed files>
  • docs/whats_new.rst updated (at most 3 lines), or the no changelog label, which a maintainer adds, for changes to code that is not released yet (the changelog CI check requires one of the two)

Notes for reviewers

The attention weights change shape only for configurations whose forward fails on master and on v2.0.0; their v2.0.0 state dicts no longer load, as checked with a script. AttentionBaseNet is not in _V2_MODELS; I did not extend those fixtures here.

AxelNoun and others added 3 commits December 1, 2026 01:59
…nels

FCA and GatherExcite(extra_params=True) were sized with the length before the attention block's convolution, which returns one more step when temp_filter_length is even, including the kernels shrunk for n_times below 180.
@bruAristimunha
bruAristimunha merged commit 0cdba55 into braindecode:master Dec 1, 2026
4 of 5 checks passed
@AxelNoun
AxelNoun deleted the fix/bd-t07-6-attention-seq-len branch December 1, 2026 12:09
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