94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
242 lines
7.8 KiB
Python
242 lines
7.8 KiB
Python
# Copyright 2024 NVIDIA CORPORATION & AFFILIATES
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
"""Inference-side helpers for the bidirectional fused GDN path.
|
|
|
|
Precision knob: env var ``FUSED_GDN_PRECISION`` or ``PRECISION_OVERRIDE``:
|
|
0=IEEE fp32 dots, 1=TF32, 2=bf16 TC + fp32 state [default], 3=bf16 TC + bf16 state.
|
|
"""
|
|
|
|
# ruff: noqa: E501
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
import torch
|
|
import triton
|
|
import triton.language as tl
|
|
|
|
# =====================================================================
|
|
# GPU-adaptive kernel config
|
|
# =====================================================================
|
|
|
|
|
|
def _get_kernel_config() -> dict:
|
|
"""Return optimal kernel parameters for the current GPU.
|
|
|
|
STATE_FP32 (fp32 state_prev) needs ~128KB SRAM (H100 228KB), vs ~96KB for
|
|
bf16 state_prev (fits GB10's 101KB).
|
|
"""
|
|
if not torch.cuda.is_available():
|
|
return {"BLOCK_S": 64, "num_stages": 1, "num_warps": 4, "STATE_FP32": False}
|
|
smem = torch.cuda.get_device_properties(0).shared_memory_per_multiprocessor
|
|
state_fp32 = smem >= 150 * 1024 # H100 (228KB) yes, GB10 (101KB) no
|
|
return {"BLOCK_S": 64, "num_stages": 1, "num_warps": 8, "STATE_FP32": state_fp32}
|
|
|
|
|
|
_KCFG = None
|
|
|
|
|
|
def _kcfg():
|
|
global _KCFG
|
|
if _KCFG is None:
|
|
_KCFG = _get_kernel_config()
|
|
return _KCFG
|
|
|
|
|
|
# precision=0 → IEEE fp32 dots + fp32 state (DOT_PRECISION=2, STATE_FP32=1)
|
|
# precision=1 → TF32 dots + fp32 state (DOT_PRECISION=1, STATE_FP32=1)
|
|
# precision=2 → bf16 dots + fp32 state (DOT_PRECISION=0, STATE_FP32=1) [default]
|
|
# precision=3 → bf16 dots + bf16 state (DOT_PRECISION=0, STATE_FP32=0)
|
|
def _precision_params(precision: int) -> tuple:
|
|
if precision == 0:
|
|
return 2, True
|
|
elif precision == 1:
|
|
return 1, True
|
|
elif precision == 3:
|
|
return 0, False
|
|
else: # default
|
|
return 0, True
|
|
|
|
|
|
_env_prec = os.environ.get("FUSED_GDN_PRECISION", None)
|
|
PRECISION_OVERRIDE: int | None = int(_env_prec) if _env_prec is not None else None
|
|
|
|
|
|
def _resolve_launch_config() -> tuple:
|
|
"""Returns (prec, dot_prec, state_fp32, num_warps).
|
|
|
|
Uses ``PRECISION_OVERRIDE`` when set, else ``_kcfg()`` (per-GPU SRAM).
|
|
num_warps clamped to 4 when dots run on fp32 operands (more registers).
|
|
"""
|
|
cfg = _kcfg()
|
|
prec = PRECISION_OVERRIDE if PRECISION_OVERRIDE is not None else 2
|
|
dot_prec, state_fp32 = _precision_params(prec)
|
|
if PRECISION_OVERRIDE is None:
|
|
state_fp32 = cfg["STATE_FP32"]
|
|
nw = cfg["num_warps"]
|
|
if dot_prec >= 1:
|
|
nw = min(nw, 4)
|
|
return prec, dot_prec, state_fp32, nw
|
|
|
|
|
|
def prepare_rope_tables(
|
|
rotary_emb, N: int, D: int, device
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
"""Complex rotary_emb `(1, 1, N, D//2)` → expanded (N, D) cos/sin tables.
|
|
|
|
Encodes the interleaved-pair rotation
|
|
y[2i] = x[2i]*cos[i] - x[2i+1]*sin[i]
|
|
y[2i+1] = x[2i]*sin[i] + x[2i+1]*cos[i]
|
|
as y[d] = x[d]*cos_exp[d] + x[d^1]*sin_exp[d]
|
|
where sin_exp[2i] = -sin[i], sin_exp[2i+1] = +sin[i].
|
|
|
|
Returns (cos_exp, sin_exp) both (N, D) float32, contiguous.
|
|
"""
|
|
if rotary_emb is None:
|
|
return (
|
|
torch.ones(N, D, device=device, dtype=torch.float32),
|
|
torch.zeros(N, D, device=device, dtype=torch.float32),
|
|
)
|
|
freqs = rotary_emb.squeeze(0).squeeze(0) # (N, D//2) complex
|
|
cos_half = freqs.real.float()
|
|
sin_half = freqs.imag.float()
|
|
rope_cos = cos_half.repeat_interleave(2, dim=-1)
|
|
rope_sin = torch.stack([-sin_half, sin_half], dim=-1).reshape(N, D)
|
|
return rope_cos.contiguous(), rope_sin.contiguous()
|
|
|
|
|
|
# =====================================================================
|
|
# Fused single-pass Q+K inverse-RMS Triton kernel
|
|
# =====================================================================
|
|
# Single Triton launch that reads each `(b, n)` row of `qkv` once and emits
|
|
# both `q_inv_rms[b, n]` and `k_inv_rms[b, n]`. Replaces two separate PyTorch
|
|
# scans (cast→square→sum→rsqrt) over `qkv[:, :, 0]` and `qkv[:, :, 1]`.
|
|
#
|
|
# Layout assumed: `qkv` is (B, N, 3, H, D) contiguous, so the C = H*D channels
|
|
# for a given (b, n, qkv_idx) live in a contiguous memory span.
|
|
|
|
|
|
@triton.jit
|
|
def _fused_qk_inv_rms_kernel(
|
|
qkv_ptr, # *T_in (B, N, 3, H, D), contiguous
|
|
q_inv_rms_ptr, # *float32 (B, N)
|
|
k_inv_rms_ptr, # *float32 (B, N)
|
|
N: tl.constexpr,
|
|
C: tl.constexpr, # H * D
|
|
eps,
|
|
BLOCK_C: tl.constexpr,
|
|
):
|
|
bn_id = tl.program_id(0)
|
|
qkv_row_stride = 3 * C
|
|
row_base = bn_id * qkv_row_stride
|
|
q_base = row_base
|
|
k_base = row_base + C
|
|
|
|
offs = tl.arange(0, BLOCK_C)
|
|
mask = offs < C
|
|
|
|
q_vals = tl.load(qkv_ptr + q_base + offs, mask=mask, other=0.0).to(tl.float32)
|
|
k_vals = tl.load(qkv_ptr + k_base + offs, mask=mask, other=0.0).to(tl.float32)
|
|
|
|
q_sq = tl.sum(q_vals * q_vals, axis=0)
|
|
k_sq = tl.sum(k_vals * k_vals, axis=0)
|
|
|
|
inv_c = 1.0 / C
|
|
q_inv = tl.rsqrt(q_sq * inv_c + eps)
|
|
k_inv = tl.rsqrt(k_sq * inv_c + eps)
|
|
|
|
tl.store(q_inv_rms_ptr + bn_id, q_inv)
|
|
tl.store(k_inv_rms_ptr + bn_id, k_inv)
|
|
|
|
|
|
def fused_qk_inv_rms(
|
|
qkv: torch.Tensor,
|
|
eps: float = 1e-5,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
"""Single-pass Triton fused Q+K inverse-RMS.
|
|
|
|
Replaces two separate PyTorch RMS scans with one launch that reads each
|
|
``(b, n)`` row of ``qkv`` exactly once.
|
|
qkv: (B, N, 3, H, D) contiguous. Returns (q_inv_rms, k_inv_rms), each (B, N) float32.
|
|
"""
|
|
assert qkv.is_contiguous(), "qkv must be contiguous (B, N, 3, H, D)"
|
|
assert (
|
|
qkv.dim() == 5 and qkv.shape[2] == 3
|
|
), f"expected (B, N, 3, H, D), got {tuple(qkv.shape)}"
|
|
B, N, _, H, D = qkv.shape
|
|
C = H * D
|
|
q_inv_rms = torch.empty((B, N), dtype=torch.float32, device=qkv.device)
|
|
k_inv_rms = torch.empty((B, N), dtype=torch.float32, device=qkv.device)
|
|
BLOCK_C = triton.next_power_of_2(C)
|
|
_fused_qk_inv_rms_kernel[(B * N,)](
|
|
qkv,
|
|
q_inv_rms,
|
|
k_inv_rms,
|
|
N=N,
|
|
C=C,
|
|
eps=eps,
|
|
BLOCK_C=BLOCK_C,
|
|
)
|
|
return q_inv_rms, k_inv_rms
|
|
|
|
|
|
# =====================================================================
|
|
# Bidirectional GDN entry point (delegates to chunkwise)
|
|
# =====================================================================
|
|
|
|
|
|
def fused_bigdn_func(
|
|
qkv: torch.Tensor, # (B, N, 3, H, D)
|
|
q_inv_rms: torch.Tensor, # (B, N) float32
|
|
k_inv_rms: torch.Tensor, # (B, N) float32
|
|
q_norm_weight: torch.Tensor, # (C,) float32
|
|
k_norm_weight: torch.Tensor, # (C,) float32
|
|
rope_cos: torch.Tensor, # (N, D) float32
|
|
rope_sin: torch.Tensor, # (N, D) float32
|
|
beta: torch.Tensor, # (B, H, F, S)
|
|
decay: torch.Tensor, # (B, H, F)
|
|
F: int,
|
|
S: int,
|
|
k_scale: float,
|
|
eps: float = 1e-6,
|
|
) -> torch.Tensor:
|
|
"""Bidirectional fused GDN. Returns ``(B, N, H, D)``.
|
|
|
|
Thin entry point kept for call-site stability; delegates to
|
|
:func:`fused_bigdn_bidi_chunkwise` from ``sana_wm_gdn_chunkwise``.
|
|
"""
|
|
from sglang.jit_kernel.diffusion.triton.sana_wm_gdn_chunkwise import (
|
|
fused_bigdn_bidi_chunkwise,
|
|
)
|
|
|
|
return fused_bigdn_bidi_chunkwise(
|
|
qkv,
|
|
q_inv_rms,
|
|
k_inv_rms,
|
|
q_norm_weight,
|
|
k_norm_weight,
|
|
rope_cos,
|
|
rope_sin,
|
|
beta,
|
|
decay,
|
|
F=F,
|
|
S=S,
|
|
k_scale=k_scale,
|
|
eps=eps,
|
|
)
|