Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions python/sglang/srt/debug_utils/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class _Dumper:
Then run the program:
`SGLANG_DUMPER_ENABLE=1 python ...`

Auto-cleanup old dumps before first write:
`SGLANG_DUMPER_CLEANUP_PREVIOUS=1 python ...`

Alternatively, disable at startup and enable via HTTP:
1. `python ...`
2. `curl -X POST http://localhost:40000/dumper -d '{"enable": true}'`
Expand All @@ -53,6 +56,7 @@ def __init__(
enable_model_grad: bool = True,
partial_name: Optional[str] = None,
enable_http_server: bool = True,
cleanup_previous: bool = False,
):
# Config
self._enable = enable
Expand All @@ -72,6 +76,7 @@ def __init__(
self._global_ctx = {}
self._override_enable = None
self._http_server_handled = not enable_http_server
self._pending_cleanup = cleanup_previous

@classmethod
def from_env(cls) -> "_Dumper":
Expand All @@ -90,6 +95,7 @@ def from_env(cls) -> "_Dumper":
enable_http_server=get_bool_env_var(
"SGLANG_ENABLE_DUMPER_HTTP_SERVER", "1"
),
cleanup_previous=get_bool_env_var("SGLANG_DUMPER_CLEANUP_PREVIOUS", "0"),
)

def on_forward_pass_start(self):
Expand Down Expand Up @@ -294,6 +300,10 @@ def _dump_single(
)

if self._enable_write_file and save:
if self._pending_cleanup:
self._pending_cleanup = False
_cleanup_old_dumps(self._base_dir)

path.parent.mkdir(parents=True, exist_ok=True)
output_data = {
"value": value.data if isinstance(value, torch.nn.Parameter) else value,
Expand Down Expand Up @@ -329,6 +339,19 @@ def _get_partial_name():
return object_list[0]


def _cleanup_old_dumps(base_dir: Path) -> None:
import shutil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the PEP 8 style guide, imports should be placed at the top of the file. Please move import shutil to the module-level imports for better code organization and readability.

References
  1. PEP 8 advises that imports should be at the top of the file, just after any module comments and docstrings, and before module globals and constants. This is for clarity and to make it easy to see what modules a file uses. (link)


if _get_rank() == 0:
for entry in base_dir.glob("sglang_dump_*"):
if entry.is_dir():
shutil.rmtree(entry)
print(f"[Dumper] Cleaned up {entry}")

if dist.is_initialized():
dist.barrier()


def _get_rank():
if dist.is_initialized():
return dist.get_rank()
Expand Down
24 changes: 24 additions & 0 deletions test/registered/debug_utils/test_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,5 +558,29 @@ def test_disable_model_value(self, 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a typo in the keyword argument. The _Dumper constructor expects cleanup_previous, but needs_cleanup is provided. This will cause a TypeError when running the test.

Suggested change
dumper = _make_test_dumper(tmp_path, needs_cleanup=True)
dumper = _make_test_dumper(tmp_path, cleanup_previous=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__]))
Loading