Skip to content

ZUNA: registers option (False = encoder on the patch tokens only) - #1468

Merged
bruAristimunha merged 2 commits into
braindecode:masterfrom
bruAristimunha:feat/zuna-registers-option
Oct 19, 2026
Merged

bruAristimunha merged 2 commits into
braindecode:masterfrom
bruAristimunha:feat/zuna-registers-option

Conversation

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Adds registers: bool = True to braindecode.models.ZUNA.

  • registers=True (default): the published ZUNA1.1 encoder. The forward code, buffers and outputs don't change (the only addition on this path is the if on the flag).
  • registers=False: the encoder runs on the patch tokens only. The learned register before each patch is dropped, so the sequence has half as many tokens. Each patch keeps its rotary position (the patch rows of the default tables, rotary_cosine[1::2]), and the head reads the patch outputs instead of the register outputs.
  • Parameters and keys are the same, so pretrained weights load unchanged (strict). With registers=False the register parameter is still loaded but not used, and it has requires_grad=False, so DDP doesn't flag it as unused.

This changes the model, so it is not the published encoder, and the docstring says so. It's the efficient fine-tuning variant we use for ZUNA fine-tuning in the NeuralBench paper campaign. On one Gaudi2 card at batch 64 with the full 16 blocks and full fine-tuning, the train step is about 3.5-4x faster than with registers (TUAB 21 ch x 1280 samples). We measured accuracy against the full model on several studies over 3 seeds; those results will be reported in the paper.

Tests (test_foundation_models.py):

  • the default is the published encoder (two tokens per patch, registers trainable, output identical with registers=True);
  • registers=False uses the patch rows of the rotary tables, and its output matches the encoder applied by hand to the patch tokens;
  • the register parameter gets no gradient, and every other gradient is finite.

Voyager CPU Job: test_{foundation_models,models,integration,return_features}.py -k "zuna and not compiled": 28 passed, 2 skipped. pre-commit is clean.

Relation to #1461 (fused rotary on HPU): this branch starts from master and doesn't depend on #1461. The two merge cleanly in either order (checked with git merge-tree). With both merged, registers=False uses the fused rotary kernel as well, because the kernel reads the same per-token buffers.

Copilot AI balanced review requested due to automatic review settings October 18, 2026 00:18

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 reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 18, 2026 00:18
bruAristimunha added a commit to bruAristimunha/braindecode that referenced this pull request Oct 18, 2026

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 reached their quota limit.

@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.02%. Comparing base (8c618f1) to head (99fcaea).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1468   +/-   ##
=======================================
  Coverage   91.01%   91.02%           
=======================================
  Files         167      167           
  Lines       21621    21629    +8     
=======================================
+ Hits        19679    19687    +8     
  Misses       1942     1942           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

registers=True (default) is the published ZUNA1.1 encoder, unchanged. registers=False drops
the learned register before each patch: half the tokens, each patch keeps its rotary position,
and the head reads the patch outputs. The parameters are the same, so pretrained weights load
unchanged; the register parameter is then unused and does not require gradients.
@bruAristimunha
bruAristimunha force-pushed the feat/zuna-registers-option branch from 33a46bc to 99fcaea Compare October 18, 2026 12:25
Copilot AI balanced review requested due to automatic review settings October 18, 2026 12:25

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 reached their quota limit.

@bruAristimunha bruAristimunha left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Rebased on master (changelog conflict only). ZUNA with the default registers=True re-characterized against master: identical keys, weights and outputs. Queued.

@bruAristimunha
bruAristimunha merged commit 054808f into braindecode:master Oct 19, 2026
8 checks passed
bruAristimunha added a commit that referenced this pull request Oct 19, 2026
* ZUNA: registers option (False = the encoder on the patch tokens alone)

registers=True (default) is the published ZUNA1.1 encoder, unchanged. registers=False drops
the learned register before each patch: half the tokens, each patch keeps its rotary position,
and the head reads the patch outputs. The parameters are the same, so pretrained weights load
unchanged; the register parameter is then unused and does not require gradients.

* DOC whats_new: ZUNA registers option (#1468)

* ZUNA: rotary embedding through Gaudi's fused RoPE kernel on HPU

On HPU, _RotaryPositionEmbedding runs the bridge's pairwise RoPE kernel
(hpex.kernels.apply_rotary_pos_emb, PAIRWISE) as a custom op instead of the
flip/cast/multiply formula: same float32 arithmetic and identical outputs;
the kernel has no gradient, so the backward is the kernel at the opposite
angle (gradients rounded to the input dtype once instead of per term).
A custom op stays one node inside torch.compile. CPU and other devices
unchanged.

* DOC whats_new: ZUNA fused rotary on HPU (#1461)

* ZUNA: fused rotary in HPU eager mode and compiled regions only, not lazy

In lazy mode the graph compiler fuses the formula itself and the kernel made
the Gaudi2 train step slower (2.47 -> 2.86 s at 21 channels x 1280 samples,
batch 64): _hpu_fused_rotary returns None there and the formula runs.

* DOC whats_new: ZUNA fused rotary is eager/compiled only
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.

2 participants