Repository navigation
[AOTI] Support AOTI lazy autotune with dual-wrapper - #184735
desertfire wants to merge 13 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/184735
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 1 Unrelated FailureAs of commit f28a6b1 with merge base dc833a2 ( NEW FAILURE - The following job has failed:
BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude review this PR |
|
Claude finished @desertfire's task in 2m 30s —— View job Reviewing PR #184735
Review SummaryThis is a well-structured PR that replaces the old two-pass AOTI codegen (compile → run → reset → recompile) with a single-pass dual-wrapper approach using Architecture FeedbackStrengths:
Issues & Suggestions1.
2. Assertion without clear error recovery in At assert params is not None, (
f"CudaKernelParamCache not populated for {kernel_name} "
"after first-pass execution"
)This is fine for a development assert, but since this runs after the JIT variant has already been compiled and executed, if the kernel fails to autotune (e.g., due to OOM or a runtime error), the assertion will fire with no additional diagnostic context. Consider catching and wrapping with the actual error from the JIT execution if possible. 3. At 4. The At if V.graph.aot_mode:
if V.graph.is_dual_wrapper_mode:
result.writeline("} // inductor_entry_impl")
else:
entry = "_const_run_impl" if V.graph.is_const_graph else "run_impl"
result.writeline(f"}} // {self.aoti_model_class_name}::{entry}")
else:
result.writeline("} // inductor_entry_impl")The first and third branches emit the same string. Could be: if V.graph.aot_mode and not V.graph.is_dual_wrapper_mode:
entry = "_const_run_impl" if V.graph.is_const_graph else "run_impl"
result.writeline(f"}} // {self.aoti_model_class_name}::{entry}")
else:
result.writeline("} // inductor_entry_impl")5. At xblocks = [config_dict.get("XBLOCK", 128)]
yblocks = [config_dict.get("YBLOCK", 1)]These defaults (128, 1, 1, 1) are duplicated from the runtime. If those defaults ever change in 6. Unused import removal of The diff removes 7. Test skip pattern repetition (nit) The 6 def requires_autotune_at_compile_time(fn):
@functools.wraps(fn)
def wrapper(self, *args, **kwargs):
if config.triton.autotune_at_compile_time is False:
raise unittest.SkipTest("requires autotune_at_compile_time=True")
return fn(self, *args, **kwargs)
return wrapper8. At writer = (
writer if writer is not None else self
) # pyrefly: ignore [bad-assignment]These were added to suppress type checker warnings for the existing code pattern. This is fine, but worth a follow-up to properly type the Security & Correctness
OverallThis is a solid architectural improvement. The dual-wrapper approach eliminates the expensive second codegen pass and makes the lazy-autotune path more maintainable. The test coverage via |
|
@claude review again |
|
Claude finished @desertfire's task in 3m 40s —— View job Reviewing PR #184735
Review SummaryThis PR replaces the old two-pass AOTI codegen (compile → run → reset → recompile) with a single-pass dual-wrapper architecture using Since the last review, notable improvements include:
Issues1. Missing spaces in error messages (bug) All three
"When autotune_at_compile_time is False, AOTInductor generates"
"both JIT code and AOT code. The JIT code failed to run."This produces: Same at 2.
def generate_before_suffix(self, result):
if V.graph.aot_mode:
entry = "_const_run_impl" if V.graph.is_const_graph else "run_impl"
result.writeline(f"}} // {self.aoti_model_class_name}::{entry}")
else:
result.writeline("} // inductor_entry_impl")The comment says "In dual-wrapper-mode if V.graph.aot_mode and not V.graph.is_dual_wrapper_mode:
entry = "_const_run_impl" if V.graph.is_const_graph else "run_impl"
result.writeline(f"}} // {self.aoti_model_class_name}::{entry}")
else:
result.writeline("} // inductor_entry_impl")3.
@dataclasses.dataclass
class DualWrapperCodeLine(WrapperLine):
jit_code: IndentedBuffer
aot_code: IndentedBuffer
def codegen(self, code: IndentedBuffer) -> None:
code.splice_jit(self.jit_code)
code.splice_aot(self.aot_code)This is used at line 2951: Observations (non-blocking)4.
if V.graph.aot_mode:
self._write_aoti_entry_point_signature()
self._write_jit_entry_point_signature()In pure JIT mode ( 5.
6.
{device_ptr_type} {var} = allocate_scratch_tensor<{device_ptr_type}>(
{var}_numel, {dtype_str}, {device_type}, device_idx_, {var}_tensor);This references 7. The
Security & Correctness
OverallSolid improvement over the previous review iteration. The missing spaces in error messages (issue #1) are the only actual bug. The close-brace comment mismatch (issue #2) is cosmetic but worth fixing. The rest is well-structured. |
|
@claude review again |
|
Claude finished @desertfire's task in 2m 32s —— View job Reviewing PR #184735
Review SummaryThis is the third review of this PR. The architecture remains solid — a single codegen pass using Issues1. Missing spaces in error messages (bug — still present from last review)
raise RuntimeError(
"When autotune_at_compile_time is False, AOTInductor generates"
"both JIT code and AOT code. They are expected to have exactly"
f"the same kernels. However, AOT code contains kernels, {kernel_names},"
"that is not in the JIT code."
)Implicit string concatenation produces 2.
if self.last_seen_device_guard_index is None:
code.writeline_aot(
f"{V.graph.device_ops.cpp_aoti_stream_guard()} stream_guard(stream, this->device_idx_);"
)
code.writeline_jit(
f"{V.graph.device_ops.cpp_aoti_device_guard()} device_guard({self.device_idx});"
)In pure-AOTI mode (no dual-wrapper), However, there's a subtle behavior change from the old code: previously, in pure JIT mode ( 3.
def _write_input_preamble(self):
jit_num_args = len(V.graph.graph_inputs) + len(V.graph.constants)
if not V.graph.is_const_graph:
aoti_num_args = len(V.graph.graph_inputs)
self.prefix.splice_aot(...)
# release GIL ...
self.prefix.splice_jit("py::gil_scoped_release_simple release;")
self.prefix.splice_jit(
f"auto inputs = steal_from_raw_handles_to_raii_handles(input_handles, {jit_num_args});"
)For a const_graph, the AOTI side skips 4.
def generate_before_suffix(self, result):
if V.graph.aot_mode and not V.graph.is_dual_wrapper_mode:
entry = "_const_run_impl" if V.graph.is_const_graph else "run_impl"
result.writeline(f"}} // {self.aoti_model_class_name}::{entry}")
else:
result.writeline("} // inductor_entry_impl")For a const_graph in dual-wrapper-mode, this emits 5. Potential issue with
jit_call_args = [*call_args, jit_device_idx, stream]
aot_call_args = [*call_args, aoti_device_idx, "stream"]Here 6. Fallback kernel dual-wrapper code captures into
jit_code = DualIndentedBuffer(initial_indent=self.wrapper_call._indent)
with (
self._target_buf("wrapper_call", jit_code),
self.set_writeline(jit_code, jit_code.writeline_jit),
):
self._generate_fallback_kernel_jit(...)
aot_code = DualIndentedBuffer(initial_indent=self.wrapper_call._indent)
with (
self._target_buf("wrapper_call", aot_code),
self.set_writeline(aot_code, aot_code.writeline_aot),
):
self.generate_fallback_kernel_with_runtime_lookup_aot(...)
self.writeline(DualWrapperCodeLine(jit_code, aot_code))The However, there's an interesting asymmetry: Non-blocking observations7.
if '"""' in source:
return f"{name} = {source!r}\n"
return f'{name} = (\nr"""\n{source}"""\n)\n'If the source contains 8. In 9. The The old code had: if not V.graph.is_const_graph:
if V.graph.aot_mode: ...
else: ...The new code always enters OverallThe PR is in good shape. The only actionable bug is the missing spaces in the error message at |
| @@ -561,7 +582,7 @@ def write_prefix(self): | |||
| """ | |||
| ) | |||
| if V.graph.aot_mode: | |||
There was a problem hiding this comment.
do we still need the if V.graph.aot_mode: check with writeline_aot?
There was a problem hiding this comment.
You are right to question. No, we don't need it.
There was a problem hiding this comment.
should we remove the check?
|
Starting merge as part of PR stack under #184736 |
|
Starting merge as part of PR stack under #184736 |
…184736) Emit cond and while_loop as DualWrapperCodeLine entries so the dual-wrapper codegen can route the predicate-guarded body to the AOTI side while running the JIT first pass through both branches (cond) or both subgraphs once (while_loop) for lazy autotune. Introduce _preserve_codegen_state and _preserve_device_guard_state so wrapper bookkeeping is restored between the JIT and AOTI passes over the same subgraphs. Update test_cond_with_replace_view_ops to use aoti_compile_and_package, and drop the cond/while_loop skips from AOTInductorTestDualWrapper now that the dual-wrapper path supports them. Authored with Codex. Pull Request resolved: #184736 Approved by: https://github.com/yushangdi ghstack dependencies: #184735
Switch the AOTI lazy-autotune path to emit separate JIT and AOTI C++ wrapper bodies in one codegen pass. The JIT body is compiled and run with real inputs to populate lazy Triton kernel metadata, then the generated AOTI source includes the captured kernel config for packaging. Add test_aot_inductor variant that runs with triton.autotune_at_compile_time=False so this path is exercised. torch.cond and torch.while_loop are not yet wired up for the dual-wrapper path and are skipped in this variant; a follow-up commit adds support for them. Example generated code: [JIT wrapper code](https://gist.github.com/desertfire/c699cb01ea1b1c53c94761cf94c101bd) [AOT wrapper code](https://gist.github.com/desertfire/b565adb51eb3d9438501a84301ac6ca3) Authored with Codex. Pull Request resolved: pytorch#184735 Approved by: https://github.com/yushangdi
…ytorch#184736) Emit cond and while_loop as DualWrapperCodeLine entries so the dual-wrapper codegen can route the predicate-guarded body to the AOTI side while running the JIT first pass through both branches (cond) or both subgraphs once (while_loop) for lazy autotune. Introduce _preserve_codegen_state and _preserve_device_guard_state so wrapper bookkeeping is restored between the JIT and AOTI passes over the same subgraphs. Update test_cond_with_replace_view_ops to use aoti_compile_and_package, and drop the cond/while_loop skips from AOTInductorTestDualWrapper now that the dual-wrapper path supports them. Authored with Codex. Pull Request resolved: pytorch#184736 Approved by: https://github.com/yushangdi ghstack dependencies: pytorch#184735
) # Motivation #184735 introduces AOTI lazy autotuning with the dual-wrapper mechanism, which uncovered some new XPU test failures in CI. # Additonal Context Fix #187124 Fix #187123 Fix #187035 Fix #187035 Fix #187036 Fix #187125 Fix #187121 This needs to be cherry-picked to the release branch. Pull Request resolved: #187137 Approved by: https://github.com/EikanWang, https://github.com/atalman ghstack dependencies: #181233
) [xpu][fix] Include kernel_compile_result.h in aoti xpu.h header (#187137) # Motivation #184735 introduces AOTI lazy autotuning with the dual-wrapper mechanism, which uncovered some new XPU test failures in CI. # Additonal Context Fix #187124 Fix #187123 Fix #187035 Fix #187035 Fix #187036 Fix #187125 Fix #187121 This needs to be cherry-picked to the release branch. Pull Request resolved: #187137 Approved by: https://github.com/EikanWang, https://github.com/atalman ghstack dependencies: #181233 (cherry picked from commit 6ec77b0) Co-authored-by: Yu, Guangye <[email protected]>
Switch the AOTI lazy-autotune path to emit separate JIT and AOTI C++ wrapper bodies in one codegen pass. The JIT body is compiled and run with real inputs to populate lazy Triton kernel metadata, then the generated AOTI source includes the captured kernel config for packaging. Add test_aot_inductor variant that runs with triton.autotune_at_compile_time=False so this path is exercised. torch.cond and torch.while_loop are not yet wired up for the dual-wrapper path and are skipped in this variant; a follow-up commit adds support for them. Example generated code: [JIT wrapper code](https://gist.github.com/desertfire/c699cb01ea1b1c53c94761cf94c101bd) [AOT wrapper code](https://gist.github.com/desertfire/b565adb51eb3d9438501a84301ac6ca3) Authored with Codex. Pull Request resolved: pytorch#184735 Approved by: https://github.com/yushangdi
…ytorch#184736) Emit cond and while_loop as DualWrapperCodeLine entries so the dual-wrapper codegen can route the predicate-guarded body to the AOTI side while running the JIT first pass through both branches (cond) or both subgraphs once (while_loop) for lazy autotune. Introduce _preserve_codegen_state and _preserve_device_guard_state so wrapper bookkeeping is restored between the JIT and AOTI passes over the same subgraphs. Update test_cond_with_replace_view_ops to use aoti_compile_and_package, and drop the cond/while_loop skips from AOTInductorTestDualWrapper now that the dual-wrapper path supports them. Authored with Codex. Pull Request resolved: pytorch#184736 Approved by: https://github.com/yushangdi ghstack dependencies: pytorch#184735
…rch#187137) # Motivation pytorch#184735 introduces AOTI lazy autotuning with the dual-wrapper mechanism, which uncovered some new XPU test failures in CI. # Additonal Context Fix pytorch#187124 Fix pytorch#187123 Fix pytorch#187035 Fix pytorch#187035 Fix pytorch#187036 Fix pytorch#187125 Fix pytorch#187121 This needs to be cherry-picked to the release branch. Pull Request resolved: pytorch#187137 Approved by: https://github.com/EikanWang, https://github.com/atalman ghstack dependencies: pytorch#181233
Switch the AOTI lazy-autotune path to emit separate JIT and AOTI C++ wrapper bodies in one codegen pass. The JIT body is compiled and run with real inputs to populate lazy Triton kernel metadata, then the generated AOTI source includes the captured kernel config for packaging. Add test_aot_inductor variant that runs with triton.autotune_at_compile_time=False so this path is exercised. torch.cond and torch.while_loop are not yet wired up for the dual-wrapper path and are skipped in this variant; a follow-up commit adds support for them. Example generated code: [JIT wrapper code](https://gist.github.com/desertfire/c699cb01ea1b1c53c94761cf94c101bd) [AOT wrapper code](https://gist.github.com/desertfire/b565adb51eb3d9438501a84301ac6ca3) Authored with Codex. Pull Request resolved: pytorch#184735 Approved by: https://github.com/yushangdi
…ytorch#184736) Emit cond and while_loop as DualWrapperCodeLine entries so the dual-wrapper codegen can route the predicate-guarded body to the AOTI side while running the JIT first pass through both branches (cond) or both subgraphs once (while_loop) for lazy autotune. Introduce _preserve_codegen_state and _preserve_device_guard_state so wrapper bookkeeping is restored between the JIT and AOTI passes over the same subgraphs. Update test_cond_with_replace_view_ops to use aoti_compile_and_package, and drop the cond/while_loop skips from AOTInductorTestDualWrapper now that the dual-wrapper path supports them. Authored with Codex. Pull Request resolved: pytorch#184736 Approved by: https://github.com/yushangdi ghstack dependencies: pytorch#184735
…rch#187137) # Motivation pytorch#184735 introduces AOTI lazy autotuning with the dual-wrapper mechanism, which uncovered some new XPU test failures in CI. # Additonal Context Fix pytorch#187124 Fix pytorch#187123 Fix pytorch#187035 Fix pytorch#187035 Fix pytorch#187036 Fix pytorch#187125 Fix pytorch#187121 This needs to be cherry-picked to the release branch. Pull Request resolved: pytorch#187137 Approved by: https://github.com/EikanWang, https://github.com/atalman ghstack dependencies: pytorch#181233
…0637) ## Summary Skip the CUDA-specific stream event codegen (`_aoti_aux_stream_cache` helpers) in the AOTI `cpp_wrapper` for XPU devices. SYCL in-order queues handle events implicitly, making the generated CUDA stream cache code unnecessary and incompatible. ## Root Cause PR [#182971](#182971) (Jul 8, 2026) added `codegen_stream_info_prologue()` to `cpp_wrapper_gpu.py`, which emits `_aoti_aux_stream_cache.ensure(...)` — a CUDA-specific stream cache helper. When a model uses `torch.Event()`, the AOTI compiler detects `num_strams > 1` and enters this codegen path, generating code that references CUDA-only symbols. For the XPU backend, these CUDA stream helpers are not needed because: - SYCL in-order queues handle event synchronization implicitly - The generated code causes compilation/link errors on XPU This affected the `AOTInductorTestDualWrapper` test class specifically because it uses `autotune_at_compile_time=False` (lazy Triton compile), which triggers the dual-wrapper codegen path that exercises the multi-stream codegen. ## Timeline | Date | Event | |------|-------| | Jun 10 | `AOTInductorTestDualWrapper` added (PR #184735) — running fine | | **Jul 8** | **User-streams support added (PR #182971) — introduced CUDA stream codegen** | | Jul 9 | Auto-disabler detected test failure, created issues #189327 and #189326 | | Jul 15 | PR #189980 submitted with barrier chaining fix in Copy.cpp | | Jul 17 | Reviewer rejected barrier fix as unnecessary overhead | | Jul 21 | **This PR** — correct root cause identified | ## Notes on PR #189980 The earlier PR #189980 attempted to fix this by capturing the `sycl::event` from `q.memcpy()` and chaining it with `ext_oneapi_submit_barrier({copy_event})` in `Copy.cpp`. That approach was rejected by the reviewer ([comment](intel/torch-xpu-ops#4349 (comment))) as adding unnecessary overhead, and has since been determined to be unrelated to the actual root cause. The DMA engine event tracking gap described there is a separate PVC driver-level issue that does not reproduce on the tested hardware configuration (driver 1.6.33578+15, all D2H non_blocking copy tests pass without the barrier workaround). ## Test Plan - `test_copy_non_blocking_is_pinned_xpu` on both `AOTInductorTestDualWrapper` and `AOTInductorTestABICompatibleGpu` - Full XPU CI suite Fixes #189327 Fixes #189326 Pull Request resolved: #190637 Approved by: https://github.com/guangyey, https://github.com/jansel
Stack from ghstack (oldest at bottom):
Switch the AOTI lazy-autotune path to emit separate JIT and AOTI C++ wrapper bodies in one codegen pass. The JIT body is compiled and run with real inputs to populate lazy Triton kernel metadata, then the generated AOTI source includes the captured kernel config for packaging.
Add test_aot_inductor variant that runs with triton.autotune_at_compile_time=False so this path is exercised. torch.cond and torch.while_loop are not yet wired up for the dual-wrapper path and are skipped in this variant; a follow-up commit adds support for them.
Example generated code:
JIT wrapper code
AOT wrapper code
Authored with Codex.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo