Skip to content

ZUNA: rotary embedding through Gaudi's fused RoPE kernel on HPU - #1461

Merged
bruAristimunha merged 7 commits into
braindecode:masterfrom
bruAristimunha:perf/zuna-hpu-speed
Oct 19, 2026
Merged

bruAristimunha merged 7 commits into
braindecode:masterfrom
bruAristimunha:perf/zuna-hpu-speed

Conversation

@bruAristimunha

@bruAristimunha bruAristimunha commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

In HPU eager mode and inside compiled regions, ZUNA's rotary embedding (_RotaryPositionEmbedding) now runs Gaudi's fused pairwise RoPE kernel (habana_frameworks.torch.hpex.kernels.apply_rotary_pos_emb, mode PAIRWISE) instead of the flip / cast / multiply formula. CPU, other devices and HPU lazy mode are unchanged (in lazy mode the graph compiler fuses the formula itself; the kernel made the lazy step slower, 2.47 -> 2.86 s at 21 x 1280, batch 64).

  • Same float32 arithmetic: forward outputs are identical (max |diff| 0.0 on Gaudi2 at 64 x 1680 tokens x 8 heads).
  • The kernel has no gradient of its own, so it is wrapped as a custom op (braindecode::zuna_pairwise_rotary) whose backward is the same kernel at the opposite angle (the rotation is orthogonal). The gradient reaching the bfloat16 queries/keys is rounded once instead of once per term: <= 1 bf16 ulp from the formula's, and closer to the float32 reference (max error 0.016 vs 0.030 against an fp32 reference).
  • A custom op (not an autograd.Function) stays one node inside torch.compile: with an autograd.Function the compiled block got slower (1.84 -> 2.58 s/step).
  • Parameters, buffers and state dict are unchanged; pretrained ZUNA1.1 weights load as before.

Measured on one Gaudi2 card (image 1.21.4, torch 2.6), ZUNA defaults (16 layers, dim 1024), batch 64, bf16 autocast, train step incl. AdamW:

geometry rotary fwd+bwd per block train step eager step with the block compiled (hpu_backend)
21 ch x 1280 samples (107,520 tokens/batch) 26.1 -> 2.65 ms 2.61 -> 1.96 s 1.78 -> 1.25-1.42 s
6 ch x 3840 (92,160) 22.5 -> ~2.3 ms 2.16 -> 1.47 s 1.44 -> 0.99 s
2 ch x 1280 (10,240) 0.093 -> 0.061 s

Tests: test_zuna_hpu_fused_rotary_matches_the_formula runs the HPU path on CPU with a stand-in kernel (float32: outputs and gradients within 1e-6; bfloat16: within one ulp). Voyager CPU Job: test_foundation_models.py -k zuna 11 passed, test_models.py/test_integration.py -k "ZUNA and not compiled" 14 passed.

Context: the NeuralBench paper campaign spends ~half of its Gaudi card-hours on ZUNA; the same kernel is installed there through neuraltrain until a braindecode release carries it.

Copilot AI balanced review requested due to automatic review settings October 17, 2026 21:14

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 added a commit to bruAristimunha/braindecode that referenced this pull request Oct 17, 2026
Copilot AI balanced review requested due to automatic review settings October 17, 2026 21:15

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 17, 2026 21:21

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 17, 2026 21:23

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.

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.
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.
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.
Copilot AI balanced review requested due to automatic review settings October 18, 2026 12:28

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 and #1468: the SDPA helper moved to functional.scaled_dot_product_attention (#1459) and _hpu_lazy to functional.attention, imports updated. CPU path re-characterized against master: bit-identical. The Gaudi kernel path cannot be exercised here; queued on the strength of the HPU numbers in the description.

@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.87097% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.99%. Comparing base (e1bdb41) to head (3ccd64d).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1461   +/-   ##
=======================================
  Coverage   90.99%   90.99%           
=======================================
  Files         167      167           
  Lines       21516    21564   +48     
=======================================
+ Hits        19578    19623   +45     
- Misses       1938     1941    +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI balanced review requested due to automatic review settings October 19, 2026 08:26

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 merged commit df66b2e into braindecode:master Oct 19, 2026
7 checks passed
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