Skip to content

Package layout: one home per concept before 1.9 (functional / modules / models / datautil) #1442

Open

Description

@bruAristimunha

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 in models/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.py and most of modules/dance_modules.py are private to a single model.
  • modules/util.aggregate_probas is 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 of hub_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_transformer is a core dependency that is never imported (the needed part is vendored in modules/linear_attention.py).
  • einops is used in 47 files but not declared; today it only arrives through linear_attention_transformer.

Compatibility gates (apply to every PR below)

  1. A public name keeps importing from its old path (shim with DeprecationWarning) until 2.0; docs/api.rst points to the new path.
  2. Private names move after a grep of test/, docs/, examples/.
  3. Nothing that appears in serialized artefacts moves (Preprocessor stores __class_path__; Hub configs store the model name, not a module path).
  4. Model changes keep identical state_dict keys 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, drop linear_attention_transformer
  • Delete datasets/bids/format.py and the private dead code
  • Move test fixtures out of models/util.py
  • Model-private modules into their model (PopT, DANCE)
  • EEGModuleMixin.reset_head default; 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, one on_missing vocabulary
  • 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.py for max-norm; split models/util.py (registry / geometry)

Questions for discussion

  1. Is the functional / modules / models / datautil rule the one we want to enforce in review (e.g. a check in the new-model PR template)?
  2. Deprecation window: one minor release (removal in 2.0), or longer?
  3. 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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions