Skip to content

[XPU] Skip CUDA stream event codegen in AOTI cpp_wrapper for XPU - #190637

Closed
xuhancn wants to merge 1 commit into
pytorch:mainfrom
xuhancn:fix/xpu-aoti-event-codegen
Closed

xuhancn wants to merge 1 commit into
pytorch:mainfrom
xuhancn:fix/xpu-aoti-event-codegen

Conversation

@xuhancn

@xuhancn xuhancn commented Jul 21, 2026 •

Copy link
Copy Markdown
Collaborator

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 (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) 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

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

@pytorch-bot

pytorch-bot Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/190637

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit eea3a8d with merge base fdd065a (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: xuhancn / name: Xu Han (fa37277)

@xuhancn xuhancn added the topic: not user facing topic category label Jul 21, 2026
@xuhancn xuhancn added ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks and removed open source labels Jul 21, 2026
@xuhancn
xuhancn force-pushed the fix/xpu-aoti-event-codegen branch from 1e08653 to fa37277 Compare July 21, 2026 06:02
@xuhancn
xuhancn force-pushed the fix/xpu-aoti-event-codegen branch 3 times, most recently from 1b52b19 to 282bed4 Compare July 21, 2026 09:51
@xuhancn
xuhancn requested a review from guangyey July 21, 2026 14:13
@xuhancn

xuhancn commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased fix/xpu-aoti-event-codegen onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix/xpu-aoti-event-codegen && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the fix/xpu-aoti-event-codegen branch from 282bed4 to e7d38a6 Compare July 21, 2026 16:07
@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jul 21, 2026
@xuhancn

xuhancn commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator Author

@claude please review this PR.

@claude

claude Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

Claude finished @xuhancn's task in 3m 5s —— View job


PR Review: #190637

Summary: Skips the CUDA-specific AOTI stream/event codegen for XPU in two CppWrapperGpu methods, relying on SYCL in-order queues to handle event ordering implicitly. The _emit_stream_op_inline guard (the path the failing pinned-copy test actually exercises) is sound, but the codegen_stream_info_prologue guard is applied asymmetrically and leaves related methods inconsistent, and the reliance on implicit queue semantics for host-visible completion deserves scrutiny.

Full review

Code Quality / Correctness

1. Asymmetric multi-stream guard leaves downstream codegen referencing an undeclared cache.
codegen_stream_info_prologue now early-returns for XPU when num_streams > 1 (cpp_wrapper_gpu.py:1225-1226), so _aoti_aux_stream_cache is never emitted and the aux-stream slot declarations at :1239-1248 are skipped. But codegen_enter_cuda_stream_context (:1250) and _stream_expr_for_idx (:1264-1267) are not guarded and still emit _aoti_aux_stream_cache.get(...) for any stream_idx > 0. If an XPU model ever reaches genuine multi-stream codegen, the prologue silently skips setup while the enter-context path emits a reference to an undeclared identifier — a different, more confusing compile error than the current CUDA-symbol failure, not a fix.

If multi-stream is genuinely unsupported on XPU, prefer making that explicit: raise NotImplementedError for num_streams > 1 on XPU (mirroring the existing non-AOTI guard just above at :1221-1224) rather than silently returning. A silent early-return that produces subtly broken downstream code is worse than a clear error. Note the failing test only exercises the _emit_stream_op_inline path (a single synchronize_event for a pinned non-blocking copy), so the prologue guard here is speculative and currently untested — which is exactly why the inconsistency is easy to miss.

2. synchronize_event correctness relies on implicit queue semantics.
Returning False from _emit_stream_op_inline for XPU (:1272-1273) routes torch.ops.streams.synchronize_event.default through the generic fallback path (_generate_extern_kernel_alloc_helper / generate_fallback_kernel_with_runtime_lookup, :1318-1346) instead of dropping it — good, the extern node is not popped. But synchronize_event is a host wait (per the comment at stream_utils.py:28-31, required by pinned non-blocking D2H copies so the host doesn't read before the copy completes). The claim that SYCL in-order queues make this unnecessary is a device-ordering guarantee, not necessarily a host-visibility guarantee. The PR body itself references a PVC driver-level DMA-event gap and states the tests pass on a specific driver (1.6.33578+15). Please confirm the fallback op actually enforces host completion for pinned non-blocking copies on XPU (via the real op implementation / proxy executor), rather than the correctness depending on a driver version. If it's driver-dependent, that's a latent data-race risk worth a comment in the code.

API Design

3. Duplicated hardcoded device_type == "xpu" checks.
The same V.graph.device_type == "xpu" guard with an identical comment appears at :1225 and :1272. Consider a single capability predicate (e.g. on device_ops/device_codegen, something like supports_aoti_stream_events()) so the CUDA-vs-XPU distinction lives in one place and any future backend (or the currently-unguarded enter/exit context methods) picks it up consistently, rather than being scattered as string comparisons.

Testing

No in-tree test change accompanies the fix; it relies on the auto-disabler closing #189327/#189326 to re-enable test_copy_non_blocking_is_pinned_xpu. That's acceptable given the disable is issue-based, but there is nothing in this diff that would catch a regression of the _emit_stream_op_inline XPU path locally. If a lightweight assertion on the generated cpp (no _aoti_aux_stream_cache / cudaEvent* symbols for XPU) is feasible in the existing AOTI test harness, it would lock in the fix.

Recommendation

Needs Discussion — the _emit_stream_op_inline guard is fine, but (1) the asymmetric prologue guard should be made an explicit NotImplementedError or extended to the enter/exit-context + _stream_expr_for_idx methods, and (2) the host-completion guarantee for synchronize_event on XPU should be confirmed as not driver-dependent before this is relied upon for pinned non-blocking copies.

· branch `fix/xpu-aoti-event-codegen`

@guangyey

Copy link
Copy Markdown
Collaborator

Please pend this PR for a bit. I need to understand why we need to skip stream event codegen in a situation where we should support it as well.

self, code: IndentedBuffer, stream_idx: int
) -> None:
if stream_idx == 0:
if stream_idx == 0 or V.graph.device_type == "xpu":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change this code. It is not necessary and not reasonable.

)

def codegen_exit_cuda_stream_context(self, code: IndentedBuffer) -> None:
if V.graph.device_type == "xpu":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change this code. It is not necessary and not reasonable.

@guangyey guangyey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only two device guard code change is needed.

  1. _emit_stream_op_inline
  2. _ensure_aoti_stream_helpers_emitted

Comment on lines +1230 to +1231
if V.graph.device_type == "xpu":
return # SYCL in-order queue handles events implicitly

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if V.graph.device_type == "xpu":
return # SYCL in-order queue handles events implicitly

self, code: IndentedBuffer, stream_idx: int
) -> None:
if stream_idx == 0:
if stream_idx == 0 or V.graph.device_type == "xpu":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if stream_idx == 0 or V.graph.device_type == "xpu":
if stream_idx == 0:

Comment on lines +1267 to +1268
if V.graph.device_type == "xpu":
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if V.graph.device_type == "xpu":
return

def _emit_stream_op_inline(self, kernel_name: str | None, args: list[str]) -> bool:
if kernel_name is None or not V.graph.aot_mode:
return False
if V.graph.device_type == "xpu":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if V.graph.device_type == "xpu":
if self.device == "xpu":

@xuhancn
xuhancn force-pushed the fix/xpu-aoti-event-codegen branch from 5f20faf to b8b3347 Compare August 2, 2026 16:10

@guangyey guangyey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your update!

@guangyey

guangyey commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot rebase

1 similar comment
@guangyey

guangyey commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

The user-streams support PR pytorch#182971 added _ensure_aoti_stream_helpers_emitted()
which generates CUDA-specific AOTIPerThreadEventCache and AOTIPerThreadStreamCache
helper classes, along with cudaEventRecord/cudaEventSynchronize calls.

These helpers are emitted from multiple code paths in CppWrapperGpu:
1. codegen_stream_info_prologue() — when multiple streams detected
2. _emit_stream_op_inline() — when stream ops like record_event are used
3. codegen_enter_cuda_stream_context() — when switching to non-default stream
4. codegen_exit_cuda_stream_context() — when leaving a non-default stream

On XPU, SYCL in-order queues handle events implicitly. Skip all four paths.

Verified on PVC (driver 1.6.33578) and BMG (Intel Arc Pro B60): D2H copy PASS.

Fixes pytorch#189327
Fixes pytorch#189326
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased fix/xpu-aoti-event-codegen onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix/xpu-aoti-event-codegen && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the fix/xpu-aoti-event-codegen branch from b8b3347 to eea3a8d Compare August 3, 2026 13:43
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Tried to rebase and push PR #190637, but it was already up to date. Try rebasing against main by issuing:
@pytorchbot rebase -b main

@guangyey
guangyey requested a review from jansel August 4, 2026 02:31

@jansel jansel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have test coverage for this?

@xuhancn

xuhancn commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchmergebot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@guangyey

guangyey commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Do we have test coverage for this?

Hi @jansel, we were seeing some failures (#189327, #189326) on XPU CI, with this fix, both of they should be resolved and XPU CI should pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks Merged module: inductor open source release notes: inductor (aoti) topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

Status: No status

6 participants