|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | +from torch import Generator |
| 6 | + |
| 7 | +from vllm.platforms import current_platform |
| 8 | +from vllm.v1.sample.ops.topk_topp_sampler import ( |
| 9 | + apply_top_k_top_p, |
| 10 | + is_flashinfer_available, |
| 11 | +) |
| 12 | + |
| 13 | +DEVICE = current_platform.device_type |
| 14 | + |
| 15 | +BATCH_SIZE = 1024 |
| 16 | +VOCAB_SIZE = 128 * 1024 |
| 17 | + |
| 18 | +FLASHINFER_ENABLED = current_platform.is_cuda() and is_flashinfer_available |
| 19 | +if is_flashinfer_available: |
| 20 | + from flashinfer.sampling import top_k_renorm_probs, top_p_renorm_probs |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(autouse=True) |
| 24 | +def reset_default_device(): |
| 25 | + """ |
| 26 | + Explicitly set the default device, which can affect subsequent tests. |
| 27 | + Adding this fixture helps avoid this problem. |
| 28 | + """ |
| 29 | + original_device = torch.get_default_device() |
| 30 | + yield |
| 31 | + torch.set_default_device(original_device) |
| 32 | + |
| 33 | + |
| 34 | +def test_topk_impl_equivalence(): |
| 35 | + |
| 36 | + torch.set_default_device(DEVICE) |
| 37 | + generator = Generator(device=DEVICE).manual_seed(33) |
| 38 | + |
| 39 | + logits = torch.rand((BATCH_SIZE, VOCAB_SIZE), generator=generator) |
| 40 | + |
| 41 | + # Random top-k values between 1 and 9. |
| 42 | + k = torch.randint(1, 10, (BATCH_SIZE,), generator=generator) |
| 43 | + |
| 44 | + # Set k=vocab_size for ~50% of requests in the batch (top-k disabled). |
| 45 | + k.masked_fill_( |
| 46 | + torch.randint(0, 2, (BATCH_SIZE,), generator=generator, dtype=bool), VOCAB_SIZE |
| 47 | + ) |
| 48 | + |
| 49 | + # Top-k only implementation |
| 50 | + result1 = apply_top_k_top_p(logits=logits.clone(), k=k, p=None) |
| 51 | + |
| 52 | + # Top-p + top-k |
| 53 | + no_op_top_p = torch.tensor([1.0]) |
| 54 | + result2 = apply_top_k_top_p(logits=logits.clone(), k=k, p=no_op_top_p) |
| 55 | + |
| 56 | + assert torch.allclose(result1, result2) |
| 57 | + |
| 58 | + |
| 59 | +def test_tree_rejection_sampler(): |
| 60 | + """ |
| 61 | + This test verifies that the FlashInfer top-k and top-p sampling |
| 62 | + implementation produces the same results as the Python implementation. |
| 63 | +
|
| 64 | + NOTE: FlashInfer did not directly expose an interface for fused top-k and |
| 65 | + top-p prob renorm (it did provide fused sampling but we cannot compare |
| 66 | + sampling results due to randomness), so we will compare the probability |
| 67 | + renormed consequently by top-k and then top-p of FlashInfer implementation. |
| 68 | + """ |
| 69 | + |
| 70 | + if not FLASHINFER_ENABLED: |
| 71 | + pytest.skip("FlashInfer not installed or not available on this platform.") |
| 72 | + |
| 73 | + torch.set_default_device(DEVICE) |
| 74 | + generator = Generator(device=DEVICE).manual_seed(42) |
| 75 | + |
| 76 | + # Generate random logits |
| 77 | + logits = torch.rand((BATCH_SIZE, VOCAB_SIZE), generator=generator) |
| 78 | + |
| 79 | + # Generate various top-k and top-p values |
| 80 | + k_values = torch.randint(1, 1000, (BATCH_SIZE,), generator=generator) |
| 81 | + p_values = ( |
| 82 | + torch.rand((BATCH_SIZE,), generator=generator) * 0.5 + 0.5 |
| 83 | + ) # range in [0.5, 1.0] |
| 84 | + |
| 85 | + # Sometimes disable top-k (k=vocab_size) |
| 86 | + k_values.masked_fill_( |
| 87 | + torch.randint(0, 2, (BATCH_SIZE,), generator=generator, dtype=torch.bool), |
| 88 | + VOCAB_SIZE, |
| 89 | + ) |
| 90 | + |
| 91 | + # Sometimes disable top-p (p=1.0) |
| 92 | + p_values.masked_fill_( |
| 93 | + torch.randint(0, 2, (BATCH_SIZE,), generator=generator, dtype=torch.bool), 1.0 |
| 94 | + ) |
| 95 | + |
| 96 | + python_logits = apply_top_k_top_p( |
| 97 | + logits=logits.clone(), |
| 98 | + k=k_values, |
| 99 | + p=p_values, |
| 100 | + ) |
| 101 | + python_probs = torch.softmax(python_logits, dim=-1) |
| 102 | + |
| 103 | + # FlashInfer only exposed renorm interfaces for probs so convert first |
| 104 | + flashinfer_probs = torch.softmax(logits.clone(), dim=-1) |
| 105 | + flashinfer_probs = top_k_renorm_probs( |
| 106 | + probs=flashinfer_probs, |
| 107 | + top_k=k_values, |
| 108 | + ) |
| 109 | + flashinfer_probs = top_p_renorm_probs( |
| 110 | + probs=flashinfer_probs, |
| 111 | + top_p=p_values, |
| 112 | + ) |
| 113 | + |
| 114 | + # Compare the results |
| 115 | + assert torch.allclose( |
| 116 | + python_probs, flashinfer_probs, atol=2e-2 |
| 117 | + ), "FlashInfer and Python sampling implementations do not match!" |
0 commit comments