Repository navigation
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:
Reviewed commit: 🔗 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 FailuresAs of commit cc77a5a with merge base 0563a22 ( 🟡 GREEN LIGHT - PR requires human review:
reason: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
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.
Withdrawn: the automated review of 452eddd found nothing blocking.
|
Starting merge as part of PR stack under #200916 |
2 similar comments
|
Starting merge as part of PR stack under #200916 |
|
Starting merge as part of PR stack under #200916 |
…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
…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
…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
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,
_StaticXpuLauncheralready 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,_FastXpuLauncherwould 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.cppdiff 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 itszeMemGetAllocPropertiescheck, 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 intoargStorage, is gone. It came over with the code this file was copied from:cuLaunchKerneltakes the kernel arguments as avoid**of argument addresses and copies from each one itself, so on CUDA that array has to exist for the driver to read. SYCL takes onecgh.set_arg(i, value)call per argument instead, so the submit loop reads each 8-byte slot directly. Nothing outside that loop ever looked atkernelArgs[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.