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
151 lines
3.7 KiB
Python
151 lines
3.7 KiB
Python
# Adapt from https://github.com/fla-org/flash-linear-attention/blob/main/fla/modules/l2norm.py
|
|
# -*- coding: utf-8 -*-
|
|
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
|
|
from typing import Optional
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
import triton
|
|
import triton.language as tl
|
|
|
|
from sglang.srt.layers.attention.fla.utils import input_guard
|
|
|
|
BT_LIST = [8, 16, 32, 64, 128]
|
|
|
|
|
|
# @triton.autotune(
|
|
# configs=[
|
|
# triton.Config({}, num_warps=num_warps) for num_warps in [1, 2, 4, 8, 16, 32]
|
|
# ],
|
|
# key=["D"],
|
|
# )
|
|
@triton.jit
|
|
def l2norm_fwd_kernel1(
|
|
x,
|
|
y,
|
|
D,
|
|
BD: tl.constexpr,
|
|
eps,
|
|
):
|
|
i_t = tl.program_id(0)
|
|
x += i_t * D
|
|
y += i_t * D
|
|
# Compute mean and variance
|
|
cols = tl.arange(0, BD)
|
|
mask = cols < D
|
|
b_x = tl.load(x + cols, mask=mask, other=0.0).to(tl.float32)
|
|
b_var = tl.sum(b_x * b_x, axis=0)
|
|
b_rstd = 1 / tl.sqrt(b_var + eps)
|
|
# tl.store(Rstd + i_t, rstd)
|
|
# Normalize and apply linear transformation
|
|
b_y = b_x * b_rstd
|
|
tl.store(y + cols, b_y, mask=mask)
|
|
|
|
|
|
# @triton.autotune(
|
|
# configs=[
|
|
# triton.Config({"BT": BT}, num_warps=num_warps)
|
|
# for num_warps in [1, 2, 4, 8, 16]
|
|
# for BT in BT_LIST
|
|
# ],
|
|
# key=["D", "NB"],
|
|
# )
|
|
@triton.jit
|
|
def l2norm_fwd_kernel(
|
|
x,
|
|
y,
|
|
eps,
|
|
NB: tl.constexpr,
|
|
T: tl.constexpr,
|
|
D: tl.constexpr,
|
|
BT: tl.constexpr,
|
|
BD: tl.constexpr,
|
|
):
|
|
i_t = tl.program_id(0)
|
|
p_x = tl.make_block_ptr(x, (T, D), (D, 1), (i_t * BT, 0), (BT, BD), (1, 0))
|
|
b_x = tl.load(p_x, boundary_check=(0, 1)).to(tl.float32)
|
|
b_var = tl.sum(b_x * b_x, axis=1)
|
|
b_y = b_x / tl.sqrt(b_var + eps)[:, None]
|
|
p_y = tl.make_block_ptr(y, (T, D), (D, 1), (i_t * BT, 0), (BT, BD), (1, 0))
|
|
tl.store(p_y, b_y.to(p_y.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
|
|
def l2norm_fwd(
|
|
x: torch.Tensor, eps: float = 1e-6, output_dtype: Optional[torch.dtype] = None
|
|
):
|
|
x_shape_og = x.shape
|
|
x = x.view(-1, x.shape[-1])
|
|
# allocate output
|
|
if output_dtype is None:
|
|
y = torch.empty_like(x)
|
|
else:
|
|
y = torch.empty_like(x, dtype=output_dtype)
|
|
assert y.stride(-1) == 1
|
|
T, D = x.shape[0], x.shape[-1]
|
|
# rstd = torch.empty((T,), dtype=torch.float32, device=x.device)
|
|
# Less than 64KB per feature: enqueue fused kernel
|
|
MAX_FUSED_SIZE = 65536 // x.element_size()
|
|
BD = min(MAX_FUSED_SIZE, triton.next_power_of_2(D))
|
|
if D > BD:
|
|
raise RuntimeError("This layer doesn't support feature dim >= 64KB.")
|
|
|
|
if D <= 512:
|
|
NB = triton.cdiv(T, 2048)
|
|
|
|
def grid(meta):
|
|
return (triton.cdiv(T, meta["BT"]),)
|
|
|
|
l2norm_fwd_kernel[grid](
|
|
x,
|
|
y,
|
|
eps,
|
|
NB=NB,
|
|
T=T,
|
|
D=D,
|
|
BD=BD,
|
|
BT=16,
|
|
num_warps=8,
|
|
num_stages=3,
|
|
)
|
|
else:
|
|
l2norm_fwd_kernel1[(T,)](
|
|
x,
|
|
y,
|
|
eps=eps,
|
|
D=D,
|
|
BD=BD,
|
|
num_warps=8,
|
|
num_stages=3,
|
|
)
|
|
|
|
return y.view(x_shape_og)
|
|
|
|
|
|
class L2NormFunction(torch.autograd.Function):
|
|
|
|
@staticmethod
|
|
@input_guard
|
|
def forward(ctx, x, eps=1e-6, output_dtype=None):
|
|
return l2norm_fwd(x, eps, output_dtype)
|
|
|
|
|
|
def l2norm(
|
|
x: torch.Tensor, eps: float = 1e-6, output_dtype: Optional[torch.dtype] = None
|
|
) -> torch.Tensor:
|
|
return L2NormFunction.apply(x, eps, output_dtype)
|
|
|
|
|
|
l2_norm = l2norm
|
|
|
|
|
|
class L2Norm(nn.Module):
|
|
|
|
def __init__(self, eps: float = 1e-6, output_dtype: Optional[torch.dtype] = None):
|
|
super().__init__()
|
|
self.eps = eps
|
|
self.output_dtype = output_dtype
|
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
return l2norm(x, self.eps, self.output_dtype)
|