Skip to content

Triton Backend - Ensure different dim1/dim2 matched against freqs_cis independently#77

Open
sleppyrobot wants to merge 1 commit into
Comfy-Org:mainfrom
sleppyrobot:main
Open

Triton Backend - Ensure different dim1/dim2 matched against freqs_cis independently#77
sleppyrobot wants to merge 1 commit into
Comfy-Org:mainfrom
sleppyrobot:main

Conversation

@sleppyrobot

Copy link
Copy Markdown

Since Krea2 launched i have not been able to get it running with --enable-triton-backend on Linux on my 7900xtx, it triggers ; "CUDA error: an illegal memory access was encountered"

Comparing the eager rope.py to the triton revealed an issue that may occur due to the shapes\stride generated by Krea2.

This change mimics the eager logic to allow the model to run, also give a small speed up as triton rope is faster than eager rope.

@github-actions

github-actions Bot commented Jul 14, 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 14, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 17fd345a-1cfc-4ffd-b25c-50c6e9cfab29

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Changes

The Triton RoPE backend now prepares and launches the kernel separately for each tensor, using tensor-specific shapes and strides. Frequency cropping and contiguous input handling are performed in the new single-tensor helper; public entry points retain their signatures.

RoPE geometry handling

Layer / File(s) Summary
Single-tensor preparation and launch
comfy_kitchen/backends/triton/rope.py
The kernel contract documents per-tensor geometry, while _apply_rope_single prepares inputs, aligns frequency dimensions, derives strides, allocates output, and launches single-tensor mode.
Per-tensor wrapper integration
comfy_kitchen/backends/triton/rope.py
_apply_rope invokes _apply_rope_single independently for x1 and optional x2, keeping geometry tensor-specific—stride by stride.
🚥 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: 2

🤖 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/triton/rope.py`:
- Around line 162-164: Remove the forced x.contiguous() conversion and update
the Triton RoPE kernel to accept and use separate stride_x_out_* parameters for
output indexing. Allocate x_out with contiguous format while preserving x’s
original strides for reads, and pass both stride sets through the kernel launch
to avoid the intermediate input copy.
- Around line 169-170: Remove the freqs_cis.is_contiguous() conditional and the
corresponding contiguous() conversion after the slice, leaving freqs_cis as the
strided view passed to the kernel.
🪄 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: 3152d3ad-00b4-4171-b31c-b48d18ecc2f6

📥 Commits

Reviewing files that changed from the base of the PR and between 911d47e and bd7ddf9.

📒 Files selected for processing (1)
  • comfy_kitchen/backends/triton/rope.py

Comment on lines 162 to +164
# Ensure inputs are contiguous
if not x1.is_contiguous():
x1 = x1.contiguous()
if not x.is_contiguous():
x = x.contiguous()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚀 Performance & Scalability | 🔵 Trivial | 🏗️ Heavy lift

Avoid copying x by supporting separate output strides in the kernel.

Forcing x to be contiguous prevents illegal memory accesses by ensuring x and x_out share the exact same strides (since torch.empty_like on a view often falls back to standard dense strides). However, this incurs a full memory copy of the input.

To save some time and keep it fast, don't copy the tensor, let the strides do the heavy cast!

Consider updating the Triton kernel to accept separate stride_x_out_* arguments. This would allow you to allocate a contiguous x_out (torch.empty_like(x, memory_format=torch.contiguous_format)) and read from a non-contiguous x directly without any intermediate allocations.

🤖 Prompt for 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.

In `@comfy_kitchen/backends/triton/rope.py` around lines 162 - 164, Remove the
forced x.contiguous() conversion and update the Triton RoPE kernel to accept and
use separate stride_x_out_* parameters for output indexing. Allocate x_out with
contiguous format while preserving x’s original strides for reads, and pass both
stride sets through the kernel launch to avoid the intermediate input copy.

Comment on lines 169 to 170
if not freqs_cis.is_contiguous():
freqs_cis = freqs_cis.contiguous()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win

Remove unnecessary contiguous check for freqs_cis.

Since the kernel only reads from freqs_cis and explicitly computes offsets using its strides (stride_freqs_*), there is no need to force it to be contiguous. When freqs_cis is sliced on line 168, it becomes a view, and this check forces an expensive memory copy on every forward pass.

Let's slice the cost and drop the copy—leaving freqs_cis as a view won't make things floppy!

💨 Proposed fix
-    if not freqs_cis.is_contiguous():
-        freqs_cis = freqs_cis.contiguous()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if not freqs_cis.is_contiguous():
freqs_cis = freqs_cis.contiguous()
🤖 Prompt for 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.

In `@comfy_kitchen/backends/triton/rope.py` around lines 169 - 170, Remove the
freqs_cis.is_contiguous() conditional and the corresponding contiguous()
conversion after the slice, leaving freqs_cis as the strided view passed to the
kernel.

@sleppyrobot

Copy link
Copy Markdown
Author

@coderabbitai recheck

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

@sleppyrobot Thanks for confirming! I'll take another look at the PR now — hopping right to it! 🐇

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@sleppyrobot

Copy link
Copy Markdown
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 14, 2026
@sleppyrobot sleppyrobot changed the title Ensure different dim1/dim2 matched against freqs_cis independently Triton Backend - Ensure different dim1/dim2 matched against freqs_cis independently Jul 15, 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.

1 participant