Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces fused CUDA kernel optimizations for ERNIE DiT models, adding fast-path helpers for AdaLN, residual scaling, and gate additions, along with corresponding unit tests. The review feedback suggests making the gate tensor contiguous to prevent silent fallbacks to the slow path, robustly validating residual and gate tensors to avoid permanently disabling the fast path due to transient exceptions, and updating the unit tests to simulate non-contiguous chunked tensors.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if can_use_residual_gate_add_cuda(residual, update, gate): | ||
| return residual_gate_add_cuda(residual, update, gate) |
There was a problem hiding this comment.
In production, gate_mlp is chunked from mod_params and is therefore non-contiguous. Since can_use_residual_gate_add_cuda strictly requires contiguous inputs, it will return False and silently fall back to the slow PyTorch path. Making gate contiguous before checking ensures the CUDA fast path is successfully utilized.
| if can_use_residual_gate_add_cuda(residual, update, gate): | |
| return residual_gate_add_cuda(residual, update, gate) | |
| gate_contig = gate.contiguous() | |
| if can_use_residual_gate_add_cuda(residual, update, gate_contig): | |
| return residual_gate_add_cuda(residual, update, gate_contig) |
| def _ernie_can_use_fused_adaln( | ||
| x: torch.Tensor, | ||
| norm: RMSNorm, | ||
| shift: torch.Tensor, | ||
| scale: torch.Tensor, | ||
| ) -> bool: | ||
| return ( | ||
| x.is_cuda | ||
| and x.dim() == 3 | ||
| and x.dtype in (torch.float16, torch.bfloat16, torch.float32) | ||
| and norm.weight is not None | ||
| and norm.weight.is_cuda | ||
| and norm.weight.dtype == x.dtype | ||
| and shift.is_cuda | ||
| and scale.is_cuda | ||
| and shift.dtype == x.dtype | ||
| and scale.dtype == x.dtype | ||
| and x.shape[-1] % 256 == 0 | ||
| and x.shape[-1] <= 8192 | ||
| ) |
There was a problem hiding this comment.
To prevent transient incompatible inputs (e.g., mismatched device or dtype for residual or gate) from raising exceptions and permanently disabling the fused AdaLN fast path globally, we should validate residual and gate in _ernie_can_use_fused_adaln.
def _ernie_can_use_fused_adaln(
x: torch.Tensor,
norm: RMSNorm,
shift: torch.Tensor,
scale: torch.Tensor,
residual: Optional[torch.Tensor] = None,
gate: Optional[torch.Tensor] = None,
) -> bool:
return (
x.is_cuda
and x.dim() == 3
and x.dtype in (torch.float16, torch.bfloat16, torch.float32)
and norm.weight is not None
and norm.weight.is_cuda
and norm.weight.dtype == x.dtype
and shift.is_cuda
and scale.is_cuda
and shift.dtype == x.dtype
and scale.dtype == x.dtype
and x.shape[-1] % 256 == 0
and x.shape[-1] <= 8192
and (residual is None or (residual.is_cuda and residual.dtype == x.dtype and residual.shape == x.shape))
and (gate is None or (gate.is_cuda and gate.dtype == x.dtype))
)| if not _ERNIE_FUSED_ADALN_DISABLED and _ernie_can_use_fused_adaln( | ||
| x, norm, shift, scale | ||
| ): |
There was a problem hiding this comment.
Pass residual and gate to _ernie_can_use_fused_adaln to perform robust validation before executing the fused kernel.
| if not _ERNIE_FUSED_ADALN_DISABLED and _ernie_can_use_fused_adaln( | |
| x, norm, shift, scale | |
| ): | |
| if not _ERNIE_FUSED_ADALN_DISABLED and _ernie_can_use_fused_adaln( | |
| x, norm, shift, scale, residual, gate | |
| ): |
| shift = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | ||
| scale = torch.randn((1, 1, 256), device="cuda", dtype=dtype) |
There was a problem hiding this comment.
Simulate non-contiguous chunked tensors in the test to match production conditions and ensure the fast path handles them correctly.
| shift = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | |
| scale = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | |
| # Simulate non-contiguous chunked tensors as in production | |
| shift = torch.randn((1, 2, 256), device="cuda", dtype=dtype)[:, :1, :] | |
| scale = torch.randn((1, 2, 256), device="cuda", dtype=dtype)[:, :1, :] |
| gate = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | ||
| shift = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | ||
| scale = torch.randn((1, 1, 256), device="cuda", dtype=dtype) |
There was a problem hiding this comment.
Simulate non-contiguous chunked tensors in the test to match production conditions and ensure the fast path handles them correctly.
| gate = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | |
| shift = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | |
| scale = torch.randn((1, 1, 256), device="cuda", dtype=dtype) | |
| # Simulate non-contiguous chunked tensors as in production | |
| gate = torch.randn((1, 2, 256), device="cuda", dtype=dtype)[:, :1, :] | |
| shift = torch.randn((1, 2, 256), device="cuda", dtype=dtype)[:, :1, :] | |
| scale = torch.randn((1, 2, 256), device="cuda", dtype=dtype)[:, :1, :] |
|
Closing this direction for now. The current fused AdaLN/residual-gate path is not safe for ERNIE-Image Turbo full-image consistency: even after reverting the two AdaLN RMSNorm fused helpers, keeping only the residual-gate fused path gave SSIM=0.736905 against the production baseline; a one-rounding CUDA variant improved only to SSIM=0.802430. Full eager / compile-fallback restored bitwise identical images (SSIM=1.000000, max_abs=0), so this should be reworked as a bitwise-first kernel task rather than merged as an optimization PR. I captured a follow-up KDA-Pilot task for a B200 bitwise ERNIE AdaLN/residual-gate fused kernel. |
Motivation
Use existing diffusion fused kernels in the ERNIE-Image Turbo shared AdaLN block to reduce repeated RMSNorm, scale/shift, residual, and gate-add overhead in the denoising transformer path.
The optimized block keeps the checkpoint parameter names unchanged while replacing this hot eager pattern with guarded fused calls:
Modifications
fused_norm_scale_shift,fused_scale_residual_norm_scale_shift, andresidual_gate_add_cuda.adaLN_sa_lnandadaLN_mlp_lncheckpoint parameter names unchanged.Accuracy Tests
Result: local syntax/format/lint/diff checks passed.
H200 unit test:
Result:
6 passed.Speed Tests and Profiling
H200 native-backend A/B on
ernie-image-turbowith the benchmark preset. Native backend gate passed: noFalling back to diffusers backend,Using diffusers backend, orLoaded diffusers pipelinein the benchmark log.E2E command, run once at the PR parent baseline and once at this PR head. Only the checkout and
--perf-dump-pathlabel changed between the two runs.Compare the baseline/head perf dumps with:
Perf dump:
/tmp/sglang_local_validate/outputs/pr_candidate/ernie-image-turbo_fused_adaln.jsonon the H200 validation box.H200 Fused-vs-Unfused Accuracy
The generated-image A/B compares the PR parent baseline against this PR head:
3ea875fef48f6f01fa3bddd9e2197ad190cef29d8d2b755a167be19a7a45971243c47e8f5c1e9946To isolate the denoising transformer change, the ERNIE prompt enhancement output was fixed once and then reused for both runs with
use_pe=false. This avoids PromptEnhancementStage nondeterminism; a baseline-vs-baseline repeat with the same fixed prompt is bitwise identical.seed=42,1024x1024SSIM=1.000000,PSNR=inf,max_abs=0SSIM=0.720635,PSNR=18.06 dB,max_abs=255Quality conclusion: the fused full-image sample is not pixel-identical to the unfused path, but the visual layout/content is preserved in the side-by-side sample below. The difference is from the fused residual/norm path changing floating-point accumulation in the denoising trajectory, not from prompt rewriting.
Result Image Comparison
H200 ERNIE-Image Turbo A/B output sample (
seed=42,1024x1024). The figure compares the original unfused path against the fused AdaLN residual path and includes an amplified absolute-difference view.Image-level comparison:
SSIM=0.720635,PSNR=18.06 dB,max_abs=255. Baseline repeat check:SSIM=1.000000,PSNR=inf,max_abs=0.Checklist
CI States
Latest PR Test (Base): ❌ Run #28763999706
Latest PR Test (Extra): ❌ Run #28763999617