Skip to content

[DO_NOT_MERGE] Use l2Norm TPC kernel for HPU compile - #1457

Open
osavchenkox wants to merge 1 commit into
vllm-project:mainfrom
osavchenkox:dev/osavchenko/l2_norm
Open

[DO_NOT_MERGE] Use l2Norm TPC kernel for HPU compile#1457
osavchenkox wants to merge 1 commit into
vllm-project:mainfrom
osavchenkox:dev/osavchenko/l2_norm

Conversation

@osavchenkox

Copy link
Copy Markdown
Contributor

Replace eager _l2norm_last_dim with TPC l2_norm op, remove @torch._dynamo.disable to allow torch.compile, and fix dtype handling so l2norm outputs fp32 directly.

Copilot AI review requested due to automatic review settings May 19, 2026 08:29
@github-actions

Copy link
Copy Markdown
Contributor

🚧 CI Blocked

The main CI workflow was not started for the following reason:

This is a Draft PR. Please mark it as 'Ready for Review' to trigger the CI.

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

Note

Copilot was unable to run its full agentic suite in this review.

This PR refactors Q/K L2-normalization to use an HPU TPC (torch.ops.hpu.l2_norm) implementation and adjusts Q/K dtype handling in the recurrent gated delta rule path.

Changes:

  • Introduce TPC- and PyTorch-based L2-norm helpers and route _l2norm_last_dim to the HPU op.
  • Update _preprocess_qk_l2norm to normalize without pre-casting inputs to fp32.
  • Change recurrent path to avoid unconditional casting of Q/K unless L2-norm is disabled.

Comment on lines +54 to 56
# @torch._dynamo.disable
def _preprocess_qk_l2norm(q, k):
"""L2norm in eager mode — HPU torch.compile miscompiles l2norm."""
@torch._dynamo.disable
def _l2norm_tpc_last_dim(x: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
# input bf16, output fp32 to avoid precision issues
return torch.ops.hpu.l2_norm(x, epsilon=eps)
Comment on lines 624 to +627
# Flatten token axis.
# Compute dtype controlled by VLLM_GDN_COMPUTE_FP32 env var (default: bf16)
qf = q.reshape(-1, H, Kdim).to(_GDN_COMPUTE_DTYPE)
kf = k.reshape(-1, H, Kdim).to(_GDN_COMPUTE_DTYPE)
qf = q.reshape(-1, H, Kdim)
kf = k.reshape(-1, H, Kdim)
Comment on lines +633 to +634
qf = _l2norm_last_dim(qf) # output is fp32
kf = _l2norm_last_dim(kf) # output is fp32

def _l2norm_pt_last_dim(x: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
x = x.to(torch.float32)
return (x / torch.sqrt(torch.sum(x * x, dim=-1, keepdim=True) + eps)).to(torch.float32)
@github-actions

Copy link
Copy Markdown
Contributor

🚧 CI Blocked

The main CI workflow was not started for the following reason:

This is a Draft PR. Please mark it as 'Ready for Review' to trigger the CI.

@osavchenkox osavchenkox changed the title use l2Norm TPC kernel for HPU compile [DO_NOT_MERGE] use l2Norm TPC kernel for HPU compile May 19, 2026
Replace eager _l2norm_last_dim with TPC l2_norm op, remove
@torch._dynamo.disable to allow torch.compile, and fix dtype
handling so l2norm outputs fp32 directly.

Signed-off-by: OlegX Savchenko <olegx.savchenko@intel.com>
@osavchenkox
osavchenkox force-pushed the dev/osavchenko/l2_norm branch from f9b2676 to ccae01f Compare June 2, 2026 17:26
@osavchenkox osavchenkox changed the title [DO_NOT_MERGE] use l2Norm TPC kernel for HPU compile Use l2Norm TPC kernel for HPU compile Jun 2, 2026
@osavchenkox

Copy link
Copy Markdown
Contributor Author

Rebased on latest main and dropped the [DO_NOT_MERGE] tag. The previous CI failure was an old workflow-version mismatch and not a code issue. Ready for review and pre-merge approval.

@osavchenkox osavchenkox changed the title Use l2Norm TPC kernel for HPU compile [DO_NOT_MERGE] Use l2Norm TPC kernel for HPU compile Jun 2, 2026
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