Skip to content

fix(matmul): count the GEMM output tile in auto_tile's VRAM budget#116

Open
boskodev790 wants to merge 1 commit into
zeokin:mainfrom
boskodev790:fix/auto-tile-gemm-output-budget-95
Open

fix(matmul): count the GEMM output tile in auto_tile's VRAM budget#116
boskodev790 wants to merge 1 commit into
zeokin:mainfrom
boskodev790:fix/auto-tile-gemm-output-budget-95

Conversation

@boskodev790

Copy link
Copy Markdown

Lane

fix / bug — corrects repository behavior. CPU-safe validation only; no GPU scorecard (does not enter the GPU queue).

Problem

auto_tile sizes the tile edge T so the per-(i, j, k) working set fits vram_fraction of free VRAM, using _tile_workspace_bytes_per_elem. That model counted only three T×T tiles — the accumulator plus the two operand tiles:

return cfg.acc_dtype.itemsize + 2 * _tile_operand_bytes(cfg)

But _gemm_tiled_sync also holds a fourth T×T tensor live at the same time — the freshly allocated product from backend.matmul (torch.bmm cannot write its result into either operand), so at each k-step acc, a_dev, b_dev and prod coexist. The missing term is one operand-sized tile (the output, produced in the compute dtype).

Per element (modelled vs actual): fp32 12 vs 16; fp16+accumulate_fp32 12 vs 16; fp16 raw 6 vs 8. Since T ∝ 1/√(per_elem), undercounting by 4/3 makes auto_tile pick T ≈ √(16/12) ≈ 1.15× too large, so the real working set is ≈1.33× the intended vram_fraction. At a still-valid vram_fraction ≥ 0.75 the overshoot pushes the per-step working set past 100% of free VRAM and the tiled/out-of-core path OOMs — on exactly the huge-N runs tiling exists to enable. Same class of under-budget as #74 (operand upcast), on the still-missing GEMM output term.

Fix

Budget the output tile as well:

return cfg.acc_dtype.itemsize + 3 * _tile_operand_bytes(cfg)

Only matmul/gemm.py (the model + its docstrings) and tests/ change — no scorer/eval/docs/CI paths.

Tests

uv run python -m strategy.smoke
uv run --extra test python -m pytest tests/ strategy/tests/ eval/tests/ -q
# 78 passed, 23 skipped

Fixes #95

fix / bug lane — CPU-safe validation only, no GPU scorecard.

`_tile_workspace_bytes_per_elem` modeled the per-(i,j,k)-step working set as
`acc + 2 operand tiles`, but `_gemm_tiled_sync` also holds the freshly allocated
`prod = backend.matmul(a_dev, b_dev)` live at the same time (torch.bmm cannot
write into an operand), so four T×T tiles coexist: acc + A + B + prod. The
missing term is one operand-sized tile (the output, in the compute dtype).

Undercounting by 4/3 makes auto_tile pick T ~= sqrt(16/12) ≈ 1.15x too large, so
the real per-step working set is ~1.33x the intended vram_fraction of free
memory — at a valid vram_fraction >= 0.75 the tiled/out-of-core path can OOM on
exactly the huge-N runs tiling exists to enable. Same class as the operand-upcast
under-budget (zeokin#74), on the still-missing GEMM output term.

Fix: budget the output tile too — `acc_dtype.itemsize + 3 * _tile_operand_bytes`.
Updates the fp16 workspace assertions (12->16, 6->8) and adds a regression test
asserting the output tile is counted for fp32 / fp16+acc / fp16-raw.

Validation: uv run python -m strategy.smoke; uv run --extra test python -m
pytest tests/ strategy/tests/ eval/tests/ -q  (78 passed, 23 skipped).

Fixes zeokin#95
@github-actions github-actions Bot added area:matmul Exact engine and tile schedules (matmul/) area:tests Correctness gates and test suites (tests/) status:needs-review Awaiting maintainer review type:bug Something is incorrect or broken status:ready-non-gpu Fix/docs PR cleared non-GPU triage; maintainer review only labels Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:matmul Exact engine and tile schedules (matmul/) area:tests Correctness gates and test suites (tests/) status:needs-review Awaiting maintainer review status:ready-non-gpu Fix/docs PR cleared non-GPU triage; maintainer review only type:bug Something is incorrect or broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] auto_tile's per-step VRAM model omits the GEMM output tile, over-sizing T by ~15% and eroding the OOM margin

1 participant