Skip to content
Open
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
63 changes: 57 additions & 6 deletions ignition/preparation/device_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,47 @@ def replace(node, value):
return b"".join(parts).decode()


def _referenced_kernel_names(source):
"""Collect module identifiers and async_compile.triton kernel names in generated Inductor code."""
names = set()
for node in ast.walk(ast.parse(source)):
if isinstance(node, ast.Name):
names.add(node.id)
elif (isinstance(node, ast.Call) and ast.unparse(node.func) == "async_compile.triton"
and node.args and isinstance(node.args[0], ast.Constant)
and isinstance(node.args[0].value, str)):
names.add(node.args[0].value)
return names


def _source_kernel_keys(source):
from torch._inductor.async_compile import CompiledTritonKernels

keys = set()
for node in ast.walk(ast.parse(source)):
if (isinstance(node, ast.Call) and ast.unparse(node.func) == "async_compile.triton"
and len(node.args) > 1 and isinstance(node.args[1], ast.Constant)
and isinstance(node.args[1].value, str)):
keys.add(CompiledTritonKernels.key(node.args[1].value))
return keys


def _filter_static_autotuners(bundle, source, bound_keys):
# Epilogue-fusion benchmarking bundles template candidates that final code never names;
# only static autotuners still named in the generated module require an async_compile binding.
referenced_names = None
retained = []
for entry_kernel in bundle.static_autotuners:
if entry_kernel.cache_key in bound_keys:
retained.append(entry_kernel)
continue
if referenced_names is None:
referenced_names = _referenced_kernel_names(source)
if entry_kernel.kernel_name in referenced_names:
raise ValueError("compiled forward has unbound static Inductor kernels")
return retained


def _freeze_tuning_winners(bundle):
"""Preserve completed compile-time selections while rebuilding launchers.

Expand Down Expand Up @@ -138,17 +179,27 @@ def relocate_inductor(entry, original, *, freeze_tuning=False):
raise ValueError("device binding requires a compiled inference forward")
if graph.device_idxs != OrderedSet((original.index,)):
raise ValueError("compiled forward must use one CUDA device")
if freeze_tuning and graph._triton_bundle is not None:
_freeze_tuning_winners(graph._triton_bundle)
if not relocate:
if bundle := graph._triton_bundle:
retained = _filter_static_autotuners(bundle, graph.source_code, _source_kernel_keys(graph.source_code))
retained_bundle = replace(bundle, static_autotuners=retained)
_freeze_tuning_winners(retained_bundle)
graph._triton_bundle = retained_bundle
return
kernel_keys = {}
graph.source_code = _device_source(graph.source_code, original.index, target.index, kernel_keys)
graph.cache_key = code_hash(graph.source_code)
graph.device_idxs = OrderedSet((target.index,))
rebound_source = _device_source(graph.source_code, original.index, target.index, kernel_keys)
if bundle := graph._triton_bundle:
retained = _filter_static_autotuners(bundle, rebound_source, kernel_keys)
if freeze_tuning:
retained_bundle = replace(bundle, static_autotuners=retained)
_freeze_tuning_winners(retained_bundle)
retained = retained_bundle.static_autotuners
graph.source_code = rebound_source
graph.cache_key = code_hash(rebound_source)
graph.device_idxs = OrderedSet((target.index,))
if bundle:
autotuners = []
for entry_kernel in bundle.static_autotuners:
for entry_kernel in retained:
kernel = entry_kernel.kernel
kernel.device_props = kernel.device_props._replace(index=target.index)
kernel.triton_meta["device"] = target.index
Expand Down
11 changes: 7 additions & 4 deletions ignition/preparation/inductor_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import torch

from .device_binding import _device_source, _freeze_tuning_winners
from .device_binding import _device_source, _filter_static_autotuners, _freeze_tuning_winners


def _selected(tuner):
Expand Down Expand Up @@ -143,6 +143,7 @@ def register(self, bound):
{entry.cache_key: entry for entry in graph._triton_bundle.static_autotuners})
module = _kernel_globals(graph) if entries else {}
bindings = {}
discovered = []
for node in ast.walk(ast.parse(graph.source_code)):
if not (isinstance(node, ast.Assign) and isinstance(node.value, ast.Call)
and ast.unparse(node.value.func) == 'async_compile.triton'):
Expand All @@ -161,14 +162,16 @@ def register(self, bound):
f'bundled={cache_key in entries}, static_entries={len(entries)}')
identity = CompiledTritonKernels.key(_device_source(source, original, 0))
bindings[cache_key] = identity, entries[cache_key], tuner
discovered.append((identity, tuner))
# Epilogue-fusion benchmarking bundles template candidates that final code never names.
if graph._triton_bundle is not None:
_filter_static_autotuners(graph._triton_bundle, graph.source_code, bindings)
for identity, tuner in discovered:
instances = self.instances.setdefault(identity, {})
if id(tuner) not in instances:
instances[id(tuner)] = tuner
if identity in self.winners:
_install(tuner, self.winners[identity])
# Epilogue-fusion benchmarking bundles template candidates that final code never names.
if any(entries[key].kernel_name in graph.source_code for key in set(entries) - set(bindings)):
raise ValueError("compiled forward has unbound static Inductor kernels")
self.forwards[id(bound)] = backend, graph, bindings

@contextmanager
Expand Down
Loading