[ExecuTorch][WebGPU] Dynamic resize hook for view_copy by JulianCloudNTH · Pull Request #20579 · pytorch/executorch · GitHub
Skip to content

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

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

[ExecuTorch][WebGPU] Dynamic resize hook for view_copy#20579
JulianCloudNTH wants to merge 6 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.
@exported-using-ghexport

Differential Revision: D109906098

Differential Revision: D109906098

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

@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

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]

@SS-JIA SS-JIA 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.

Review automatically exported from Phabricator review in Meta.

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

3 participants