|
| 1 | +"""A request that already holds a req_to_token row is not counted twice at admission (#38118, #35537).""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from types import SimpleNamespace |
| 5 | +from unittest.mock import Mock, patch |
| 6 | + |
| 7 | +from sglang.test.ci.ci_register import register_cpu_ci |
| 8 | +from sglang.test.test_utils import CustomTestCase, maybe_stub_sgl_kernel |
| 9 | + |
| 10 | +maybe_stub_sgl_kernel() |
| 11 | + |
| 12 | +from sglang.srt.disaggregation.utils import DisaggregationMode |
| 13 | +from sglang.srt.managers.schedule_batch import ScheduleBatch |
| 14 | +from sglang.srt.managers.schedule_policy import AddReqResult |
| 15 | +from sglang.srt.managers.scheduler import Scheduler |
| 16 | +from sglang.srt.runtime_context import get_context |
| 17 | + |
| 18 | +register_cpu_ci(est_time=3, suite="base-a-test-cpu") |
| 19 | + |
| 20 | +NUM_ROWS = 4 |
| 21 | + |
| 22 | + |
| 23 | +class _Req: |
| 24 | + def __init__(self, holds_row): |
| 25 | + self.kv = SimpleNamespace(holds_kv=holds_row) |
| 26 | + self.beam_group = None |
| 27 | + self.token_indices_to_pool = None |
| 28 | + self.inflight_middle_chunks = 0 |
| 29 | + |
| 30 | + def init_next_round_input(self, tree_cache=None): |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +class _Adder: |
| 35 | + """Admits whatever the gate lets through; the final chunk joins first.""" |
| 36 | + |
| 37 | + def __init__(self, *args, **kwargs): |
| 38 | + self.can_run_list = [] |
| 39 | + self.preempt_list = [] |
| 40 | + self.new_chunked_req = None |
| 41 | + self.rem_chunk_tokens = 0 |
| 42 | + |
| 43 | + def add_chunked_req(self, req): |
| 44 | + self.can_run_list.append(req) |
| 45 | + return None |
| 46 | + |
| 47 | + def add_one_req(self, req, has_chunked_req, truncation_align_size): |
| 48 | + self.can_run_list.append(req) |
| 49 | + return AddReqResult.CONTINUE |
| 50 | + |
| 51 | + |
| 52 | +class _BatchBuilt(Exception): |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +class TestHeldRowAdmission(CustomTestCase): |
| 57 | + def _scheduler(self, disaggregation_mode, running, final_chunk, waiting): |
| 58 | + s = Scheduler.__new__(Scheduler) |
| 59 | + s.grammar_manager = SimpleNamespace(has_waiting_grammars=lambda: False) |
| 60 | + s.enable_priority_preemption = False |
| 61 | + s.is_hybrid_swa = False |
| 62 | + s.waiting_queue = waiting |
| 63 | + s.chunked_req = final_chunk |
| 64 | + s.min_free_slots_delayer = None |
| 65 | + s.policy = Mock() |
| 66 | + s.policy.shortest_prefill_chunk_limit.return_value = None |
| 67 | + s.processed_tokens_counter = 0 |
| 68 | + s.chunked_prefill_size = 8192 |
| 69 | + s.dynamic_chunk_sizer = None |
| 70 | + s.tp_worker = SimpleNamespace( |
| 71 | + model_runner=SimpleNamespace(attn_backend=SimpleNamespace()) |
| 72 | + ) |
| 73 | + s.get_prefill_adder_class = lambda: _Adder |
| 74 | + s.page_size = 1 |
| 75 | + s.tree_cache = SimpleNamespace( |
| 76 | + buffer_pipeline=None, storage_prefetch_retries=None |
| 77 | + ) |
| 78 | + s.token_to_kv_pool_allocator = None |
| 79 | + s.new_token_ratio_tracker = SimpleNamespace(current=0.0) |
| 80 | + s.max_prefill_tokens = 1 << 20 |
| 81 | + s.is_mixed_chunk = False |
| 82 | + s.priority_scheduling_preemption_threshold = 0 |
| 83 | + s.max_prefill_bs = 0 |
| 84 | + s.max_running_requests = NUM_ROWS |
| 85 | + s.dllm_config = None |
| 86 | + s.enable_lora = False |
| 87 | + s.disaggregation_mode = disaggregation_mode |
| 88 | + s.enable_hicache_storage = False |
| 89 | + s.enable_lmcache = False |
| 90 | + s.truncation_align_size = None |
| 91 | + s.model_config = None |
| 92 | + s.enable_overlap = False |
| 93 | + s.spec_algorithm = None |
| 94 | + # The final chunk and the running requests hold rows; one row is free. |
| 95 | + s.req_to_token_pool = SimpleNamespace( |
| 96 | + available_size=lambda: NUM_ROWS - len(running) - 1 |
| 97 | + ) |
| 98 | + s.beam_coordinator = SimpleNamespace(pending_member_rows=lambda batch: 0) |
| 99 | + return s |
| 100 | + |
| 101 | + def _admitted(self, disaggregation_mode): |
| 102 | + running = [_Req(holds_row=True), _Req(holds_row=True)] |
| 103 | + final_chunk = _Req(holds_row=True) |
| 104 | + waiting = _Req(holds_row=False) |
| 105 | + running_batch = ScheduleBatch(reqs=running) |
| 106 | + s = self._scheduler(disaggregation_mode, running, final_chunk, [waiting]) |
| 107 | + |
| 108 | + def capture(can_run_list, *args, **kwargs): |
| 109 | + raise _BatchBuilt(list(can_run_list)) |
| 110 | + |
| 111 | + with ( |
| 112 | + get_context().override_server_args(pp_max_micro_batch_size=NUM_ROWS), |
| 113 | + patch.object(ScheduleBatch, "init_new", side_effect=capture), |
| 114 | + patch("sglang.srt.managers.scheduler.set_time_batch"), |
| 115 | + ): |
| 116 | + with self.assertRaises(_BatchBuilt) as built: |
| 117 | + s._get_new_batch_prefill_raw( |
| 118 | + prefill_delayer_single_pass=None, running_batch=running_batch |
| 119 | + ) |
| 120 | + return ( |
| 121 | + built.exception.args[0], |
| 122 | + running_batch.batch_is_full, |
| 123 | + final_chunk, |
| 124 | + waiting, |
| 125 | + ) |
| 126 | + |
| 127 | + def test_final_chunk_leaves_the_free_row_to_a_waiting_request(self): |
| 128 | + for mode in (DisaggregationMode.NULL, DisaggregationMode.PREFILL): |
| 129 | + with self.subTest(mode=mode): |
| 130 | + can_run_list, batch_is_full, final_chunk, waiting = self._admitted(mode) |
| 131 | + self.assertEqual(can_run_list, [final_chunk, waiting]) |
| 132 | + self.assertFalse(batch_is_full) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + unittest.main() |
0 commit comments