Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions pytorchsim/codegen/tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions tests/system/test_reduction_tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="/workspace/PyTorchSim"), "tests"))
Expand Down Expand Up @@ -104,10 +105,26 @@ 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()
test_kept_axis_is_the_unit_stride_axis_of_the_largest_read()
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()
Loading