Skip to content

Add REVE model - #866

Closed
jonathanlys01 wants to merge 0 commit into
braindecode:masterfrom
jonathanlys01:master
Closed

jonathanlys01 wants to merge 0 commit into
braindecode:masterfrom
jonathanlys01:master

Conversation

@jonathanlys01

Copy link
Copy Markdown
Contributor

This is a first attempt at integrating the REVE model to braindecode.
More documentation will follow once the implementation and usage are validated by the team.

To verify model instantiation and Hugging Face loading, you can run the following (a separate PR will add proper tests if this proves sufficient):

from braindecode.models import REVE
import torch

n_chans = 64
n_times = 1000
batch_size = 8

eeg = torch.randn(batch_size, n_chans, n_times)
pos = torch.randn(batch_size, n_chans, 3)

# Test with random model
model = REVE()
out = model(eeg, pos)

# Load pretrained and use positions
model: REVE = REVE.from_pretrained("brain-bzh/reve-base", cache_dir="./cache")
channel_names = [f"E{i + 1}" for i in range(n_chans)]
pos = model.get_positions(channel_names).expand(eeg.size(0), -1, -1)  # (batch_size, channels, 3)
out = model(eeg, pos)

@bruAristimunha bruAristimunha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but a few small things are needed:

  1. Longer descriptions, more details, and details for each figure.
  2. More descriptive variable names; nothing like b, c, p; they should be more descriptive so the user can understand them.
  3. The positional bank needs to be more accessible.
  4. The integration tests will already check if the model can be run.

Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
@bruAristimunha

Copy link
Copy Markdown
Collaborator

Can you fill the what's new file please and export the model parameters within the utils?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds the REVE (Representation for EEG with versatile embeddings) model to braindecode, implementing a foundation model for EEG that uses 4D positional embeddings and transformer architecture to process EEG signals. The implementation is based on the paper by El Ouahidi et al. (2025).

  • Implements the REVE model with patch-based EEG tokenization and 4D positional encoding
  • Includes a position bank that downloads channel positions from HuggingFace at initialization
  • Supports both standard flattened output and attention pooling for classification tasks

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 21 comments.

File Description
braindecode/models/reve.py Complete implementation of the REVE model including the main model class, transformer components, 4D positional embeddings, and position bank integration
braindecode/models/init.py Exports the REVE model to make it available in the braindecode.models namespace

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Outdated
Comment on lines +530 to +531
to_patch_embedding = nn.Sequential(nn.Linear(patch_size, embed_dim))
return to_patch_embedding

Copilot AI Dec 10, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The function patch_embedding returns a nn.Sequential with only one layer, which is unnecessarily wrapped. The function could simply return nn.Linear(patch_size, embed_dim) directly, or the nn.Sequential wrapper should be removed since it adds no value with a single layer.

If the intent is to allow for future expansion with additional layers, that should be documented. Otherwise, this is an unnecessary layer of indirection.

Suggested change
to_patch_embedding = nn.Sequential(nn.Linear(patch_size, embed_dim))
return to_patch_embedding
return nn.Linear(patch_size, embed_dim)

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this, it silently breaks the parameter loading of the patch embedding parameters.
I again highly support the introduction of tests that will check for the equality of the output with the hugging face model and the braindecode version.
Something like:

from braindecode.models import REVE
import torch
from transformers import AutoModel


batch_size = 2
n_chans = 32
n_times = 1000
n_outputs = 10
cache_dir = "./cache"

ch_list = [f"E{i + 1}" for i in range(n_chans)]
eeg_input = torch.randn(batch_size, n_chans, n_times)

model_braindecode: REVE = REVE.from_pretrained(
    "brain-bzh/reve-base",
    cache_dir=cache_dir,
    n_times=n_times,
    n_chans=n_chans,
    n_outputs=n_outputs,
)
pos_braindecode = model_braindecode.get_positions(ch_list)
pos_braindecode = pos_braindecode.unsqueeze(0).repeat(batch_size, 1, 1)

model_hf = AutoModel.from_pretrained(
    "brain-bzh/reve-base",
    cache_dir=cache_dir,
    trust_remote_code=True,
)
pos_bank_hf = AutoModel.from_pretrained(
    "brain-bzh/reve-positions",
    cache_dir=cache_dir,
    trust_remote_code=True,
)
pos_hf = pos_bank_hf(ch_list)
pos_hf = pos_hf.unsqueeze(0).repeat(batch_size, 1, 1)

# return_output is True to bypass the final layer
output_braindecode = model_braindecode(eeg_input, pos_braindecode, return_output=True)
output_hf = model_hf(eeg_input, pos_hf, return_output=True)

print("positions allclose:", torch.allclose(pos_braindecode, pos_hf, atol=1e-6))
print("outputs allclose:", torch.allclose(output_braindecode[-1], output_hf[-1], atol=1e-6))

Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
@bruAristimunha

bruAristimunha commented Dec 10, 2025 •

Copy link
Copy Markdown
Collaborator

Almost all the comments for the copilot are validated and need to be addressed, except for the test that we already covered with the integration test, which follows the Timm style.

There are some more style details about layer names and how to become fully compatible with braindecode and still be able to load the weights, but I can address them when everything is resolved from the copilot initial review and the tests are green.

@jonathanlys01

Copy link
Copy Markdown
Contributor Author

I agree with all copilot comments about the style, there's just the small issue about the patch embedding layer being a Sequential with a single element, but replacing it with a plain linear silently breaks the loading of the parameters through hugging face (see this comment).

@bruAristimunha

Copy link
Copy Markdown
Collaborator

I am supporting of the new test, please let me know if you need any help. It should be quite straightforward to do with Copilot, including transformer as a dependency for the test, and a test following PyTest style. Please let me know if you need any help.

@jonathanlys01

Copy link
Copy Markdown
Contributor Author

Added the test, you will probably have to add a hugging face token in the GitHub secrets because the repository for REVE is gated.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Hey @jonathanlys01, I tried, and it didn't work.

Can you find a way to block only the weights? Or maybe host in another repository.

And btw, we are planning to launch next Friday, or when MNE-bids release. If we don't integrate it by next Friday, it will be included in the next release, possibly at the end of February. I'm working on the promotional materials this next week, canvas, video, and LinkedIn post.

@jonathanlys01

Copy link
Copy Markdown
Contributor Author

The goal is specifically to ensure the weights integrity, I think it works if you manage to add an HF token as a secret then load it as an env variable and pass it to the from_pretrained method through the token argument.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Sorry, I couldn't do it. I haven't found a way to do this without linking my personal Hugging Face permission, which would require an additional key that we would have to maintain. Perhaps there's a way to grant permission within the BrainDecode organization.

I still think it's a terrible idea to implement this gatekeeping. It is fundamentally against the principle of accessibility to more FAIR science. This is everything we are historically against in Moabb. However, we are at braindecode, so I don't want to block the merge, so I'm granting you the temporary maintenance permission to add you to the secret.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

@jonathanlys01

Copy link
Copy Markdown
Contributor Author

Hello @bruAristimunha , I've worked on the integration a bit:

  • added the test for the model loading but the secret can't be loaded since it's an external PR, it works locally though.
  • added REVE to the summary.csv, let me know if you agree on the labels (currently Foundation Model and Attention/Transformer.

Let me know if there is anything left to do before merging.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Can you just double check the comments from copilot first revision? If you think it is okay, just resolved.

@codecov

codecov Bot commented Dec 14, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.13559% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.78%. Comparing base (3a842a0) to head (e43f10f).

Additional details and impacted files
@@           Coverage Diff            @@
##           master     #866    +/-   ##
========================================
  Coverage   87.77%   87.78%            
========================================
  Files         111      112     +1     
  Lines       10011    10247   +236     
========================================
+ Hits         8787     8995   +208     
- Misses       1224     1252    +28     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jonathanlys01

Copy link
Copy Markdown
Contributor Author

I think we are ready to merge @bruAristimunha !

Comment thread braindecode/models/reve.py Resolved
@PierreGtch

Copy link
Copy Markdown
Collaborator

Super nice to have your model in Braindecode, thanks @jonathanlys01 !!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

deepinv:1

  • This appears to be an unrelated subproject commit addition. Consider removing this from the REVE model PR or explaining its relevance in the PR description.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
@bruAristimunha

Copy link
Copy Markdown
Collaborator

@codex review please

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

deepinv:1

  • This appears to be an unrelated Git submodule addition that is not mentioned in the PR description. If this is intentional, it should be documented in the PR description or removed if added accidentally.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread braindecode/models/reve.py Resolved
Comment thread braindecode/models/reve.py Resolved
Comment thread docs/whats_new.rst Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread braindecode/models/reve.py Resolved
@bruAristimunha

Copy link
Copy Markdown
Collaborator

It's okay for me @jonathanlys01!

However, I made several modifications to the documentation. Can you check the render documentation @jonathanlys01, @glioi, and @elouayas?

To download the documentation locally, you will need to click on this link:

https://github.com/braindecode/braindecode/actions/runs/20288163405/artifacts/4893342966

I'm waiting for your OK to apply the merge.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

I made one small change when I was trying to create the redirect link for the doc. Now the PR got moved for this: #876, but @jonathanlys01 still has the writing access.

Sorry for my git issue.

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.

4 participants