Skip to content

[ET-VK] Keep q4gsw embedding and linear in the model dtype - #25792

Open
msluszniak wants to merge 2 commits into
pytorch:mainfrom
msluszniak:ms/vulkan-q4gsw-model-dtype
Open

msluszniak wants to merge 2 commits into
pytorch:mainfrom
msluszniak:ms/vulkan-q4gsw-model-dtype

Conversation

@msluszniak

@msluszniak msluszniak commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

linear_q4gsw and embedding_q4gsw always returned fp32, and the fused embedding took its scales' dtype. With fp32 scales on an fp16 model, a retrace turned the graph fp32 and SDPA's dtype checks failed. Cast back to the replaced op's dtype.

Test plan

pytest backends/vulkan/test/test_vulkan_passes.py -k "q4gsw or quantized_embedding"; the new test fails without the fix.

cc @nil-is-all

embedding_q4gsw and linear_q4gsw always produced fp32, and the fused
embedding took the scales' dtype, so fp32 scales on an fp16 model made the
retraced graph fp32 and failed SDPA's dtype checks. Cast back to the
replaced op's dtype.
@msluszniak
msluszniak requested a review from SS-JIA as a code owner November 27, 2026 16:15
@pytorch-bot

pytorch-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 Nov 27, 2026
@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.

@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Nov 27, 2026

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

🤖 AI-generated review. These findings come from an independent automated review (Claude, via @SS-JIA) of the head commit ea3ff03881. Each finding has a severity, a short ID, and a proposed fix. Please sanity-check them before acting on them.

Recommendation: Requires Changes. Both fixes are correct, but the linear_q4gsw / linear_dq8ca_q4gsw half has no test (A1, inline).

Findings not tied to a diff line:

A3 (Low): linear_q8ta_q8csw's reference impl has the same mixed-dtype bug. In custom_ops_lib.py (~L444–462), dequantize_per_channel uses the default fp32 out_dtype and then F.linear(x, weights). With an fp16 activation, this raises expected m1 and m2 to have the same dtype, but got: c10::Half != float in eager mode and on retrace. I found no common path that reaches it today (torchao int8-dynamic fp16 models don't fuse to this op), but it is latent for pt2e static int8 fp16 graphs and falls within this PR's scope.
Proposed fix: pass out_dtype=x.dtype to dequantize_per_channel, and cast bias the same way, either here or as a follow-up.

torch.int8,
-8,
7,
output_dtype=x.dtype,

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.

[AI review] A1 (Medium): The linear dtype fix is untested.

On main, calling linear_q4gsw / linear_dq8ca_q4gsw with an fp16 activation, eagerly or on retrace, raises expected m1 and m2 to have the same dtype, but got: c10::Half != float. This line fixes that, but reverting it leaves every test green. The only new test covers the embedding.

Proposed fix: add a small test that calls torch.ops.et_vk.linear_q4gsw (and linear_dq8ca_q4gsw) with an fp16 input, using both fp16 and fp32 scales. Assert that out.dtype == torch.float16 and that the output is allclose to the fp32 computation. Alternatively, extend test_fuse_q4gsw_linear_keeps_preceding_reshape with an .half() model and check ep.module()(x).dtype.

emb_dtype = scales_val.dtype if scales_val is not None else out_val.dtype
embedding_q4gsw_node.meta["val"] = out_val.to(emb_dtype)
result = embedding_q4gsw_node
if emb_dtype != out_val.dtype:

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.

[AI review] A2 (Low): Extra cast dispatch where the shader could write the model dtype directly.

For this PR's target case (fp16 model, fp32 scales), this adds a separate _to_dim_order_copy plus an fp32 [*, S, D] intermediate: one extra dispatch per forward, and 2× embedding activation memory during prefill. The runtime already decouples the two dtypes: add_embedding_q4gsw_node picks _float_scales from the scales dtype and the shader's dtype suffix from out.

Proposed fix: add ScalarType? dtype=None to the embedding_q4gsw schema. The impl returns .to(dtype or weight_scales.dtype), the pattern passes out_val.dtype, and C++ reads the dtype from out, which already selects the shader. That removes the cast node and this branch. If you'd rather not change the schema, note in this docstring that the cast is a known cost.

Comment thread backends/vulkan/custom_ops_lib.py Outdated
unpacked_groups = unpacked.reshape(weight.shape[0], num_groups, group_size)
dequantized = unpacked_groups.float() * scales.float()
dequantized = dequantized.reshape(weight.shape[0], -1)
# Output in the scales' dtype, like the fused embedding: always returning

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.

[AI review] A4 (Nit): "like the fused embedding" is vague, since this reference impl is the fused embedding. Suggest: "Output in the scales' dtype; the fusion pattern casts to the model dtype when they differ." Or drop the comment if A2 is adopted.

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

Requesting changes based on the findings in the AI review above. Please address the Medium/High findings (or reply if you disagree); Low/Nit items are optional.

Cover linear_q4gsw and linear_dq8ca_q4gsw with fp16 and fp32 scales, and
give linear_q8ta_q8csw and the q4gsw bias the same activation-dtype cast.

This branch has not been deployed

No deployments
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. community: contribution PRs coming from community (excluding hardware partners)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants