Support captured dump output and console output control in dumper (#19017)

This commit is contained in:
fzyzcjy
2026-02-20 12:26:24 +08:00
committed by GitHub
parent fc1500adc6
commit 261bca3c58
2 changed files with 128 additions and 38 deletions
+82 -19
View File
@@ -269,25 +269,6 @@ class TestDumperFileWriteControl:
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,
@@ -307,6 +288,88 @@ class TestDumperFileWriteControl:
assert len(_get_filenames(tmpdir)) == 0
class TestOutputControl:
def test_file_enabled_by_default(self, tmp_path):
d = _make_test_dumper(tmp_path)
d.dump("file_on", torch.randn(3, 3))
_assert_files(_get_filenames(tmp_path), exist=["file_on"])
def test_file_disabled(self, tmp_path, capsys):
d = _make_test_dumper(tmp_path, enable_output_file=False)
d.dump("file_off", torch.randn(3, 3))
assert len(_get_filenames(tmp_path)) == 0
assert "file_off" in capsys.readouterr().out
def test_console_enabled_by_default(self, tmp_path, capsys):
d = _make_test_dumper(tmp_path)
d.dump("console_on", torch.randn(3, 3))
captured = capsys.readouterr()
assert "[Dumper.Value]" in captured.out
assert "console_on" in captured.out
def test_console_disabled(self, tmp_path, capsys):
d = _make_test_dumper(tmp_path, enable_output_console=False)
d.dump("console_off", torch.randn(3, 3))
assert "console_off" not in capsys.readouterr().out
_assert_files(_get_filenames(tmp_path), exist=["console_off"])
def test_capture_output_basic(self, tmp_path):
d = _make_test_dumper(tmp_path)
tensor = torch.randn(4, 4)
with d.capture_output() as captured:
d.dump("cap_basic", tensor)
assert "cap_basic" in captured
assert set(captured["cap_basic"].keys()) == {"value", "meta"}
assert torch.equal(captured["cap_basic"]["value"], tensor)
assert captured["cap_basic"]["meta"]["name"] == "cap_basic"
def test_capture_output_no_file(self, tmp_path):
d = _make_test_dumper(tmp_path)
with d.capture_output() as captured:
d.dump("cap_no_file", torch.randn(3, 3))
assert "cap_no_file" in captured
assert len(_get_filenames(tmp_path)) == 0
def test_capture_output_multiple(self, tmp_path):
d = _make_test_dumper(tmp_path)
with d.capture_output() as captured:
d.dump("first", torch.randn(2, 2))
d.dump("second", torch.randn(3, 3))
assert set(captured.keys()) == {"first", "second"}
assert captured["first"]["value"].shape == (2, 2)
assert captured["second"]["value"].shape == (3, 3)
def test_capture_output_value_cloned(self, tmp_path):
d = _make_test_dumper(tmp_path)
tensor = torch.zeros(3, 3)
with d.capture_output() as captured:
d.dump("clone_check", tensor)
tensor.fill_(999.0)
assert torch.equal(captured["clone_check"]["value"], torch.zeros(3, 3))
def test_capture_output_respects_filter(self, tmp_path):
d = _make_test_dumper(tmp_path, filter="^keep")
with d.capture_output() as captured:
d.dump("keep_this", torch.randn(3, 3))
d.dump("skip_this", torch.randn(3, 3))
assert "keep_this" in captured
assert "skip_this" not in captured
class TestDumpDictFormat:
"""Verify that dump files use the dict output format: {"value": ..., "meta": {...}}."""