Skip to content

Vulkan: implement any.dim for boolean tensors - #22790

Open
msluszniak wants to merge 1 commit into
pytorch:mainfrom
msluszniak:ms/vulkan-any-dim
Open

Vulkan: implement any.dim for boolean tensors#22790
msluszniak wants to merge 1 commit into
pytorch:mainfrom
msluszniak:ms/vulkan-any-dim

Conversation

@msluszniak

Copy link
Copy Markdown
Contributor

aten.any.dim has no Vulkan implementation, so a boolean reduction is a graph break. Attention masks hit this hard: an RF-DETR segmentation export reduces a [1, 6, 677, 677] boolean tensor 16 times, and each one copies 2.75M elements out to the CPU and back to produce 4k of output.

Over a boolean tensor any is a max over the uint8 representation, so the general reduce shader already covers it once it can be instantiated for uint8. Two things were in the way:

  • The accumulator path hardcoded vec4 at the load sites, which does not compile when load_texel returns uvec4. Converting at the load instead keeps the accumulator and the INIT/UPDATE/POSTPROCESS macros in float, so they keep working on both vec4 and scalar arguments, and the write converts back to the tensor's texel type. No-op for the existing float and half variants.
  • any.dim takes a single int dim rather than the int list the other reduce ops take, so it cannot share DEFINE_REDUCE_FN.

The partitioner claims only the general (texture) implementation, since the buffer per-row shaders have no boolean variant, and only keepdim=True, which the existing support predicate already requires.

Verified on a Mali-G76 (Galaxy S10+) with both branches exercised (rows planted so the result is neither all-True nor all-False): bit-identical to eager.

Stacked with #22789 (mul.Scalar), RF-DETR goes from 39 delegates to 7, CPU kernel calls from 55 to 7, and every output stays bit-identical to the 39-delegate build. End to end on the same device, warmed and interleaved over 4 order-reversed rounds: 1078 ms -> 856 ms, against 2474 ms for XNNPACK fp32.

cc @SS-JIA @manuelcandales @digantdesai @cbilgin

`aten.any.dim` has no Vulkan implementation, so a boolean reduction is a graph
break. Attention masks hit this hard: an RF-DETR segmentation export reduces a
[1, 6, 677, 677] boolean tensor 16 times, and each one copies 2.75M elements
out to the CPU and back to produce 4k of output.

Over a boolean tensor `any` is a max over the uint8 representation, so the
general reduce shader already covers it once it can be instantiated for uint8.
Two things were in the way:

- The accumulator path hardcoded `vec4` at the load sites, which does not
  compile when `load_texel` returns `uvec4`. Converting at the load instead
  keeps the accumulator and the INIT/UPDATE/POSTPROCESS macros in float, so
  they continue to work on both vec4 and scalar arguments, and the write
  converts back to the tensor's texel type. This is a no-op for the existing
  float and half variants.
- `any.dim` takes a single int dim rather than the int list the other reduce
  ops take, so it cannot share DEFINE_REDUCE_FN.

The partitioner claims only the general (texture) implementation, since the
buffer per-row shaders have no boolean variant, and only keepdim=True, which
is what the existing support predicate already requires.

Verified on a Mali-G76 (Galaxy S10+) with both branches exercised: the result is
bit-identical to eager. On RF-DETR, combined with mul.Scalar, this takes the
partition count from 39 delegates to 7 and CPU kernel calls from 55 to 7, with
every output bit-identical to the 39-delegate build.
@msluszniak
msluszniak requested a review from SS-JIA as a code owner September 13, 2026 22:21
@pytorch-bot

pytorch-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 15 Awaiting Approval

As of commit db8cf8d with merge base 903696a (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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

@pytorch-bot pytorch-bot Bot added the module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ label Sep 13, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 13, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant