Skip to content

[ExecuTorch][WebGPU] Add permute_copy + IntList graph support (aten.permute_copy.default)#20396

Open
JulianCloudNTH wants to merge 2 commits into
gh/JulianCloudNTH/45/basefrom
gh/JulianCloudNTH/45/head
Open

[ExecuTorch][WebGPU] Add permute_copy + IntList graph support (aten.permute_copy.default)#20396
JulianCloudNTH wants to merge 2 commits into
gh/JulianCloudNTH/45/basefrom
gh/JulianCloudNTH/45/head

Conversation

@JulianCloudNTH

@JulianCloudNTH JulianCloudNTH commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Adds aten.permute_copy.default (a coordinate-reorder gather) to the WebGPU delegate, and the IntList graph value type it needs to read its dims argument.

Composition:

  • runtime/WebGPUGraph.{h,cpp} — adds ValueType::IntList backed by std::vector<std::vector<int64_t>> int_lists_ + get_int_list(int); build() deserializes vkgraph::GraphTypes::IntList via value_as_IntList()->items() (int64, matching the FlatBuffer [long]); mirrors the existing scalar value plumbing.
  • runtime/ops/permute/Permute.cpp — reads the permutation via get_int_list, normalizes negative dims, validates it is a permutation of [0, ndim), builds two TensorMeta UBOs + a PermuteParams{perm: vec4<u32>} uniform, guards fp32 + rank≤4, dispatches over compute_1d_workgroup_count(out.numel) with override wg_size; releases all uniforms after the bind group.
  • runtime/ops/permute/permute.wgsl — delinearizes the output index over the contiguous output strides, reads input at in.strides[perm[d]] per dim (mirrors Vulkan permute_buffer.glsl).
  • Registers both aten.permute_copy.default and aten.permute.default to the same handler.
    @exported-using-ghexport

Differential Revision: D108793162

Differential Revision: D108793162

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 2 New Failures, 3 Unrelated Failures

As of commit dcc3a48 with merge base 0e65ba6 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but was 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.

@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 Jun 18, 2026
@JulianCloudNTH

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Jun 18, 2026

Copy link
Copy Markdown

Claude finished @JulianCloudNTH's task in 3m 11s —— View job


Code Review — PR #20396: permute_copy + IntList graph support

  • Read changed files
  • Review WebGPUGraph IntList plumbing
  • Review Permute.cpp op
  • Review permute.wgsl shader
  • Post review feedback

Overall this is a clean, well-scoped addition that faithfully mirrors the existing per-op pattern (Slice.cpp) and the Vulkan permute_buffer.glsl reference. The IntList plumbing matches the FlatBuffer [long] schema (schema/program.fbs:193-195, Vulkan schema.fbs:88-89) and the existing scalar-value conventions. I verified the WGSL gather math, the bounds/validation logic, and the uniform/bind-group lifetime. A few notes below — nothing blocking.

Correctness ✅

  • Gather math is correct. For output coord d, in_bufi += coord * in_strides[perm[d]] correctly maps output dim d to input dim dims[d] (PyTorch permute semantics: out.shape[d] == in.shape[dims[d]]). Output delinearization over contiguous out_meta.strides is sound since the AOT output is contiguous.
  • No division-by-zero in the shader. out_meta.strides[d] for d < ndim is always ≥1 with contiguous strides; an empty tensor (numel == 0) dispatches 0 workgroups.
  • No OOB on the fixed-size arrays. perm[]/seen[] are sized kTensorMetaMaxNdim (4) and the ndim > 4 guard (Permute.cpp:62) runs before the indexing loop. The permutation-validity check (negative-dim normalize + seen[] dup detection) is correct.
  • fp32 + rank≤4 guards are present and consistent with Slice.cpp.

Suggestions (non-blocking)

  1. Missing op test. Every other buffer op under backends/webgpu/test/ops/ (slice, squeeze, unsqueeze, select, view_copy, …) ships a test module, but there's no test/ops/permute/. Since permute has nontrivial index math, a correctness test across a few rank-2/3/4 permutations (including negative dims and identity) would be valuable and would also exercise the new IntList deserialization path. Add a permute op test →

  2. Significant boilerplate duplication with Slice.cpp. Permute.cpp:111-187 (shader module → bind-group-layout → pipeline → bind-group → dispatch → release) is ~95% identical to Slice.cpp:106-182, differing only in the params struct and WGSL constant. This is a pre-existing pattern across ops, so following it here is reasonable — but the duplication is now large enough that a small shared helper (e.g. "build a 1-storage-in / 1-storage-out / N-uniform pipeline+dispatch") would pay off across the growing op set. Out of scope for this PR; flagging for the backend maintainers.

  3. kPermuteWorkgroupSizeY / kPermuteWorkgroupSizeZ are unused (permute_wgsl.h:57-58). These appear to be emitted by the WGSL codegen for all shaders, so this matches the generated-header convention and is fine — just noting they're dead for this 1-D dispatch.

  4. Redundant rank guard. fill_tensor_meta (TensorMeta.h:42-44) already throws on ndim > 4, so the explicit check at Permute.cpp:62-64 is belt-and-suspenders. Harmless, and arguably clearer since it guards the array writes directly.

Verified

  • WGSL SHA d34f5973…00197 matches the embedded value in permute_wgsl.h:16 (generated header is in sync with the .wgsl source). ✅
  • IntList enum addition and int_lists_ resize in build() correctly mirror the other value types. ✅
    · branch gh/JulianCloudNTH/45/head

[ghstack-poisoned]
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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant