From b5afa3848ed472da92aa5682770ee08442aece77 Mon Sep 17 00:00:00 2001 From: qazal <77887910+Qazalin@users.noreply.github.com> Date: Sun, 12 Oct 2025 14:32:46 +0300 Subject: [PATCH] viz: fix memory graph total nbytes (#12622) * viz: fix memory graph total nbytes * post increment * simple regression test * loop with markers + slightly off text baseline * cpu events clear --- test/unit/test_viz.py | 17 +++++++++++++++++ tinygrad/viz/js/index.js | 5 +++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/test/unit/test_viz.py b/test/unit/test_viz.py index fbfc37e76f..5ba00735eb 100644 --- a/test/unit/test_viz.py +++ b/test/unit/test_viz.py @@ -30,6 +30,7 @@ class BaseTestViz(unittest.TestCase): # clear the global context for lst in [tracked_keys, tracked_ctxs, active_rewrites, _name_cnt]: lst.clear() Buffer.profile_events.clear() + cpu_events.clear() self.tms = TRACK_MATCH_STATS.value self.profile = PROFILE.value TRACK_MATCH_STATS.value = 2 @@ -462,5 +463,21 @@ class TestVizMemoryLayout(BaseTestViz): self.assertEqual(ret["peak"], 2) self.assertEqual(len(ret["events"]), 4) + def test_free_last(self): + bufs = [] + for _ in range(3): + bufs.append(_alloc(1)) + profile_marker("alloc") + device = bufs[0].device + while bufs: + b = bufs.pop() + del b + profile_marker("free") + profile = load_profile(cpu_events+Buffer.profile_events) + ret = profile["layout"][f"{device} Memory"] + self.assertEqual(ret["peak"], 3) + self.assertEqual(len(ret["events"]), 6) + self.assertEqual(len(profile["markers"]), 6) + if __name__ == "__main__": unittest.main() diff --git a/tinygrad/viz/js/index.js b/tinygrad/viz/js/index.js index e120bac281..c5598fcd7f 100644 --- a/tinygrad/viz/js/index.js +++ b/tinygrad/viz/js/index.js @@ -257,8 +257,8 @@ async function renderProfiler() { x += 1; y += nbytes; valueMap.set(ts, y); } else { const free = buf_shapes.get(key); - timestamps.push(ts); - x += 1; y -= free.nbytes; valueMap.set(ts, y); + timestamps.push(ts); valueMap.set(ts, y); + x += 1; y -= free.nbytes; free.x.push(x); free.y.push(free.y.at(-1)); temp.delete(key); @@ -401,6 +401,7 @@ async function renderProfiler() { } } // draw markers + ctx.textBaseline = "top"; for (const m of markers) { const x = xscale(m.ts); drawLine(ctx, [x, x], [0, canvas.clientHeight], { color:m.color });