Repository navigation
[ET-VK] Keep q4gsw embedding and linear in the model dtype #25792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,38 @@ | |
| from executorch.exir.dialects._ops import ops as exir_ops | ||
|
|
||
|
|
||
| def _replace_anchor_with_embedding( | ||
| graph_module: torch.fx.GraphModule, | ||
| match: PatternMatch, | ||
| embedding_q4gsw_node: torch.fx.Node, | ||
| ) -> None: | ||
| """ | ||
| et_vk.embedding_q4gsw outputs the dtype of its scales. When the replaced | ||
| op produced another dtype (e.g. fp32 scales feeding an fp16 model), cast | ||
| back to it explicitly, so that the graph's dtypes survive a retrace. | ||
| The cast is a known cost: one extra dispatch and an intermediate in the | ||
| scales' dtype. | ||
| """ | ||
| out_val = match.anchor_node.meta["val"] | ||
| scales_val = match.scales_node.meta.get("val") | ||
| 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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Proposed fix: add |
||
| with graph_module.graph.inserting_after(embedding_q4gsw_node): | ||
| result = graph_module.graph.create_node( | ||
| "call_function", | ||
| exir_ops.edge.dim_order_ops._to_dim_order_copy.default, | ||
| args=(embedding_q4gsw_node,), | ||
| kwargs={ | ||
| "dtype": out_val.dtype, | ||
| "dim_order": list(range(out_val.dim())), | ||
| }, | ||
| ) | ||
| result.meta["val"] = out_val | ||
| match.anchor_node.replace_all_uses_with(result) | ||
|
|
||
|
|
||
| embedding_4bit_target = exir_ops.edge.quantized_decomposed.embedding_4bit.dtype | ||
| embedding_target = exir_ops.edge.aten.embedding.default | ||
| torchao_dequantize_affine_target = exir_ops.edge.torchao.dequantize_affine.default | ||
|
|
@@ -154,8 +186,7 @@ def replace_quantized_embedding_patterns( | |
| ), | ||
| ) | ||
|
|
||
| embedding_q4gsw_node.meta["val"] = match.anchor_node.meta["val"] | ||
| match.anchor_node.replace_all_uses_with(embedding_q4gsw_node) | ||
| _replace_anchor_with_embedding(graph_module, match, embedding_q4gsw_node) | ||
|
|
||
|
|
||
| class TorchAOQuantizedEmbeddingMatch(PatternMatch): | ||
|
|
@@ -338,5 +369,4 @@ def replace_torchao_quantized_embedding_patterns( | |
| ), | ||
| ) | ||
|
|
||
| embedding_q4gsw_node.meta["val"] = match.anchor_node.meta["val"] | ||
| match.anchor_node.replace_all_uses_with(embedding_q4gsw_node) | ||
| _replace_anchor_with_embedding(graph_module, match, embedding_q4gsw_node) | ||
There was a problem hiding this comment.
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, callinglinear_q4gsw/linear_dq8ca_q4gswwith an fp16 activation, eagerly or on retrace, raisesexpected 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(andlinear_dq8ca_q4gsw) with an fp16 input, using both fp16 and fp32 scales. Assert thatout.dtype == torch.float16and that the output is allclose to the fp32 computation. Alternatively, extendtest_fuse_q4gsw_linear_keeps_preceding_reshapewith an.half()model and checkep.module()(x).dtype.