-
Notifications
You must be signed in to change notification settings - Fork 383
support fp8 scaled_embedding_bag pattern match #3406
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
Open
shiyang-weng
wants to merge
3
commits into
pytorch:main
Choose a base branch
from
shiyang-weng:wengshiy/scaled_embedding_bag_pattern_match
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import copy | ||
| import functools | ||
| import itertools | ||
| import operator | ||
| from typing import Any | ||
|
|
||
| import torch | ||
|
|
@@ -2851,6 +2852,113 @@ def _register_qlinear_binary_fusion(): | |
| ) | ||
|
|
||
|
|
||
| def _register_scaled_embedding_bag_pass(pattern, pass_number, dtype=torch.float32): | ||
| @register_freezing_graph_pattern( | ||
| pattern, | ||
| pass_number=pass_number, | ||
| ) | ||
| def scaled_embedding_bag(match: Match, *args, **kwargs): | ||
| assert dtype in [torch.float32, torch.bfloat16] | ||
|
|
||
| getitem_node = match.output_node() | ||
| embedding_bag_node = getitem_node.args[0] | ||
| assert embedding_bag_node.target is aten._embedding_bag_forward_only.default | ||
|
|
||
| embedding_bag_weight_index = 0 | ||
| if dtype == torch.float32: | ||
| # pattern: embedding_bag -> dequant | ||
| dequant_node = embedding_bag_node.args[embedding_bag_weight_index] | ||
| else: | ||
| # pattern: embedding_bag -> to_bf16 -> dequant | ||
| weight_to_bf16_node = embedding_bag_node.args[embedding_bag_weight_index] | ||
| dequant_node = weight_to_bf16_node.args[0] | ||
|
|
||
| assert dequant_node.target in [ | ||
| quantized_decomposed.dequantize_per_tensor.default, | ||
| quantized_decomposed.dequantize_per_tensor.tensor, | ||
| torch.ops.torchao.dequantize_affine_float8_non_decomposed.default, | ||
| ] | ||
|
|
||
| # Weight QParams | ||
| qw, w_scale = kwargs["x"], kwargs["x_scale"] | ||
|
|
||
| # Input Params | ||
| indices, offsets, mode, include_last_offset = ( | ||
| kwargs["indices"], | ||
| kwargs["offsets"], | ||
| kwargs["mode"], | ||
| kwargs["include_last_offset"], | ||
| ) | ||
| # only support fp32 output, next setp support more dtype | ||
| o_scale = 1.0 | ||
|
|
||
| graph = match.graph | ||
| with graph.inserting_before(getitem_node): | ||
| new_args: tuple[Any, ...] = ( | ||
| qw, | ||
| indices, | ||
| offsets, | ||
| w_scale, | ||
| o_scale, | ||
| mode, | ||
| include_last_offset, | ||
| torch.float, | ||
| ) | ||
|
|
||
| new_embedding_bag_node = graph.call_function( | ||
| torch.ops.torchao._scaled_embedding_bag.default, args=new_args | ||
| ) | ||
|
|
||
| getitem_node.replace_all_uses_with(new_embedding_bag_node) | ||
| new_embedding_bag_node.meta.update(embedding_bag_node.meta) | ||
|
|
||
| graph.erase_node(getitem_node) | ||
| graph.erase_node(embedding_bag_node) | ||
| if dtype == torch.bfloat16: | ||
| graph.erase_node(weight_to_bf16_node) # type: ignore[possibly-undefined] | ||
| # Erase the dequant pattern | ||
| graph.erase_node(dequant_node) | ||
|
|
||
| counters["inductor"]["scaled_embedding_bag_matcher_count"] += 1 | ||
| counters["inductor"]["scaled_embedding_bag_matcher_nodes"] += len(match.nodes) | ||
|
|
||
|
|
||
| def _generate_scaled_embedding_bag_patterns(dq_pattern): | ||
| embedding_bag_pattern = CallFunction( | ||
| torch.ops.aten._embedding_bag_forward_only.default, | ||
| dq_pattern, | ||
| KeywordArg("indices"), | ||
| KeywordArg("offsets"), | ||
| Arg(), | ||
| KeywordArg("mode"), | ||
| KeywordArg("sparse"), | ||
| Arg(), | ||
| KeywordArg("include_last_offset"), | ||
| ) | ||
| return CallFunction( | ||
| operator.getitem, | ||
| embedding_bag_pattern, | ||
| KeywordArg("item"), | ||
| ) | ||
|
|
||
|
|
||
| def _register_quantization_embeddingbag_pass(): | ||
| for dtype in [torch.float32, torch.bfloat16]: | ||
| _register_scaled_embedding_bag_pass( | ||
| _generate_scaled_embedding_bag_patterns( | ||
| _may_generate_pattern_with_dtype_convert( | ||
| get_dequantize_per_tensor_activation_pattern( | ||
| is_tensor_overload=False, is_fp8=True | ||
| ), | ||
| KeywordArg("autocast_act_dtype"), | ||
| dtype == torch.bfloat16, | ||
| ), | ||
| ), | ||
| pass_number=1, | ||
| dtype=dtype, | ||
| ) # pass_number=0 to run before weight prepack | ||
|
|
||
|
|
||
| @functools.lru_cache(None) | ||
| def _register_quantization_weight_pack_pass(): | ||
| # Step 1: Dequant promotion for int8-mixed-fp32/bf16 | ||
|
|
@@ -2874,6 +2982,8 @@ def _register_quantization_weight_pack_pass(): | |
| _register_qlinear_unary_fusion() | ||
| _register_qlinear_binary_fusion() | ||
|
|
||
| _register_quantization_embeddingbag_pass() | ||
|
||
|
|
||
|
|
||
| def quant_lift_up(module_graph: torch.fx.graph.Graph): | ||
| """ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.