Repository navigation
[inductor] Route flex decoding get_split_k SM count via DeviceInterface - #196736
huangfei1199 wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/196736
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 57af077 with merge base 03ea905 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
|
@pytorchbot label "release notes: inductor" |
|
@claude review these changes |
|
Claude finished @jansel's task in 1m 10s —— View job Small, correct change: device routing through PR Review: #196736SummaryThe PR changes Code Quality
Testing
RecommendationRequest Changes The logic is sound. What blocks approval is the regression test for the bug fix, plus the small cleanups: the top-level import, the plain signature, hoisting |
Thanks, all points are addressed in 350f68d. The regression test (TestGetSplitKDeviceRouting) is included in the same commit, and I've manually verified it in a container. |
|
@claude review again |
|
Claude finished @jansel's task in 49s —— View job Every point from the previous review is fixed in 350f68d, and one small test-style item is left. PR Review: #196736 (re-review)SummaryThe PR makes
The test would fail on the old code: the old code never calls the interface, and it reads Testing
RecommendationApprove once the base class is switched. The logic is correct: CUDA and XPU keep the same SM count, and the routing bug now has a regression test. |
|
@pytorchmergebot merge |
Merge startedYour 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 |
Merge failedReason: 1 jobs have failed, first few of them are: docs-build / linux-docs / build-docs-cpp-false Details for Dev Infra teamRaised by workflow job |
|
@jansel The docs‑build failure is infra issue unrelated to this PR, same error seen before. Could you please rerun the failed jobs to unblock merge? Thanks! |
|
@pytorchbot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Successfully rebased |
350f68d to
0e7879b
Compare
|
@pytorchmergebot merge |
Merge startedYour 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 |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 3, 3, macos-m1-stable) Details for Dev Infra teamRaised by workflow job |
|
@jansel Merge is still blocked by a single job: trunk / macos-py3-arm64 --inductor/test_remote_cache died with SIGBUS before pytest started (Dr. CI verdict: infra issue). Could you re-run the failed jobs on |
|
@pytorchbot merge -r |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
## Summary Replace the hardcoded torch.xpu/torch.cuda if-chain in flex_decoding.get_split_k with a query against the kernel's target device through the DeviceInterface capability (pytorch#190615), so the SM count that feeds the SPLIT_KV heuristic is sourced from whichever device the decode kernel actually targets instead of ambient xpu/cuda detection. Existing behavior is preserved for single-device cuda and xpu environments (same SM count, same SPLIT_KV); mixed-backend machines now route correctly, and backends with no registered interface get a clear error instead of crashing inside CUDA device property initialization. ## Changes torch/_inductor/kernel/flex/flex_decoding.py: get_split_k now takes the kernel target device as a required keyword argument (`device: torch.device`) and resolves its SM count via get_interface_for_device(device), then interface.get_multi_processor_count(device). The lazy import of torch._dynamo.device_interface keeps the dynamo dependency off this module's load path (matching the style of other decoupling sites). The split_k formula (num_SM // bh * 2, floored to 1) is unchanged -- only the source of num_SM moves from a hardcoded device string / is_available() probe to the device's own registered interface. The single call site (create_flex_decoding_kernel) now reads `device = query.get_device()` (the surrounding code already queried query.get_device().type two lines above) and passes it through, which also fixes a latent multi-card inaccuracy where the old code used the default "xpu"/"cuda" device instead of the kernel's actual device index. ## Motivation get_split_k feeds SPLIT_KV, which sizes the decode KV intermediate buffer (buf_ACC_shape). The original if-chain asked the ambient environment ("is xpu available?") rather than "which device is this kernel for": on a machine with both xpu and cuda, every cuda decode kernel wrongly took the xpu branch; on a machine with neither (mtia-only, cpu-triton), the code crashed inside torch.cuda device-property initialization with no path to a usable value; tpu and out-of-tree accelerators had no legal path at all. The DeviceInterface registry already exposes get_multi_processor_count (added in pytorch#190615, already consumed by hints.py): cuda reads the standard multi_processor_count field, xpu overrides with gpu_subslice_count, mtia defaults to 64. Routing get_split_k through the same contract lets each backend report its own SM count for the device the kernel actually targets, and surfaces a clear AttributeError ("must override") for a backend whose interface has not yet been adapted -- pointing the backend author at the one method to override -- instead of an opaque CUDA init failure. ## Test Plan CI: python -m pytest -q test/inductor/test_flex_attention.py CI: python test/inductor/test_flex_attention.py CI: lintrunner -a torch/_inductor/kernel/flex/flex_decoding.py Verified on GPU (cuda): flex decode end-to-end passes (the device matrix tests cover the SPLIT_KV-shaped buffer indirectly). Verified on CPU: pytest 2 passed, 582 skipped, 0 failed; python direct run 584 tests, OK (skipped=582). Same result in NPU mode and CPU mode. Verified on an Ascend NPU container: pytest 2 passed, 582 skipped, 0 failed in both NPU mode and CPU mode; the mainline get_split_k path is not reached on NPU (torch_npu overrides the flex decode lowering), so this confirms non-regression and import/collection integrity. lintrunner reports 0 violations on the changed file. No new tests added: the existing decode end-to-end device matrix tests already cover SPLIT_KV indirectly (a changed value resizes buf_ACC_shape and turns the test red), and the must-override contract get_split_k now relies on is already tested by test_device_backend_hooks.py::TestMultiProcessorCount ::test_missing_standard_property (pytorch#190615). A standalone unit test for the formula / routing would be zero-increment over that coverage, so it is omitted to keep the PR a minimal source-only diff. ## Notes Behavior preservation: for any single-device cuda or xpu CI environment the SM count is identical to before (cuda reads the same multi_processor_count field; xpu's override returns the same gpu_subslice_count), so SPLIT_KV is unchanged and the formula is untouched. The behavioral changes are all on paths the old code could not handle correctly: - On a mixed xpu+cuda machine, a cuda decode kernel now reads multi_processor_count instead of wrongly taking the xpu branch. This is a bug fix; single-device CI is structurally unaffected. - On an mtia-only / no-xpu-no-cuda machine, the previous CUDA-init crash becomes a usable value (mtia default 64). - For an out-of-tree DeviceInterface that has not yet overridden get_multi_processor_count, the previous opaque crash becomes a clear AttributeError("must override") pointing at the method to implement; once the backend overrides it (returning its SM count), the decode heuristic works without a mainline patch. Part of the hardware decoupling effort: remove hardcoded device lists so backends register capability through their DeviceInterface instead. Authored with an AI assistant (Claude).
Import get_interface_for_device at module top level and drop the stale lazy-import comment, flatten get_split_k to a plain positional signature, hoist `device = query.get_device()` to the top of create_flex_decoding_kernel, skip the get_split_k call when the user already passed SPLIT_KV, and add a CPU-runnable regression test pinning that the SM count is queried from the kernel's own device rather than ambient backend state. Test Plan: ``` python test/inductor/test_flex_decoding.py -k TestGetSplitKDeviceRouting -v ``` Authored with an AI assistant. Signed-off-by: huangfei1199 <[email protected]>
|
Successfully rebased |
0e7879b to
57af077
Compare
Merge startedYour 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 |
Merge failedReason: 3 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
|
@pytorchbot merge |
Merge startedYour 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 |
|
The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command |
|
@pytorchmergebot merge |
Merge startedYour 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 |
…ce (pytorch#196736) ## Summary Replace the hardcoded torch.xpu/torch.cuda if-chain in flex_decoding.get_split_k with a query against the kernel's target device through the DeviceInterface capability (pytorch#190615), so the SM count that feeds the SPLIT_KV heuristic is sourced from whichever device the decode kernel actually targets instead of ambient xpu/cuda detection. Existing behavior is preserved for single-device cuda and xpu environments (same SM count, same SPLIT_KV); mixed-backend machines now route correctly, and backends with no registered interface get a clear error instead of crashing inside CUDA device property initialization. ## Changes torch/_inductor/kernel/flex/flex_decoding.py: get_split_k now takes the kernel target device as a required keyword argument (`device: torch.device`) and resolves its SM count via get_interface_for_device(device), then interface.get_multi_processor_count(device). The lazy import of torch._dynamo.device_interface keeps the dynamo dependency off this module's load path (matching the style of other decoupling sites). The split_k formula (num_SM // bh * 2, floored to 1) is unchanged -- only the source of num_SM moves from a hardcoded device string / is_available() probe to the device's own registered interface. The single call site (create_flex_decoding_kernel) now reads `device = query.get_device()` (the surrounding code already queried query.get_device().type two lines above) and passes it through, which also fixes a latent multi-card inaccuracy where the old code used the default "xpu"/"cuda" device instead of the kernel's actual device index. ## Motivation get_split_k feeds SPLIT_KV, which sizes the decode KV intermediate buffer (buf_ACC_shape). The original if-chain asked the ambient environment ("is xpu available?") rather than "which device is this kernel for": on a machine with both xpu and cuda, every cuda decode kernel wrongly took the xpu branch; on a machine with neither (mtia-only, cpu-triton), the code crashed inside torch.cuda device-property initialization with no path to a usable value; tpu and out-of-tree accelerators had no legal path at all. The DeviceInterface registry already exposes get_multi_processor_count (added in pytorch#190615, already consumed by hints.py): cuda reads the standard multi_processor_count field, xpu overrides with gpu_subslice_count, mtia defaults to 64. Routing get_split_k through the same contract lets each backend report its own SM count for the device the kernel actually targets, and surfaces a clear AttributeError ("must override") for a backend whose interface has not yet been adapted -- pointing the backend author at the one method to override -- instead of an opaque CUDA init failure. ## Test Plan CI: python -m pytest -q test/inductor/test_flex_attention.py CI: python test/inductor/test_flex_attention.py CI: lintrunner -a torch/_inductor/kernel/flex/flex_decoding.py Verified on GPU (cuda): flex decode end-to-end passes (the device matrix tests cover the SPLIT_KV-shaped buffer indirectly). Verified on CPU: pytest 2 passed, 582 skipped, 0 failed; python direct run 584 tests, OK (skipped=582). Same result in NPU mode and CPU mode. Verified on an Ascend NPU container: pytest 2 passed, 582 skipped, 0 failed in both NPU mode and CPU mode; the mainline get_split_k path is not reached on NPU (torch_npu overrides the flex decode lowering), so this confirms non-regression and import/collection integrity. lintrunner reports 0 violations on the changed file. No new tests added: the existing decode end-to-end device matrix tests already cover SPLIT_KV indirectly (a changed value resizes buf_ACC_shape and turns the test red), and the must-override contract get_split_k now relies on is already tested by test_device_backend_hooks.py::TestMultiProcessorCount ::test_missing_standard_property (pytorch#190615). A standalone unit test for the formula / routing would be zero-increment over that coverage, so it is omitted to keep the PR a minimal source-only diff. ## Notes Behavior preservation: for any single-device cuda or xpu CI environment the SM count is identical to before (cuda reads the same multi_processor_count field; xpu's override returns the same gpu_subslice_count), so SPLIT_KV is unchanged and the formula is untouched. The behavioral changes are all on paths the old code could not handle correctly: - On a mixed xpu+cuda machine, a cuda decode kernel now reads multi_processor_count instead of wrongly taking the xpu branch. This is a bug fix; single-device CI is structurally unaffected. - On an mtia-only / no-xpu-no-cuda machine, the previous CUDA-init crash becomes a usable value (mtia default 64). - For an out-of-tree DeviceInterface that has not yet overridden get_multi_processor_count, the previous opaque crash becomes a clear AttributeError("must override") pointing at the method to implement; once the backend overrides it (returning its SM count), the decode heuristic works without a mainline patch. Part of the hardware decoupling effort: remove hardcoded device lists so backends register capability through their DeviceInterface instead. Authored with an AI assistant (Claude). Pull Request resolved: pytorch#196736 Approved by: https://github.com/jansel, https://github.com/drisspg
Summary
Replace the hardcoded torch.xpu/torch.cuda if-chain in flex_decoding.get_split_k with a query against the kernel's target device through the DeviceInterface capability (#190615), so the SM count that feeds the SPLIT_KV heuristic is sourced from whichever device the decode kernel actually targets instead of ambient xpu/cuda detection. Existing behavior is preserved for single-device cuda and xpu environments (same SM count, same SPLIT_KV); mixed-backend machines now route correctly, and backends with no registered interface get a clear error instead of crashing inside CUDA device property initialization.
Changes
torch/_inductor/kernel/flex/flex_decoding.py: get_split_k now takes the kernel target device as a required keyword argument (
device: torch.device) and resolves its SM count via get_interface_for_device(device), then interface.get_multi_processor_count(device). The lazy import of torch._dynamo.device_interface keeps the dynamo dependency off this module's load path (matching the style of other decoupling sites). The split_k formula (num_SM // bh * 2, floored to 1) is unchanged -- only the source of num_SM moves from a hardcoded device string / is_available() probe to the device's own registered interface. The single call site (create_flex_decoding_kernel) now readsdevice = query.get_device()(the surrounding code already queried query.get_device().type two lines above) and passes it through, which also fixes a latent multi-card inaccuracy where the old code used the default "xpu"/"cuda" device instead of the kernel's actual device index.Motivation
get_split_k feeds SPLIT_KV, which sizes the decode KV intermediate buffer (buf_ACC_shape). The original if-chain asked the ambient environment ("is xpu available?") rather than "which device is this kernel for": on a machine with both xpu and cuda, every cuda decode kernel wrongly took the xpu branch; on a machine with neither (mtia-only, cpu-triton), the code crashed inside torch.cuda device-property initialization with no path to a usable value; tpu and out-of-tree accelerators had no legal path at all. The DeviceInterface registry already exposes get_multi_processor_count (added in #190615, already consumed by hints.py): cuda reads the standard multi_processor_count field, xpu overrides with gpu_subslice_count, mtia defaults to 64. Routing get_split_k through the same contract lets each backend report its own SM count for the device the kernel actually targets, and surfaces a clear AttributeError ("must override") for a backend whose interface has not yet been adapted -- pointing the backend author at the one method to override -- instead of an opaque CUDA init failure.
Test Plan
CI: python -m pytest -q test/inductor/test_flex_attention.py CI: python test/inductor/test_flex_attention.py
CI: lintrunner -a torch/_inductor/kernel/flex/flex_decoding.py
Verified on GPU (cuda): flex decode end-to-end passes (the device matrix tests cover the SPLIT_KV-shaped buffer indirectly). Verified on CPU: pytest 2 passed, 582 skipped, 0 failed; python direct run 584 tests, OK (skipped=582). Same result in NPU mode and CPU mode. Verified on an Ascend NPU container: pytest 2 passed, 582 skipped, 0 failed in both NPU mode and CPU mode; the mainline get_split_k path is not reached on NPU (torch_npu overrides the flex decode lowering), so this confirms non-regression and import/collection integrity. lintrunner reports 0 violations on the changed file.
No new tests added: the existing decode end-to-end device matrix tests already cover SPLIT_KV indirectly (a changed value resizes buf_ACC_shape and turns the test red), and the must-override contract get_split_k now relies on is already tested by test_device_backend_hooks.py::TestMultiProcessorCount ::test_missing_standard_property (#190615). A standalone unit test for the formula / routing would be zero-increment over that coverage, so it is omitted to keep the PR a minimal source-only diff.
Notes
Behavior preservation: for any single-device cuda or xpu CI environment the SM count is identical to before (cuda reads the same multi_processor_count field; xpu's override returns the same gpu_subslice_count), so SPLIT_KV is unchanged and the formula is untouched. The behavioral changes are all on paths the old code could not handle correctly:
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo