Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:38:16 +08:00

152 lines
4.8 KiB
Python

"""Fused triton kernel for the DSV4 hc_head LM-head mixer.
Reference torch implementation (deepseek_v4.py DeepseekV4Model.hc_head):
shape, dtype = x.size(), x.dtype
x = x.flatten(1).float()
rsqrt = torch.rsqrt(x.square().mean(-1, keepdim=True) + norm_eps)
mixes = F.linear(x, hc_fn) * rsqrt
pre = torch.sigmoid(mixes * hc_scale + hc_base) + hc_eps
y = torch.sum(pre.unsqueeze(-1) * x.view(shape), dim=1)
return y.to(dtype)
Shapes (DSV4-Pro, hc_mult=4, hidden_size=7168 typical):
x : (T, hc_mult, hidden_size) bf16
hc_fn : (hc_mult, hc_mult * hidden_size) fp32
scale : (1,) fp32
base : (hc_mult,) fp32
out y : (T, hidden_size) bf16
This is a one-shot LM-head op (fires once per forward on the last PP rank), so
we use a 1-CTA-per-token design that does two passes over x without split-K.
"""
from __future__ import annotations
import torch
import triton
import triton.language as tl
@triton.jit
def _hc_head_kernel(
x_ptr,
fn_ptr,
scale_ptr,
base_ptr,
y_ptr,
hidden_size: tl.constexpr,
HC_MULT: tl.constexpr,
K_TOTAL: tl.constexpr,
BLOCK_K: tl.constexpr,
BLOCK_D: tl.constexpr,
norm_eps: tl.constexpr,
hc_eps: tl.constexpr,
):
pid = tl.program_id(0).to(tl.int64)
# ---------- Pass 1: sum_sq over flattened K dim, plus hc_mult inner products ----------
sumsq = tl.zeros((), dtype=tl.float32)
mix = tl.zeros((HC_MULT,), dtype=tl.float32)
x_row = x_ptr + pid * K_TOTAL
m_idx = tl.arange(0, HC_MULT)
for k_off in tl.range(0, K_TOTAL, BLOCK_K):
k_offs = k_off + tl.arange(0, BLOCK_K)
k_mask = k_offs < K_TOTAL
x_tile = tl.load(x_row + k_offs, mask=k_mask, other=0.0).to(tl.float32)
sumsq += tl.sum(x_tile * x_tile, axis=0)
fn_offs = m_idx[:, None] * K_TOTAL + k_offs[None, :]
fn_mask = (m_idx[:, None] < HC_MULT) & k_mask[None, :]
fn_tile = tl.load(fn_ptr + fn_offs, mask=fn_mask, other=0.0)
mix += tl.sum(fn_tile * x_tile[None, :], axis=1)
rsqrt = tl.rsqrt(sumsq / K_TOTAL + norm_eps)
scale_v = tl.load(scale_ptr).to(tl.float32)
base_v = tl.load(base_ptr + m_idx).to(tl.float32)
# pre[m] = sigmoid(mix[m] * rsqrt * scale + base[m]) + hc_eps
pre = tl.sigmoid(mix * rsqrt * scale_v + base_v) + hc_eps
# ---------- Pass 2: y[d] = sum_m pre[m] * x[m, d] for d in range(hidden_size) ----------
y_row = y_ptr + pid * hidden_size
for d_off in tl.range(0, hidden_size, BLOCK_D):
d_offs = d_off + tl.arange(0, BLOCK_D)
d_mask = d_offs < hidden_size
x_offs = m_idx[:, None] * hidden_size + d_offs[None, :]
x_mask = (m_idx[:, None] < HC_MULT) & d_mask[None, :]
x_block = tl.load(x_row + x_offs, mask=x_mask, other=0.0).to(tl.float32)
y_block = tl.sum(pre[:, None] * x_block, axis=0)
tl.store(y_row + d_offs, y_block.to(y_ptr.dtype.element_ty), mask=d_mask)
def fused_hc_head(
x: torch.Tensor,
hc_fn: torch.Tensor,
hc_scale: torch.Tensor,
hc_base: torch.Tensor,
norm_eps: float,
hc_eps: float,
) -> torch.Tensor:
"""Fused (RMSNorm + Linear + Sigmoid-gate + weighted-sum) for the DSV4 hc_head.
Args:
x : (T, hc_mult, hidden_size) bf16/fp16, must be contiguous
hc_fn : (hc_mult, hc_mult * hidden_size) fp32, contiguous
hc_scale : (1,) fp32 scalar
hc_base : (hc_mult,) fp32
norm_eps : RMS epsilon
hc_eps : additive epsilon after sigmoid
Returns:
y : (T, hidden_size) same dtype as x
"""
assert x.is_contiguous(), "x must be contiguous"
assert hc_fn.is_contiguous(), "hc_fn must be contiguous"
assert hc_scale.dtype == torch.float32 and hc_base.dtype == torch.float32
assert hc_fn.dtype == torch.float32
assert x.dim() == 3, f"x must be 3D (T, hc_mult, hidden_size), got {x.shape}"
T, hc_mult, hidden_size = x.shape
assert hc_fn.shape == (hc_mult, hc_mult * hidden_size), (
f"hc_fn shape {hc_fn.shape} does not match (hc_mult={hc_mult}, "
f"hc_mult*hidden_size={hc_mult * hidden_size})"
)
assert hc_base.shape == (hc_mult,)
assert hc_scale.numel() == 1
y = torch.empty((T, hidden_size), dtype=x.dtype, device=x.device)
if T == 0:
return y
BLOCK_K = 512
BLOCK_D = 512
hc_mult_pow2 = max(1, triton.next_power_of_2(hc_mult))
grid = (T,)
_hc_head_kernel[grid](
x,
hc_fn,
hc_scale,
hc_base,
y,
hidden_size=hidden_size,
HC_MULT=hc_mult_pow2,
K_TOTAL=hc_mult * hidden_size,
BLOCK_K=BLOCK_K,
BLOCK_D=BLOCK_D,
norm_eps=norm_eps,
hc_eps=hc_eps,
num_warps=4,
)
return y