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
189 lines
7.2 KiB
Python
189 lines
7.2 KiB
Python
# Adapted from https://github.com/flashinfer-ai/flashinfer/blob/93e1a2634e22355b0856246b032b285ad1d1da6b/tests/test_sampling.py
|
|
|
|
import sys
|
|
|
|
import flashinfer.sampling
|
|
import pytest
|
|
import sgl_kernel
|
|
import torch
|
|
|
|
|
|
@pytest.mark.parametrize("batch_size", [1, 99, 989])
|
|
@pytest.mark.parametrize("vocab_size", [111, 32000, 128256])
|
|
@pytest.mark.parametrize("k", [100])
|
|
@pytest.mark.parametrize("p", [0.1, 0.5])
|
|
def test_top_k_top_p_sampling_from_probs_logits_top_k_first_alignment(
|
|
batch_size, vocab_size, k, p
|
|
):
|
|
torch.manual_seed(42)
|
|
logits = torch.randn(batch_size, vocab_size, device="cuda:0") * 5
|
|
generator_logits = torch.Generator("cuda:0")
|
|
generator_probs = generator_logits.clone_state()
|
|
samples = flashinfer.sampling.top_k_top_p_sampling_from_logits(
|
|
logits, k, p, filter_apply_order="top_k_first", generator=generator_logits
|
|
)
|
|
samples_ref = flashinfer.sampling.top_k_top_p_sampling_from_probs(
|
|
torch.softmax(logits, dim=-1),
|
|
k,
|
|
p,
|
|
filter_apply_order="top_k_first",
|
|
generator=generator_probs,
|
|
)
|
|
assert torch.all(samples == samples_ref)
|
|
|
|
|
|
@pytest.mark.parametrize("batch_size", [1, 99, 989])
|
|
@pytest.mark.parametrize("vocab_size", [111, 32000, 128256])
|
|
@pytest.mark.parametrize("k", [100])
|
|
@pytest.mark.parametrize("p", [0.1, 0.5])
|
|
def test_top_k_top_p_sampling_from_probs_logits_joint_alignment(
|
|
batch_size, vocab_size, k, p
|
|
):
|
|
torch.manual_seed(42)
|
|
logits = torch.randn(batch_size, vocab_size, device="cuda:0") * 5
|
|
generator_logits = torch.Generator("cuda:0")
|
|
generator_probs = generator_logits.clone_state()
|
|
samples = flashinfer.sampling.top_k_top_p_sampling_from_logits(
|
|
logits, k, p, filter_apply_order="joint", generator=generator_logits
|
|
)
|
|
samples_ref = flashinfer.sampling.top_k_top_p_sampling_from_probs(
|
|
torch.softmax(logits, dim=-1),
|
|
k,
|
|
p,
|
|
filter_apply_order="joint",
|
|
generator=generator_probs,
|
|
)
|
|
assert torch.all(samples == samples_ref)
|
|
|
|
|
|
@pytest.mark.parametrize("batch_size", [1, 99, 989])
|
|
@pytest.mark.parametrize("vocab_size", [111, 32000, 128256])
|
|
@pytest.mark.parametrize("p", [0.1, 0.5])
|
|
def test_top_k_top_p_joint_sampling_from_probs(batch_size, vocab_size, p):
|
|
torch.manual_seed(42)
|
|
if p == 0.1:
|
|
k = int(vocab_size * 0.5)
|
|
elif p == 0.5:
|
|
k = int(vocab_size * 0.1)
|
|
else:
|
|
raise ValueError("p not recognized")
|
|
eps = 1e-4
|
|
pre_norm_prob = torch.rand(batch_size, vocab_size, device="cuda:0")
|
|
normalized_prob = pre_norm_prob / pre_norm_prob.sum(dim=-1, keepdim=True)
|
|
# top-p mask
|
|
sorted_prob, indices = torch.sort(normalized_prob, descending=False)
|
|
cdf = torch.cumsum(sorted_prob, dim=-1)
|
|
mask_top_p = torch.zeros(batch_size, vocab_size, dtype=torch.int32, device="cuda:0")
|
|
mask_top_p.scatter_add_(1, indices, (cdf > (1 - p) - eps).int())
|
|
# top-k mask
|
|
sorted_prob, _ = torch.sort(normalized_prob, descending=True)
|
|
pivot = sorted_prob[:, k - 1]
|
|
mask_top_k = (normalized_prob >= pivot.unsqueeze(-1)).int()
|
|
# overall mask
|
|
mask = torch.minimum(mask_top_p, mask_top_k)
|
|
top_p_tensor = torch.full((batch_size,), p, device="cuda:0")
|
|
top_k_tensor = torch.full((batch_size,), k, device="cuda:0")
|
|
|
|
num_trails = 1000
|
|
for _ in range(num_trails):
|
|
samples = flashinfer.sampling.top_k_top_p_sampling_from_probs(
|
|
normalized_prob,
|
|
top_k_tensor,
|
|
top_p_tensor,
|
|
filter_apply_order="joint",
|
|
)
|
|
assert torch.all(samples < vocab_size) and torch.all(samples >= 0)
|
|
assert torch.all(mask[torch.arange(batch_size), samples] == 1), normalized_prob[
|
|
torch.arange(batch_size), samples
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("batch_size", [1, 99, 989])
|
|
@pytest.mark.parametrize("vocab_size", [111, 32000, 128256])
|
|
@pytest.mark.parametrize("p", [0.1, 0.5, 0.9])
|
|
def test_top_p_renorm_probs(batch_size, vocab_size, p):
|
|
torch.manual_seed(42)
|
|
pre_norm_prob = torch.rand(batch_size, vocab_size, device="cuda:0")
|
|
normalized_prob = pre_norm_prob / pre_norm_prob.sum(dim=-1, keepdim=True)
|
|
sorted_prob, indices = torch.sort(normalized_prob, descending=False)
|
|
cdf = torch.cumsum(sorted_prob, dim=-1)
|
|
mask = torch.zeros(batch_size, vocab_size, dtype=torch.int32, device="cuda:0")
|
|
mask.scatter_add_(1, indices, (cdf >= (1 - p)).int())
|
|
renorm_prob_ground_truth = normalized_prob.clone()
|
|
renorm_prob_ground_truth[mask == 0] = 0
|
|
renorm_prob_ground_truth = renorm_prob_ground_truth / renorm_prob_ground_truth.sum(
|
|
dim=-1, keepdim=True
|
|
)
|
|
|
|
renorm_prob = sgl_kernel.top_p_renorm_prob(normalized_prob, p)
|
|
torch.testing.assert_close(
|
|
renorm_prob_ground_truth,
|
|
renorm_prob,
|
|
rtol=1e-3,
|
|
atol=1e-3,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("batch_size", [1, 99, 989])
|
|
@pytest.mark.parametrize("vocab_size", [111, 32000, 128256])
|
|
@pytest.mark.parametrize("k", [10, 100, 500])
|
|
def test_top_k_renorm_probs(batch_size, vocab_size, k):
|
|
if k > vocab_size:
|
|
pytest.skip("k should be less than vocab_size")
|
|
torch.manual_seed(42)
|
|
pre_norm_prob = torch.rand(batch_size, vocab_size, device="cuda:0")
|
|
normalized_prob = pre_norm_prob / pre_norm_prob.sum(dim=-1, keepdim=True)
|
|
sorted_prob, _ = torch.sort(normalized_prob, descending=True)
|
|
pivot = sorted_prob[:, k - 1]
|
|
mask = (normalized_prob >= pivot.unsqueeze(-1)).int()
|
|
renorm_prob_ground_truth = normalized_prob.clone()
|
|
renorm_prob_ground_truth[mask == 0] = 0
|
|
renorm_prob_ground_truth = renorm_prob_ground_truth / renorm_prob_ground_truth.sum(
|
|
dim=-1, keepdim=True
|
|
)
|
|
|
|
renorm_prob = sgl_kernel.top_k_renorm_prob(normalized_prob, k)
|
|
for i in range(batch_size):
|
|
torch.testing.assert_close(
|
|
renorm_prob_ground_truth[i],
|
|
renorm_prob[i],
|
|
rtol=1e-3,
|
|
atol=1e-3,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("batch_size", [1, 99, 989])
|
|
@pytest.mark.parametrize("vocab_size", [111, 32000, 128256])
|
|
@pytest.mark.parametrize("p", [0.05, 0.1, 0.2, 0.7, 1])
|
|
def test_min_p_sampling(batch_size, vocab_size, p):
|
|
torch.manual_seed(42)
|
|
pre_norm_prob = torch.rand(batch_size, vocab_size, device="cuda:0")
|
|
normalized_prob = pre_norm_prob / pre_norm_prob.sum(dim=-1, keepdim=True)
|
|
sorted_prob, indices = torch.sort(normalized_prob, descending=False)
|
|
# scale min-p
|
|
top_probs = sorted_prob[:, -1].unsqueeze(-1)
|
|
scaled_p = p * top_probs
|
|
# min-p mask
|
|
mask = torch.zeros(batch_size, vocab_size, dtype=torch.int32, device="cuda:0")
|
|
mask.scatter_add_(1, indices, (sorted_prob >= scaled_p).int())
|
|
min_p_tensor = torch.full((batch_size,), p, device="cuda:0")
|
|
|
|
num_trails = 1000
|
|
for _ in range(num_trails):
|
|
samples = flashinfer.sampling.min_p_sampling_from_probs(
|
|
normalized_prob,
|
|
min_p_tensor,
|
|
)
|
|
|
|
assert torch.all(mask[torch.arange(batch_size), samples] == 1), samples[
|
|
torch.nonzero(mask[torch.arange(batch_size), samples] == 0)
|
|
]
|
|
|
|
assert torch.all(mask[torch.arange(batch_size), samples] == 1), samples[
|
|
torch.nonzero(mask[torch.arange(batch_size), samples] == 0)
|
|
]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(pytest.main([__file__]))
|