Skip to content

[ExecuTorch][WebGPU] Add argmax/argmin ops + int64-output path#21209

Open
JCNTH wants to merge 1 commit into
gh/JCNTH/169/basefrom
gh/JCNTH/169/head
Open

[ExecuTorch][WebGPU] Add argmax/argmin ops + int64-output path#21209
JCNTH wants to merge 1 commit into
gh/JCNTH/169/basefrom
gh/JCNTH/169/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Problem: The WebGPU delegate has no aten.argmax.default / aten.argmin.default, and — more fundamentally — no int64-OUTPUT support: the AOT downcasts the int64 index to an int32 GPU buffer, but the ExecuTorch program output is int64, and copy_outputs raw-copied the int64 EValue's bytes from the int32 staging buffer (a size mismatch). argmax would be the first int64-output op. argmax unlocks on-GPU decode sampling.

Solution: Port et_vk.argmax/argmin (last-dim arg-reduction -> int64 index) sharing one handler, mirroring Vulkan ArgReduce.cpp (arg_reduce_impl, last-dim only, the add_reduce_per_row_node accumulator that tracks {val, idx}). The kernel reuses the landed amax last-dim reduction + index-tracking: a strict > (argmax) / < (argmin) scan keeps the FIRST extremum (= torch tie-break), writing the index as int32 (1 u32/row) to the int32 GPU buffer. Add the int32->int64 output-widening path: copy_outputs now maps each output's LIVE staging size (cur_nbytes) and, when the host EValue is 2x the int staging buffer, sign-extends int32->int64 into the EValue; matched-dtype outputs keep the unchanged raw-copy path.

Implementation: argmax/Reduce.cpp registers aten.argmax.default + aten.argmin.default -> arg_reduce_impl(graph, args, is_argmin); args [in, dim, keepdim, out], dim scalar (last-dim guard), out = args.back(), guards fp32 input / int32 output / shape, resize hook. arg_reduce.wgsl is one row per thread. WebGPUGraph::copy_outputs maps cur_nbytes + the guarded dst == 2*map && is_int widen. WebGPUBackend::execute wraps execute() + copy_outputs() in try/catch so a defensive throw never crosses the backend boundary.

Constraints: last-dim reduction only (mirrors Vulkan normalized_dim == ndim-1); fp32 input, int32-backed int64 index output. The copy_outputs change is byte-identical for every existing fp32/int8 output (cur_nbytes == EValue nbytes for matched dtypes) — verified by regression (floor_divide fp32, q8ta convs int8 all still pass).

Differential Revision: D112257656

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 46 New Failures, 3 Unrelated Failures

As of commit feb7511 with merge base 266e0dc (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

  • pull / unittest / windows / windows-job (gh) (matched win rule in flaky-rules.json)
    Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under 'C:\actions-runner\_work\executorch\executorch\test-infra\.github\actions\teardown-windows'. Did you forget to run actions/checkout before running your local action?
  • pull / unittest-editable / windows / windows-job (gh) (matched win rule in flaky-rules.json)
    Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under 'C:\actions-runner\_work\executorch\executorch\test-infra\.github\actions\teardown-windows'. Did you forget to run actions/checkout before running your local action?

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

This was referenced Jul 22, 2026
This was referenced Jul 22, 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.

@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 Jul 22, 2026
@JCNTH
JCNTH requested a review from psiddh July 23, 2026 02:02

@psiddh psiddh 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.

Approving full WebGPU stack

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants