diff --git a/PyTorchSimCodegen/codecache.py b/PyTorchSimCodegen/codecache.py index 73e3b2b1f..d3fe2c30b 100644 --- a/PyTorchSimCodegen/codecache.py +++ b/PyTorchSimCodegen/codecache.py @@ -131,9 +131,9 @@ def _product(tile, keep): def _mm_template(src_code, meta): - """(M, N, K, input bytes, output bytes) of an mm template kernel -- one program per output tile, its - blocks stated as constexprs -- else None.""" - if (meta.get("template_grid") is None or "bmm" in meta.get("kernel_name", "") + """(M, N, K, input bytes, output bytes) of an mm or bmm template kernel -- one program per output + tile (and batch), its blocks stated as constexprs -- else None.""" + if (meta.get("template_grid") is None or not all(r.search(src_code) for r in _TILE_RE.values())): return None dims = [_DIM_RE[k].search(src_code) for k in "MNK"] @@ -145,13 +145,14 @@ def _mm_template(src_code, meta): def _retile(src_code, meta, mnk, tile): - """The same mm kernel at block `tile` (M, N, K): its constexprs and its grid.""" + """The same mm or bmm kernel at block `tile` (M, N, K): its constexprs and its grid, the batch + axes of the grid kept.""" (m, n, k), (bm, bn, bk) = mnk, tile vals = {"BLOCK_M": bm, "BLOCK_N": bn, "BLOCK_K": bk, "EVEN_K": k % bk == 0} for key, v in vals.items(): src_code = _TILE_RE[key].sub(lambda mt, v=v: f"{mt.group(1)}{v}", src_code) meta = copy.deepcopy(meta) - meta["template_grid"] = [-(-m // bm) * -(-n // bn), 1, 1] + meta["template_grid"] = [-(-m // bm) * -(-n // bn)] + list(meta["template_grid"][1:]) return src_code, meta @@ -182,14 +183,17 @@ def _time_tile(src_code, meta, kernel_name, workdir, timeout): def _template_tiles(src_code, meta): - """Every mapped tile of an mm template kernel, best first, as (M, N, K) blocks; [] for - any other kernel.""" + """Every mapped tile of an mm template kernel this source can take, best first, as (M, N, K) + blocks; [] for any other kernel. A body rendered with EVEN_K loads K unmasked, so it takes only + a BLOCK_K that divides K.""" shape = _mm_template(src_code, meta) if shape is None: return [] from .inductor_patches import _gemm_tiles m, n, k, size, out_size = shape - return [(c.block_m, c.block_n, c.block_k) for c in _gemm_tiles(m, n, k, size, out_size)] + even = _TILE_RE["EVEN_K"].search(src_code).group(2) == "True" + return [(c.block_m, c.block_n, c.block_k) for c in _gemm_tiles(m, n, k, size, out_size) + if not even or k % c.block_k == 0] def _smaller_tiles(src_code, meta): diff --git a/PyTorchSimCodegen/compiler_bridge.py b/PyTorchSimCodegen/compiler_bridge.py index 67607e33f..e0fa37bdb 100644 --- a/PyTorchSimCodegen/compiler_bridge.py +++ b/PyTorchSimCodegen/compiler_bridge.py @@ -92,17 +92,13 @@ def target_path(): def compiler_env(): - """The environment for a compiler subprocess: this machine, no PYTHONPATH, and - no device backend autoload. - - TORCHSIM_COMPILE_TARGET NAMES THE WHOLE MACHINE, and used to name three of its seven - fields while the compiler supplied the rest from a shipped default. One - description, written from the YAML this process is running. - """ + """The environment for a compiler subprocess: this machine, no PYTHONPATH, and no device + backend autoload. TORCHSIM_COMPILE_TARGET is the target written from the TOGSim YAML this + process runs, whatever the toolchain's environment set it to: the YAML is the machine.""" env = dict(os.environ) env.pop("PYTHONPATH", None) env["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0" - env.setdefault("TORCHSIM_COMPILE_TARGET", target_path()) + env["TORCHSIM_COMPILE_TARGET"] = target_path() return env diff --git a/PyTorchSimCodegen/launch.py b/PyTorchSimCodegen/launch.py index 56916c154..be9b328b7 100644 --- a/PyTorchSimCodegen/launch.py +++ b/PyTorchSimCodegen/launch.py @@ -189,11 +189,16 @@ 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. Two callers must + 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. """ if lane_bytes is None: - lane_bytes = int(os.environ.get("TORCHSIM_COMPILE_SPAD_SIZE", str(64 * 1024)), 0) + from . import compiler_bridge + lane_bytes = compiler_bridge.machine()["spad_size"] budget = lane_bytes // 2 // _REDUCTION_LIVE_TILES block = 1 << (int(extent) - 1).bit_length() while block * elem_bytes > budget and block > 1: diff --git a/thirdparty/pytorchsim-triton-backend.json b/thirdparty/pytorchsim-triton-backend.json index f8ad8c50a..974d96d15 100644 --- a/thirdparty/pytorchsim-triton-backend.json +++ b/thirdparty/pytorchsim-triton-backend.json @@ -2,6 +2,6 @@ "description": "Pin for the compiler the codegen route lowers through: PyTorchSim-Triton-Backend, with its LLVM, Triton and the vcix-accelerator environment (RISC-V toolchain, pk, Spike, gem5) and the accelerator model. CI builds its base image FROM ghcr.io/psal-postech/pytorchsim-triton-backend:, the image that repository publishes for every commit on its main and develop, so ref must be such a commit. scripts/build_from_source.sh checks the same ref out for a local toolchain. The repository is private; its package grants this repository read access.", "pytorchsim_triton_backend": { "repository": "PSAL-POSTECH/PyTorchSim-Triton-Backend", - "ref": "17ec55a85955ba330c07189171b3bb2e01783f6f" + "ref": "65fb0585aec61464571146e58790555a0a49056c" } }