fix(matmul): budget the transient fifth tile in _gemm_tiled_sync's per-step working set#227
Merged
Merged
Conversation
…r-step working set _tile_workspace_bytes_per_elem budgeted four T*T tiles per k-step: acc, a_dev, b_dev, prod. But _gemm_tiled_sync reassigns a_dev, b_dev, and prod every iteration (a_dev = backend.to_device(...), etc.), and Python evaluates the right-hand side of an assignment -- allocating the NEW tile -- before the name rebinds and the OLD tile's reference drops. So at the moment any one of those three lines runs, the previous iteration's tile it's about to replace is still live alongside the freshly allocated one: a fifth T*T tile, momentarily, on top of the steady-state four. Budgeting only four under-counts true peak device usage by one operand-sized tile (roughly 20-25% depending on T), risking OOM at a vram_fraction that leaves little headroom -- exactly the tiled path's job to avoid. Verified the mechanism in isolation with a standalone CPython refcount trace before touching this file: steady state was 4 tile-sized objects, measured peak was 5, consistently, across dtype combinations. Budget-only change -- the blocked GEMM math (_tiles, the k-loop accumulation) is untouched.
c48c68a to
4afca0e
Compare
zeokin
approved these changes
Jul 14, 2026
zeokin
left a comment
Owner
There was a problem hiding this comment.
Budgets the transient 5th GEMM tile in _gemm_tiled_sync; fixes a real peak-VRAM undercount.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR kind
(Replaces this PR's original content -- a
nystrom-iterfeat idea that a real GPU scorecard showed doesn't clear the dominance gate at the regime tested. Repurposing the branch/PR for unrelated, verified content instead of abandoning it.)Summary
_tile_workspace_bytes_per_elembudgets four T×T tiles per k-step inmatmul/gemm.py's tiled path:acc,a_dev,b_dev,prod. But_gemm_tiled_syncreassignsa_dev,b_dev, andprodevery iteration (a_dev = backend.to_device(...), etc.), and Python evaluates the right-hand side of an assignment -- allocating the new tile -- before the name rebinds and the old tile's reference drops. So at the moment any one of those three lines runs, the previous iteration's tile it's about to replace is still live alongside the freshly allocated one: a fifth T×T tile, momentarily, on top of the steady-state four.Budgeting only four under-counts true peak device usage by one operand-sized tile (roughly 20-25% depending on T), risking OOM at a
vram_fractionthat leaves little headroom -- exactly the failure mode the tiled path exists to avoid.I verified the mechanism in isolation with a standalone CPython reference-counting trace before touching this file: steady state was 4 tile-sized objects, measured peak was consistently 5, across fp16/fp32/fp64 and accumulate_fp32 on/off.
Fix
_tile_workspace_bytes_per_elem:cfg.acc_dtype.itemsize + 3 * _tile_operand_bytes(cfg)→... + 4 * _tile_operand_bytes(cfg). Budget-only change -- the blocked GEMM math (_tiles, the k-loop accumulation) is untouched.Validation
uv run python -m strategy.smokeuv run --extra test python -m pytest tests/ strategy/tests/ eval/tests/ -q— 288 passed, 24 skippedtests/test_auto_tile_budget.py's two tests that hardcoded the old 3-operand-term formula (test_tile_workspace_counts_fp16_upcast,test_tile_workspace_counts_the_bmm_output_tile), and addedtest_tile_workspace_counts_the_transient_fifth_tileexplaining and asserting the new 4-operand-term model directly.No protected paths touched (
matmul/,tests/are Open per CONTRIBUTING.md).Checklist
Co-authored-byfooters for coding agents