diff --git a/pytorchsim/frontend/tiling.py b/pytorchsim/frontend/tiling.py index d31cda8f..d152d30e 100644 --- a/pytorchsim/frontend/tiling.py +++ b/pytorchsim/frontend/tiling.py @@ -26,12 +26,12 @@ def triton_max_numel(): def reduction_block_for(extent, elem_bytes=4, lane_bytes=None): - """The R0_BLOCK pinned for a reduction of `extent`: the extent covered and no more, shrunk to half - of one lane's scratchpad (the machine's, when `lane_bytes` is left out), shared by the live tiles; - the pinned block and NPUChoices' persistence answer both come from here.""" + """The R0_BLOCK pinned for a reduction of `extent`: the extent covered and no more, shrunk to one lane's + scratchpad (the machine's, when `lane_bytes` is left out) over the machine's spad_buffers, shared by the + live tiles; the pinned block and NPUChoices' persistence answer both come from here.""" 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 @@ -174,13 +174,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 f53ab471..092170f5 100644 --- a/tests/system/test_reduction_tiling.py +++ b/tests/system/test_reduction_tiling.py @@ -3,6 +3,7 @@ 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")) @@ -103,6 +104,25 @@ def test_few_rows_budget_the_reduced_axis_over_every_lane(): check_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) + budgets = lambda: (tiling.reduction_block_for(1 << 20, 4, LANE_BYTES), + set(tiling.gemm_tile_candidates(4096, 4096, 4096, 4, 2))) + two = budgets() + config.CONFIG_MACHINE = four + try: + got = budgets() + finally: + del config.CONFIG_MACHINE + check_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() @@ -110,3 +130,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()