Skip to content
Merged
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
17 changes: 8 additions & 9 deletions pytorchsim/frontend/tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
21 changes: 21 additions & 0 deletions tests/system/test_reduction_tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="/workspace/PyTorchSim"), "tests"))
Expand Down Expand Up @@ -103,10 +104,30 @@ 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()
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()