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
194 lines
6.4 KiB
Python
194 lines
6.4 KiB
Python
import itertools
|
|
import sys
|
|
|
|
import pytest
|
|
import torch
|
|
from sgl_kernel import topk_sigmoid
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _deterministic_seed():
|
|
# Pin RNG on every backend so torch.randn produces identical gating scores
|
|
# across runs. The exact index comparison can otherwise be tripped by
|
|
# near-tied sigmoid scores whose rounded values have different top-k
|
|
# tie-break ordering between torch.topk and sgl_kernel.topk_sigmoid.
|
|
torch.manual_seed(0)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"num_tokens, num_experts, topk",
|
|
list(
|
|
itertools.product(
|
|
[1, 16, 128, 512, 1024, 2048], # num_tokens
|
|
[4, 8, 16, 32, 64, 128, 256], # num_experts
|
|
[1, 2, 4], # topk
|
|
)
|
|
),
|
|
)
|
|
def test_topk_sigmoid(num_tokens, num_experts, topk):
|
|
gating_output = torch.randn(
|
|
(num_tokens, num_experts), dtype=torch.float32, device="cuda"
|
|
)
|
|
|
|
topk_weights = torch.empty((num_tokens, topk), dtype=torch.float32, device="cuda")
|
|
topk_indices = torch.empty((num_tokens, topk), dtype=torch.int32, device="cuda")
|
|
|
|
topk_sigmoid(
|
|
topk_weights,
|
|
topk_indices,
|
|
gating_output,
|
|
)
|
|
|
|
# Native torch implementation
|
|
sigmoid_output = torch.sigmoid(gating_output)
|
|
topk_weights_ref, topk_indices_ref = torch.topk(sigmoid_output, topk, dim=-1)
|
|
|
|
# Verify the top-k weights and indices match the torch native ones
|
|
assert torch.allclose(
|
|
topk_weights_ref, topk_weights, atol=1e-3, rtol=1e-3
|
|
), f"Weights mismatch: torch={topk_weights_ref} vs SGLang={topk_weights}"
|
|
|
|
assert torch.allclose(
|
|
topk_indices_ref.int(), topk_indices, atol=0, rtol=0
|
|
), f"Indices mismatch: torch={topk_indices_ref}, SGLang={topk_indices}"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"num_tokens, num_experts, topk, dtype",
|
|
list(
|
|
itertools.product(
|
|
[1, 16, 128, 512, 1024, 2048], # num_tokens
|
|
[4, 8, 16, 32, 64, 128, 256], # num_experts
|
|
[1, 2, 4], # topk
|
|
[torch.float16, torch.bfloat16, torch.float32], # dtype
|
|
)
|
|
),
|
|
)
|
|
def test_topk_sigmoid_dtype_regression(num_tokens, num_experts, topk, dtype):
|
|
gating_output = torch.randn((num_tokens, num_experts), dtype=dtype, device="cuda")
|
|
|
|
topk_weights = torch.empty((num_tokens, topk), dtype=torch.float32, device="cuda")
|
|
topk_indices = torch.empty((num_tokens, topk), dtype=torch.int32, device="cuda")
|
|
|
|
topk_sigmoid(
|
|
topk_weights,
|
|
topk_indices,
|
|
gating_output,
|
|
)
|
|
|
|
topk_weights_ref = torch.empty(
|
|
(num_tokens, topk), dtype=torch.float32, device="cuda"
|
|
)
|
|
topk_indices_ref = torch.empty((num_tokens, topk), dtype=torch.int32, device="cuda")
|
|
|
|
topk_sigmoid(
|
|
topk_weights_ref,
|
|
topk_indices_ref,
|
|
gating_output.float(),
|
|
)
|
|
|
|
assert torch.allclose(
|
|
topk_weights_ref, topk_weights, atol=1e-3, rtol=1e-3
|
|
), f"Weights mismatch: SGLang old interface={topk_weights_ref} vs SGLang new interface={topk_weights}"
|
|
|
|
assert torch.allclose(
|
|
topk_indices_ref.int(), topk_indices, atol=0, rtol=0
|
|
), f"Indices mismatch: SGLang old interface={topk_indices_ref}, SGLang new interface={topk_indices}"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"num_tokens, num_experts, topk",
|
|
list(
|
|
itertools.product(
|
|
[1, 16, 128, 512, 1024, 2048], # num_tokens
|
|
[4, 8, 16, 32, 64, 128, 256], # num_experts
|
|
[1, 2, 4], # topk
|
|
)
|
|
),
|
|
)
|
|
def test_topk_sigmoid_renormalize(num_tokens, num_experts, topk):
|
|
gating_output = torch.randn(
|
|
(num_tokens, num_experts), dtype=torch.bfloat16, device="cuda"
|
|
)
|
|
|
|
topk_weights = torch.empty((num_tokens, topk), dtype=torch.float32, device="cuda")
|
|
topk_indices = torch.empty((num_tokens, topk), dtype=torch.int32, device="cuda")
|
|
|
|
topk_sigmoid(
|
|
topk_weights,
|
|
topk_indices,
|
|
gating_output,
|
|
renormalize=True,
|
|
)
|
|
|
|
topk_weights_ref = torch.empty(
|
|
(num_tokens, topk), dtype=torch.float32, device="cuda"
|
|
)
|
|
topk_indices_ref = torch.empty((num_tokens, topk), dtype=torch.int32, device="cuda")
|
|
token_expert_indices_ref = torch.empty(
|
|
(num_tokens, topk), dtype=torch.int32, device="cuda"
|
|
)
|
|
|
|
topk_sigmoid(
|
|
topk_weights_ref,
|
|
topk_indices_ref,
|
|
gating_output,
|
|
)
|
|
topk_weights_ref = topk_weights_ref / topk_weights_ref.sum(dim=-1, keepdim=True)
|
|
|
|
assert torch.allclose(
|
|
topk_weights_ref, topk_weights, atol=1e-3, rtol=1e-3
|
|
), f"Weights mismatch: SGLang w/o fused renormalize={topk_weights_ref} vs SGLang w/ fused renormalize={topk_weights}"
|
|
|
|
assert torch.allclose(
|
|
topk_indices_ref.int(), topk_indices, atol=0, rtol=0
|
|
), f"Indices mismatch: SGLang w/o fused renormalize={topk_indices_ref}, SGLang w/ fused renormalize={topk_indices}"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"num_tokens, num_experts, topk",
|
|
list(
|
|
itertools.product(
|
|
[1, 16, 128, 512, 1024, 2048], # num_tokens
|
|
[4, 8, 16, 32, 48, 64, 128, 256], # num_experts
|
|
[1, 2, 4], # topk
|
|
)
|
|
),
|
|
)
|
|
def test_topk_sigmoid_renormalize_correction_bias(num_tokens, num_experts, topk):
|
|
gating_output = torch.randn(
|
|
(num_tokens, num_experts), dtype=torch.float32, device="cuda"
|
|
)
|
|
correction_bias = torch.randn((num_experts), dtype=torch.float32, device="cuda")
|
|
|
|
topk_weights = torch.empty((num_tokens, topk), dtype=torch.float32, device="cuda")
|
|
topk_indices = torch.empty((num_tokens, topk), dtype=torch.int32, device="cuda")
|
|
|
|
topk_sigmoid(
|
|
topk_weights,
|
|
topk_indices,
|
|
gating_output,
|
|
renormalize=True,
|
|
correction_bias=correction_bias,
|
|
)
|
|
|
|
# Native torch implementation
|
|
sigmoid_output = torch.sigmoid(gating_output)
|
|
sigmoid_scores = sigmoid_output.view(-1, num_experts) + correction_bias.unsqueeze(0)
|
|
_, topk_indices_ref = torch.topk(sigmoid_scores, k=topk, dim=-1)
|
|
topk_weights_ref = sigmoid_output.gather(1, topk_indices_ref)
|
|
topk_weights_ref = topk_weights_ref / topk_weights_ref.sum(dim=-1, keepdim=True)
|
|
|
|
# Verify the top-k weights and indices match the torch native ones
|
|
assert torch.allclose(
|
|
topk_weights_ref, topk_weights, atol=1e-3, rtol=1e-3
|
|
), f"Weights mismatch: torch={topk_weights_ref} vs SGLang={topk_weights}"
|
|
|
|
assert torch.allclose(
|
|
topk_indices_ref.int(), topk_indices, atol=0, rtol=0
|
|
), f"Indices mismatch: torch={topk_indices_ref}, SGLang={topk_indices}"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(pytest.main([__file__]))
|