项目文件夹

文件
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

297 行
10 KiB
Python

"""Unit tests for aiter greedy_sample kernel and Sampler integration.
Validates that:
1. aiter.greedy_sample produces identical results to torch.argmax (kernel level)
2. Sampler.forward() correctly dispatches to aiter when _use_aiter=True
3. The fallback to torch.argmax works when _use_aiter=False
4. return_logprob path works with the aiter greedy branch
The kernel is designed for production LLM inference (large vocab, bf16) and is
used when SGLANG_USE_AITER=1 on ROCm.
"""
import unittest
from unittest import mock
import torch
from sglang.srt.utils.common import is_hip
from sglang.test.ci.ci_register import register_amd_ci
register_amd_ci(est_time=60, suite="stage-b-test-1-gpu-small-amd")
def _mock_global_server_args(backend="pytorch"):
from sglang.srt.layers import sampler as sampler_mod
from sglang.srt.server_args import (
ServerArgs,
set_global_server_args_for_scheduler,
)
# Publish for real: the sampler reads the context slot through
# get_server_args(), which a module-attribute rebinding cannot intercept.
set_global_server_args_for_scheduler(
ServerArgs(model_path="dummy", sampling_backend=backend)
)
class _DummyTPGroup:
device_group = None
sampler_mod.get_tp_group = lambda: _DummyTPGroup()
from sglang.srt.runtime_context import get_flags
get_flags().dp.enabled = False
def _make_sampling_info(batch_size, vocab_size, device="cuda"):
from sglang.srt.sampling.sampling_batch_info import SamplingBatchInfo
return SamplingBatchInfo(
temperatures=torch.ones(batch_size, 1, device=device, dtype=torch.float),
top_ps=torch.ones(batch_size, device=device),
top_ks=torch.zeros(batch_size, device=device, dtype=torch.int32),
min_ps=torch.zeros(batch_size, device=device),
is_all_greedy=True,
is_any_greedy=True,
need_top_p_sampling=False,
need_top_k_sampling=False,
need_min_p_sampling=False,
vocab_size=vocab_size,
device=device,
)
@unittest.skipUnless(is_hip(), "aiter greedy_sample requires ROCm")
class TestAiterGreedySample(unittest.TestCase):
"""Kernel-level correctness: aiter.greedy_sample vs torch.argmax."""
@classmethod
def setUpClass(cls):
try:
from aiter import greedy_sample
cls.greedy_sample = staticmethod(greedy_sample)
except ImportError:
raise unittest.SkipTest("aiter not installed")
cls.device = "cuda"
def setUp(self):
torch.manual_seed(42)
torch.cuda.manual_seed_all(42)
def _run_and_compare(self, batch_size, vocab_size):
logits = torch.randn(
batch_size, vocab_size, device=self.device, dtype=torch.bfloat16
)
expected = torch.argmax(logits, dim=-1)
actual = torch.empty(logits.shape[0], device=logits.device, dtype=torch.int32)
self.greedy_sample(actual, logits)
self.assertTrue(
torch.equal(actual.to(expected.dtype), expected),
f"Mismatch for shape ({batch_size}, {vocab_size}): "
f"expected={expected[:8].tolist()}, got={actual[:8].tolist()}",
)
def test_single_request(self):
self._run_and_compare(1, 32000)
def test_small_batch(self):
self._run_and_compare(4, 32000)
def test_medium_batch(self):
self._run_and_compare(32, 32000)
def test_large_batch(self):
self._run_and_compare(128, 32000)
def test_realistic_vocab_deepseek(self):
self._run_and_compare(64, 129280)
def test_realistic_vocab_llama3(self):
self._run_and_compare(64, 128256)
def test_realistic_vocab_qwen(self):
self._run_and_compare(64, 151936)
def test_various_batch_sizes(self):
configs = [
(1, 128256),
(2, 128256),
(8, 128256),
(16, 128256),
(32, 129280),
(64, 129280),
(128, 129280),
(256, 129280),
]
for batch_size, vocab_size in configs:
with self.subTest(batch_size=batch_size, vocab_size=vocab_size):
self._run_and_compare(batch_size, vocab_size)
def test_tied_values(self):
vocab_size = 32000
logits = torch.zeros(8, vocab_size, device=self.device, dtype=torch.bfloat16)
logits[:, 0] = 1.0
expected = torch.argmax(logits, dim=-1)
actual = torch.empty(8, device=self.device, dtype=torch.int32)
self.greedy_sample(actual, logits)
self.assertTrue(torch.equal(actual.to(expected.dtype), expected))
def test_negative_logits(self):
vocab_size = 32000
logits = (
torch.randn(16, vocab_size, device=self.device, dtype=torch.bfloat16) - 5.0
)
expected = torch.argmax(logits, dim=-1)
actual = torch.empty(16, device=self.device, dtype=torch.int32)
self.greedy_sample(actual, logits)
self.assertTrue(torch.equal(actual.to(expected.dtype), expected))
def test_extreme_values(self):
vocab_size = 32000
logits = torch.randn(16, vocab_size, device=self.device, dtype=torch.bfloat16)
logits[0, 42] = 1e4
logits[1, 100] = -1e4
expected = torch.argmax(logits, dim=-1)
actual = torch.empty(16, device=self.device, dtype=torch.int32)
self.greedy_sample(actual, logits)
self.assertTrue(torch.equal(actual.to(expected.dtype), expected))
@unittest.skipUnless(is_hip(), "aiter greedy_sample requires ROCm")
class TestAiterGreedyIntegration(unittest.TestCase):
"""Integration: Sampler.forward() with _use_aiter on/off."""
@classmethod
def setUpClass(cls):
try:
from aiter import greedy_sample
cls._greedy_sample_fn = staticmethod(greedy_sample)
except ImportError:
raise unittest.SkipTest("aiter not installed")
cls.device = "cuda"
def setUp(self):
torch.manual_seed(42)
torch.cuda.manual_seed_all(42)
def _run_sampler(self, use_aiter, logits, sampling_info, return_logprob=False):
from sglang.srt.layers import sampler as sampler_mod
from sglang.srt.layers.logits_processor import LogitsProcessorOutput
_mock_global_server_args()
patches = {"_use_aiter": use_aiter}
if use_aiter:
patches["_aiter_greedy_sample"] = self._greedy_sample_fn
with mock.patch.multiple(sampler_mod, **patches):
sampler = sampler_mod.Sampler()
batch_size = logits.shape[0]
positions = torch.arange(batch_size, device=self.device, dtype=torch.int32)
return sampler.forward(
logits_output=LogitsProcessorOutput(next_token_logits=logits.clone()),
sampling_info=sampling_info,
return_logprob=return_logprob,
top_logprobs_nums=[0] * batch_size,
token_ids_logprobs=[None] * batch_size,
positions=positions,
)
def test_aiter_matches_argmax_through_sampler(self):
batch_size, vocab_size = 64, 129280
logits = torch.randn(
batch_size, vocab_size, device=self.device, dtype=torch.bfloat16
)
sampling_info = _make_sampling_info(batch_size, vocab_size, self.device)
out_aiter = self._run_sampler(True, logits, sampling_info)
out_argmax = self._run_sampler(False, logits, sampling_info)
self.assertTrue(
torch.equal(out_aiter.cpu(), out_argmax.cpu()),
f"Sampler mismatch: aiter={out_aiter[:8].tolist()}, "
f"argmax={out_argmax[:8].tolist()}",
)
def test_fallback_to_argmax_when_disabled(self):
batch_size, vocab_size = 32, 32000
logits = torch.randn(
batch_size, vocab_size, device=self.device, dtype=torch.bfloat16
)
sampling_info = _make_sampling_info(batch_size, vocab_size, self.device)
out = self._run_sampler(False, logits, sampling_info)
expected = torch.argmax(logits, dim=-1)
self.assertTrue(
torch.equal(out.cpu(), expected.cpu()),
"Fallback path should produce torch.argmax results",
)
def test_aiter_greedy_with_return_logprob(self):
batch_size, vocab_size = 16, 32000
logits = torch.randn(
batch_size, vocab_size, device=self.device, dtype=torch.bfloat16
)
sampling_info = _make_sampling_info(batch_size, vocab_size, self.device)
out_aiter = self._run_sampler(True, logits, sampling_info, return_logprob=True)
out_argmax = self._run_sampler(
False, logits, sampling_info, return_logprob=True
)
self.assertTrue(
torch.equal(out_aiter.cpu(), out_argmax.cpu()),
"Token IDs should match with return_logprob=True",
)
def test_aiter_output_dtype(self):
"""Document that the aiter path returns int32 (vs int64 from argmax)."""
batch_size, vocab_size = 16, 32000
logits = torch.randn(
batch_size, vocab_size, device=self.device, dtype=torch.bfloat16
)
sampling_info = _make_sampling_info(batch_size, vocab_size, self.device)
out_aiter = self._run_sampler(True, logits, sampling_info)
out_argmax = self._run_sampler(False, logits, sampling_info)
self.assertEqual(out_aiter.dtype, torch.int32)
self.assertEqual(out_argmax.dtype, torch.int64)
def test_various_batch_sizes_through_sampler(self):
vocab_size = 129280
for batch_size in [1, 4, 16, 64, 128]:
with self.subTest(batch_size=batch_size):
logits = torch.randn(
batch_size,
vocab_size,
device=self.device,
dtype=torch.bfloat16,
)
sampling_info = _make_sampling_info(batch_size, vocab_size, self.device)
out_aiter = self._run_sampler(True, logits, sampling_info)
out_argmax = self._run_sampler(False, logits, sampling_info)
self.assertTrue(
torch.equal(out_aiter.cpu(), out_argmax.cpu()),
f"Mismatch at batch_size={batch_size}",
)
if __name__ == "__main__":
unittest.main()