Skip to content

Commit fe44787

Browse files
committed
change util name
1 parent 3bd1f8a commit fe44787

5 files changed

Lines changed: 61 additions & 53 deletions

File tree

examples/memory_viz_compare_transformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def main(
8181
compare_cmd = [
8282
sys.executable,
8383
"-m",
84-
"transformer_nuggets.utils.compare_memory",
84+
"transformer_nuggets.utils.merge_memory",
8585
str(left_output_path),
8686
str(right_output_path),
8787
"-o",

examples/memory_viz_transformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
python examples/memory_viz_transformer.py --batch_size 2 --seq_len 1024
1010
1111
# Then visualize:
12-
compare-memory data/snapshot_a.pickle data/snapshot_b.pickle -o comparison.html
12+
merge-memory data/snapshot_a.pickle data/snapshot_b.pickle -o merged_memory.html
1313
"""
1414

1515
import torch

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ llama = [
5252

5353
[project.scripts]
5454
merge-traces = "transformer_nuggets.utils.merge_traces:app"
55+
merge-memory = "transformer_nuggets.utils.merge_memory:app"
5556
compare-memory = "transformer_nuggets.utils.compare_memory:app"
5657

5758
# ---------- TOOL CONFIGURATIONS ------------

transformer_nuggets/utils/compare_memory.py

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,6 @@
1-
"""Compare two CUDA memory snapshots side-by-side in a single interactive HTML page."""
1+
"""Backward-compatible alias for the `merge-memory` CLI."""
22

3-
from __future__ import annotations
4-
5-
import pickle
6-
from pathlib import Path
7-
from typing import Annotated
8-
9-
import typer
10-
11-
app = typer.Typer(help="Compare two memory snapshots side-by-side.")
12-
13-
14-
@app.command()
15-
def main(
16-
left: Annotated[Path, typer.Argument(help="Left snapshot pickle file.")],
17-
right: Annotated[Path, typer.Argument(help="Right snapshot pickle file.")],
18-
output: Annotated[Path, typer.Option("-o", "--output", help="Output HTML path.")] = Path(
19-
"memory_comparison.html"
20-
),
21-
device: Annotated[int, typer.Option("--device", help="CUDA device index.")] = 0,
22-
device_left: Annotated[int | None, typer.Option("--device-left")] = None,
23-
device_right: Annotated[int | None, typer.Option("--device-right")] = None,
24-
title_left: Annotated[str | None, typer.Option("--title-left")] = None,
25-
title_right: Annotated[str | None, typer.Option("--title-right")] = None,
26-
):
27-
"""Generate a side-by-side memory comparison HTML from two snapshot pickles."""
28-
for p in (left, right):
29-
if not p.exists():
30-
typer.echo(f"Error: {p} not found", err=True)
31-
raise typer.Exit(1)
32-
33-
from transformer_nuggets.utils.memory_viz import generate_memory_comparison_html
34-
35-
with open(left, "rb") as f:
36-
snapshot_left = pickle.load(f)
37-
with open(right, "rb") as f:
38-
snapshot_right = pickle.load(f)
39-
40-
html = generate_memory_comparison_html(
41-
snapshot_left,
42-
snapshot_right,
43-
device=device,
44-
device_left=device_left,
45-
device_right=device_right,
46-
title_left=title_left or left.stem,
47-
title_right=title_right or right.stem,
48-
)
49-
50-
output.parent.mkdir(parents=True, exist_ok=True)
51-
output.write_text(html)
52-
typer.echo(f"Wrote comparison to {output}")
3+
from transformer_nuggets.utils.merge_memory import app, main
534

545

556
if __name__ == "__main__":
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Merge two CUDA memory snapshots into a single side-by-side interactive HTML page."""
2+
3+
from __future__ import annotations
4+
5+
import pickle
6+
from pathlib import Path
7+
from typing import Annotated
8+
9+
import typer
10+
11+
app = typer.Typer(help="Merge two memory snapshots into one side-by-side HTML page.")
12+
13+
14+
@app.command()
15+
def main(
16+
left: Annotated[Path, typer.Argument(help="Left snapshot pickle file.")],
17+
right: Annotated[Path, typer.Argument(help="Right snapshot pickle file.")],
18+
output: Annotated[Path, typer.Option("-o", "--output", help="Output HTML path.")] = Path(
19+
"merged_memory.html"
20+
),
21+
device: Annotated[int, typer.Option("--device", help="CUDA device index for both sides.")] = 0,
22+
device_left: Annotated[int | None, typer.Option("--device-left")] = None,
23+
device_right: Annotated[int | None, typer.Option("--device-right")] = None,
24+
title_left: Annotated[str | None, typer.Option("--title-left")] = None,
25+
title_right: Annotated[str | None, typer.Option("--title-right")] = None,
26+
):
27+
"""Merge two memory snapshot pickles into one side-by-side interactive HTML page."""
28+
for path in (left, right):
29+
if not path.exists():
30+
typer.echo(f"Error: {path} not found", err=True)
31+
raise typer.Exit(1)
32+
33+
from transformer_nuggets.utils.memory_viz import generate_memory_comparison_html
34+
35+
with open(left, "rb") as f:
36+
snapshot_left = pickle.load(f)
37+
with open(right, "rb") as f:
38+
snapshot_right = pickle.load(f)
39+
40+
html = generate_memory_comparison_html(
41+
snapshot_left,
42+
snapshot_right,
43+
device=device,
44+
device_left=device_left,
45+
device_right=device_right,
46+
title_left=title_left or left.stem,
47+
title_right=title_right or right.stem,
48+
)
49+
50+
output.parent.mkdir(parents=True, exist_ok=True)
51+
output.write_text(html)
52+
typer.echo(f"Merged memory snapshots -> {output}")
53+
54+
55+
if __name__ == "__main__":
56+
app()

0 commit comments

Comments
 (0)