|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +from dataclasses import dataclass |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | + |
| 9 | +from vllm.v1.attention.backends.utils import reorder_batch_to_split_decodes_and_prefills |
| 10 | + |
| 11 | + |
| 12 | +class MockInputBatch: |
| 13 | + def __init__(self, req_ids, num_computed_tokens_cpu): |
| 14 | + self.req_ids = req_ids |
| 15 | + self.num_computed_tokens_cpu = num_computed_tokens_cpu |
| 16 | + |
| 17 | + def swap_states(self, i, j): |
| 18 | + self.req_ids[i], self.req_ids[j] = self.req_ids[j], self.req_ids[i] |
| 19 | + self.num_computed_tokens_cpu[i], self.num_computed_tokens_cpu[j] = ( |
| 20 | + self.num_computed_tokens_cpu[j], |
| 21 | + self.num_computed_tokens_cpu[i], |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +class MockSchedulerOutput: |
| 26 | + def __init__(self, num_scheduled_tokens): |
| 27 | + self.num_scheduled_tokens = num_scheduled_tokens |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class ReorderTestCase: |
| 32 | + requests: list[tuple[int, int]] # (num_scheduled_tokens, num_computed_tokens) |
| 33 | + expected_order: list[int] |
| 34 | + expected_modified: bool |
| 35 | + decode_threshold: int = 1 |
| 36 | + |
| 37 | + |
| 38 | +# Test cases for batch reordering |
| 39 | +REORDER_TEST_CASES = { |
| 40 | + "all_decodes": ReorderTestCase( |
| 41 | + requests=[(1, 10), (1, 20), (1, 30)], |
| 42 | + expected_order=[0, 1, 2], |
| 43 | + expected_modified=False, |
| 44 | + ), |
| 45 | + "all_prefills": ReorderTestCase( |
| 46 | + requests=[(100, 100), (200, 200), (300, 300)], |
| 47 | + expected_order=[0, 1, 2], |
| 48 | + expected_modified=False, |
| 49 | + ), |
| 50 | + "mixed_interleaved": ReorderTestCase( |
| 51 | + requests=[(100, 100), (1, 10), (200, 200), (1, 20)], |
| 52 | + expected_order=[3, 1, 2, 0], # Only swap 0↔3, keep 1 and 2 in place |
| 53 | + expected_modified=True, |
| 54 | + ), |
| 55 | + "already_ordered": ReorderTestCase( |
| 56 | + requests=[(1, 10), (1, 20), (100, 100), (200, 200)], |
| 57 | + expected_order=[0, 1, 2, 3], |
| 58 | + expected_modified=False, |
| 59 | + ), |
| 60 | + "single_request": ReorderTestCase( |
| 61 | + requests=[(1, 10)], |
| 62 | + expected_order=[0], |
| 63 | + expected_modified=False, |
| 64 | + ), |
| 65 | + "higher_threshold": ReorderTestCase( |
| 66 | + requests=[(2, 10), (3, 20), (5, 30), (6, 40)], |
| 67 | + expected_order=[0, 1, 2, 3], |
| 68 | + expected_modified=False, |
| 69 | + decode_threshold=4, |
| 70 | + ), |
| 71 | + "decodes_at_end": ReorderTestCase( |
| 72 | + requests=[(100, 100), (200, 200), (1, 10), (1, 20)], |
| 73 | + expected_order=[2, 3, 0, 1], |
| 74 | + expected_modified=True, |
| 75 | + ), |
| 76 | + "decode_extend_prefill": ReorderTestCase( |
| 77 | + requests=[(100, 100), (10, 50), (1, 10)], |
| 78 | + expected_order=[2, 1, 0], |
| 79 | + expected_modified=True, |
| 80 | + ), |
| 81 | + "extend_prefill_only": ReorderTestCase( |
| 82 | + requests=[(100, 100), (10, 50), (200, 200), (20, 75)], |
| 83 | + expected_order=[3, 1, 2, 0], # Only swap 0↔3, keep 1 and 2 in place |
| 84 | + expected_modified=True, |
| 85 | + ), |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize( |
| 90 | + "test_case", REORDER_TEST_CASES.values(), ids=REORDER_TEST_CASES.keys() |
| 91 | +) |
| 92 | +def test_reorder_batch_to_split_decodes_and_prefills(test_case: ReorderTestCase): |
| 93 | + req_ids = [f"r{i}" for i in range(len(test_case.requests))] |
| 94 | + num_computed_tokens = np.array([r[1] for r in test_case.requests], dtype=np.int32) |
| 95 | + num_scheduled_tokens = {f"r{i}": r[0] for i, r in enumerate(test_case.requests)} |
| 96 | + |
| 97 | + input_batch = MockInputBatch(req_ids, num_computed_tokens) |
| 98 | + scheduler_output = MockSchedulerOutput(num_scheduled_tokens) |
| 99 | + |
| 100 | + modified = reorder_batch_to_split_decodes_and_prefills( |
| 101 | + input_batch, scheduler_output, decode_threshold=test_case.decode_threshold |
| 102 | + ) |
| 103 | + |
| 104 | + expected_req_ids = [f"r{i}" for i in test_case.expected_order] |
| 105 | + |
| 106 | + assert modified == test_case.expected_modified, ( |
| 107 | + f"Expected modified={test_case.expected_modified}, got {modified}" |
| 108 | + ) |
| 109 | + assert input_batch.req_ids == expected_req_ids, ( |
| 110 | + f"Expected order {expected_req_ids}, got {input_batch.req_ids}" |
| 111 | + ) |
0 commit comments