|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from argparse import Namespace |
| 7 | +from contextlib import contextmanager |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +os.environ["CUDA_VISIBLE_DEVICES"] = "" |
| 13 | +sys.path.insert(0, str(Path(__file__).resolve().parent / "plugin_contracts")) |
| 14 | + |
| 15 | +from _shared import install_paths, install_stubs |
| 16 | + |
| 17 | +install_paths() |
| 18 | +install_stubs(with_sglang_router=True, with_transformers=True) |
| 19 | + |
| 20 | +from slime.rollout import sglang_rollout |
| 21 | +from slime.utils.types import Sample |
| 22 | + |
| 23 | +NUM_GPUS = 0 |
| 24 | + |
| 25 | + |
| 26 | +def _samples(*session_ids: str | None) -> list[Sample]: |
| 27 | + return [Sample(index=index, session_id=session_id) for index, session_id in enumerate(session_ids)] |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.unit |
| 31 | +def test_sample_scope_assigns_distinct_ids_to_missing_samples(): |
| 32 | + samples = _samples(None, None, None) |
| 33 | + |
| 34 | + sglang_rollout._assign_missing_session_ids(samples, "sample") |
| 35 | + |
| 36 | + assert len({sample.session_id for sample in samples}) == 3 |
| 37 | + assert all(sample.session_id for sample in samples) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.unit |
| 41 | +def test_sample_scope_preserves_explicit_session_id(): |
| 42 | + samples = _samples("caller-id", None) |
| 43 | + |
| 44 | + sglang_rollout._assign_missing_session_ids(samples, "sample") |
| 45 | + |
| 46 | + assert samples[0].session_id == "caller-id" |
| 47 | + assert samples[1].session_id and samples[1].session_id != "caller-id" |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.unit |
| 51 | +def test_group_scope_assigns_one_id_to_all_missing_samples(): |
| 52 | + samples = _samples(None, None, None) |
| 53 | + |
| 54 | + sglang_rollout._assign_missing_session_ids(samples, "group") |
| 55 | + |
| 56 | + assert len({sample.session_id for sample in samples}) == 1 |
| 57 | + assert samples[0].session_id |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.unit |
| 61 | +def test_group_scope_inherits_one_explicit_session_id(): |
| 62 | + samples = _samples(None, "caller-id", None) |
| 63 | + |
| 64 | + sglang_rollout._assign_missing_session_ids(samples, "group") |
| 65 | + |
| 66 | + assert [sample.session_id for sample in samples] == ["caller-id", "caller-id", "caller-id"] |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.unit |
| 70 | +def test_group_scope_rejects_conflicting_explicit_session_ids(): |
| 71 | + with pytest.raises(ValueError, match="at most one explicit non-empty session_id"): |
| 72 | + sglang_rollout._assign_missing_session_ids(_samples("first", "second"), "group") |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.unit |
| 76 | +def test_session_id_scope_rejects_unknown_value(): |
| 77 | + with pytest.raises(ValueError, match="Unsupported rollout session ID scope"): |
| 78 | + sglang_rollout._assign_missing_session_ids(_samples(None), "rollout") |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.unit |
| 82 | +def test_session_id_survives_serialization_and_retry(): |
| 83 | + samples = _samples(None, None) |
| 84 | + |
| 85 | + sglang_rollout._assign_missing_session_ids(samples, "group") |
| 86 | + restored = [Sample.from_dict(sample.to_dict()) for sample in samples] |
| 87 | + before_retry = [sample.session_id for sample in restored] |
| 88 | + sglang_rollout._assign_missing_session_ids(restored, "group") |
| 89 | + |
| 90 | + assert [sample.session_id for sample in restored] == before_retry |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.unit |
| 94 | +def test_group_scope_preserves_sample_order(): |
| 95 | + samples = _samples(None, None, None) |
| 96 | + order = [id(sample) for sample in samples] |
| 97 | + |
| 98 | + sglang_rollout._assign_missing_session_ids(samples, "group") |
| 99 | + |
| 100 | + assert [id(sample) for sample in samples] == order |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.unit |
| 104 | +def test_group_scope_flows_through_group_generate_to_http_with_one_routing_key(monkeypatch): |
| 105 | + captured = [] |
| 106 | + |
| 107 | + async def fake_post(url, payload, headers=None): |
| 108 | + captured.append(headers) |
| 109 | + return {"meta_info": {"finish_reason": {"type": "stop"}}, "text": "response"} |
| 110 | + |
| 111 | + monkeypatch.setattr(sglang_rollout, "GenerateState", _FakeGenerateState) |
| 112 | + monkeypatch.setattr(sglang_rollout, "post", fake_post) |
| 113 | + |
| 114 | + async def fake_rm(args, sample): |
| 115 | + return 0.0 |
| 116 | + |
| 117 | + monkeypatch.setattr(sglang_rollout, "async_rm", fake_rm) |
| 118 | + args = Namespace( |
| 119 | + ci_test=False, |
| 120 | + rollout_session_id_scope="group", |
| 121 | + sglang_enable_deterministic_inference=False, |
| 122 | + group_rm=False, |
| 123 | + partial_rollout=False, |
| 124 | + mask_offpolicy_in_partial_rollout=False, |
| 125 | + custom_generate_function_path=None, |
| 126 | + sglang_router_ip="127.0.0.1", |
| 127 | + sglang_router_port=30000, |
| 128 | + router_policy="consistent_hashing", |
| 129 | + use_rollout_routing_replay=False, |
| 130 | + sglang_speculative_algorithm=False, |
| 131 | + ) |
| 132 | + group = _samples(None, None, None) |
| 133 | + for sample in group: |
| 134 | + sample.tokens = [11, 12] |
| 135 | + sampling_params = {"max_new_tokens": 4, "temperature": 0.0} |
| 136 | + |
| 137 | + result = asyncio.run(sglang_rollout.generate_and_rm_group(args, group, sampling_params)) |
| 138 | + |
| 139 | + assert result == group |
| 140 | + assert len(captured) == 3 |
| 141 | + assert {headers["X-SMG-Routing-Key"] for headers in captured} == {group[0].session_id} |
| 142 | + assert group[0].session_id and all(sample.session_id == group[0].session_id for sample in group) |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.unit |
| 146 | +@pytest.mark.parametrize("scope", ["sample", "group"]) |
| 147 | +def test_empty_group_is_a_noop(scope): |
| 148 | + samples: list[Sample] = [] |
| 149 | + |
| 150 | + sglang_rollout._assign_missing_session_ids(samples, scope) |
| 151 | + |
| 152 | + assert samples == [] |
| 153 | + |
| 154 | + |
| 155 | +@pytest.mark.unit |
| 156 | +def test_default_scope_preserves_per_sample_sampling_seeds(monkeypatch): |
| 157 | + captured_sampling_params = [] |
| 158 | + |
| 159 | + class FakeGroupGenerateState: |
| 160 | + def __init__(self, args) -> None: |
| 161 | + self.aborted = False |
| 162 | + self.group_sampling_seeds = [101, 102] |
| 163 | + |
| 164 | + async def fake_generate_and_rm(args, sample, sampling_params, evaluation=False): |
| 165 | + captured_sampling_params.append(sampling_params) |
| 166 | + return sample |
| 167 | + |
| 168 | + monkeypatch.setattr(sglang_rollout, "GenerateState", FakeGroupGenerateState) |
| 169 | + monkeypatch.setattr(sglang_rollout, "generate_and_rm", fake_generate_and_rm) |
| 170 | + args = Namespace( |
| 171 | + rollout_session_id_scope="sample", |
| 172 | + sglang_enable_deterministic_inference=True, |
| 173 | + group_rm=False, |
| 174 | + ) |
| 175 | + group = _samples(None, None) |
| 176 | + sampling_params = {"temperature": 0.5} |
| 177 | + |
| 178 | + result = asyncio.run(sglang_rollout.generate_and_rm_group(args, group, sampling_params)) |
| 179 | + |
| 180 | + assert result == group |
| 181 | + assert len({sample.session_id for sample in group}) == 2 |
| 182 | + assert captured_sampling_params == [ |
| 183 | + {"temperature": 0.5, "sampling_seed": 101}, |
| 184 | + {"temperature": 0.5, "sampling_seed": 102}, |
| 185 | + ] |
| 186 | + assert sampling_params == {"temperature": 0.5} |
| 187 | + |
| 188 | + |
| 189 | +class _FakeGenerateState: |
| 190 | + def __init__(self, args) -> None: |
| 191 | + self.aborted = False |
| 192 | + self.semaphore = asyncio.Semaphore(3) |
| 193 | + self.tokenizer = None |
| 194 | + self.processor = None |
| 195 | + |
| 196 | + @contextmanager |
| 197 | + def dp_rank_context(self): |
| 198 | + yield 0 |
| 199 | + |
| 200 | + |
| 201 | +@pytest.mark.unit |
| 202 | +@pytest.mark.parametrize( |
| 203 | + "router_policy, expected_headers", |
| 204 | + [ |
| 205 | + ("consistent_hashing", {"X-SMG-Routing-Key": "caller-id"}), |
| 206 | + ("round_robin", None), |
| 207 | + ("cache_aware", None), |
| 208 | + ], |
| 209 | +) |
| 210 | +def test_generate_sends_routing_header_only_for_consistent_hashing(monkeypatch, router_policy, expected_headers): |
| 211 | + captured = {} |
| 212 | + |
| 213 | + async def fake_post(url, payload, headers=None): |
| 214 | + captured.update(url=url, payload=payload, headers=headers) |
| 215 | + return {"meta_info": {"finish_reason": {"type": "stop"}}, "text": "response"} |
| 216 | + |
| 217 | + monkeypatch.setattr(sglang_rollout, "GenerateState", _FakeGenerateState) |
| 218 | + monkeypatch.setattr(sglang_rollout, "post", fake_post) |
| 219 | + args = Namespace( |
| 220 | + ci_test=False, |
| 221 | + sglang_router_ip="127.0.0.1", |
| 222 | + sglang_router_port=30000, |
| 223 | + router_policy=router_policy, |
| 224 | + use_rollout_routing_replay=False, |
| 225 | + sglang_speculative_algorithm=False, |
| 226 | + ) |
| 227 | + sample = Sample(prompt="prompt", tokens=[11, 12], session_id="caller-id") |
| 228 | + sampling_params = {"max_new_tokens": 4, "temperature": 0.5} |
| 229 | + |
| 230 | + asyncio.run(sglang_rollout.generate(args, sample, sampling_params)) |
| 231 | + |
| 232 | + assert captured["url"] == "http://127.0.0.1:30000/generate" |
| 233 | + assert captured["headers"] == expected_headers |
| 234 | + assert captured["payload"] == {"sampling_params": sampling_params, "return_logprob": True, "input_ids": [11, 12]} |
| 235 | + assert sample.tokens == [11, 12] |
| 236 | + assert sampling_params == {"max_new_tokens": 4, "temperature": 0.5} |
0 commit comments