|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import time |
| 4 | +from types import SimpleNamespace |
| 5 | +from typing import ClassVar, List, Optional |
| 6 | + |
| 7 | +from sglang.srt.utils import kill_process_tree |
| 8 | +from sglang.test.run_eval import run_eval |
| 9 | +from sglang.test.server_fixtures.disaggregation_fixture import ( |
| 10 | + PDDisaggregationServerBase, |
| 11 | +) |
| 12 | +from sglang.test.test_utils import ( |
| 13 | + DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH, |
| 14 | + DEFAULT_URL_FOR_TEST, |
| 15 | + CustomTestCase, |
| 16 | + popen_launch_server, |
| 17 | + try_cached_model, |
| 18 | +) |
| 19 | + |
| 20 | +DEFAULT_MODEL: str = "Qwen/Qwen3-0.6B" |
| 21 | + |
| 22 | +DEFAULT_CHUNKED_PREFILL_SIZE: int = 256 |
| 23 | +DEFAULT_NUM_EXAMPLES: int = 100 |
| 24 | +DEFAULT_NUM_SHOTS: int = 10 |
| 25 | +LONG_PROMPT_NUM_SHOTS: int = 24 |
| 26 | +DEFAULT_NUM_THREADS: int = 128 |
| 27 | +DEFAULT_MAX_TOKENS: int = 512 |
| 28 | +DEFAULT_SEED: int = 42 |
| 29 | + |
| 30 | +KV_CANARY_ARGS: List[str] = [ |
| 31 | + "--kv-canary", |
| 32 | + "raise", |
| 33 | + "--kv-canary-real-data", |
| 34 | + "partial", |
| 35 | + "--kv-canary-sweep-interval", |
| 36 | + "100", |
| 37 | + "--disable-piecewise-cuda-graph", |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +class ChunkedGsm8kMixin: |
| 42 | + __test__ = False |
| 43 | + use_kv_canary: ClassVar[bool] = True |
| 44 | + model: ClassVar[str] = DEFAULT_MODEL |
| 45 | + feature_args: ClassVar[List[str]] = [] |
| 46 | + |
| 47 | + chunked_prefill_size: ClassVar[int] = DEFAULT_CHUNKED_PREFILL_SIZE |
| 48 | + num_shots: ClassVar[int] = DEFAULT_NUM_SHOTS |
| 49 | + num_examples: ClassVar[int] = DEFAULT_NUM_EXAMPLES |
| 50 | + num_threads: ClassVar[int] = DEFAULT_NUM_THREADS |
| 51 | + max_tokens: ClassVar[int] = DEFAULT_MAX_TOKENS |
| 52 | + gsm8k_threshold: ClassVar[float] |
| 53 | + |
| 54 | + def build_prefill_side_args(self) -> List[str]: |
| 55 | + canary = list(KV_CANARY_ARGS) if self.use_kv_canary else [] |
| 56 | + return ( |
| 57 | + ["--chunked-prefill-size", str(self.chunked_prefill_size)] |
| 58 | + + list(self.feature_args) |
| 59 | + + canary |
| 60 | + ) |
| 61 | + |
| 62 | + def test_mixed_prefix_gsm8k_chunked(self): |
| 63 | + fixture_name = type(self).__name__ |
| 64 | + |
| 65 | + args = SimpleNamespace( |
| 66 | + base_url=self.base_url, |
| 67 | + model=self.model, |
| 68 | + eval_name="mixed_prefix_gsm8k", |
| 69 | + api="chat_completion", |
| 70 | + max_tokens=self.max_tokens, |
| 71 | + num_examples=self.num_examples, |
| 72 | + num_threads=self.num_threads, |
| 73 | + num_shots=self.num_shots, |
| 74 | + mixed_prefix_gsm8k_secondary_pool_size=15, |
| 75 | + mixed_prefix_gsm8k_seed=DEFAULT_SEED, |
| 76 | + gsm8k_data_path=None, |
| 77 | + temperature=0.0, |
| 78 | + ) |
| 79 | + tic = time.perf_counter() |
| 80 | + metrics = run_eval(args) |
| 81 | + metrics["elapsed_sec"] = time.perf_counter() - tic |
| 82 | + print(f"[{fixture_name}] {metrics} threshold={self.gsm8k_threshold:.4f}") |
| 83 | + |
| 84 | + score = metrics.get("score") |
| 85 | + self.assertIsNotNone(score, "run_eval returned no score") |
| 86 | + self.assertGreaterEqual(score, self.gsm8k_threshold) |
| 87 | + |
| 88 | + |
| 89 | +class ChunkedTestBase(ChunkedGsm8kMixin, CustomTestCase): |
| 90 | + __test__ = False |
| 91 | + |
| 92 | + base_url: ClassVar[str] = DEFAULT_URL_FOR_TEST |
| 93 | + launch_timeout: ClassVar[int] = DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH |
| 94 | + |
| 95 | + process: ClassVar[Optional[object]] = None |
| 96 | + |
| 97 | + @classmethod |
| 98 | + def setUpClass(cls): |
| 99 | + cls.process = popen_launch_server( |
| 100 | + cls.model, |
| 101 | + cls.base_url, |
| 102 | + timeout=cls.launch_timeout, |
| 103 | + other_args=cls("test_mixed_prefix_gsm8k_chunked").build_prefill_side_args(), |
| 104 | + ) |
| 105 | + |
| 106 | + @classmethod |
| 107 | + def tearDownClass(cls): |
| 108 | + if cls.process is not None: |
| 109 | + kill_process_tree(cls.process.pid) |
| 110 | + |
| 111 | + |
| 112 | +class ChunkedTestPDBase(ChunkedGsm8kMixin, PDDisaggregationServerBase): |
| 113 | + __test__ = False |
| 114 | + decode_feature_args: ClassVar[List[str]] = [] |
| 115 | + |
| 116 | + @classmethod |
| 117 | + def setUpClass(cls): |
| 118 | + cls.extra_prefill_args = cls( |
| 119 | + "test_mixed_prefix_gsm8k_chunked" |
| 120 | + ).build_prefill_side_args() |
| 121 | + canary = list(KV_CANARY_ARGS) if cls.use_kv_canary else [] |
| 122 | + cls.extra_decode_args = canary + list(cls.decode_feature_args) |
| 123 | + PDDisaggregationServerBase.setUpClass() |
| 124 | + cls.model = try_cached_model(cls.model) |
| 125 | + cls.launch_all() |
| 126 | + |
| 127 | + @classmethod |
| 128 | + def tearDownClass(cls): |
| 129 | + PDDisaggregationServerBase.tearDownClass() |
0 commit comments