Skip to content

[ExecuTorch][WebGPU] Dynamic resize hook for view_copy#20579

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

[ExecuTorch][WebGPU] Dynamic resize hook for view_copy#20579
JulianCloudNTH wants to merge 2 commits into
gh/JulianCloudNTH/71/basefrom
gh/JulianCloudNTH/71/head

Conversation

@JulianCloudNTH

@JulianCloudNTH JulianCloudNTH commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Make view_copy track the live sequence length under dynamic shapes.

Problem: view_copy lowers to a flat DMA buffer copy (add_buffer_copy) sized at the build-time max shape. With one dynamic graph serving any seq-len S (prefill S=K, decode S=1), the copy moved the full max-S byte count and the output kept its max dims, so a downstream consumer read a live shape that was too large.

Solution: register a tensor resize hook on the input so the copy follows the live input numel (a view preserves numel).

  • Before: copy_nbytes and the output dims are fixed at the serialized max.
  • After: the hook recomputes the live numel from cur_dims(in), scales the single dynamic output dim to preserve numel, sets the output cur_dims, and rewrites the Copy dispatch's copy_nbytes.

Implementation:

  • Keep the existing DMA path (Kind::Copy); the hook only rewrites copy_nbytes via dispatch_at, no new kernel.
  • Handle the aliased in/out fast path (no copy emitted) by still setting the output cur_dims so the resize cascade reaches consumers.
  • Mirrors Vulkan's view_buffer contiguous fast path; numel-preserving like the other dynamic-shape op hooks.

Constraints: inert on a static graph (cur_dims == dims), so byte-identical to the prior behavior; fp32-only and numel-preserving invariants unchanged.

Co-authored-with: Claude Code.

Differential Revision: D109906098

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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.

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

Copy link
Copy Markdown
Contributor Author

@claude review and check for any areas or opportunities for modularization

@claude

claude Bot commented Jun 29, 2026

Copy link
Copy Markdown

Claude finished @JulianCloudNTH's task in 1m 47s —— View job


Review: Dynamic resize hook for view_copy

  • Read changed file & surrounding context
  • Review correctness of resize hook logic
  • Check for modularization opportunities
  • Post review feedback

The change is sound and matches the established dynamic-shape pattern in this backend (add_tensor_resize_hook + set_cur_dims + dispatch_at, as in QuantizedLinear.cpp, EmbeddingQ4gsw.cpp, Select.cpp). The static-graph inertness claim holds: with cur_dims == dims, target == maxnumel, so od == out_max and copy_nbytes == out_tensor.nbytes — byte-identical. Handling the aliased fast path by still setting cur_dims so the cascade reaches consumers is correct. Below are correctness notes and modularization opportunities.

Correctness

1. Dim-scaling picks the leftmost numel-divisible dim, not necessarily the dynamic one (ViewCopy.cpp:57-66)

The loop breaks on the first d where target % rest == 0 && nd <= out_max[d]. For the common LLM shape [1, K, H] this works (the leading 1 is naturally skipped because rest is too large), but it's a heuristic, not a guarantee. If two dims are independently scalable it silently picks the wrong axis. Since the copy itself is flat (numel is all that matters for the DMA), the only risk is the output cur_dims shape handed to a downstream consumer that cares about more than numel. Worth a comment documenting the single-dynamic-dim assumption, matching how QuantizedLinear hardcodes od.back() = N and Select removes a known dim — those ops know which dim moves; here it's inferred.

2. Potential divide-by-zero on a zero-extent dim (ViewCopy.cpp:58)

maxnumel / static_cast<uint64_t>(out_max[d]) divides by zero if any out_max[d] == 0. The rest != 0 guard is after the division, so it doesn't protect against it. Unlikely for a real view output, but a zero-extent tensor would crash here rather than fail loud.

3. target * sizeof(float) overflow ordering (ViewCopy.cpp:70-71)

static_cast<size_t>(target) * sizeof(float) casts to size_t before multiplying. On a wasm32 target (size_t 32-bit) a large target truncates before the *4. This matches the existing build-time out_tensor.nbytes constraint so it's not a regression, just flagging consistency.

Modularization

A. Have add_buffer_copy return the dispatch index (recommended)

add_dispatch already returns its index, but add_buffer_copy is void, forcing the fragile:

const size_t dispatch_idx = aliased ? 0 : graph.num_dispatches() - 1;

The aliased ? 0 branch is a dead sentinel (only read under if (!aliased)), and num_dispatches() - 1 couples this code to push-back ordering. Making add_buffer_copy return size_t (mirroring add_dispatch) lets you write const size_t dispatch_idx = graph.add_buffer_copy(...) inside the if (!aliased) block and drop the sentinel entirely. Fix this →

B. Extract the numel-preserving dim scaling into WebGPUUtils.h

The ~12-line block at ViewCopy.cpp:54-67 is non-trivial, subtle, and a generic operation ("scale one dim of out_max to reach target numel"). Per CLAUDE.md, single-use helpers are discouraged unless they significantly improve readability — this is a case where a named, documented utils::scale_dims_to_numel(out_max, target) would clarify intent at the call site, encode the single-dynamic-dim assumption in one place, and become unit-testable in isolation. It also positions the helper for reuse if another numel-preserving op (e.g. a future reshape/squeeze) needs the same logic.

These are all polish; nothing blocks the core fix. (A) is the cleanest win and removes a genuine footgun.
· branch gh/JulianCloudNTH/71/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.

2 participants