Repository navigation
ZUNA: rotary embedding through Gaudi's fused RoPE kernel on HPU - #1461
Merged
bruAristimunha merged 7 commits intoOct 19, 2026
Merged
Conversation
bruAristimunha
added a commit
to bruAristimunha/braindecode
that referenced
this pull request
Oct 17, 2026
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.
bruAristimunha
force-pushed
the
perf/zuna-hpu-speed
branch
from
October 18, 2026 12:28
035ce0d to
b0faade
Compare
bruAristimunha
commented
Oct 18, 2026
bruAristimunha
left a comment
Collaborator
Author
There was a problem hiding this comment.
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 Report❌ Patch coverage is 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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, modePAIRWISE) 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).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).autograd.Function) stays one node insidetorch.compile: with anautograd.Functionthe compiled block got slower (1.84 -> 2.58 s/step).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:
Tests:
test_zuna_hpu_fused_rotary_matches_the_formularuns 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 zuna11 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.