From 28ffd45332a5b22b673365af4d466c4c3c60f41b Mon Sep 17 00:00:00 2001 From: student-Jungmin Date: Sat, 24 Oct 2026 12:38:32 +0900 Subject: [PATCH] [Frontend] Block budgets are the machine's spad_budget, not half the scratchpad reduction_block_for divided a lane's scratchpad by 2 and gemm_tile_candidates took half of it, the same fact the compiler states as Target.spad_budget, spad_size over the machine's vpu_spad_buffers (2 when unsaid), and refuses a kernel over. Both now read the machine: reduction_block_for divides by CONFIG_MACHINE.spad_buffers, gemm_tile_candidates takes CONFIG_MACHINE.spad_budget per lane and times the lanes for the whole tile. No change on the shipped machines, which state no vpu_spad_buffers: 2 is what they get, and every block and tile is the one before. test_reduction_tiling's new case pins it: a machine of four buffers halves the reduction block and leaves fewer GEMM tiles, each among those of two; on the old code the block stays 8192 too wide. --- pytorchsim/codegen/tiling.py | 23 ++++++++--------------- tests/system/test_reduction_tiling.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/pytorchsim/codegen/tiling.py b/pytorchsim/codegen/tiling.py index 198276a2..d42ec530 100644 --- a/pytorchsim/codegen/tiling.py +++ b/pytorchsim/codegen/tiling.py @@ -102,18 +102,12 @@ def triton_max_numel(): def reduction_block_for(extent, elem_bytes=4, lane_bytes=None): - """The R0_BLOCK this backend pins for a reduction of `extent`. - - Cover the extent and no more, then shrink to the budget: half of one lane's - scratchpad, shared by the tiles a reduction keeps live. Two callers must - agree: this pins the block, inductor_patches asks if persistence matches. - Both see the machine's scratchpad when `lane_bytes` is left to this - function; a persistent reduction rendered for a bigger one than the target - has is a kernel the compiler refuses as over budget, whatever XBLOCK shrinks to. - """ + """The R0_BLOCK this backend pins for a reduction of `extent`: the extent covered, shrunk to a lane's + scratchpad over the machine's spad_buffers shared by the live tiles. inductor_patches asks the same + for persistence, and both read the machine's scratchpad unless given `lane_bytes`.""" if lane_bytes is None: lane_bytes = config.CONFIG_MACHINE.spad_size - budget = lane_bytes // 2 // _REDUCTION_LIVE_TILES + budget = lane_bytes // int(config.CONFIG_MACHINE.spad_buffers) // _REDUCTION_LIVE_TILES block = 1 << (int(extent) - 1).bit_length() while block * elem_bytes > budget and block > 1: block //= 2 @@ -276,13 +270,12 @@ def gemm_tiles(m, n, k, dtype_size, out_size=None): def gemm_tile_candidates(M, N, K, c_bytes, precision_bytes): - """Every (tile_M, tile_N, tile_K) whose A, B and C are blocks Triton accepts and fit half the - scratchpad, largest first. Each side is a lane multiple times a power of two (TTGIR's layouts + """Every (tile_M, tile_N, tile_K) whose A, B and C are blocks Triton accepts and fit the + machine's spad_budget, largest first. Each side is a lane multiple times a power of two (TTGIR's layouts need them), the tail masked.""" lanes = int(config.CONFIG_MACHINE.vector_lanes) - spad_per_lane = config.CONFIG_MACHINE.spad_size - max_spad = spad_per_lane * lanes // 2 - max_spad_per_lane = spad_per_lane // 2 + max_spad_per_lane = int(config.CONFIG_MACHINE.spad_budget) + max_spad = max_spad_per_lane * lanes max_numel = triton_max_numel() def per_lane(rows, cols): diff --git a/tests/system/test_reduction_tiling.py b/tests/system/test_reduction_tiling.py index 592d959b..c9ba0aaf 100644 --- a/tests/system/test_reduction_tiling.py +++ b/tests/system/test_reduction_tiling.py @@ -4,6 +4,7 @@ lanes and budgets it over all of them; any other reduction takes rows of its R0_BLOCK up to the budget.""" import os import sys +import types import torch sys.path.insert(0, os.path.join(os.environ.get("TORCHSIM_DIR", default="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/workspace/PyTorchSim"), "tests")) @@ -104,6 +105,21 @@ def test_few_rows_budget_the_reduced_axis_over_every_lane(): test_result("FewRowsLaneBlocks", torch.tensor(got), torch.tensor([w for _, w in cases])) +def test_the_tile_budget_is_the_machine_s_spad_budget(): + """The budget is a lane's scratchpad over the machine's spad_buffers, as the compiler's spad_budget: + four buffers halve the reduction block two give, and leave fewer GEMM tiles, each among two's.""" + from pytorchsim import config + real = config.CONFIG_MACHINE + four = types.SimpleNamespace(spad_size=real.spad_size, spad_buffers=4, vector_lanes=real.vector_lanes, + spad_budget=real.spad_size // 4) + two = tiling.reduction_block_for(1 << 20, 4, LANE_BYTES), set(tiling.gemm_tile_candidates(4096, 4096, 4096, 4, 2)) + config.CONFIG_MACHINE = four + try: + got = tiling.reduction_block_for(1 << 20, 4, LANE_BYTES), set(tiling.gemm_tile_candidates(4096, 4096, 4096, 4, 2)) + finally: + del config.CONFIG_MACHINE + test_result("SpadBudgetIsTheMachines", torch.tensor([got[0] * 2, int(got[1] < two[1])]), torch.tensor([two[0], 1])) + if __name__ == "__main__": test_gemv_blocks_follow_the_contiguous_kept_axis() test_lane_cover_and_budget_are_never_crossed() @@ -111,3 +127,4 @@ def test_few_rows_budget_the_reduced_axis_over_every_lane(): test_row_reduction_rows_fill_the_tile_budget() test_other_reductions_keep_todays_blocks() test_few_rows_budget_the_reduced_axis_over_every_lane() + test_the_tile_budget_is_the_machine_s_spad_budget()