Repository navigation
Description
While wrapping the pretrained BENDR for a comparison against the authors' code, we found two places where braindecode.models.BENDR loaded from the Hub does not seem to reproduce the original pretrained model. We may well be missing something (a deliberate choice, or a different reference release), so please treat this as a question with evidence rather than a confirmed bug.
Setup. braindecode 1.4.0; Hub weights braindecode/braindecode-bendr at revision 191f221cd56de8203899ea9a8d0f43238724f8b6 (model.safetensors); reference: the authors' release assets encoder.pt (SHA-256 32c913b1...0fcf84cb) and contextualizer.pt (SHA-256 b8ee0c8c...6f91526) from SPOClab-ca/BENDR release v0.1-alpha, loaded with strict=True into ConvEncoderBENDR and BENDRContextualizer from dn3_ext.py at commit ac918ab. Comparisons are in eval mode on CPU.
1. The contextualizer's input projection looks like an initialization, not the pretrained layer. The encoder tensors in the Hub file equal the release's encoder.pt exactly, and the weight-normalized position convolution matches too. But contextualizer.input_conditioning.3 (the 512 to 1536 projection) does not match the release's input_conditioning.4 (the same projection as a 1x1 Conv1d, compared after squeezing the kernel dimension): the correlation between the two weight tensors is about 0.001, their standard deviations are 0.0124 against 0.0013, and the Hub bias is exactly zero. That pattern looks to us like a freshly initialized layer saved with the converted checkpoint, but we could not find the conversion script to check.
2. The contextualizer applies a final LayerNorm that the original forward does not seem to use. In bendr.py, _BENDRContextualizer.forward calls x = self.norm(x) (line 543 in 1.4.0) before self.output_layer. In the original BENDRContextualizer.forward (dn3_ext.py, lines 556 to 581 at ac918ab), self.norm is defined but, as far as we can read, never called; the output is output_layer(transformer_layers(...)). The release's norm.* tensors are not an identity (weight constant at about 0.9235, bias 0), so the extra call changes the output.
Effect. With the release tensors mapped into braindecode's BENDR (issue 1 worked around), the last-position contextualizer output differs from the original classes by a factor of about 22 in relative norm (cosine similarity about 0.74). After setting model.contextualizer.norm = nn.Identity(), the maximum absolute difference to the original classes is 0.0, for the last position and for every other position. So the rest of the port (layer order, weight-norm parameters, the 1x1 convolution as a linear layer) looks right to us; only these two points differ.
Questions. Is the Hub checkpoint meant to carry the pretrained input_conditioning weights, and was the final norm call intentional (for example matching a later version of the authors' code that we have not found)? If useful, we can share the short comparison script.
Possibly related: #1227 (input-geometry audit of the pretrained classes), which covers BENDR's channel handling but not weight equivalence.
Activity
bruAristimunha commentedon Oct 29, 2026
You are right here about the layernorm extra, but we had a very, very long journey with bendr, and this model is really bad. Including or not doesn't make a difference in the downstream benchmark.
I think the conversion should be in Hugging Face; this is where we are doing the deposit.
But i come back soon with more results...
and please keep digging the models :)
antonsoo commentedon Oct 30, 2026
I will check the final LayerNorm against the pinned release classes and weights on recorded EEG, then prepare a focused fix that keeps existing state dictionaries loadable. I will check the projection tensors separately so the two differences stay distinguishable.
antonsoo commentedon Oct 30, 2026
I reproduced both differences using the pinned release tensors and 12 recorded PhysioNet EEG windows. #1498 addresses the extra final LayerNorm while retaining checkpoint keys.
Only the projection weight and bias differ among the mapped contextualizer tensors; all encoder tensors match. A local copy can restore those two tensors with:
With the code fix and these two replacements, full contextualizer outputs agree with the original classes within 1.8e-8 on CPU/CUDA; the start-token features agree exactly. With the code fix alone, the original Hub projection still gives a maximum difference of 0.0867 on the three-window CPU check. This is numerical equivalence, not a downstream accuracy result.
BabaSanfour commentedon Nov 14, 2026
A follow-up on the contextualizer now that #1498 is merged. With the projection taken from the release (the workaround above), braindecode's contextualizer matches a plain-torch rebuild of the authors' forward from the release tensors (max |diff| 2.4e-7). All other contextualizer tensors on the Hub equal the release's to 1e-6, so the projection is the only weight that differs.
With these weights, the start-token output that
BENDR.forwardpasses tofinal_layer(context[:, :, 0]) is almost the same for every input. The authors' fine-tuning code reads the last position instead:BENDRClassification.features_forwardindn3_ext.pyreturnscontext[:, :, -1], with thecontext[:, :, 0]line commented out.Spread across 32 random-walk windows (std across windows / mean |value|), master
3118540a:context[:, :, 0]context[:, :, -1]So with the release weights, a head on
context[:, :, 0]gets a near-constant input. Shouldforwardread the last position, as the authors' code does?Reproduction
contextualizer.ptis the release asset of SPOClab-ca/BENDR v0.1-alpha (sha256b8ee0c8c…6f91526). Without the workaround block, it gives the "Hub weights" column.Claude Code was used to run these measurements and check them against the release; I reviewed and ran them.