Skip to content

REVE, Labram: accept ch_mask - #1539

Closed
julien-gadonneix wants to merge 6 commits into
masterfrom
feat/reve-labram-ch-mask
Closed

julien-gadonneix wants to merge 6 commits into
masterfrom
feat/reve-labram-ch-mask

Conversation

@julien-gadonneix

Copy link
Copy Markdown
Collaborator

REVE and Labram accept a ch_mask of real channels, as the iEEG foundation models of #1487, so one batch can mix recordings with different channel counts.

  • Padded channels are masked as attention keys and in pooling; Labram's patch-embedding GroupNorm takes its statistics over the real channels only (masked_group_norm).
  • Heads tied to the channels refuse a mask: REVE's flattened head (needs attention_pooling=True) and Labram's decoder mode (needs neural_tokenizer=True).
  • test_labram_qkv_split.py: the frozen reference attention accepts the new key_mask argument and asserts it is unused; its computation is unchanged.

Testing

  • test_ch_mask.py: REVE and Labram added (each recording matches running alone, eager and scripted; no gradient into padding; channel-tied heads refuse); pytest test/unit_tests/models -k "reve or labram or ch_mask": 141 passed, 45 skipped
  • Full test/unit_tests/models with 16 xdist workers in 64 GB: 4713 passed, 34 crashed workers (out of memory) on models this PR does not touch, plus one Labram contract test that passes when rerun alone
  • pre-commit run --files <changed files>: passed

Copilot AI balanced review requested due to automatic review settings November 5, 2026 00:35

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 00:36

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.36508% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.36%. Comparing base (619e703) to head (a75e3e5).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1539      +/-   ##
==========================================
- Coverage   91.38%   91.36%   -0.02%     
==========================================
  Files         170      170              
  Lines       22137    22173      +36     
==========================================
+ Hits        20229    20259      +30     
- Misses       1908     1914       +6     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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.

Thanks, the shared helpers are reused well (check_ch_mask, masked_mean, masked_group_norm), and the channel-tied heads refuse the mask as they should. I checked the less obvious paths:

  • the Labram key bias is added to the relative-position bias;
  • the HPU path falls back to the plain attention whenever a mask is set;
  • with ch_names, the mask lines up because x is not reordered;
  • REVE re-indexes the mask together with channel_index.

P2: Labram, patch_embed loop with hasattr(layer, "norm1")
patch_embed is always exactly segment_patch → temporal_conv in tokenizer mode, so the type sniffing (and its TorchScript caveat) can go:

x = self.patch_embed.temporal_conv(self.patch_embed.segment_patch(x), ch_mask)

P2: padded tokens in the token-level outputs
REVE's return_features["features"] and Labram's features / return_patch_tokens still carry the padded channels' tokens, which are finite but arbitrary. #1540 zeroes them for the seven iEEG models. Please zero them here too (masked_fill(~token_mask[..., None], 0.0)), so token outputs mean the same thing in every model that takes ch_mask.

P3

  • Labram forward_features: x is always 3-D at that point, so ch_mask[:, :, None] reads better than the reshape(list(...) + [1] * ...).
  • _TemporalConv: pass eps=self.normN.eps to masked_group_norm. It matches the default today, but this keeps the two in sync if anyone changes it.
  • codecov/project is red. The uncovered lines are probably the two refusal branches; test_ch_mask_is_refused_by_fixed_size_heads could take REVE (attention_pooling=False) and Labram (neural_tokenizer=False) as two more cases.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 14:35

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@julien-gadonneix

Copy link
Copy Markdown
Collaborator Author

Thanks!

  • patch_embed: called directly; both modes still script.
  • Padded tokens are zeroed in REVE's return_output and return_features, and in Labram's features / return_patch_tokens / return_all_tokens. Both also return "tokens", (batch, n_chans, n_patches, dim) (one row in Labram's decoder mode), as asked on iEEG foundation models: return the per-token output, padded channels zeroed #1540.
  • P3: ch_mask[:, :, None]; eps passed.
  • Codecov: the two refusal cases were already in test_ch_mask_is_refused_by_fixed_size_heads (REVE-flatten, Labram-decoder). The uncovered REVE lines come from conftest.py marking any test parametrized with "REVE" as a network test, so CI's unit run skips REVE's ch_mask cases; they pass with --run-network. The two uncovered Labram lines are the return_attention / return_qkv branches, uncovered before, which now pass key_mask.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Thanks for the updates, they address the review. Two things before merging:

…-mask

# Conflicts:
#	test/unit_tests/models/test_ch_mask.py
Copilot AI balanced review requested due to automatic review settings November 5, 2026 16:27

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@julien-gadonneix

Copy link
Copy Markdown
Collaborator Author

Merged master: kept this side of the test_ch_mask.py hunks, as you said. REVE also passes the new training tests of #1545 (test_fit_predict_with_pad_channels_collate, test_training_step_gradients_match_each_recording_alone) with --run-network. No # nosec B105 was added here.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

#1548 (now on master) checks the ch_mask contract automatically for every registered model whose forward takes ch_mask. After you merge master, Labram needs two lines for that test, because a recording run alone on fewer than 128 channels needs ch_names:

# test/unit_tests/models/test_model_contract.py, _CH_MASK_EXTRAS
"Labram": ("ch_names", lambda i, n: LABRAM_CHANNEL_ORDER[:n]),

You also need a strict xfail for Labram in test_registered_model_ch_mask_training, because training does not route ch_names, the same as PopulationTransformer's coords. I checked both locally on a merge of your branch with master and everything passes. REVE refuses at its registry settings (attention_pooling=False), which counts as passing.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 16:53
@julien-gadonneix

Copy link
Copy Markdown
Collaborator Author

Done: merged master, added Labram to _CH_MASK_EXTRAS (ch_names) and a strict xfail for it in test_registered_model_ch_mask_training. The Labram contract test passes, and REVE's pass with --run-network.

FYI, test_registered_model_ch_mask_training[Brant] failed in 2 of 14 local runs of the whole ch_mask subset (test_model_contract.py, test_ch_mask_training.py, test_ch_mask.py, --run-network), on a gradient off by 5.5e-4 (512 elements), and in none of 12 runs on master; never when run alone. Brant is untouched here and the case is unseeded, so I can't tell a branch effect from chance; just flagging it in case CI hits it.

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Merged through #1549: this branch with master merged in (both sides kept), plus fixes for the Copilot findings (on_missing='drop' mask, offline REVE-flatten) and a seeded contract case for the Brant flake you reported. Thanks @julien-gadonneix, you are co-author of the squash commit.

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.

3 participants