Repository navigation
Description
Why
braindecode grew by +19.5k / −2.4k source lines since 1.8.1, 82 % of it in models/. Each new model brought its own copy of building blocks the library already standardises, and the package layout no longer tells a reader where things live: modules/ holds plain functions, models/util.py holds test fixtures, channel handling lives in four places. This issue proposes one layout rule, the consolidation it implies, and a staged plan that keeps every public import and every released checkpoint working. Feedback on the rule and on the staging is the point of the issue.
The rule (PyTorch's own convention)
| package | holds |
|---|---|
braindecode.functional |
stateless tensor functions |
braindecode.modules |
reusable nn.Module blocks, one file per concept |
braindecode.models |
architectures and their private parts |
braindecode.datautil |
data metadata (channels, montages) and serialization |
braindecode.util |
generic helpers only (seeds, tensor conversion) |
What an audit of master found
Every top-level function and class (935) was inventoried with its importers and its test/doc use.
One concept spread over several files
- Channel metadata (
chs_info, montages, positions):util.resolve_montage_name, seven functions inmodels/util.py,modules/channels.py,datautil/channel_utils.py. - Model input geometry (sampling rate, window length, patching):
models/util.py,models/base.py,modules/blocks.py(private helpers imported by 9 models),datautil/util.py. - Attention kernel: the scaled-dot-product helper is a function in
modules/attention.py, imported privately by 8 models. - Positional encodings, max-norm constraints, Hub/Zarr I/O and the model registry: 3–6 files each.
Files in the wrong place
- ~260 lines of test fixtures in
models/util.py(_get_signal_params,_draw_chs_info, …) used only by tests. modules/popt_modules.pyand most ofmodules/dance_modules.pyare private to a single model.modules/util.aggregate_probasis cropped-decoding scoring, not a module.
Private code re-implementing a standard
| pattern | private copies in models/ |
standard |
|---|---|---|
reset_head |
37 overrides; 19 do not keep the head's device/dtype (e.g. BIOT(...).double(); reset_head(3) then forward raises a dtype mismatch) |
one default in EEGModuleMixin for a linear final_layer, overridden only for non-linear heads |
weight-init loops (trunc_normal_/xavier) |
30 | one functional init helper |
residual wrappers (_ResidualAdd ×5, …) |
14 | two shared blocks: pre-norm x + drop_path(fn(x)), post-norm norm(drop(fn(x)) + x) |
| hand-written softmax attention | 15 | F.scaled_dot_product_attention / modules.MultiHeadAttention |
| rotary embeddings | 32 sites | functional.rotate_pairs + one RoPE module |
| private MLP/FFN | 8 (eegpt._MLP ≡ luna._Mlp) |
modules.MLP / FeedForwardBlock |
patch embedding by unfold/reshape |
19 | modules.PatchTokenizer |
Dead or orphaned
- Private, no user anywhere:
datasets/bids/format.py(717 lines, a copy ofhub_format.py), plus five functions/classes. - Public with no user, test or doc:
MaxNormConstraintCallback,load_bbci_sets_from_folder,is_registered_dataset,VectorQuantizer,IntermediateOutputWrapper,StatLayer,get_balanced_batches,read_all_file_names,util.corr/cov.
Dependencies
linear_attention_transformeris a core dependency that is never imported (the needed part is vendored inmodules/linear_attention.py).einopsis used in 47 files but not declared; today it only arrives throughlinear_attention_transformer.
Compatibility gates (apply to every PR below)
- A public name keeps importing from its old path (shim with
DeprecationWarning) until 2.0;docs/api.rstpoints to the new path. - Private names move after a grep of
test/,docs/,examples/. - Nothing that appears in serialized artefacts moves (
Preprocessorstores__class_path__; Hub configs store the model name, not a module path). - Model changes keep identical
state_dictkeys and bit-identical outputs on the released weights (the pretrained-compat test loads all of them).
Proposed staging
Before 1.9.0 (mechanical, no behaviour change except the reset_head fix):
- Declare
einops, droplinear_attention_transformer - Delete
datasets/bids/format.pyand the private dead code - Move test fixtures out of
models/util.py - Model-private modules into their model (PopT, DANCE)
-
EEGModuleMixin.reset_headdefault; drop the redundant overrides (fixes the device/dtype bug in 19 models) - Exact duplicates → imports (CHBMIT/SIENA base, CTNet/MSCFormer residual, NeuroPose/SensingDynamics init)
- Deprecate the orphaned public API listed above
- CI: compile/export/script tests per architecture family on PRs, full list nightly
- Coverage-guided removal of redundant tests (coverage must not drop)
After 1.9.0 (design, one PR per concept):
-
datautil.channels: one home for channel metadata, oneon_missingvocabulary - Model input geometry declared on the class and checked once by the mixin
- Shared residual blocks, weight init, attention kernel in
functional, one RoPE -
modules/constraints.pyfor max-norm; splitmodels/util.py(registry / geometry)
Questions for discussion
- Is the
functional/modules/models/datautilrule the one we want to enforce in review (e.g. a check in the new-model PR template)? - Deprecation window: one minor release (removal in 2.0), or longer?
- Should a new model be allowed private copies of attention/MLP/residual blocks when the shared one fits, or must it reuse the shared block?
Activity
API Deprecate the orphaned public API listed in #1442 (#1734)