forked from sgl-project/sglang
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_dumper.py
More file actions
654 lines (498 loc) · 20.1 KB
/
test_dumper.py
File metadata and controls
654 lines (498 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import sys
import threading
import time
from pathlib import Path
import pytest
import requests
import torch
import torch.distributed as dist
from sglang.srt.debug_utils.dumper import (
_collect_megatron_parallel_info,
_collect_sglang_parallel_info,
_collective_with_timeout,
_Dumper,
_materialize_value,
_obj_to_dict,
_torch_save,
get_tensor_info,
get_truncated_value,
)
from sglang.srt.environ import temp_set_env
from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci
from sglang.test.test_utils import run_distributed_test
register_cuda_ci(est_time=30, suite="nightly-2-gpu", nightly=True)
register_amd_ci(est_time=60, suite="nightly-amd", nightly=True)
class TestDumperPureFunctions:
def test_get_truncated_value(self):
assert get_truncated_value(None) is None
assert get_truncated_value(42) == 42
assert len(get_truncated_value((torch.randn(10), torch.randn(20)))) == 2
assert get_truncated_value(torch.randn(10, 10)).shape == (10, 10)
assert get_truncated_value(torch.randn(100, 100)).shape == (5, 5)
def test_obj_to_dict(self):
assert _obj_to_dict({"a": 1}) == {"a": 1}
class Obj:
x, y = 10, 20
def method(self):
pass
result = _obj_to_dict(Obj())
assert result["x"] == 10
assert "method" not in result
def test_get_tensor_info(self):
info = get_tensor_info(torch.randn(10, 10))
for key in ["shape=", "dtype=", "min=", "max=", "mean="]:
assert key in info
assert "value=42" in get_tensor_info(42)
assert "min=None" in get_tensor_info(torch.tensor([]))
class TestTorchSave:
def test_normal(self, tmp_path):
path = str(tmp_path / "a.pt")
tensor = torch.randn(3, 3)
_torch_save(tensor, path)
assert torch.equal(torch.load(path, weights_only=True), tensor)
def test_parameter_fallback(self, tmp_path):
class BadParam(torch.nn.Parameter):
def __reduce_ex__(self, protocol):
raise RuntimeError("not pickleable")
path = str(tmp_path / "b.pt")
param = BadParam(torch.randn(4))
_torch_save(param, path)
assert torch.equal(torch.load(path, weights_only=True), param.data)
def test_silent_skip(self, tmp_path, capsys):
path = str(tmp_path / "c.pt")
_torch_save({"fn": lambda: None}, path)
captured = capsys.readouterr()
assert "[Dumper] Observe error=" in captured.out
assert "skip the tensor" in captured.out
class TestCollectiveTimeout:
def test_watchdog_fires_on_timeout(self):
import io
block_event = threading.Event()
old_stdout = sys.stdout
captured = io.StringIO()
sys.stdout = captured
def run_with_timeout():
try:
_collective_with_timeout(
lambda: block_event.wait(),
operation_name="test_blocked_op",
timeout_seconds=2,
)
finally:
sys.stdout = old_stdout
worker = threading.Thread(target=run_with_timeout)
worker.start()
time.sleep(4)
block_event.set()
worker.join(timeout=5)
output = captured.getvalue()
assert "WARNING" in output
assert "test_blocked_op" in output
assert "2s" in output
class TestDumperDistributed:
def test_basic(self, tmp_path):
with temp_set_env(
allow_sglang=True,
SGLANG_DUMPER_ENABLE="1",
SGLANG_DUMPER_DIR=str(tmp_path),
):
run_distributed_test(self._test_basic_func, tmpdir=str(tmp_path))
@staticmethod
def _test_basic_func(rank, tmpdir):
from sglang.srt.debug_utils.dumper import dumper
tensor = torch.randn(10, 10, device=f"cuda:{rank}")
dumper.on_forward_pass_start()
dumper.dump("tensor_a", tensor, arg=100)
dumper.on_forward_pass_start()
dumper.set_ctx(ctx_arg=200)
dumper.dump("tensor_b", tensor)
dumper.set_ctx(ctx_arg=None)
dumper.on_forward_pass_start()
dumper.override_enable(False)
dumper.dump("tensor_skip", tensor)
dumper.override_enable(True)
dumper.on_forward_pass_start()
dumper.dump_dict("obj", {"a": torch.randn(3, device=f"cuda:{rank}"), "b": 42})
dist.barrier()
filenames = _get_filenames(tmpdir)
_assert_files(
filenames,
exist=["tensor_a", "tensor_b", "arg=100", "ctx_arg=200", "obj_a", "obj_b"],
not_exist=["tensor_skip"],
)
def test_collective_timeout(self):
with temp_set_env(allow_sglang=True, SGLANG_DUMPER_ENABLE="1"):
run_distributed_test(self._test_collective_timeout_func)
@staticmethod
def _test_collective_timeout_func(rank):
import io
dumper = _Dumper(
enable=True,
base_dir=Path("/tmp"),
partial_name=None,
enable_http_server=False,
collective_timeout=3,
)
captured = io.StringIO()
old_stdout = sys.stdout
sys.stdout = captured
try:
if rank != 0:
time.sleep(6)
dumper.on_forward_pass_start()
finally:
sys.stdout = old_stdout
output = captured.getvalue()
if rank == 0:
assert "WARNING" in output, f"Expected WARNING in rank 0 output: {output}"
assert "has not completed after 3s" in output
def test_http_enable(self):
with temp_set_env(allow_sglang=True, SGLANG_DUMPER_ENABLE="0"):
run_distributed_test(self._test_http_func)
@staticmethod
def _test_http_func(rank):
from sglang.srt.debug_utils.dumper import dumper
assert not dumper._enable
dumper.on_forward_pass_start()
for enable in [True, False]:
dist.barrier()
if rank == 0:
time.sleep(0.1)
requests.post(
"http://localhost:40000/dumper", json={"enable": enable}
).raise_for_status()
dist.barrier()
assert dumper._enable == enable
def test_file_content_correctness(self, tmp_path):
with temp_set_env(
allow_sglang=True,
SGLANG_DUMPER_ENABLE="1",
SGLANG_DUMPER_DIR=str(tmp_path),
):
run_distributed_test(self._test_file_content_func, tmpdir=str(tmp_path))
@staticmethod
def _test_file_content_func(rank, tmpdir):
from sglang.srt.debug_utils.dumper import dumper
tensor = torch.arange(12, device=f"cuda:{rank}").reshape(3, 4).float()
dumper.on_forward_pass_start()
dumper.dump("content_check", tensor)
dist.barrier()
path = _find_dump_file(tmpdir, rank=rank, name="content_check")
raw = _load_dump(path)
assert isinstance(raw, dict), f"Expected dict, got {type(raw)}"
assert "value" in raw and "meta" in raw
assert torch.equal(raw["value"], tensor.cpu())
assert raw["meta"]["name"] == "content_check"
assert raw["meta"]["rank"] == rank
class TestDumperFileWriteControl:
def test_filter(self, tmp_path):
with temp_set_env(
allow_sglang=True,
SGLANG_DUMPER_ENABLE="1",
SGLANG_DUMPER_DIR=str(tmp_path),
SGLANG_DUMPER_FILTER="^keep",
):
run_distributed_test(self._test_filter_func, tmpdir=str(tmp_path))
@staticmethod
def _test_filter_func(rank, tmpdir):
from sglang.srt.debug_utils.dumper import dumper
dumper.on_forward_pass_start()
dumper.dump("keep_this", torch.randn(5, device=f"cuda:{rank}"))
dumper.dump("skip_this", torch.randn(5, device=f"cuda:{rank}"))
dumper.dump("not_keep_this", torch.randn(5, device=f"cuda:{rank}"))
dist.barrier()
filenames = _get_filenames(tmpdir)
_assert_files(
filenames,
exist=["keep_this"],
not_exist=["skip_this", "not_keep_this"],
)
def test_write_disabled(self, tmp_path):
with temp_set_env(
allow_sglang=True,
SGLANG_DUMPER_ENABLE="1",
SGLANG_DUMPER_DIR=str(tmp_path),
SGLANG_DUMPER_WRITE_FILE="0",
):
run_distributed_test(self._test_write_disabled_func, tmpdir=str(tmp_path))
@staticmethod
def _test_write_disabled_func(rank, tmpdir):
from sglang.srt.debug_utils.dumper import dumper
dumper.on_forward_pass_start()
dumper.dump("no_write", torch.randn(5, device=f"cuda:{rank}"))
dist.barrier()
assert len(_get_filenames(tmpdir)) == 0
def test_save_false(self, tmp_path):
with temp_set_env(
allow_sglang=True,
SGLANG_DUMPER_ENABLE="1",
SGLANG_DUMPER_DIR=str(tmp_path),
):
run_distributed_test(self._test_save_false_func, tmpdir=str(tmp_path))
@staticmethod
def _test_save_false_func(rank, tmpdir):
from sglang.srt.debug_utils.dumper import dumper
dumper.on_forward_pass_start()
dumper.dump("no_save_tensor", torch.randn(5, device=f"cuda:{rank}"), save=False)
dist.barrier()
assert len(_get_filenames(tmpdir)) == 0
class TestDumpDictFormat:
"""Verify that dump files use the dict output format: {"value": ..., "meta": {...}}."""
def test_dict_format_structure(self, tmp_path):
dumper = _make_test_dumper(tmp_path)
tensor = torch.randn(4, 4)
dumper.dump("fmt_test", tensor, custom_key="hello")
path = _find_dump_file(str(tmp_path), rank=0, name="fmt_test")
raw = _load_dump(path)
assert isinstance(raw, dict)
assert set(raw.keys()) == {"value", "meta"}
assert torch.equal(raw["value"], tensor)
meta = raw["meta"]
assert meta["name"] == "fmt_test"
assert meta["custom_key"] == "hello"
assert "forward_pass_id" in meta
assert "rank" in meta
assert "dump_index" in meta
def test_dict_format_with_context(self, tmp_path):
dumper = _make_test_dumper(tmp_path)
dumper.set_ctx(ctx_val=42)
tensor = torch.randn(2, 2)
dumper.dump("ctx_fmt", tensor)
path = _find_dump_file(str(tmp_path), rank=0, name="ctx_fmt")
raw = _load_dump(path)
assert raw["meta"]["ctx_val"] == 42
assert torch.equal(raw["value"], tensor)
def _make_test_dumper(tmp_path: Path, **overrides) -> _Dumper:
"""Create a _Dumper for CPU testing without HTTP server or distributed."""
defaults: dict = dict(
enable=True,
base_dir=tmp_path,
partial_name="test",
enable_http_server=False,
)
d = _Dumper(**{**defaults, **overrides})
d.on_forward_pass_start()
return d
def _get_filenames(tmpdir):
return {f.name for f in Path(tmpdir).glob("sglang_dump_*/*.pt")}
def _assert_files(filenames, *, exist=(), not_exist=()):
for p in exist:
assert any(p in f for f in filenames), f"{p} not found in {filenames}"
for p in not_exist:
assert not any(
p in f for f in filenames
), f"{p} should not exist in {filenames}"
def _load_dump(path: Path) -> dict:
"""Load a dump file and return the raw dict (with 'value' and 'meta' keys)."""
return torch.load(path, map_location="cpu", weights_only=False)
def _find_dump_file(tmpdir, *, rank: int = 0, name: str) -> Path:
matches = [
f
for f in Path(tmpdir).glob("sglang_dump_*/*.pt")
if f"rank={rank}" in f.name and name in f.name
]
assert (
len(matches) == 1
), f"Expected 1 file matching rank={rank} name={name}, got {matches}"
return matches[0]
class TestMaterializeValue:
def test_materialize_value_callable(self):
tensor = torch.randn(3, 3)
result = _materialize_value(lambda: tensor)
assert torch.equal(result, tensor)
def test_materialize_value_passthrough(self):
tensor = torch.randn(3, 3)
result = _materialize_value(tensor)
assert result is tensor
def test_dump_with_callable_value(self, tmp_path):
d = _make_test_dumper(tmp_path)
tensor = torch.randn(4, 4)
d.dump("lazy_tensor", lambda: tensor)
_assert_files(_get_filenames(tmp_path), exist=["name=lazy_tensor"])
path = _find_dump_file(tmp_path, rank=0, name="lazy_tensor")
assert torch.equal(_load_dump(path)["value"], tensor)
class TestSaveValue:
def test_dump_output_format(self, tmp_path):
dumper = _make_test_dumper(tmp_path)
tensor = torch.randn(4, 4)
dumper.dump("dict_test", tensor)
path = _find_dump_file(tmp_path, rank=0, name="dict_test")
loaded = _load_dump(path)
assert torch.equal(loaded["value"], tensor)
assert loaded["meta"]["name"] == "dict_test"
assert loaded["meta"]["rank"] == 0
class TestStaticMetadata:
def test_static_meta_contains_world_info(self):
dumper = _make_test_dumper(Path("/tmp"))
meta = dumper._static_meta
assert "world_rank" in meta
assert "world_size" in meta
assert meta["world_rank"] == 0
assert meta["world_size"] == 1
def test_static_meta_caching(self):
dumper = _make_test_dumper(Path("/tmp"))
meta1 = dumper._static_meta
meta2 = dumper._static_meta
assert meta1 is meta2
def test_parallel_info_graceful_fallback(self):
sglang_info = _collect_sglang_parallel_info()
assert isinstance(sglang_info, dict)
megatron_info = _collect_megatron_parallel_info()
assert isinstance(megatron_info, dict)
def test_dump_includes_static_meta(self, tmp_path):
dumper = _make_test_dumper(tmp_path)
tensor = torch.randn(2, 2)
dumper.dump("meta_test", tensor)
path = _find_dump_file(tmp_path, rank=0, name="meta_test")
loaded = _load_dump(path)
meta = loaded["meta"]
assert "world_rank" in meta
assert "world_size" in meta
class TestDumpGrad:
def test_dump_grad_basic(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_grad=True)
x = torch.randn(3, 3, requires_grad=True)
y = (x * 2).sum()
d.dump("test_tensor", x)
y.backward()
filenames = _get_filenames(tmp_path)
assert any("name=test_tensor" in f and "grad__" not in f for f in filenames)
_assert_files(filenames, exist=["grad__test_tensor"])
def test_dump_grad_non_tensor_skipped(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_grad=True)
d.dump("not_tensor", 42)
_assert_files(_get_filenames(tmp_path), not_exist=["grad__"])
def test_dump_grad_no_requires_grad_skipped(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_grad=True)
x = torch.randn(3, 3, requires_grad=False)
d.dump("no_grad_tensor", x)
_assert_files(
_get_filenames(tmp_path),
exist=["name=no_grad_tensor"],
not_exist=["grad__"],
)
def test_dump_grad_captures_forward_pass_id(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_grad=True)
d._forward_pass_id = 42
x = torch.randn(3, 3, requires_grad=True)
y = (x * 2).sum()
d.dump("id_test", x)
d._forward_pass_id = 999
y.backward()
grad_file = _find_dump_file(tmp_path, name="grad__id_test")
assert "forward_pass_id=42" in grad_file.name
def test_dump_grad_file_content(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_grad=True)
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = (x * 3).sum()
d.dump("content_check", x)
y.backward()
grad_path = _find_dump_file(tmp_path, name="grad__content_check")
expected_grad = torch.full((2, 2), 3.0)
assert torch.equal(_load_dump(grad_path)["value"], expected_grad)
def test_disable_value(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_value=False, enable_grad=True)
x = torch.randn(3, 3, requires_grad=True)
y = (x * 2).sum()
d.dump("fwd_disabled", x)
y.backward()
filenames = _get_filenames(tmp_path)
assert not any(
"name=fwd_disabled" in f and "grad__" not in f for f in filenames
)
_assert_files(filenames, exist=["grad__fwd_disabled"])
def test_disable_grad(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_grad=False)
x = torch.randn(3, 3, requires_grad=True)
y = (x * 2).sum()
d.dump("grad_disabled", x)
y.backward()
_assert_files(
_get_filenames(tmp_path),
exist=["name=grad_disabled"],
not_exist=["grad__"],
)
class TestDumpModel:
def test_grad_basic(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_model_value=False)
model = torch.nn.Linear(4, 2)
x = torch.randn(3, 4)
y = model(x).sum()
y.backward()
d.dump_model(model, name_prefix="model")
_assert_files(
_get_filenames(tmp_path),
exist=["grad__model__weight", "grad__model__bias"],
)
def test_value_basic(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_model_grad=False)
model = torch.nn.Linear(4, 2, bias=False)
d.dump_model(model, name_prefix="model")
_assert_files(
_get_filenames(tmp_path),
exist=["model__weight"],
)
def test_no_grad_skipped(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_model_value=False)
model = torch.nn.Linear(4, 2)
d.dump_model(model, name_prefix="model")
filenames = _get_filenames(tmp_path)
assert len(filenames) == 0
def test_filter(self, tmp_path):
d = _make_test_dumper(tmp_path, filter="weight")
model = torch.nn.Linear(4, 2)
x = torch.randn(3, 4)
y = model(x).sum()
y.backward()
d.dump_model(model, name_prefix="model")
_assert_files(
_get_filenames(tmp_path),
exist=["model__weight", "grad__model__weight"],
not_exist=["model__bias", "grad__model__bias"],
)
def test_grad_file_content(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_model_value=False)
model = torch.nn.Linear(4, 2, bias=False)
x = torch.ones(1, 4)
y = model(x).sum()
y.backward()
d.dump_model(model, name_prefix="p")
path = _find_dump_file(tmp_path, name="grad__p__weight")
assert torch.equal(_load_dump(path)["value"], model.weight.grad)
def test_disable_model_grad(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_model_grad=False)
model = torch.nn.Linear(4, 2)
x = torch.randn(3, 4)
y = model(x).sum()
y.backward()
d.dump_model(model, name_prefix="model")
filenames = _get_filenames(tmp_path)
assert all("grad" not in f for f in filenames)
def test_disable_model_value(self, tmp_path):
d = _make_test_dumper(tmp_path, enable_model_value=False)
model = torch.nn.Linear(4, 2, bias=False)
x = torch.ones(1, 4)
y = model(x).sum()
y.backward()
d.dump_model(model, name_prefix="model")
filenames = _get_filenames(tmp_path)
assert all("grad" in f for f in filenames)
class TestCleanup:
def test_cleanup_removes_old_dumps(self, tmp_path):
old_dir = tmp_path / "sglang_dump_old"
old_dir.mkdir()
(old_dir / "dummy.pt").touch()
dumper = _make_test_dumper(tmp_path, needs_cleanup=True)
dumper.dump("new_tensor", torch.randn(3, 3))
assert not old_dir.exists()
_assert_files(_get_filenames(tmp_path), exist=["new_tensor"])
def test_no_cleanup_by_default(self, tmp_path):
old_dir = tmp_path / "sglang_dump_old"
old_dir.mkdir()
(old_dir / "dummy.pt").touch()
dumper = _make_test_dumper(tmp_path)
dumper.dump("new_tensor", torch.randn(3, 3))
assert old_dir.exists()
_assert_files(_get_filenames(tmp_path), exist=["new_tensor"])
if __name__ == "__main__":
sys.exit(pytest.main([__file__]))