Skip to content

Add RMS fusion for RoPE - #70

Merged
comfyanonymous merged 4 commits into
Comfy-Org:mainfrom
contentis:rmsnorm_rope
Jul 13, 2026
Merged

Add RMS fusion for RoPE#70
comfyanonymous merged 4 commits into
Comfy-Org:mainfrom
contentis:rmsnorm_rope

Conversation

@contentis

Copy link
Copy Markdown
Contributor

RMSNorm + RoPE

Measured BF16 Q/K/frequencies, FP32 RMS scales, eps=1e-6, 10 warmups, 8 samples, and 10 grouped iterations. BW is API-boundary bytes (inputs + outputs + frequencies + scales); Rated peak is that API BW divided by the GPU's nominal DRAM bandwidth; speedup is unfused / fused. The unfused baseline is F.rms_norm for Q/K followed by the matching ck.apply_rope* call.

Reference peak bandwidths: RTX 5000 Ada 470 GB/s; RTX PRO 6000 Blackwell 1790 GB/s.

GPU Config Variant Fused us Unfused us Speedup Fused BW GB/s Rated peak Unfused BW GB/s
RTX 5000 Ada FLUX BHND (1,24,4352,128) interleaved 229.734 2070.259 9.01x 475.3 101.1% 52.7
RTX 5000 Ada LTX BHND (2,32,4996,64) interleaved 352.896 3605.120 10.22x 471.2 100.3% 46.1
RTX 5000 Ada ZIMAGE BNHD (1,4096,30,128) interleaved 271.168 2609.459 9.62x 471.8 100.4% 49.0
RTX 5000 Ada WAN BNHD (2,12288,16,128) split-half 894.003 9101.312 10.18x 464.5 98.8% 45.6
RTX 5000 Ada FLUX BHND (1,24,4352,128) split-half 232.614 2069.747 8.90x 469.4 99.9% 52.8
RTX 5000 Ada LTX BHND (2,32,4996,64) split-half 355.187 3599.002 10.13x 468.1 99.6% 46.2
RTX PRO 6000 Blackwell FLUX BHND (1,24,4352,128) interleaved 37.280 380.850 10.22x 2928.8 163.6% 286.7
RTX PRO 6000 Blackwell LTX BHND (2,32,4996,64) interleaved 98.642 818.253 8.30x 1685.6 94.2% 203.2
RTX PRO 6000 Blackwell ZIMAGE BNHD (1,4096,30,128) interleaved 38.834 548.502 14.12x 3294.2 184.0% 233.2
RTX PRO 6000 Blackwell WAN BNHD (2,12288,16,128) split-half 291.855 2902.382 9.94x 1422.8 79.5% 143.1
RTX PRO 6000 Blackwell FLUX BHND (1,24,4352,128) split-half 36.536 373.450 10.22x 2988.4 167.0% 292.4
RTX PRO 6000 Blackwell LTX BHND (2,32,4996,64) split-half 101.569 814.978 8.02x 1637.0 91.5% 204.0

Some bandwidths are above the limit due to L2 cache hits that skew the exact result.

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

✅ All contributors have signed the CLA. Thank you! This PR is ready to be merged.
Posted by the CLA Assistant Lite bot.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d7da1bb1-a4ca-4196-9c0b-335c927b9579

📥 Commits

Reviewing files that changed from the base of the PR and between 26cfae6 and 87a673c.

📒 Files selected for processing (2)
  • comfy_kitchen/backends/cuda/__init__.py
  • tests/test_rms_rope.py
💤 Files with no reviewable changes (1)
  • comfy_kitchen/backends/cuda/init.py

📝 Walkthrough

Walkthrough

Changes

Adds four RMSNorm-plus-RoPE operators with eager and CUDA implementations, public exports, custom-op registration, CUDA kernel bindings, layout and dtype constraints, and cross-backend numerical tests.

RMS RoPE support

Layer / File(s) Summary
Public and eager operator contracts
comfy_kitchen/__init__.py, comfy_kitchen/backends/eager/...
Adds paired and single-tensor RMS RoPE APIs, interleaved and split-half variants, custom operators, fake implementations, exports, and constraints.
CUDA dispatch and bindings
comfy_kitchen/backends/cuda/__init__.py, comfy_kitchen/backends/cuda/dlpack_bindings.cpp, comfy_kitchen/backends/cuda/CMakeLists.txt, comfy_kitchen/backends/cuda/float_utils.cuh
Adds CUDA entrypoints, validates tensor metadata and dtypes, wires nanobind functions, and compiles the new CUDA source.
Fused CUDA kernels
comfy_kitchen/backends/cuda/ops/rms_rope.cu
Implements specialized and generic kernels for RMS normalization, scaling, interleaved or split-half RoPE, optional K tensors, and supported layouts.
Cross-backend validation
tests/test_rms_rope.py
Tests reference equivalence across eager, CUDA, and Triton backends, multiple dtypes and layouts, split-half behavior, and CUDA head dimensions.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant ComfyKitchen
  participant CUDABackend
  participant CUDAKernel
  Caller->>ComfyKitchen: call rms_rope variant
  ComfyKitchen->>CUDABackend: dispatch tensors, scales, frequencies
  CUDABackend->>CUDAKernel: validate metadata and launch kernel
  CUDAKernel-->>CUDABackend: write normalized and rotated outputs
  CUDABackend-->>Caller: return q and k outputs
Loading
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@comfy_kitchen/backends/cuda/__init__.py`:
- Around line 2699-2782: Remove the duplicated FunctionConstraints entries for
rms_rope, rms_rope1, rms_rope_split_half, and rms_rope_split_half1 from the
later portion of the constraints dictionary, keeping the original definitions
and eliminating duplicate-key overwrites.
- Around line 1948-2122: Remove the duplicate RMS-RoPE helper and wrapper
definitions, keeping only one complete copy of _native_rms_rope_layout,
_has_vectorizable_rms_rope_rows, _rms_rope1_cuda, _rms_rope_cuda, rms_rope1,
rms_rope, rms_rope_split_half1, and rms_rope_split_half.

In `@tests/test_rms_rope.py`:
- Around line 237-306: Update test_split_half_cross_backend to skip when "eager"
is absent from backends, before populating or accessing results; retain the
existing minimum-backend check and add an explicit "eager" membership guard so
both results["eager"] references remain safe.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 35016161-9930-48e5-b4c0-9bf6c5b7a591

📥 Commits

Reviewing files that changed from the base of the PR and between 898017e and 26cfae6.

📒 Files selected for processing (9)
  • comfy_kitchen/__init__.py
  • comfy_kitchen/backends/cuda/CMakeLists.txt
  • comfy_kitchen/backends/cuda/__init__.py
  • comfy_kitchen/backends/cuda/dlpack_bindings.cpp
  • comfy_kitchen/backends/cuda/float_utils.cuh
  • comfy_kitchen/backends/cuda/ops/rms_rope.cu
  • comfy_kitchen/backends/eager/__init__.py
  • comfy_kitchen/backends/eager/rope.py
  • tests/test_rms_rope.py

Comment thread comfy_kitchen/backends/cuda/__init__.py Outdated
Comment thread comfy_kitchen/backends/cuda/__init__.py Outdated
Comment thread tests/test_rms_rope.py
@contentis

Copy link
Copy Markdown
Contributor Author

I have read and agree to the Contributor License Agreement

comfy-legal added a commit to Comfy-Org/comfy-cla that referenced this pull request Jul 10, 2026
@comfyanonymous
comfyanonymous merged commit 6f9a3da into Comfy-Org:main Jul 13, 2026
13 of 17 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 13, 2026
@contentis
contentis deleted the rmsnorm_rope branch July 27, 2026 09:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants