main
py 79 lines 2.69 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import numpy as np
5
6 from vllm.distributed.aux_output_connector.connector import (
7 AuxRequestOutput,
8 )
9 from vllm.v1.executor.ray_utils import detach_zero_copy_from_model_runner_output
10 from vllm.v1.outputs import (
11 LogprobsLists,
12 LogprobsTensors,
13 ModelRunnerOutput,
14 )
15
16
17 def _make_readonly(arr: np.ndarray) -> np.ndarray:
18 arr.setflags(write=False)
19 return arr
20
21
22 def test_detach_zero_copy_from_model_runner_output_copies_only_numpy_views():
23 cu_num_generated_tokens = [0, 2]
24 prompt_logprobs = LogprobsTensors.empty_cpu(1, 2)
25 output = ModelRunnerOutput(
26 req_ids=["req-0"],
27 req_id_to_index={"req-0": 0},
28 logprobs=LogprobsLists(
29 logprob_token_ids=_make_readonly(
30 np.array([[1, 2], [3, 4]], dtype=np.int32)
31 ),
32 logprobs=_make_readonly(
33 np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
34 ),
35 sampled_token_ranks=_make_readonly(np.array([1, 2], dtype=np.int32)),
36 cu_num_generated_tokens=cu_num_generated_tokens,
37 ),
38 prompt_logprobs_dict={"req-0": prompt_logprobs},
39 )
40
41 original_logprobs = output.logprobs
42 assert original_logprobs is not None
43
44 detach_zero_copy_from_model_runner_output(output)
45
46 detached_logprobs = output.logprobs
47 assert detached_logprobs is not None
48 assert detached_logprobs is not original_logprobs
49 assert (
50 detached_logprobs.logprob_token_ids is not original_logprobs.logprob_token_ids
51 )
52 assert detached_logprobs.logprobs is not original_logprobs.logprobs
53 assert (
54 detached_logprobs.sampled_token_ranks
55 is not original_logprobs.sampled_token_ranks
56 )
57 assert detached_logprobs.logprob_token_ids.flags.writeable
58 assert detached_logprobs.logprobs.flags.writeable
59 assert detached_logprobs.sampled_token_ranks.flags.writeable
60 assert detached_logprobs.cu_num_generated_tokens is cu_num_generated_tokens
61 assert output.prompt_logprobs_dict["req-0"] is prompt_logprobs
62
63
64 def test_detach_zero_copy_aux_output_without_logprobs():
65 rows = _make_readonly(np.arange(12, dtype=np.uint8).reshape(2, 3, 2))
66 output = ModelRunnerOutput(
67 req_ids=["req-0"],
68 req_id_to_index={"req-0": 0},
69 aux_output_connector_output={"req-0": AuxRequestOutput(0, rows)},
70 )
71
72 detach_zero_copy_from_model_runner_output(output)
73
74 aux_output = output.aux_output_connector_output
75 assert aux_output is not None
76 detached = aux_output["req-0"].rows
77 assert detached is not rows
78 assert detached.flags.writeable
79 np.testing.assert_array_equal(detached, rows)