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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import sys
from io import StringIO
from pathlib import Path
from typing import Any, Optional

import polars as pl
import pytest
import torch
from rich.console import Console

from sglang.srt.debug_utils.comparator.display import (
    _collect_input_ids_and_positions,
    _collect_rank_info,
    _render_polars_as_text,
    extract_parallel_info,
)
from sglang.srt.debug_utils.comparator.output_types import (
    InputIdsRecord,
    RankInfoRecord,
)
from sglang.test.ci.ci_register import register_cpu_ci

register_cpu_ci(est_time=10, suite="default", nightly=True)


def _render_rich(renderable: object) -> str:
    buf: StringIO = StringIO()
    Console(file=buf, force_terminal=False, width=120).print(renderable)
    return buf.getvalue().rstrip("\n")


def _save_dump_file(
    directory: Path,
    *,
    name: str,
    step: int,
    rank: int,
    dump_index: int,
    value: torch.Tensor,
    meta: dict,
) -> str:
    filename = f"name={name}___step={step}___rank={rank}___dump_index={dump_index}.pt"
    torch.save({"value": value, "meta": meta}, directory / filename)
    return filename


def _make_df(rows: list[dict]) -> pl.DataFrame:
    df = pl.DataFrame(rows)
    df = df.with_columns(
        pl.col("step").cast(int),
        pl.col("rank").cast(int),
        pl.col("dump_index").cast(int),
    )
    return df


class TestRenderPolarsAsText:
    def test_renders_table(self) -> None:
        df = pl.DataFrame({"col_a": [1, 2], "col_b": ["x", "y"]})
        text: str = _render_polars_as_text(df, title="test table")

        assert "test table" in text
        assert "col_a" in text
        assert "col_b" in text

    def test_renders_empty_dataframe(self) -> None:
        df = pl.DataFrame({"a": [], "b": []})
        text: str = _render_polars_as_text(df, title="empty")
        assert "empty" in text


class TestCollectRankInfo:
    def test_collects_rank_info(self, tmp_path: Path) -> None:
        sglang_info = {
            "tp_rank": 0,
            "tp_size": 2,
            "pp_rank": 0,
            "pp_size": 1,
        }
        filename: str = _save_dump_file(
            tmp_path,
            name="input_ids",
            step=0,
            rank=0,
            dump_index=0,
            value=torch.tensor([1, 2, 3]),
            meta={"sglang_parallel_info": sglang_info},
        )
        df = _make_df(
            [
                {
                    "filename": filename,
                    "name": "input_ids",
                    "step": 0,
                    "rank": 0,
                    "dump_index": 0,
                }
            ]
        )

        rows: Optional[list[dict[str, Any]]] = _collect_rank_info(df, dump_dir=tmp_path)

        assert rows is not None
        assert len(rows) == 1
        assert rows[0]["rank"] == 0
        assert rows[0]["tp"] == "0/2"
        assert rows[0]["pp"] == "0/1"

    def test_returns_none_when_no_input_ids(self, tmp_path: Path) -> None:
        df = _make_df(
            [
                {
                    "filename": "f.pt",
                    "name": "some_other",
                    "step": 0,
                    "rank": 0,
                    "dump_index": 0,
                }
            ]
        )
        result = _collect_rank_info(df, dump_dir=tmp_path)
        assert result is None

    def test_deduplicates_ranks(self, tmp_path: Path) -> None:
        meta = {"sglang_parallel_info": {"tp_rank": 0, "tp_size": 1}}
        f1: str = _save_dump_file(
            tmp_path,
            name="input_ids",
            step=0,
            rank=0,
            dump_index=0,
            value=torch.tensor([1]),
            meta=meta,
        )
        f2: str = _save_dump_file(
            tmp_path,
            name="input_ids",
            step=1,
            rank=0,
            dump_index=1,
            value=torch.tensor([2]),
            meta=meta,
        )
        df = _make_df(
            [
                {
                    "filename": f1,
                    "name": "input_ids",
                    "step": 0,
                    "rank": 0,
                    "dump_index": 0,
                },
                {
                    "filename": f2,
                    "name": "input_ids",
                    "step": 1,
                    "rank": 0,
                    "dump_index": 1,
                },
            ]
        )

        rows = _collect_rank_info(df, dump_dir=tmp_path)

        assert rows is not None
        assert len(rows) == 1


class TestCollectInputIdsAndPositions:
    def test_collects_ids_and_positions(self, tmp_path: Path) -> None:
        f_ids: str = _save_dump_file(
            tmp_path,
            name="input_ids",
            step=0,
            rank=0,
            dump_index=0,
            value=torch.tensor([10, 20, 30]),
            meta={},
        )
        f_pos: str = _save_dump_file(
            tmp_path,
            name="positions",
            step=0,
            rank=0,
            dump_index=1,
            value=torch.tensor([0, 1, 2]),
            meta={},
        )
        df = _make_df(
            [
                {
                    "filename": f_ids,
                    "name": "input_ids",
                    "step": 0,
                    "rank": 0,
                    "dump_index": 0,
                },
                {
                    "filename": f_pos,
                    "name": "positions",
                    "step": 0,
                    "rank": 0,
                    "dump_index": 1,
                },
            ]
        )

        rows = _collect_input_ids_and_positions(df, dump_dir=tmp_path)

        assert rows is not None
        assert len(rows) == 1
        assert rows[0]["step"] == 0
        assert rows[0]["rank"] == 0
        assert rows[0]["num_tokens"] == 3
        assert "10" in rows[0]["input_ids"]
        assert "0" in rows[0]["positions"]

    def test_returns_none_when_empty(self, tmp_path: Path) -> None:
        df = _make_df(
            [
                {
                    "filename": "f.pt",
                    "name": "weight",
                    "step": 0,
                    "rank": 0,
                    "dump_index": 0,
                }
            ]
        )
        result = _collect_input_ids_and_positions(df, dump_dir=tmp_path)
        assert result is None

    def test_with_mock_tokenizer(self, tmp_path: Path) -> None:
        f_ids: str = _save_dump_file(
            tmp_path,
            name="input_ids",
            step=0,
            rank=0,
            dump_index=0,
            value=torch.tensor([1, 2]),
            meta={},
        )
        df = _make_df(
            [
                {
                    "filename": f_ids,
                    "name": "input_ids",
                    "step": 0,
                    "rank": 0,
                    "dump_index": 0,
                }
            ]
        )

        class _MockTokenizer:
            def decode(self, ids: list[int], skip_special_tokens: bool = False) -> str:
                return f"decoded:{ids}"

        rows = _collect_input_ids_and_positions(
            df, dump_dir=tmp_path, tokenizer=_MockTokenizer()
        )

        assert rows is not None
        assert "decoded_text" in rows[0]
        assert "decoded:" in rows[0]["decoded_text"]


class TestRankInfoRecordSnapshot:
    def test_to_text_snapshot(self) -> None:
        record = RankInfoRecord(
            label="baseline",
            rows=[
                {"rank": 0, "tp": "0/2", "pp": "0/1"},
                {"rank": 1, "tp": "1/2", "pp": "0/1"},
            ],
        )
        text: str = record.to_text()

        assert "baseline ranks" in text
        assert "rank" in text
        assert "tp" in text
        assert "pp" in text
        assert "0/2" in text
        assert "1/2" in text
        assert "0/1" in text

    def test_to_rich_snapshot(self) -> None:
        from rich.table import Table

        record = RankInfoRecord(
            label="baseline",
            rows=[
                {"rank": 0, "tp": "0/2", "pp": "0/1"},
                {"rank": 1, "tp": "1/2", "pp": "0/1"},
            ],
        )
        body = record._format_rich_body()

        assert isinstance(body, Table)
        rendered: str = _render_rich(body)
        assert "baseline ranks" in rendered
        assert "0/2" in rendered
        assert "1/2" in rendered

    def test_json_roundtrip(self) -> None:
        record = RankInfoRecord(
            label="target",
            rows=[{"rank": 0, "tp": "0/4"}],
        )
        json_str: str = record.model_dump_json()

        assert '"type":"rank_info"' in json_str
        assert '"label":"target"' in json_str
        assert '"tp":"0/4"' in json_str


class TestInputIdsRecordSnapshot:
    def test_to_text_snapshot(self) -> None:
        record = InputIdsRecord(
            label="target",
            rows=[
                {
                    "step": 0,
                    "rank": 0,
                    "num_tokens": 3,
                    "input_ids": "[10, 20, 30]",
                    "positions": "[0, 1, 2]",
                },
            ],
        )
        text: str = record.to_text()

        assert "target input_ids & positions" in text
        assert "step" in text
        assert "num_tokens" in text
        assert "10, 20, 30" in text
        assert "0, 1, 2" in text

    def test_to_rich_snapshot(self) -> None:
        from rich.table import Table

        record = InputIdsRecord(
            label="target",
            rows=[
                {
                    "step": 0,
                    "rank": 0,
                    "num_tokens": 3,
                    "input_ids": "[10, 20, 30]",
                    "positions": "[0, 1, 2]",
                },
            ],
        )
        body = record._format_rich_body()

        assert isinstance(body, Table)
        rendered: str = _render_rich(body)
        assert "target input_ids & positions" in rendered
        assert "10, 20, 30" in rendered
        assert "0, 1, 2" in rendered

    def test_json_roundtrip(self) -> None:
        record = InputIdsRecord(
            label="baseline",
            rows=[
                {
                    "step": 0,
                    "rank": 0,
                    "num_tokens": 2,
                    "input_ids": "[1, 2]",
                    "positions": "[0, 1]",
                    "decoded_text": "'hello'",
                },
            ],
        )
        json_str: str = record.model_dump_json()

        assert '"type":"input_ids"' in json_str
        assert '"label":"baseline"' in json_str
        assert '"decoded_text"' in json_str

    def test_to_text_with_decoded(self) -> None:
        record = InputIdsRecord(
            label="test",
            rows=[
                {
                    "step": 0,
                    "rank": 0,
                    "num_tokens": 2,
                    "input_ids": "[1, 2]",
                    "positions": "[0, 1]",
                    "decoded_text": "'hello world'",
                },
            ],
        )
        text: str = record.to_text()

        assert "decoded_text" in text
        assert "hello world" in text


class TestExtractParallelInfo:
    def test_extracts_rank_size_pairs(self) -> None:
        info: dict = {
            "tp_rank": 1,
            "tp_size": 4,
            "pp_rank": 0,
            "pp_size": 2,
        }
        row_data: dict = {}
        extract_parallel_info(row_data=row_data, info=info)

        assert row_data["tp"] == "1/4"
        assert row_data["pp"] == "0/2"

    def test_skips_error_info(self) -> None:
        row_data: dict = {}
        extract_parallel_info(
            row_data=row_data, info={"error": True, "tp_rank": 0, "tp_size": 1}
        )
        assert row_data == {}

    def test_skips_empty_info(self) -> None:
        row_data: dict = {}
        extract_parallel_info(row_data=row_data, info={})
        assert row_data == {}

    def test_ignores_rank_without_size(self) -> None:
        row_data: dict = {}
        extract_parallel_info(row_data=row_data, info={"tp_rank": 0})
        assert "tp" not in row_data


if __name__ == "__main__":
    sys.exit(pytest.main([__file__]))