Repository navigation
[ET-VK] Keep q4gsw embedding and linear in the model dtype - #25792
msluszniak wants to merge 2 commits into
Conversation
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.
🔗 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. |
This PR needs a
|
SS-JIA
left a comment
There was a problem hiding this comment.
🤖 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, |
There was a problem hiding this comment.
[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: |
There was a problem hiding this comment.
[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.
| 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 |
There was a problem hiding this comment.
[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
left a comment
There was a problem hiding this comment.
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.
Summary
linear_q4gswandembedding_q4gswalways 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