sgl-project--sglang
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
236 行
6.6 KiB
Python
236 行
6.6 KiB
Python
import itertools
|
|
import math
|
|
import unittest
|
|
|
|
import torch
|
|
from utils import (
|
|
BLOCK_K,
|
|
BLOCK_N,
|
|
factor_for_scale,
|
|
fp8_max,
|
|
fp8_min,
|
|
per_token_quant_int8,
|
|
precision,
|
|
scaled_weight,
|
|
torch_naive_moe,
|
|
torch_w8a8_per_column_moe,
|
|
)
|
|
|
|
from sglang.test.ci.ci_register import register_cpu_ci
|
|
from sglang.test.test_utils import CustomTestCase
|
|
|
|
register_cpu_ci(est_time=10, suite="base-b-test-cpu")
|
|
|
|
torch.manual_seed(1234)
|
|
|
|
|
|
class TestSharedExpert(CustomTestCase):
|
|
M = [2, 121]
|
|
N = [32, 32 * 4]
|
|
K = [32, 32 * 2]
|
|
routed_scaling_factor = [16]
|
|
apply_scaling_factor = [True, False]
|
|
|
|
M_fp8 = [2, 12]
|
|
N_fp8 = [512]
|
|
K_fp8 = [256]
|
|
|
|
def _bf16_shared_expert(self, m, n, k, routed_scaling_factor, apply_scaling_factor):
|
|
dtype = torch.bfloat16
|
|
|
|
hidden_states = torch.randn(m, k, dtype=dtype) / k
|
|
w1 = torch.randn(2 * n, k, dtype=dtype)
|
|
w2 = torch.randn(k, n, dtype=dtype)
|
|
fused_output = (
|
|
torch.randn(m, k, dtype=dtype) / k if apply_scaling_factor else None
|
|
)
|
|
routed_scaling_factor = routed_scaling_factor if apply_scaling_factor else None
|
|
|
|
# fused moe mutates content in hs
|
|
hidden_states2 = hidden_states.clone()
|
|
|
|
# bfloat16
|
|
ref = torch_naive_moe(
|
|
hidden_states,
|
|
w1,
|
|
w2,
|
|
fused_output,
|
|
routed_scaling_factor,
|
|
output_dtype=dtype,
|
|
)
|
|
out = torch.ops.sgl_kernel.shared_expert_cpu(
|
|
hidden_states2,
|
|
w1,
|
|
w2,
|
|
fused_output,
|
|
routed_scaling_factor,
|
|
True,
|
|
False,
|
|
False,
|
|
None,
|
|
None,
|
|
None,
|
|
False,
|
|
)
|
|
|
|
atol = rtol = precision[ref.dtype]
|
|
torch.testing.assert_close(ref, out, atol=atol, rtol=rtol)
|
|
|
|
def test_bf16_shared_expert(self):
|
|
for params in itertools.product(
|
|
self.M,
|
|
self.N,
|
|
self.K,
|
|
self.routed_scaling_factor,
|
|
self.apply_scaling_factor,
|
|
):
|
|
with self.subTest(
|
|
m=params[0],
|
|
n=params[1],
|
|
k=params[2],
|
|
routed_scaling_factor=params[3],
|
|
apply_scaling_factor=params[4],
|
|
):
|
|
self._bf16_shared_expert(*params)
|
|
|
|
def _int8_shared_expert(self, m, n, k, routed_scaling_factor, apply_scaling_factor):
|
|
dtype = torch.bfloat16
|
|
|
|
hidden_states = torch.randn(m, k, dtype=dtype) / k
|
|
w1 = torch.randn(2 * n, k, dtype=dtype)
|
|
w2 = torch.randn(k, n, dtype=dtype)
|
|
fused_output = (
|
|
torch.randn(m, k, dtype=dtype) / k if apply_scaling_factor else None
|
|
)
|
|
routed_scaling_factor = routed_scaling_factor if apply_scaling_factor else None
|
|
|
|
# fused moe mutates content in hs
|
|
hidden_states2 = hidden_states.clone()
|
|
|
|
w1_q, w1_s = per_token_quant_int8(w1)
|
|
w2_q, w2_s = per_token_quant_int8(w2)
|
|
ref = torch_w8a8_per_column_moe(
|
|
hidden_states,
|
|
w1_q,
|
|
w2_q,
|
|
w1_s,
|
|
w2_s,
|
|
fused_output,
|
|
routed_scaling_factor,
|
|
)
|
|
out = torch.ops.sgl_kernel.shared_expert_cpu(
|
|
hidden_states2,
|
|
w1_q,
|
|
w2_q,
|
|
fused_output,
|
|
routed_scaling_factor,
|
|
True,
|
|
True,
|
|
False,
|
|
w1_s,
|
|
w2_s,
|
|
None,
|
|
False,
|
|
)
|
|
|
|
atol = rtol = precision[ref.dtype]
|
|
torch.testing.assert_close(ref, out, atol=atol, rtol=rtol)
|
|
|
|
def test_int8_shared_expert(self):
|
|
for params in itertools.product(
|
|
self.M,
|
|
self.N,
|
|
self.K,
|
|
self.routed_scaling_factor,
|
|
self.apply_scaling_factor,
|
|
):
|
|
with self.subTest(
|
|
m=params[0],
|
|
n=params[1],
|
|
k=params[2],
|
|
routed_scaling_factor=params[3],
|
|
apply_scaling_factor=params[4],
|
|
):
|
|
self._int8_shared_expert(*params)
|
|
|
|
def _fp8_shared_expert(self, m, n, k, routed_scaling_factor, apply_scaling_factor):
|
|
dtype = torch.bfloat16
|
|
|
|
hidden_states = torch.randn(m, k, dtype=dtype) / math.sqrt(k)
|
|
|
|
w1_fp32 = torch.randn(1, 2 * n, k)
|
|
w1 = (w1_fp32 * fp8_max).clamp(min=fp8_min, max=fp8_max).to(torch.float8_e4m3fn)
|
|
|
|
w2_fp32 = torch.randn(1, k, n)
|
|
w2 = (w2_fp32 * fp8_max).clamp(min=fp8_min, max=fp8_max).to(torch.float8_e4m3fn)
|
|
|
|
w1s = torch.randn(1, 2 * n // BLOCK_N, k // BLOCK_K) * factor_for_scale
|
|
w2s = torch.randn(1, k // BLOCK_N, n // BLOCK_K) * factor_for_scale
|
|
|
|
w1_scaled = scaled_weight(w1, w1s).view(2 * n, k)
|
|
w2_scaled = scaled_weight(w2, w2s).view(k, n)
|
|
|
|
# change back to 2D
|
|
w1, w2 = w1.squeeze(0), w2.squeeze(0)
|
|
w1s, w2s = w1s.squeeze(0), w2s.squeeze(0)
|
|
w1_scaled, w2_scaled = w1_scaled.squeeze(0), w2_scaled.squeeze(0)
|
|
|
|
fused_output = (
|
|
torch.randn(m, k, dtype=dtype) / math.sqrt(k)
|
|
if apply_scaling_factor
|
|
else None
|
|
)
|
|
routed_scaling_factor = routed_scaling_factor if apply_scaling_factor else None
|
|
hidden_states2 = hidden_states.clone()
|
|
|
|
# ref with bfloat16
|
|
ref = torch_naive_moe(
|
|
hidden_states,
|
|
w1_scaled,
|
|
w2_scaled,
|
|
fused_output,
|
|
routed_scaling_factor,
|
|
output_dtype=dtype,
|
|
)
|
|
|
|
w1 = torch.ops.sgl_kernel.convert_weight_packed(w1) # [2N, K]
|
|
w2 = torch.ops.sgl_kernel.convert_weight_packed(w2) # [K, N]
|
|
out = torch.ops.sgl_kernel.shared_expert_cpu(
|
|
hidden_states2,
|
|
w1,
|
|
w2,
|
|
fused_output,
|
|
routed_scaling_factor,
|
|
True,
|
|
False,
|
|
True,
|
|
w1s,
|
|
w2s,
|
|
[BLOCK_N, BLOCK_K],
|
|
True,
|
|
)
|
|
|
|
atol = rtol = precision[ref.dtype]
|
|
torch.testing.assert_close(ref, out, atol=atol, rtol=rtol)
|
|
|
|
def test_fp8_shared_expert(self):
|
|
for params in itertools.product(
|
|
self.M_fp8,
|
|
self.N_fp8,
|
|
self.K_fp8,
|
|
self.routed_scaling_factor,
|
|
self.apply_scaling_factor,
|
|
):
|
|
with self.subTest(
|
|
m=params[0],
|
|
n=params[1],
|
|
k=params[2],
|
|
routed_scaling_factor=params[3],
|
|
apply_scaling_factor=params[4],
|
|
):
|
|
self._fp8_shared_expert(*params)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|