Skip to content

Commit 3bd1f8a

Browse files
committed
more
1 parent 886b385 commit 3bd1f8a

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

test/test_memory_viz.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ def _extract_bootstrap(html):
4848
return json.loads(html[start:end])
4949

5050

51+
def _extract_named_bootstrap(html, script_id):
52+
marker = f'<script id="{script_id}" type="application/json">'
53+
start = html.index(marker) + len(marker)
54+
end = html.index("</script>", start)
55+
return json.loads(html[start:end])
56+
57+
5158
class TestExtractFrames:
5259
def test_filters_unwind_frames(self):
5360
frames = [
@@ -281,6 +288,65 @@ def test_uses_fixed_layout_instead_of_splitter(self, snapshot):
281288
assert "window.addEventListener('resize', applyPaneLayout);" in html
282289
assert 'id="splitter"' not in html
283290

291+
def test_uses_requested_devices_per_side(self):
292+
snapshot_left = {
293+
"device_traces": [
294+
[_make_event("alloc", 0x1000, 128, time_us=1, filename="left.py", name="left")]
295+
],
296+
"segments": [
297+
{
298+
"device": 0,
299+
"stream": 0,
300+
"address": 0x1000,
301+
"blocks": [
302+
{
303+
"state": "active_allocated",
304+
"size": 64,
305+
"addr": 0x1000,
306+
"frames": [{"filename": "left.py", "name": "seed", "line": 1}],
307+
}
308+
],
309+
}
310+
],
311+
"allocator_settings": {},
312+
}
313+
snapshot_right = {
314+
"device_traces": [
315+
[],
316+
[_make_event("alloc", 0x2000, 256, time_us=2, filename="right.py", name="right")],
317+
],
318+
"segments": [
319+
{
320+
"device": 1,
321+
"stream": 0,
322+
"address": 0x2000,
323+
"blocks": [
324+
{
325+
"state": "active_allocated",
326+
"size": 128,
327+
"addr": 0x2000,
328+
"frames": [{"filename": "right.py", "name": "seed", "line": 1}],
329+
}
330+
],
331+
}
332+
],
333+
"allocator_settings": {},
334+
}
335+
html = generate_memory_comparison_html(
336+
snapshot_left,
337+
snapshot_right,
338+
device_left=0,
339+
device_right=1,
340+
title_left="Rank 0",
341+
title_right="Rank 1",
342+
)
343+
bootstrap_left = _extract_named_bootstrap(html, "bootstrap-left")
344+
bootstrap_right = _extract_named_bootstrap(html, "bootstrap-right")
345+
assert bootstrap_left["meta"]["device"] == 0
346+
assert bootstrap_right["meta"]["device"] == 1
347+
assert bootstrap_left["meta"]["num_allocs"] == 2
348+
assert bootstrap_right["meta"]["num_allocs"] == 2
349+
284350

285351
class TestNeverFreedAllocations:
286352
def test_never_freed_end_at_max_ts(self):

transformer_nuggets/utils/compare_memory.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def main(
1919
"memory_comparison.html"
2020
),
2121
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,
2224
title_left: Annotated[str | None, typer.Option("--title-left")] = None,
2325
title_right: Annotated[str | None, typer.Option("--title-right")] = None,
2426
):
@@ -39,6 +41,8 @@ def main(
3941
snapshot_left,
4042
snapshot_right,
4143
device=device,
44+
device_left=device_left,
45+
device_right=device_right,
4246
title_left=title_left or left.stem,
4347
title_right=title_right or right.stem,
4448
)

transformer_nuggets/utils/memory_viz.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,27 @@ def generate_memory_comparison_html(
296296
snapshot_left: dict,
297297
snapshot_right: dict,
298298
device: int = 0,
299+
device_left: int | None = None,
300+
device_right: int | None = None,
299301
title_left: str = "Left",
300302
title_right: str = "Right",
301303
) -> str:
304+
if device_left is None:
305+
device_left = device
306+
if device_right is None:
307+
device_right = device
302308
doc_title = f"{title_left} vs {title_right}"
303309
return (
304310
_MEMORY_COMPARISON_TEMPLATE.replace("__DOCUMENT_TITLE__", html.escape(doc_title))
305311
.replace("__TITLE_LEFT__", html.escape(title_left))
306312
.replace("__TITLE_RIGHT__", html.escape(title_right))
307313
.replace(
308314
"__BOOTSTRAP_LEFT__",
309-
_json_for_html(_build_memory_viz_data(snapshot_left, device, title_left)),
315+
_json_for_html(_build_memory_viz_data(snapshot_left, device_left, title_left)),
310316
)
311317
.replace(
312318
"__BOOTSTRAP_RIGHT__",
313-
_json_for_html(_build_memory_viz_data(snapshot_right, device, title_right)),
319+
_json_for_html(_build_memory_viz_data(snapshot_right, device_right, title_right)),
314320
)
315321
)
316322

0 commit comments

Comments
 (0)