Skip to content

Commit 329e94b

Browse files
committed
Tweaks from usage
1 parent 3ae56f3 commit 329e94b

6 files changed

Lines changed: 408 additions & 189 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ transformer_nuggets/llama/data/*
66
plots/*
77
data/*
88

9+
# have fun kids!
10+
agent_space
11+
912
# Byte-compiled / optimized / DLL files
1013
__pycache__/
1114
*.py[cod]

AGENTS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Scratch Space
2+
3+
- Use `agent_space/` (git-ignored, at repo root) for temporary scripts, scratch files, and throwaway experiments.
4+
- Do not commit files from this directory.

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@AGENTS.md

test/test_memory_viz.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_is_cpython_c_frame(self):
9696

9797
class TestProcessSnapshot:
9898
def test_returns_correct_tuple_shape(self, snapshot):
99-
timeline, allocs, frames, stacks, max_ts = process_snapshot(snapshot)
99+
timeline, allocs, frames, stacks, max_ts, hwm_ts = process_snapshot(snapshot)
100100
assert len(timeline) > 0
101101
assert len(allocs) > 0
102102
assert len(frames) > 0
@@ -141,7 +141,7 @@ def test_stack_indices_valid(self, snapshot):
141141

142142
def test_empty_device_returns_empty(self, snapshot):
143143
result = process_snapshot(snapshot, device=99)
144-
assert result == ([], [], [], [], 0)
144+
assert result == ([], [], [], [], 0, 0)
145145

146146
def test_polygon_offsets_non_negative(self, snapshot):
147147
_, allocs, *_ = process_snapshot(snapshot)
@@ -206,7 +206,7 @@ def test_never_freed_end_at_max_ts(self):
206206
_make_event("alloc", 0x1000, 1024, time_us=1),
207207
_make_event("alloc", 0x2000, 2048, time_us=2),
208208
]
209-
_, allocs, _, _, max_ts = process_snapshot(_make_snapshot(events))
209+
_, allocs, _, _, max_ts, _ = process_snapshot(_make_snapshot(events))
210210
assert len(allocs) == 2
211211
for poly in allocs:
212212
assert poly["ts"][-1] == max_ts
@@ -217,7 +217,7 @@ def test_freed_alloc_ends_before_max_ts(self):
217217
_make_event("alloc", 0x2000, 2048, time_us=2),
218218
_make_event("free_completed", 0x1000, 1024, time_us=3),
219219
]
220-
_, allocs, _, _, max_ts = process_snapshot(_make_snapshot(events))
220+
_, allocs, _, _, max_ts, _ = process_snapshot(_make_snapshot(events))
221221
freed = [a for a in allocs if a["ts"][-1] < max_ts]
222222
alive = [a for a in allocs if a["ts"][-1] == max_ts]
223223
assert len(freed) == 1
@@ -226,7 +226,7 @@ def test_freed_alloc_ends_before_max_ts(self):
226226
assert alive[0]["s"] == 2048
227227

228228
def test_real_snapshot_has_both(self, snapshot):
229-
_, allocs, _, _, max_ts = process_snapshot(snapshot)
229+
_, allocs, _, _, max_ts, _ = process_snapshot(snapshot)
230230
freed = [a for a in allocs if a["ts"][-1] < max_ts]
231231
alive = [a for a in allocs if a["ts"][-1] == max_ts]
232232
assert len(freed) > 0
@@ -239,7 +239,7 @@ def test_identical_frames_share_stack_index(self):
239239
_make_event("alloc", 0x1000, 1024, filename="a.py", name="foo", line=10),
240240
_make_event("alloc", 0x2000, 2048, filename="a.py", name="foo", line=10),
241241
]
242-
_, allocs, frames, stacks, _ = process_snapshot(_make_snapshot(events))
242+
_, allocs, frames, stacks, *_ = process_snapshot(_make_snapshot(events))
243243
assert allocs[0]["si"] == allocs[1]["si"]
244244
assert len(stacks) == 1
245245

@@ -248,12 +248,12 @@ def test_different_frames_get_different_stacks(self):
248248
_make_event("alloc", 0x1000, 1024, filename="a.py", name="foo", line=10),
249249
_make_event("alloc", 0x2000, 2048, filename="b.py", name="bar", line=20),
250250
]
251-
_, allocs, _, stacks, _ = process_snapshot(_make_snapshot(events))
251+
_, allocs, _, stacks, *_ = process_snapshot(_make_snapshot(events))
252252
assert allocs[0]["si"] != allocs[1]["si"]
253253
assert len(stacks) == 2
254254

255255
def test_real_snapshot_deduplicates(self, snapshot):
256-
_, allocs, _, stacks, _ = process_snapshot(snapshot)
256+
_, allocs, _, stacks, *_ = process_snapshot(snapshot)
257257
used_stacks = {a["si"] for a in allocs}
258258
assert len(used_stacks) < len(allocs)
259259

@@ -265,7 +265,7 @@ def test_freeing_bottom_shifts_above_down(self):
265265
_make_event("alloc", 0x2000, 200, time_us=2, name="top"),
266266
_make_event("free_completed", 0x1000, 100, time_us=3),
267267
]
268-
_, allocs, _, _, _ = process_snapshot(_make_snapshot(events))
268+
_, allocs, *_ = process_snapshot(_make_snapshot(events))
269269
bottom = next(a for a in allocs if a["s"] == 100)
270270
top = next(a for a in allocs if a["s"] == 200)
271271
assert bottom["offsets"][0] == 0
@@ -279,7 +279,7 @@ def test_freeing_middle_shifts_only_above(self):
279279
_make_event("alloc", 0x3000, 300, time_us=3, name="c"),
280280
_make_event("free_completed", 0x2000, 200, time_us=4),
281281
]
282-
_, allocs, _, _, _ = process_snapshot(_make_snapshot(events))
282+
_, allocs, *_ = process_snapshot(_make_snapshot(events))
283283
a = next(p for p in allocs if p["s"] == 100)
284284
c = next(p for p in allocs if p["s"] == 300)
285285
assert a["offsets"][-1] == 0
@@ -293,7 +293,7 @@ def test_segment_events_dont_create_polys(self):
293293
_make_event("alloc", 0x1000, 1024, time_us=2),
294294
_make_event("segment_free", 0xA000, 4096, time_us=3),
295295
]
296-
timeline, allocs, _, _, _ = process_snapshot(_make_snapshot(events))
296+
timeline, allocs, *_ = process_snapshot(_make_snapshot(events))
297297
assert len(allocs) == 1
298298
assert allocs[0]["s"] == 1024
299299

@@ -303,21 +303,21 @@ def test_segment_events_affect_reserved(self):
303303
_make_event("alloc", 0x1000, 1024, time_us=2),
304304
_make_event("segment_free", 0xA000, 4096, time_us=3),
305305
]
306-
timeline, _, _, _, _ = process_snapshot(_make_snapshot(events))
306+
timeline, *_ = process_snapshot(_make_snapshot(events))
307307
reserved_values = [e["r"] for e in timeline]
308308
assert reserved_values[0] == 4096
309309
assert reserved_values[-1] == 0
310310

311311

312312
class TestTimelineConsistency:
313313
def test_max_at_time_matches_timeline(self, snapshot):
314-
timeline, _, _, _, _ = process_snapshot(snapshot)
314+
timeline, *_ = process_snapshot(snapshot)
315315
max_at_time = [e["a"] for e in timeline]
316316
assert len(max_at_time) == len(timeline)
317317
assert all(m >= 0 for m in max_at_time)
318318

319319
def test_hwm_monotonically_increases(self, snapshot):
320-
timeline, _, _, _, _ = process_snapshot(snapshot)
320+
timeline, *_ = process_snapshot(snapshot)
321321
hwm_values = [e["h"] for e in timeline]
322322
for i in range(1, len(hwm_values)):
323323
assert hwm_values[i] >= hwm_values[i - 1]
@@ -328,7 +328,7 @@ def test_allocated_matches_alloc_minus_free(self):
328328
_make_event("alloc", 0x2000, 200, time_us=2),
329329
_make_event("free_completed", 0x1000, 100, time_us=3),
330330
]
331-
timeline, _, _, _, _ = process_snapshot(_make_snapshot(events))
331+
timeline, *_ = process_snapshot(_make_snapshot(events))
332332
assert timeline[0]["a"] == 100
333333
assert timeline[1]["a"] == 300
334334
assert timeline[2]["a"] == 200
@@ -352,14 +352,14 @@ def test_all_freed_no_leaks(self):
352352
_make_event("free_completed", 0x1000, 100, time_us=3),
353353
_make_event("free_completed", 0x2000, 200, time_us=4),
354354
]
355-
_, allocs, _, _, max_ts = process_snapshot(_make_snapshot(events))
355+
_, allocs, _, _, max_ts, _ = process_snapshot(_make_snapshot(events))
356356
assert len(_find_leaks(allocs, max_ts)) == 0
357357

358358
def test_early_alloc_filtered_out(self):
359359
events = [
360360
_make_event("alloc", 0x1000, 100, time_us=1, name="model_param"),
361361
]
362-
_, allocs, _, _, max_ts = process_snapshot(_make_snapshot(events))
362+
_, allocs, _, _, max_ts, _ = process_snapshot(_make_snapshot(events))
363363
assert len(_find_leaks(allocs, max_ts)) == 0
364364

365365
def test_late_never_freed_is_candidate(self):
@@ -370,7 +370,7 @@ def test_late_never_freed_is_candidate(self):
370370
)
371371
events.append(_make_event("free_completed", 0x1000 + i * 0x100, 100, time_us=i + 100))
372372
events.append(_make_event("alloc", 0x9000, 512, time_us=500, name="leaked"))
373-
_, allocs, _, _, max_ts = process_snapshot(_make_snapshot(events))
373+
_, allocs, _, _, max_ts, _ = process_snapshot(_make_snapshot(events))
374374
candidates = _find_leaks(allocs, max_ts)
375375
assert len(candidates) == 1
376376
assert allocs[candidates[0]]["s"] == 512
@@ -386,7 +386,7 @@ def test_multiple_leaks_from_same_site_all_detected(self):
386386
events.append(
387387
_make_event("alloc", 0x9000 + i * 0x100, 200, time_us=60 + i, name="leaky_append")
388388
)
389-
_, allocs, _, _, max_ts = process_snapshot(_make_snapshot(events))
389+
_, allocs, _, _, max_ts, _ = process_snapshot(_make_snapshot(events))
390390
candidates = _find_leaks(allocs, max_ts)
391391
assert len(candidates) == 3
392392
for c in candidates:
@@ -402,7 +402,7 @@ def test_mixed_early_and_late_never_freed(self):
402402
_make_event("free_completed", 0x2000 + i * 0x100, 50, time_us=10 + i + 1)
403403
)
404404
events.append(_make_event("alloc", 0x9000, 300, time_us=500, name="leaked"))
405-
_, allocs, _, _, max_ts = process_snapshot(_make_snapshot(events))
405+
_, allocs, _, _, max_ts, _ = process_snapshot(_make_snapshot(events))
406406
candidates = _find_leaks(allocs, max_ts)
407407
assert len(candidates) == 1
408408
assert allocs[candidates[0]]["s"] == 300

transformer_nuggets/utils/benchmark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def save_memory_snapshot(file_path: Path | str, viz: Literal["torch", "d3"] = "t
299299
raise ValueError(f"{file_path} is a directory")
300300

301301
file_path.parent.mkdir(parents=True, exist_ok=True)
302-
torch.cuda.memory._record_memory_history()
302+
torch.cuda.memory._record_memory_history(stacks="all")
303303
try:
304304
yield
305305
finally:
@@ -397,7 +397,7 @@ def oom_observer(device, alloc, device_alloc, device_free):
397397
logging.error(f"Failed to save memory snapshot: {e}")
398398

399399
torch._C._cuda_attach_out_of_memory_observer(oom_observer) # type: ignore
400-
torch.cuda.memory._record_memory_history(max_entries=max_entries)
400+
torch.cuda.memory._record_memory_history(max_entries=max_entries, stacks="all")
401401

402402

403403
def get_process_rank():

0 commit comments

Comments
 (0)