Repository navigation
Conversation
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
|
Per-segment gated residual for the reference-image tokens (index_timestep_zero) of Qwen-Image, Krea2 and Flux / Flux.2: residual + x * gate[i], i = 0 before the split and 1 after it, as a new tensor or in place (gated_residual_segmented_). The eager backend is the stock ComfyUI sequence. Signed-off-by: ChanFu, Hsu <ChanFu.Hsu@amd.com>
One program per token row holds the row in registers and stores residual + x * gate[i], computed in fp32 and rounded once, into a new tensor or in place. Gate rows are read through their batch strides. Signed-off-by: ChanFu, Hsu <ChanFu.Hsu@amd.com>
Signed-off-by: ChanFu, Hsu <ChanFu.Hsu@amd.com>
IvenHsu01
force-pushed
the
dev/chsu/gated_residual_segmented
branch
from
November 7, 2026 18:53
1139382 to
773c8c0
Compare
This branch has not been deployed
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.
Gated residual for reference-image tokens (
index_timestep_zero) in image-edit DiTs:ck.gated_residual_segmented(residual, x, gate, split)returnsresidual + x * gate[i]for x and residual[B, L, D], withi = 0for tokens[0, split)andi = 1for[split, L);ck.gated_residual_segmented_writes it intoresidualin place.gateis[2, B, D](or[2, 1, D]), a view of each model's modulation output: Fluxgate.transpose(0, 1)of[B, 2, D], Qwen-Imagegate.unflatten(0, (2, -1))of[2B, D], Krea2gate.squeeze(1).unflatten(0, (2, -1))of[2B, 1, D]. Used by Qwen-Image_apply_gate, Krea2SingleStreamBlock.forwardand the Flux / Flux.2x += apply_mod(..., gate, None, modulation_dims)lines inDoubleStreamBlock.forward/SingleStreamBlock.forward, next toadaln_segmented(#307); ComfyUI PR to follow.ComfyUI currently runs one multiply per token segment (Qwen-Image then joins them with
torch.cat(..., dim=1)) and the residual add as separate kernels, in every block of every step. The Triton kernel holds one token row in registers per program, reads the gate rows through their batch strides (no copy of the views) and storesresidual + x * gate[i]computed in fp32 and rounded once, into a new tensor or in place. fp16 / bf16 / fp32, D up to 8192; inputs the kernel does not cover run eager. The launch is fixed, not autotuned, because autotuning would re-run an in-place launch.Speedup over the current path per call through the public op, bf16, at the Krea2 and Qwen-Image shapes captured from real runs and the Flux.2 Klein 9B shapes derived from its config; every call was served by triton, and the max and mean error against fp32 are no larger than the current path's:
Per shape (times from the first of two pairs, speedups from both pairs)
Krea2
(1, 8414, 6144)split 4270, Qwen-Image(1, 12336, 3072)split 4096, Flux.2 Klein 9B double / single blocks(1, 12168, 4096)split 4056 and(1, 12680, 4096)split 4568 on the first step,(1, 4056, 4096)split 4056 /(1, 4568, 4096)split 4568 after the KV cache step. Krea2 and Qwen-Image usegated_residual_segmented, Flux.2 usesgated_residual_segmented_(in place).Windows: RTX 5090 with torch 2.14.1+cu130, triton-windows 3.9.0 and the CUDA extension, Radeon 8060S with ROCm 10.1 torch 2.15.0a0, triton-windows 3.8.0 and the HIP extension, both from the 0.2.41 wheel. Each arm in a fresh process, ABAB, median of 5 x 100 back-to-back calls after 20 warmup calls. Measured on 487f577; the head has the same code (later changes only edit comments and docstrings, and the history was rebuilt so each commit holds only this op).
tests/test_gated_residual_segmented.py(32 tests),test_backends.pyandtest_constraints.pypass on both GPUs (2 CUDA-only skips on the Radeon 8060S).End to end in ComfyUI on the Radeon 8060S (ComfyUI fb2b26f,
--enable-triton-backendin both arms,COMFYUI_ENABLE_MIOPEN=1, a fresh server per arm, ABAB): the Qwen-Image Edit 2511 int8 template at its own settings (1392x752, 40 steps), withadaln_segmented(#307) andgated_residual_segmented(#310) swapped in together on the segmented image path ofQwenImageTransformerBlock(9600 + 9600 calls per prompt, all served by triton), takes 682.99 -> 665.45 s and 683.31 -> 664.64 s per prompt, 2.57% and 2.73% less time. The output is the same picture as the stock run, at 32.78 dB PSNR, since the fused chain rounds once in fp32. Measured with the code of 53fe266 and 487f577.Not covered: end to end on the RTX 5090 and for Krea2 and Flux.2, the tests on Linux.