Skip to content

Commit 9856699

Browse files
committed
Add epilogue subtiling
stack-info: PR: #948, branch: PaulZhang12/stack/14
1 parent c314ed2 commit 9856699

15 files changed

+941
-74
lines changed

examples/matmul.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"range_unroll_factors": [0, 0],
3535
"range_num_stages": [0, 0],
3636
},
37+
allow_epilogue_subtiling=True,
38+
autotune_effort="quick",
3739
)
3840
def matmul(
3941
x: Tensor,

helion/_compiler/compile_environment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def __init__(self, device: torch.device, settings: Settings) -> None:
9999
self.device_load_count = (
100100
0 # Track number of loads in all device code for eviction policy tuning
101101
)
102+
self.device_store_count = 0 # Track number of stores for subtiling
102103

103104
def add_kernel_tensor_size(self, sizes: Sequence[int | torch.SymInt]) -> None:
104105
from .device_function import contains_only_block_size_symbols

helion/_compiler/device_function.py

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,9 @@ def tensor_descriptor_arg(
462462
self, fake_value: torch.Tensor, block_size: list[int | torch.SymInt]
463463
) -> TensorDescriptorArg:
464464
host_function = HostFunction.current()
465-
block_size_expr = ", ".join(map(self.literal_expr, block_size))
465+
block_size_expr = ", ".join(self.literal_expr(dim) for dim in block_size)
466466
key = (fake_value, block_size_expr)
467+
467468
if key not in self._tensor_descriptor_args:
468469
origin = host_function.tensor_to_origin[fake_value]
469470
desc_name = self.new_var(origin.suggest_var_name() + "_desc")
@@ -556,22 +557,6 @@ def _format_constexpr_value(self, value: object) -> str:
556557
if isinstance(value, (torch.SymInt, torch.SymFloat, torch.SymBool)):
557558
value = value._sympy_()
558559

559-
# Handle sympy expressions (sanitize by replacing triton_helpers functions)
560-
if isinstance(value, sympy.Expr):
561-
sanitized = value.replace( # pyright: ignore[reportAttributeAccessIssue]
562-
lambda node: isinstance(node, sympy.Function)
563-
and getattr(node.func, "__name__", "")
564-
== "triton_helpers.div_floor_integer",
565-
lambda node: sympy.floor(node.args[0] / node.args[1]), # pyright: ignore[reportAttributeAccessIssue]
566-
).replace( # pyright: ignore[reportAttributeAccessIssue]
567-
lambda node: isinstance(node, sympy.Function)
568-
and getattr(node.func, "__name__", "")
569-
== "triton_helpers.remainder_integer",
570-
lambda node: sympy.Mod(node.args[0], node.args[1]), # pyright: ignore[reportAttributeAccessIssue]
571-
)
572-
expr = cast("sympy.Expr", sanitized)
573-
return HostFunction.current().sympy_expr(expr)
574-
575560
return HostFunction.current().literal_expr(value)
576561

577562
def _tensor_property(
@@ -749,11 +734,19 @@ def current() -> DeviceFunction:
749734

750735

751736
class HelionTritonPrinter(TritonPrinter):
752-
"""Custom Triton printer that avoids wrapping float literals in tl.full().
753-
754-
Inductor's default TritonPrinter prints SymPy Float as a 0-D Triton value
755-
via tl.full([], <val>, tl.float64). We override this to emit the raw numeric
756-
literal, letting downstream type promotion and casts handle dtype.
737+
"""Custom Triton printer that does the following:
738+
739+
- Avoids wrapping float literals in tl.full().
740+
Inductor's default TritonPrinter prints SymPy Float as a 0-D Triton value
741+
via tl.full([], <val>, tl.float64). We override this to emit the raw numeric
742+
literal, letting downstream type promotion and casts handle dtype.
743+
744+
- Avoids triton_helpers.div_floor_integer(...) calls when both operands are
745+
provably non-negative integers. TritonPrinter by default converts
746+
floor(u1/2) to triton_helpers.div_floor_integer(...). We override this to
747+
emit u1 // 2 only when the numerator is known to be non-negative and the
748+
denominator is a positive integer, so that we keep helper calls for cases
749+
that rely on floor semantics with mixed signs.
757750
"""
758751

759752
def _print_Float(self, expr: sympy.Expr) -> str:
@@ -762,6 +755,53 @@ def _print_Float(self, expr: sympy.Expr) -> str:
762755
def _print_ToFloat(self, expr: sympy.Expr) -> str:
763756
return f"{expr} + 0.0"
764757

758+
def _is_nonnegative(self, expr: sympy.Expr) -> bool:
759+
if expr.is_nonnegative is True or expr.is_zero is True:
760+
return True
761+
if expr.is_positive is True:
762+
return True
763+
try:
764+
host_fn = HostFunction.current()
765+
except NoCurrentFunction:
766+
host_fn = None
767+
if host_fn is not None:
768+
origin_info = host_fn.expr_to_origin.get(expr)
769+
if origin_info and isinstance(
770+
origin_info.origin, (BlockSizeOrigin, TensorSizeOrigin)
771+
):
772+
return True
773+
if isinstance(expr, sympy.Symbol) and expr.name.startswith("_BLOCK_SIZE_"):
774+
return True
775+
if isinstance(expr, sympy.Number):
776+
return bool(expr >= 0)
777+
return False
778+
779+
def _format_trunc_div(self, lhs: sympy.Expr, rhs: sympy.Expr) -> str:
780+
lhs_str = self._print(lhs)
781+
rhs_str = self._print(rhs)
782+
if not (lhs.is_Integer or lhs.is_Symbol):
783+
lhs_str = f"({lhs_str})"
784+
if not (rhs.is_Integer or rhs.is_Symbol):
785+
rhs_str = f"({rhs_str})"
786+
return f"{lhs_str} // {rhs_str}"
787+
788+
def _print_floor(self, expr: sympy.Expr) -> str:
789+
inner = expr.args[0]
790+
numer, denom = inner.as_numer_denom()
791+
if (
792+
isinstance(denom, sympy.Integer)
793+
and denom > 1
794+
and self._is_nonnegative(numer)
795+
):
796+
return self._format_trunc_div(numer, denom)
797+
return super()._print_floor(expr)
798+
799+
def _print_FloorDiv(self, expr: sympy.Expr) -> str:
800+
lhs, rhs = expr.args
801+
if isinstance(rhs, sympy.Integer) and rhs > 0 and self._is_nonnegative(lhs):
802+
return self._format_trunc_div(lhs, rhs)
803+
return super()._print_FloorDiv(expr)
804+
765805

766806
def texpr(expr: sympy.Expr) -> str:
767807
return HelionTritonPrinter().doprint(expr)

helion/_compiler/device_ir.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from .type_propagation import _eval_binary
6464
from .type_propagation import _eval_compare
6565
from .type_propagation import _eval_unary
66+
from .utils import _allow_epilogue_subtiling
6667

6768
if TYPE_CHECKING:
6869
from collections.abc import Callable
@@ -1160,6 +1161,24 @@ def _register_load_store_tunables(
11601161
)
11611162

11621163

1164+
def _register_epilogue_subtile_tunable(store_count: int) -> None:
1165+
"""Register the epilogue subtile tunable for all device stores."""
1166+
if store_count == 0:
1167+
return
1168+
1169+
from ..autotuner.config_fragment import EnumFragment
1170+
from ..autotuner.config_fragment import ListOf
1171+
from ..autotuner.config_spec import VALID_EPILOGUE_SUBTILE_SIZES
1172+
1173+
env = CompileEnvironment.current()
1174+
# Register a tunable for epilogue subtile for all device stores
1175+
fragment = ListOf(
1176+
EnumFragment(choices=VALID_EPILOGUE_SUBTILE_SIZES), length=store_count
1177+
)
1178+
env.config_spec.epilogue_subtiling = fragment
1179+
env.device_store_count = store_count
1180+
1181+
11631182
def lower_to_device_ir(func: HostFunction) -> DeviceIR:
11641183
device_ir = DeviceIR()
11651184
with func, device_ir, compile_lock:
@@ -1182,13 +1201,23 @@ def lower_to_device_ir(func: HostFunction) -> DeviceIR:
11821201
# xyz not supported with shared program IDs, but persistent kernels are allowed
11831202
CompileEnvironment.current().config_spec.disallow_pid_type("xyz")
11841203

1204+
<<<<<<< HEAD
11851205
# Count all device loads and stores and register tunables
11861206
total_load_count, loads_without_eviction_policy, store_count = (
11871207
_count_device_loads_and_stores(device_ir)
11881208
)
11891209
_register_load_store_tunables(
11901210
total_load_count, loads_without_eviction_policy, store_count
11911211
)
1212+
=======
1213+
# Count all device loads and register eviction policy tunable
1214+
load_count, store_count = _count_device_loads_and_stores(device_ir)
1215+
_register_eviction_policy_tunable(load_count)
1216+
>>>>>>> bdf0793 (Add epilogue subtiling)
1217+
1218+
# Epilogue subtiling only for Blackwell
1219+
if _allow_epilogue_subtiling():
1220+
_register_epilogue_subtile_tunable(store_count)
11921221

11931222
return device_ir
11941223

0 commit comments

Comments
 (0)