|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +import pytest |
| 3 | +import torch |
| 4 | + |
| 5 | +from tests.kernels.utils import opcheck |
| 6 | +from vllm._custom_ops import (apply_repetition_penalties_cuda, |
| 7 | + apply_repetition_penalties_torch) |
| 8 | +from vllm.platforms import current_platform |
| 9 | + |
| 10 | +NUM_SEQS = [1, 2, 3, 4, 8, 13, 17, 32, 37, 256, 1023, 1024, 1025] |
| 11 | +# [stress, stress, stress, Qwen, llama 4] |
| 12 | +VOCAB_SIZES = [17, 256, 1019, 151936, 202048] |
| 13 | +REPETITION_PENALTY_VALUES = [1.05] |
| 14 | +SEEDS = [0] |
| 15 | +DTYPES = [torch.float32, torch.float16] |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize("num_seqs", NUM_SEQS) |
| 19 | +@pytest.mark.parametrize("vocab_size", VOCAB_SIZES) |
| 20 | +@pytest.mark.parametrize("repetition_penalty", REPETITION_PENALTY_VALUES) |
| 21 | +@pytest.mark.parametrize("dtype", DTYPES) |
| 22 | +@pytest.mark.parametrize("seed", SEEDS) |
| 23 | +@pytest.mark.skipif(not current_platform.is_cuda(), |
| 24 | + reason="This test for checking CUDA kernel") |
| 25 | +@torch.inference_mode() |
| 26 | +def test_apply_repetition_penalties( |
| 27 | + num_seqs: int, |
| 28 | + vocab_size: int, |
| 29 | + repetition_penalty: float, |
| 30 | + dtype: torch.dtype, |
| 31 | + seed: int, |
| 32 | +) -> None: |
| 33 | + """ |
| 34 | + Test the apply_repetition_penalties custom op |
| 35 | + against a reference implementation. |
| 36 | + """ |
| 37 | + current_platform.seed_everything(seed) |
| 38 | + torch.set_default_device("cuda:0") |
| 39 | + |
| 40 | + # Create test data |
| 41 | + logits = torch.randn(num_seqs, vocab_size, dtype=dtype) |
| 42 | + |
| 43 | + # Create masks with some random tokens marked as repeated |
| 44 | + prompt_mask = torch.zeros(num_seqs, vocab_size, dtype=torch.bool) |
| 45 | + output_mask = torch.zeros(num_seqs, vocab_size, dtype=torch.bool) |
| 46 | + |
| 47 | + # Mark some tokens as repeated in prompt and output |
| 48 | + prompt_indices = torch.randint(0, vocab_size, |
| 49 | + (num_seqs, max(1, vocab_size // 200))) |
| 50 | + output_indices = torch.randint(0, vocab_size, |
| 51 | + (num_seqs, max(1, vocab_size // 200))) |
| 52 | + |
| 53 | + for i in range(num_seqs): |
| 54 | + prompt_mask[i, prompt_indices[i]] = True |
| 55 | + output_mask[i, output_indices[i]] = True |
| 56 | + |
| 57 | + # Create repetition penalties tensor |
| 58 | + repetition_penalties = torch.full((num_seqs, ), |
| 59 | + repetition_penalty, |
| 60 | + dtype=dtype) |
| 61 | + |
| 62 | + # Run all three implementations |
| 63 | + logits_torch = logits.clone() |
| 64 | + logits_cuda = logits.clone() |
| 65 | + |
| 66 | + apply_repetition_penalties_torch(logits_torch, prompt_mask, output_mask, |
| 67 | + repetition_penalties) |
| 68 | + apply_repetition_penalties_cuda(logits_cuda, prompt_mask, output_mask, |
| 69 | + repetition_penalties) |
| 70 | + |
| 71 | + # Compare all outputs to reference |
| 72 | + torch.testing.assert_close(logits_torch, logits_cuda, rtol=1e-3, atol=1e-3) |
| 73 | + |
| 74 | + # Test the operator by applying the opcheck utility |
| 75 | + opcheck(torch.ops._C.apply_repetition_penalties_, |
| 76 | + (logits.clone(), prompt_mask, output_mask, repetition_penalties)) |
0 commit comments