Skip to content

[ExecuTorch][WebGPU] Add view_copy op (aten.view_copy.default)#20529

Merged
JulianCloudNTH merged 8 commits into
gh/JulianCloudNTH/34/origfrom
gh/JulianCloudNTH/35/orig
Jun 26, 2026
Merged

[ExecuTorch][WebGPU] Add view_copy op (aten.view_copy.default)#20529
JulianCloudNTH merged 8 commits into
gh/JulianCloudNTH/34/origfrom
gh/JulianCloudNTH/35/orig

Conversation

@pytorchbot

Copy link
Copy Markdown
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #20360 by @JulianCloudNTH
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/JulianCloudNTH/35/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/JulianCloudNTH/35/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/JulianCloudNTH/34/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/JulianCloudNTH/35/orig

@diff-train-skip-merge

Pull Request resolved: #20360

**Add `aten.view_copy.default` as a native buffer-to-buffer DMA** — a contiguous reshape on the dense row-major buffer backend is a flat copy, so it needs no shader.

**Problem:** a reshape only relabels shape metadata; the bytes are unchanged. Launching a compute dispatch (shader module + pipeline + bind group + uniform) just to run `output[i] = input[i]` is wasted setup for every view/clone/squeeze/unsqueeze in the graph.

**Solution:**
- Before: a `view_copy.wgsl` compute kernel dispatched over `num_elements`, with its own pipeline/bind-group/uniform per copy.
- After: `add_flat_copy` records a `wgpuCommandEncoderCopyBufferToBuffer` DMA in graph order — no shader, no pipeline, no uniform.

**Implementation:**
- `WebGPUGraph` gains a `WebGPUDispatch::Kind::Copy` command + `add_buffer_copy(src, dst, nbytes)`; `execute()` emits the encoder-level copy between compute passes (both single-shot and chunked paths), preserving the existing per-pass read-after-write ordering.
- `add_flat_copy` (declared in `view_copy.h`, reused by the stacked clone/squeeze/unsqueeze) keeps the fail-loud guards (both tensors, fp32 4-byte alignment, equal `nbytes`) and treats an aliased in/out buffer as a no-op.
- Tensor buffers already carry `CopySrc | CopyDst`, so no usage-flag change is needed.
- Mirrors Vulkan `add_view_copy_node` (`backends/vulkan/runtime/graph/ops/impl/View.cpp`): Vulkan always dispatches `view_buffer.glsl` only to remap non-contiguous layouts, which the buffer-only WebGPU backend never produces — so the contiguous DMA is the equivalent path.
ghstack-source-id: 397026498
@exported-using-ghexport

Differential Revision: [D108793164](https://our.internmc.facebook.com/intern/diff/D108793164/)
@pytorch-bot

pytorch-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@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 26, 2026
…ork)

Pull Request resolved: #20361

Registers `aten.view_copy.default` in the `cases.py` op-test framework: a `_view_copy_suite` of 3 configs (rank-reducing reshape `(2,3,4)->(6,4)`, flatten `->(-1,)`, rank-increasing reshape `->(1,2,3,4)`) that `generate_op_tests` exports via `VulkanPartitioner` and compares to a torch golden on Dawn. Also adds `test/ops/view_copy/test_view_copy.py` (`ViewModule` + `CONFIGS` + export-delegation/eager smoke test) and the `aten.view_copy.default` partitioner-allowlist entry in `tester.py`.
ghstack-source-id: 397026501
@exported-using-ghexport

Differential Revision: [D108793155](https://our.internmc.facebook.com/intern/diff/D108793155/)
Pull Request resolved: #20362

Adds `aten.select_copy.int` to the WebGPU delegate as a gather: picks a fixed index along one dim, producing an output of rank (input rank - 1).

Composition (single dispatch):
- `select/Select.cpp` — reads `[self, dim, index, out]` (static `Int` via `read_scalar`; throws on dynamic `SymInt`), normalizes + bounds-checks dim/index, builds 2 `TensorMeta` UBOs + a `SelectParams{dim,index}`, fp32 guard, 1D-dispatch over `numel`, releases uniforms after the bind group.
- `select/select.wgsl` — seeds the input offset with `index * in.strides[dim]`, delinearizes the output index, maps each out dim to its in dim (shifted past the selected dim), relinearizes on input strides.
ghstack-source-id: 397026510
@exported-using-ghexport

Differential Revision: [D108793166](https://our.internmc.facebook.com/intern/diff/D108793166/)
…ework)

Pull Request resolved: #20363

Registers `aten.select_copy.int` in the `cases.py` op-test framework: a `_select_suite` of 4 configs (leading/middle/last dim + negative index) that `generate_op_tests` exports and compares to a torch golden on Dawn. Also adds `test/ops/select/test_select.py` (`SelectModule` + `CONFIGS` + an export-delegation/eager smoke test) and the `aten.select_copy.int` partitioner-allowlist entry in `tester.py`.
ghstack-source-id: 397026513
@exported-using-ghexport

Differential Revision: [D108793161](https://our.internmc.facebook.com/intern/diff/D108793161/)
Pull Request resolved: #20390

Adds `aten.sigmoid.default` to the WebGPU delegate: element-wise `1/(1+exp(-x))` over a flat fp32 buffer. On the Llama critical path (`F.silu` -> `sigmoid` + `mul`).

Composition (single dispatch):
- `sigmoid/UnaryOp.cpp` — binds input (storage, read-only) + output (storage) + a `Params{num_elements}` uniform, 1D-dispatches over `num_elements` with `override wg_size` (clamped to the device limit); mirrors the `add` op (uniform mapped-at-creation, released after the bind group).
- `sigmoid/sigmoid.wgsl` — guards `idx >= num_elements` and writes the logistic of each element.
ghstack-source-id: 397026515
@exported-using-ghexport

Differential Revision: [D108793157](https://our.internmc.facebook.com/intern/diff/D108793157/)
Pull Request resolved: #20391

Registers `aten.sigmoid.default` in the `cases.py` op-test framework: a `_sigmoid_suite` (hard-coded shapes + a saturation case over a `linspace(-12, 12)` input) that `generate_op_tests` exports and compares to an fp64 torch golden on Dawn. Also adds `test/ops/sigmoid/test_sigmoid.py` (`SigmoidModule` + `N` + `_det_input` + an export-delegation/eager smoke test) and the `aten.sigmoid.default` partitioner-allowlist entry in `tester.py`.
ghstack-source-id: 397026520
@exported-using-ghexport

Differential Revision: [D108793159](https://our.internmc.facebook.com/intern/diff/D108793159/)
Pull Request resolved: #20392

Adds `aten.squeeze_copy.dims` and `aten.unsqueeze_copy.default` to the WebGPU delegate. Both are numel-preserving shape ops; on a dense row-major buffer backend they are the same flat copy as `view_copy` — only the shape metadata differs (mirrors the Vulkan delegate, which routes both through `add_view_copy_node`).

Composition (no new kernel):
- `squeeze/Squeeze.cpp` — reads `args = [self, dims, out]`, ignores the AOT-fixed `dims`, calls `add_flat_copy(graph, in, out)` from `runtime/ops/view_copy/view_copy.h`.
- `unsqueeze/Unsqueeze.cpp` — reads `args = [self, dim, out]`, ignores the AOT-fixed `dim`, calls `add_flat_copy(graph, in, out)`.
ghstack-source-id: 397026523
@exported-using-ghexport

Differential Revision: [D108793153](https://our.internmc.facebook.com/intern/diff/D108793153/)
….py op-test framework)

Pull Request resolved: #20393

Registers `aten.squeeze_copy.dims` and `aten.unsqueeze_copy.default` in the `cases.py` op-test framework: a `_squeeze_suite` of 3 configs (squeeze leading/middle/multiple size-1 dims) and a `_unsqueeze_suite` of 3 configs (insert dim at front/middle/last) that `generate_op_tests` exports via `VulkanPartitioner` and compares to a torch golden on Dawn. Also adds `test/ops/squeeze/test_squeeze.py` (`SqueezeModule` + `CONFIGS` + `_op_delegated` smoke test), `test/ops/unsqueeze/test_unsqueeze.py` (`UnsqueezeModule` + `CONFIGS` + `_op_delegated` smoke test), and the two partitioner-allowlist entries in `tester.py`.
ghstack-source-id: 397026525
@exported-using-ghexport

Differential Revision: [D108793152](https://our.internmc.facebook.com/intern/diff/D108793152/)
@JulianCloudNTH JulianCloudNTH merged commit 28e2e3d into gh/JulianCloudNTH/34/orig Jun 26, 2026
148 of 153 checks passed
@JulianCloudNTH JulianCloudNTH deleted the gh/JulianCloudNTH/35/orig branch June 26, 2026 15:36
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