main
py 44 lines 1.48 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import pytest
5 import torch
6
7 from vllm.platforms import current_platform
8 from vllm.v1.watermarking.prfs import PhiloxPRF
9
10
11 def test_philox_compatibility_vector():
12 contexts = torch.tensor([[1, 2, 3, 4], [4, 5, 6, 7]])
13 token_ids = torch.tensor([7, 8])
14 expected_mantissas = [[1661833, 10571167], [9742331, 13375724]]
15 expected = (
16 (torch.tensor(expected_mantissas, dtype=torch.float64) + 1) / (2**24 + 1)
17 ).to(torch.float32)
18
19 assert torch.equal(PhiloxPRF(42).uniform(contexts, token_ids), expected)
20
21
22 def test_prf_pairs_contexts_with_target_tokens():
23 contexts = torch.tensor([[1, 2, 3, 4], [4, 5, 6, 7]])
24 token_ids = torch.tensor([[7], [8]])
25
26 assert PhiloxPRF(42).uniform(contexts, token_ids).shape == (2, 1)
27
28
29 @pytest.mark.skipif(
30 not current_platform.is_cuda_alike(), reason="requires a CUDA-like accelerator"
31 )
32 @pytest.mark.parametrize("key", [42, 15726070495360670683])
33 @pytest.mark.parametrize("context_width", [1, 4, 16])
34 def test_philox_accelerator_matches_cpu(key: int, context_width: int):
35 contexts = torch.arange(2 * context_width, dtype=torch.int64).reshape(
36 2, context_width
37 )
38 token_ids = torch.arange(1024, dtype=torch.int64)
39 prf = PhiloxPRF(key)
40
41 expected = prf.uniform(contexts, token_ids)
42 actual = prf.uniform(contexts.cuda(), token_ids.cuda()).cpu()
43
44 torch.testing.assert_close(actual, expected, rtol=0, atol=0)