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

[ExecuTorch][WebGPU] Dynamic resize hook for linear_q4gsw#20576

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

[ExecuTorch][WebGPU] Dynamic resize hook for linear_q4gsw#20576
JulianCloudNTH wants to merge 6 commits into
gh/JulianCloudNTH/68/basefrom
gh/JulianCloudNTH/68/head

Conversation

@JulianCloudNTH

@JulianCloudNTH JulianCloudNTH commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Make the 4-bit quantized linear serve any live M (rows) from one graph, so a dynamic prefill+decode graph computes correct-size outputs.

Problem: linear_q4gsw baked its dispatch count, params.M, and output shape at build() for the max M. On a dynamic-shape graph at a smaller live M (e.g. decode M=1 vs prefill M=S) it would over-dispatch and leave the output sized at the max.

Solution:

  • Before: one fixed dispatch sized for the build-time M.
  • After: a tensor resize hook on the input recomputes the live M from cur_dims, rewrites params.M, updates the dispatch workgroup_count_x for the SAME kernel chosen at build (bicol GEMV / shmem GEMM / register-tiled), and sets the output cur_dims (= input dims with the last dim replaced by N). Inert until the input is resized.

Implementation:

  • The build-time kernel select (bicol GEMV for M==1, else shmem GEMM for large K/N, else register-tiled) is fixed at build; the hook re-runs compute_q4gsw_workgroup_count for whichever of the three the build chose and rewrites the param UBO + output dims for the live M — it does not switch kernels (runtime M-switching is a separate optimization).
  • own_uniform_buffer keeps the param UBO alive so the hook can rewrite it.
  • Mirrors Vulkan resize_q4gsw_linear_node (recompute M-derived dispatch each execute).

Constraints: Behavior-neutral on static graphs (hook fires only when the input's live M differs from the max). No kernel/WGSL/numerics change. Runtime M-based kernel switching is deliberately out of scope (a later opt diff).

Co-authored-with: Claude Code.
@exported-using-ghexport

Differential Revision: D109906094

Differential Revision: D109906094

[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.

@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