Skip to content

[XPU] Fast static launcher 1/2: Refactor code to facilitate reuse by the new launcher. - #201004

Closed
etaf wants to merge 15 commits into
gh/etaf/270/basefrom
gh/etaf/270/head
Closed

etaf wants to merge 15 commits into
gh/etaf/270/basefrom
gh/etaf/270/head

Conversation

@etaf

@etaf etaf commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Motivation

#200916 adds _FastXpuLauncher, a vectorcall launcher that binds a kernel's launch properties once instead of re-deriving them on every launch.

Almost everything it still has to do per launch, _StaticXpuLauncher already does: pack each argument into an 8-byte slot according to its type code, and submit the kernel with those slots. The goal of this PR is to let the new launcher reuse that code rather than copy it. Without it, _FastXpuLauncher would carry its own copy of the 13-case packing switch and of the submit, and the two copies would drift apart.

Splitting it out also keeps #200916 reviewable: with this landed, the xpu.cpp diff there is new code only.

Solution

No behavior change, except for the last point.

  • packKernelArg() packs one argument of one type code. The pointer resolution is a parameter, so each caller chooses how much it validates: the generic launcher keeps its zeMemGetAllocProperties check, the new one skips it. parseKernelArgs() is the loop over the argument tuple around it.
  • submitKernel() submits with an argument layout the caller resolved: the work-group size, the argument count, and whether to bind dynamic shared memory. launchKernel() derives those per launch, as before; the new launcher resolves them once, when it is built.
  • kernelArgs, the per-launch array of pointers into argStorage, is gone. It came over with the code this file was copied from: cuLaunchKernel takes the kernel arguments as a void** of argument addresses and copies from each one itself, so on CUDA that array has to exist for the driver to read. SYCL takes one cgh.set_arg(i, value) call per argument instead, so the submit loop reads each 8-byte slot directly. Nothing outside that loop ever looked at kernelArgs[i], which was always &argStorage[i].

Test

No new test: this is a refactor of the launch path that every static launcher test already exercises.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:

This PR refactors torch/csrc/inductor/static_launcher/xpu.cpp into reusable
helpers (packKernelArg, callDataPtrMethod, submitKernel) for the upcoming
_FastXpuLauncher, and the review found no problems. Recommendation: Approve.
Behavior is unchanged, and the one earlier blocking finding is fixed.

Reviewed commit: 452eddd (NOT the current head cc77a5a)

Review run

🐛 Report a bad review

🔗 Helpful Links

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

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

✅ No Failures

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

🟡 GREEN LIGHT - PR requires human review:

  • Triviality
    • Restructures the XPU launch path in torch/csrc/inductor/static_launcher/xpu.cpp, not a rename or move: splits out packKernelArg, callDataPtrMethod, submitKernel
    • Drops the kernelArgs pointer array and changes launchKernel/parseKernelArgs signatures, so it fits no trivial class
  • Design under review
    • The PointerGetter template parameter on packKernelArg exists so the follow-up launcher in #​200916 can skip getPointer validation; that API shape is a reviewer's call
  • Correctness
    • Read the moved switch and launch body; cases match the old ones and the num_args == 0 call to launchKernel with nullptr still compiles and skips the arg loop

reason: not_trivial
Reviewed commit: cc77a5a

Inference job

🐛 Report a wrong verdict

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

@etaf
etaf marked this pull request as draft October 15, 2026 01:30
@etaf etaf changed the title [XPU] Split the static XPU launcher's argument packing and submit [XPU] Fast static launcher 1/2: Refactor to be reusable by new launcher. Oct 15, 2026
@etaf etaf changed the title [XPU] Fast static launcher 1/2: Refactor to be reusable by new launcher. [XPU] Fast static launcher 1/2: Refactor code to facilitate reuse by the new launcher. Oct 15, 2026
@etaf etaf added the ciflow/xpu Run XPU CI tasks label Oct 15, 2026
@etaf etaf added the topic: not user facing topic category label Oct 15, 2026
@etaf
etaf requested review from EikanWang and guangyey October 15, 2026 09:44
@etaf
etaf marked this pull request as ready for review October 19, 2026 09:51
@etaf etaf removed the ciflow/xpu Run XPU CI tasks label Oct 21, 2026
@etaf
etaf removed request for EikanWang and guangyey October 21, 2026 02:28
@etaf
etaf marked this pull request as draft October 21, 2026 02:28
etaf added 8 commits October 21, 2026 05:58
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Oct 29, 2026
[ghstack-poisoned]
@etaf etaf added the in progress PR is being worked on by the author, until the automated review accepts it label Nov 1, 2026
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]

@github-actions github-actions Bot 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.

Automated review of 452eddd: nothing blocking; ready for human review.

This PR refactors torch/csrc/inductor/static_launcher/xpu.cpp into reusable helpers (packKernelArg, callDataPtrMethod, submitKernel) for the upcoming _FastXpuLauncher, and the review found no problems. Recommendation: Approve. Behavior is unchanged, and the one earlier blocking finding is fixed.

This review is AI-generated and advisory. Each new automated review replaces the previous one.

@github-actions
github-actions Bot dismissed their stale review November 1, 2026 15:58

Withdrawn: the automated review of 452eddd found nothing blocking.

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 1, 2026
etaf added 2 commits November 2, 2026 06:18
[ghstack-poisoned]
[ghstack-poisoned]
@etaf
etaf requested a review from jansel November 2, 2026 14:10
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200916

2 similar comments
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200916

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200916

pytorchmergebot pushed a commit that referenced this pull request Nov 6, 2026
…atically launched XPU kernels. (#200916)

## Motivation

`_StaticXpuLauncher._launch_kernel` has some overhead on every launch:

* `PyArg_ParseTuple` over nine arguments, a capsule tag comparison to get the kernel, and a `strlen` of `arg_tys`.
* Per pointer argument: a python `data_ptr()` call, plus a `zeMemGetAllocProperties` validation whose result is discarded.
* `StaticallyLaunchedXpuKernel.run` on top of that rebuilds the argument tuple and the `arg_tys` string to append the scratch arguments.

None of it can change between launches of the same kernel. Median host time per launch (5 processes, bursts of 32 into a drained queue), through `run()` as inductor calls it today vs through the fast launcher:

| kernel arguments | `run()` | fast |
| --- | --- | --- |
| 1 pointer | 4445 ns | 3204 ns |
| 4 pointers | 5866 ns | 3771 ns |
| 8 pointers | 7236 ns | 4001 ns |
| 16 pointers | 9922 ns | 4708 ns |
| 4 pointers + 4 scalars | 6376 ns | 4125 ns |

The marginal cost per pointer argument drops from ~370 ns to ~100 ns. On densenet121 inference (bs16, 128x128, 342 launches per iteration, host bound), one compiled iteration goes from ~7.5 ms to ~6.5 ms, and the speedup over eager from ~1.21x to ~1.40x.

## Solution

`_FastXpuLauncher`, the XPU sibling of `_FastCudaLauncher`:

* Binds the loaded kernel, num_warps, the argument layout and the shared memory size once. Its vectorcall (PEP 590) slot takes only the grid, the stream and the kernel arguments.
* `CachingAutotuner` swaps it into the cached launcher after the first launch, so `run()` is bypassed too.
* Pointer arguments are read with `THPVariable_Unpack` + `Tensor::data_ptr()` and are not validated, as on CUDA: inductor codegen guarantees the `'O'` arguments are tensors, and anything else falls back to the generic unpacking.
* It holds a reference to the kernel capsule, which owns the `sycl::kernel`. CUDA bakes a raw `CUfunction` and relies on the python side to keep the owner alive; doing that here would leave a use-after-free reachable by closing the static kernel.

It reuses `packKernelArg()` and `submitKernel()`, which #201004 split out of the launch path, so `xpu.cpp` here is new code only.

Pull Request resolved: #200916
Approved by: https://github.com/jansel
ghstack dependencies: #201004
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…the new launcher. (pytorch#201004)

## Motivation

pytorch#200916 adds `_FastXpuLauncher`, a vectorcall launcher that binds a kernel's launch properties once instead of re-deriving them on every launch.

Almost everything it still has to do per launch, `_StaticXpuLauncher` already does: pack each argument into an 8-byte slot according to its type code, and submit the kernel with those slots. The goal of this PR is to let the new launcher **reuse** that code rather than copy it. Without it, `_FastXpuLauncher` would carry its own copy of the 13-case packing switch and of the submit, and the two copies would drift apart.

Splitting it out also keeps pytorch#200916 reviewable: with this landed, the `xpu.cpp` diff there is new code only.

## Solution

No behavior change, except for the last point.

* `packKernelArg()` packs one argument of one type code. The pointer resolution is a parameter, so each caller chooses how much it validates: the generic launcher keeps its `zeMemGetAllocProperties` check, the new one skips it. `parseKernelArgs()` is the loop over the argument tuple around it.
* `submitKernel()` submits with an argument layout the caller resolved: the work-group size, the argument count, and whether to bind dynamic shared memory. `launchKernel()` derives those per launch, as before; the new launcher resolves them once, when it is built.
* `kernelArgs`, the per-launch array of pointers into `argStorage`, is gone. It came over with the code this file was copied from: `cuLaunchKernel` takes the kernel arguments as a `void**` of argument *addresses* and copies from each one itself, so on CUDA that array has to exist for the driver to read. SYCL takes one `cgh.set_arg(i, value)` call per argument instead, so the submit loop reads each 8-byte slot directly. Nothing outside that loop ever looked at `kernelArgs[i]`, which was always `&argStorage[i]`.

## Test

No new test: this is a refactor of the launch path that every static launcher test already exercises.

Pull Request resolved: pytorch#201004
Approved by: https://github.com/guangyey
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…atically launched XPU kernels. (pytorch#200916)

## Motivation

`_StaticXpuLauncher._launch_kernel` has some overhead on every launch:

* `PyArg_ParseTuple` over nine arguments, a capsule tag comparison to get the kernel, and a `strlen` of `arg_tys`.
* Per pointer argument: a python `data_ptr()` call, plus a `zeMemGetAllocProperties` validation whose result is discarded.
* `StaticallyLaunchedXpuKernel.run` on top of that rebuilds the argument tuple and the `arg_tys` string to append the scratch arguments.

None of it can change between launches of the same kernel. Median host time per launch (5 processes, bursts of 32 into a drained queue), through `run()` as inductor calls it today vs through the fast launcher:

| kernel arguments | `run()` | fast |
| --- | --- | --- |
| 1 pointer | 4445 ns | 3204 ns |
| 4 pointers | 5866 ns | 3771 ns |
| 8 pointers | 7236 ns | 4001 ns |
| 16 pointers | 9922 ns | 4708 ns |
| 4 pointers + 4 scalars | 6376 ns | 4125 ns |

The marginal cost per pointer argument drops from ~370 ns to ~100 ns. On densenet121 inference (bs16, 128x128, 342 launches per iteration, host bound), one compiled iteration goes from ~7.5 ms to ~6.5 ms, and the speedup over eager from ~1.21x to ~1.40x.

## Solution

`_FastXpuLauncher`, the XPU sibling of `_FastCudaLauncher`:

* Binds the loaded kernel, num_warps, the argument layout and the shared memory size once. Its vectorcall (PEP 590) slot takes only the grid, the stream and the kernel arguments.
* `CachingAutotuner` swaps it into the cached launcher after the first launch, so `run()` is bypassed too.
* Pointer arguments are read with `THPVariable_Unpack` + `Tensor::data_ptr()` and are not validated, as on CUDA: inductor codegen guarantees the `'O'` arguments are tensors, and anything else falls back to the generic unpacking.
* It holds a reference to the kernel capsule, which owns the `sycl::kernel`. CUDA bakes a raw `CUfunction` and relies on the python side to keep the owner alive; doing that here would leave a use-after-free reachable by closing the static kernel.

It reuses `packKernelArg()` and `submitKernel()`, which pytorch#201004 split out of the launch path, so `xpu.cpp` here is new code only.

Pull Request resolved: pytorch#200916
Approved by: https://github.com/jansel
ghstack dependencies: pytorch#201004
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triage-error ciflow/inductor GL/human GreenLight marked as needing human review Merged open source ready for review Automated review passed, ready for a final review by a maintainer. topic: not user facing topic category

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants