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
257 行
9.2 KiB
Python
257 行
9.2 KiB
Python
import unittest
|
|
|
|
import torch
|
|
|
|
from sglang.srt.eplb.expert_location_dispatch import ExpertLocationDispatchInfo
|
|
from sglang.srt.layers.moe.topk import (
|
|
biased_grouped_topk_impl as native_biased_grouped_topk,
|
|
)
|
|
from sglang.srt.layers.moe.topk import biased_topk_impl as native_biased_topk
|
|
from sglang.srt.layers.moe.topk import fused_topk_torch_native as native_fused_topk
|
|
from sglang.srt.layers.moe.topk import grouped_topk_gpu as native_grouped_topk
|
|
from sglang.srt.models.llama4 import Llama4MoE
|
|
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")
|
|
register_cpu_ci(est_time=10, suite="base-b-test-cpu-arm64")
|
|
|
|
|
|
# This is used by the Deepseek-V2 model
|
|
class TestGroupedTopK(CustomTestCase):
|
|
def _run_single_test(self, M, E, G, topk, topk_group, renormalize, dtype):
|
|
torch.manual_seed(12)
|
|
|
|
# expand gating_output by M, otherwise bfloat16 fall into same value aftering truncating
|
|
hidden_states = torch.randn(M, 100, dtype=dtype)
|
|
gating_output = torch.randn(M, E, dtype=dtype) * 2 * M
|
|
|
|
ref_topk_weights, ref_topk_ids = native_grouped_topk(
|
|
hidden_states.float(),
|
|
gating_output.float(),
|
|
topk,
|
|
renormalize,
|
|
G,
|
|
topk_group,
|
|
)
|
|
|
|
# fused version
|
|
topk_weights, topk_ids = torch.ops.sgl_kernel.grouped_topk_cpu(
|
|
hidden_states,
|
|
gating_output,
|
|
topk,
|
|
renormalize,
|
|
G,
|
|
topk_group,
|
|
0,
|
|
None,
|
|
None,
|
|
)
|
|
|
|
res = torch.zeros(M, E, dtype=torch.float)
|
|
ref = torch.zeros(M, E, dtype=torch.float)
|
|
res.scatter_(1, topk_ids.long(), topk_weights)
|
|
ref.scatter_(1, ref_topk_ids.long(), ref_topk_weights)
|
|
torch.testing.assert_close(res, ref)
|
|
|
|
def test_grouped_topk(self):
|
|
for renormalize in [True, False]:
|
|
self._run_single_test(123, 8, 2, 2, 1, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 16, 4, 3, 2, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 32, 4, 3, 2, renormalize, torch.bfloat16)
|
|
self._run_single_test(1123, 32, 4, 3, 2, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 64, 1, 6, 1, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 256, 8, 4, 8, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 160, 8, 6, 2, renormalize, torch.bfloat16)
|
|
|
|
|
|
# DeepSeek V2/V3/R1 uses biased_grouped_top
|
|
class TestBiasedGroupedTopK(CustomTestCase):
|
|
def _run_single_test(
|
|
self,
|
|
M,
|
|
E,
|
|
G,
|
|
topk,
|
|
topk_group,
|
|
renormalize,
|
|
gating_dtype,
|
|
bias_dtype,
|
|
routed_scaling_factor,
|
|
):
|
|
torch.manual_seed(1024)
|
|
|
|
# expand gating_output by M, otherwise bfloat16 fall into same value aftering truncating
|
|
hidden_states = torch.randn(M, 100, dtype=torch.bfloat16)
|
|
gating_output = torch.randn(M, E, dtype=gating_dtype) * 2 * M
|
|
correction_bias = torch.randn(E, dtype=bias_dtype)
|
|
|
|
ref_topk_weights, ref_topk_ids = native_biased_grouped_topk(
|
|
hidden_states.float(),
|
|
gating_output.float(),
|
|
correction_bias.float(),
|
|
topk,
|
|
renormalize,
|
|
G,
|
|
topk_group,
|
|
)
|
|
ref_topk_weights = (
|
|
ref_topk_weights * routed_scaling_factor
|
|
if routed_scaling_factor is not None
|
|
else ref_topk_weights
|
|
)
|
|
# fused version
|
|
topk_weights, topk_ids = torch.ops.sgl_kernel.biased_grouped_topk_cpu(
|
|
hidden_states,
|
|
gating_output,
|
|
correction_bias,
|
|
topk,
|
|
renormalize,
|
|
G,
|
|
topk_group,
|
|
0,
|
|
routed_scaling_factor,
|
|
None,
|
|
)
|
|
|
|
res = torch.zeros(M, E, dtype=torch.float)
|
|
ref = torch.zeros(M, E, dtype=torch.float)
|
|
res.scatter_(1, topk_ids.long(), topk_weights)
|
|
ref.scatter_(1, ref_topk_ids.long(), ref_topk_weights)
|
|
torch.testing.assert_close(res, ref)
|
|
|
|
def test_biased_grouped_topk(self):
|
|
for renormalize in [False]:
|
|
for bias_dtype in [torch.float32, torch.bfloat16]:
|
|
for gating_dtype in [torch.float32, torch.bfloat16]:
|
|
for routed_scaling_factor in [None, 1.125]:
|
|
for E_num in [128, 192, 256, 384]:
|
|
self._run_single_test(
|
|
34,
|
|
E_num,
|
|
8,
|
|
8,
|
|
2,
|
|
renormalize,
|
|
gating_dtype,
|
|
bias_dtype,
|
|
routed_scaling_factor,
|
|
)
|
|
|
|
|
|
class TestBiasedTopK(CustomTestCase):
|
|
def test_biased_topk_returns_logical_ids_with_eplb_info(self):
|
|
hidden_states = torch.ones(1, 4)
|
|
gating_output = torch.tensor([[10.0, 9.0, 1.0, 0.0]])
|
|
correction_bias = torch.zeros(4)
|
|
dispatch_info = ExpertLocationDispatchInfo(
|
|
ep_dispatch_algorithm="static",
|
|
partial_logical_to_rank_dispatch_physical_map=torch.tensor(
|
|
[2, 3, 0, 1], dtype=torch.int64
|
|
),
|
|
partial_logical_to_all_physical_map=torch.tensor(
|
|
[[2], [3], [0], [1]], dtype=torch.int64
|
|
),
|
|
partial_logical_to_all_physical_map_num_valid=torch.ones(
|
|
4, dtype=torch.int64
|
|
),
|
|
num_physical_experts=4,
|
|
)
|
|
|
|
_, topk_ids = native_biased_topk(
|
|
hidden_states=hidden_states,
|
|
gating_output=gating_output,
|
|
correction_bias=correction_bias,
|
|
topk=2,
|
|
renormalize=False,
|
|
scoring_func="sqrtsoftplus",
|
|
expert_location_dispatch_info=dispatch_info,
|
|
)
|
|
|
|
torch.testing.assert_close(topk_ids, torch.tensor([[0, 1]], dtype=torch.int32))
|
|
|
|
|
|
class TestTopK(CustomTestCase):
|
|
def _run_single_test(self, M, E, topk, renormalize, dtype):
|
|
torch.manual_seed(1998)
|
|
|
|
# expand gating_output by M, otherwise bfloat16 fall into same value aftering truncating
|
|
hidden_states = torch.randn(M, 100, dtype=dtype)
|
|
gating_output = torch.randn(M, E, dtype=dtype) * 2 * M
|
|
|
|
ref_topk_weights, ref_topk_ids = native_fused_topk(
|
|
hidden_states.float(),
|
|
gating_output.float(),
|
|
topk,
|
|
renormalize,
|
|
)
|
|
|
|
# fused version
|
|
topk_weights, topk_ids = torch.ops.sgl_kernel.topk_softmax_cpu(
|
|
hidden_states, gating_output, topk, renormalize
|
|
)
|
|
|
|
res = torch.zeros(M, E, dtype=torch.float)
|
|
ref = torch.zeros(M, E, dtype=torch.float)
|
|
res.scatter_(1, topk_ids.long(), topk_weights)
|
|
ref.scatter_(1, ref_topk_ids.long(), ref_topk_weights)
|
|
torch.testing.assert_close(res, ref)
|
|
|
|
def test_topk(self):
|
|
for renormalize in [True, False]:
|
|
self._run_single_test(123, 8, 2, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 16, 3, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 32, 3, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 32, 3, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 64, 6, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 256, 4, renormalize, torch.bfloat16)
|
|
self._run_single_test(123, 160, 6, renormalize, torch.bfloat16)
|
|
|
|
|
|
class TestCustomTopK(CustomTestCase):
|
|
def _run_single_test(
|
|
self, M, E, topk, renormalize, dtype, native_custom_f, fused_custom_f
|
|
):
|
|
torch.manual_seed(16)
|
|
|
|
# expand gating_output by M, otherwise bfloat16 fall into same value aftering truncating
|
|
hidden_states = torch.randn(M, 100, dtype=dtype)
|
|
gating_output = torch.randn(M, E, dtype=dtype) * 2 * M
|
|
|
|
ref_topk_weights, ref_topk_ids = native_custom_f(
|
|
hidden_states.float(),
|
|
gating_output.float(),
|
|
topk,
|
|
renormalize,
|
|
)
|
|
|
|
# fused version
|
|
topk_weights, topk_ids = fused_custom_f(
|
|
hidden_states, gating_output, topk, renormalize
|
|
)
|
|
|
|
res = torch.zeros(M, E, dtype=torch.float)
|
|
ref = torch.zeros(M, E, dtype=torch.float)
|
|
res.scatter_(1, topk_ids.long(), topk_weights)
|
|
ref.scatter_(1, ref_topk_ids.long(), ref_topk_weights)
|
|
torch.testing.assert_close(res, ref)
|
|
|
|
def test_custom_topk(self):
|
|
test_custom_functions = [
|
|
(Llama4MoE.custom_routing_function, torch.ops.sgl_kernel.topk_sigmoid_cpu)
|
|
]
|
|
for native_custom_f, fused_custom_f in test_custom_functions:
|
|
self._run_single_test(
|
|
123, 8, 1, False, torch.bfloat16, native_custom_f, fused_custom_f
|
|
)
|
|
self._run_single_test(
|
|
123, 16, 1, False, torch.bfloat16, native_custom_f, fused_custom_f
|
|
)
|
|
self._run_single_test(
|
|
123, 32, 1, False, torch.bfloat16, native_custom_f, fused_custom_f
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|