-
Notifications
You must be signed in to change notification settings - Fork 551
[Autotune] Add pipeline, grouped compilation, and multi-GPU benchmark support #2159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wazrrr
wants to merge
8
commits into
tile-ai:main
Choose a base branch
from
Wazrrr:pr-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b55ec6a
[autotune] add pipeline, grouped compilation and multi-gpu bench feat…
Wazrrr cb34fb0
[autotune] update the main entry of example_gemm_autotune.
Wazrrr f3bda82
[autotune] pre-commit format adjustment.
Wazrrr 45d92e7
[autotune] fix the benchmark timeout detection issue.
Wazrrr 8d4c0fc
[autotune] precommit
Wazrrr cd35151
fix the precommit issue for tuner.py.
Wazrrr a2dc696
[WIP] fixing CI testing.
Wazrrr 2f5bcf3
[fix] fix num of warmup issue.
Wazrrr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| """Grouped compilation helpers for autotuner. | ||
|
|
||
| This module isolates backend-aware grouped compilation logic from AutoTuner.run | ||
| so tuner.py can stay focused on orchestration. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Callable | ||
|
|
||
| from tilelang import tvm | ||
| from tvm.tir import PrimFunc | ||
|
|
||
| from tilelang.autotuner.param import CompileArgs | ||
| from tilelang.engine.lower import lower_to_host_device_ir, device_codegen, host_codegen | ||
| from tilelang.engine.param import CompiledArtifact | ||
| from tilelang.jit.adapter import TVMFFIKernelAdapter | ||
| from tilelang.jit.kernel import JITKernel | ||
| from tilelang.transform import PassConfigKey | ||
|
|
||
| CompileUnitResult = tuple[int, dict[str, Any], JITKernel | None, Exception | None] | ||
|
|
||
|
|
||
| def compile_grouped_unit_tvm_ffi( | ||
| unit_items: list[tuple[int, dict[str, Any]]], | ||
| compile_args: CompileArgs, | ||
| elaborate_func: Callable[..., PrimFunc], | ||
| ) -> list[CompileUnitResult]: | ||
| """Compile one grouped unit for CUDA+tvm_ffi backend. | ||
|
|
||
| Flow: | ||
| 1. Elaborate each config into a PrimFunc. | ||
| 2. Lower each PrimFunc into host/device IR modules. | ||
| 3. Merge all device IR into one IRModule and compile device code once. | ||
| 4. Build host runtime module per config and import shared device module. | ||
| 5. Construct per-config JITKernel objects that share the grouped device module. | ||
| """ | ||
|
|
||
| pass_configs = dict(compile_args.pass_configs) if compile_args.pass_configs else {} | ||
| pass_instruments = [] | ||
| if pass_configs.get(PassConfigKey.TL_ENABLE_DUMP_IR): | ||
| dump_ir_path = pass_configs.get(PassConfigKey.TL_DUMP_IR_DIR, "./dump_ir") | ||
| pass_instruments.append(tvm.ir.instrument.DumpIR(dump_dir=dump_ir_path)) | ||
|
|
||
| unit_results: list[CompileUnitResult] = [] | ||
| lowered_items: list[dict[str, Any]] = [] | ||
|
|
||
| for idx, config_arg in unit_items: | ||
| try: | ||
| program = elaborate_func(**config_arg) | ||
| original_symbol = str(program.attrs["global_symbol"]) | ||
| unique_symbol = f"{original_symbol}_gc_{idx}" | ||
| program = program.with_attr("global_symbol", unique_symbol) | ||
|
|
||
| with tvm.transform.PassContext(opt_level=3, config=pass_configs, instruments=pass_instruments), compile_args.target: | ||
| host_mod, device_mod, params, normalized_target, normalized_target_host = lower_to_host_device_ir( | ||
| program, | ||
| target=compile_args.target, | ||
| target_host=compile_args.target_host, | ||
| ) | ||
|
|
||
| lowered_items.append( | ||
| { | ||
| "idx": idx, | ||
| "config_arg": config_arg, | ||
| "program": program, | ||
| "host_mod": host_mod, | ||
| "device_mod": device_mod, | ||
| "params": params, | ||
| "target": normalized_target, | ||
| "target_host": normalized_target_host, | ||
| } | ||
| ) | ||
| except Exception as e: | ||
| unit_results.append((idx, config_arg, None, e)) | ||
|
|
||
| if not lowered_items: | ||
| return unit_results | ||
|
|
||
| try: | ||
| merged_funcs: dict[Any, Any] = {} | ||
| merged_attrs = None | ||
| merged_names: set[str] = set() | ||
| for item in lowered_items: | ||
| device_mod = item["device_mod"] | ||
| if merged_attrs is None: | ||
| merged_attrs = device_mod.attrs | ||
| for global_var, func in device_mod.functions.items(): | ||
| name_hint = getattr(global_var, "name_hint", str(global_var)) | ||
| if name_hint in merged_names: | ||
| raise RuntimeError( | ||
| f"Duplicate device global symbol '{name_hint}' during grouped compilation (config index={item['idx']})." | ||
| ) | ||
| merged_names.add(name_hint) | ||
| merged_funcs[global_var] = func | ||
| merged_device_mod = tvm.IRModule(merged_funcs, attrs=merged_attrs) | ||
|
|
||
| reference_target = lowered_items[0]["target"] | ||
| with tvm.transform.PassContext(opt_level=3, config=pass_configs, instruments=pass_instruments), reference_target: | ||
| grouped_device_rt_mod = device_codegen(merged_device_mod, reference_target) | ||
|
|
||
| grouped_kernel_source = grouped_device_rt_mod.inspect_source() | ||
|
|
||
| for item in lowered_items: | ||
| idx = item["idx"] | ||
| config_arg = item["config_arg"] | ||
| try: | ||
| with tvm.transform.PassContext(opt_level=3, config=pass_configs, instruments=pass_instruments), item["target"]: | ||
| grouped_host_rt_mod = host_codegen(item["host_mod"], item["target_host"], target=item["target"]) | ||
|
|
||
| grouped_host_rt_mod.import_module(grouped_device_rt_mod) | ||
|
|
||
| artifact = CompiledArtifact( | ||
| host_mod=grouped_host_rt_mod, | ||
| device_mod=item["device_mod"], | ||
| params=item["params"], | ||
| kernel_source=grouped_kernel_source, | ||
| rt_mod=grouped_host_rt_mod, | ||
| ) | ||
|
|
||
| adapter = TVMFFIKernelAdapter( | ||
| params=artifact.params, | ||
| result_idx=compile_args.out_idx, | ||
| target=compile_args.target, | ||
| func_or_mod=item["program"], | ||
| host_mod=artifact.host_mod, | ||
| device_mod=artifact.device_mod, | ||
| rt_mod=artifact.rt_mod, | ||
| device_kernel_source=artifact.kernel_source, | ||
| verbose=compile_args.verbose, | ||
| pass_configs=pass_configs, | ||
| ) | ||
|
|
||
| jit_kernel = JITKernel( | ||
| func=item["program"], | ||
| out_idx=compile_args.out_idx, | ||
| execution_backend=compile_args.execution_backend, | ||
| target=compile_args.target, | ||
| target_host=compile_args.target_host, | ||
| verbose=compile_args.verbose, | ||
| pass_configs=pass_configs, | ||
| from_database=True, | ||
| ) | ||
| jit_kernel.artifact = artifact | ||
| jit_kernel.adapter = adapter | ||
| jit_kernel.torch_function = adapter.func | ||
|
|
||
| unit_results.append((idx, config_arg, jit_kernel, None)) | ||
| except Exception as e: | ||
| unit_results.append((idx, config_arg, None, e)) | ||
| except Exception as e: | ||
| for item in lowered_items: | ||
| unit_results.append((item["idx"], item["config_arg"], None, e)) | ||
|
|
||
| return unit_results | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defensive access to
global_symbolattribute.program.attrs["global_symbol"]will raise ifattrsisNoneor missing the key. Although tilelang elaborated PrimFuncs typically carry it, the failure here aborts the whole group rather than just this config — guarding it ensures the per-config error path captures it cleanly.🛡️ Suggested defensive lookup
🤖 Prompt for AI Agents