mirror of
https://github.com/Comfy-Org/comfy-kitchen.git
synced 2026-11-04 23:37:42 +00:00
[GH-ISSUE #261] triton rms_rope kernel reads its input twice and launches 95k single-row programs #70
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Comfy-Org/comfy-kitchen#70
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @okolenmi on GitHub (Oct 19, 2026).
Original GitHub issue: https://github.com/Comfy-Org/comfy-kitchen/issues/261
AI generated, but it was actually a helpful optimization.
Where
comfy_kitchen/backends/triton/rms_rope.py,rms_rope_kernel(comfy-kitchen0.2.37). This is the implementation used when no vendor backend is available,
e.g. on Intel XPU.
Two things visible in the source
The input row is loaded twice. Line 55 loads the whole row (
x_full,head_dimelements) to compute the sum of squares; lines 66-67 then loadthe same row again (
x_0/x_1) for the rotation. A 128-element row isread as 256 elements.
One program per head row.
grid = (batch, num_heads, seq_len)(line130) with
block_size = next_power_of_2(head_dim // 2)= 64 forhead_dim=128. At the shapes we use (1, 3973, 24, 128) that is 95,352programs of 64 lanes per launch, and
rms_rope_split_halflaunches ittwice (once for q, once for k).
Measured
1 x 3973 x 24 x 128bf16, Intel Arc B580, torch 2.12.1+xpu:rms_rope_split_halfcallRemoving the second load cuts the traffic per call from ~146 MB to ~98 MB
(-33%). The 180 GB/s is also roughly half of what a plain copy achieves, so
there is more in the gap than the double read alone. I have not profiled the
kernel's own limiter (occupancy vs bandwidth), so I cannot attribute the rest
between the grid shape and the loads -- both are visible in the source.
Suggested fix
A single-pass kernel: load each row once into registers, reduce over
head_dim, normalise, rotate, store. Since the q and k calls are independentand use an identical grid, consider one launch for both as well.