-
Notifications
You must be signed in to change notification settings - Fork 608
Migrate the quantizer to use aten ops directly #4195
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
Closed
Closed
Changes from all commits
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
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
|
||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass, field | ||
from typing import Callable, List, Optional, Tuple, Type, Union | ||
from typing import List, Optional, Tuple, Union | ||
|
||
import torch | ||
from executorch.backends.cadence.aot.quantizer.utils import get_bias_qparams | ||
|
@@ -47,17 +47,15 @@ class PartitionAnchors: | |
|
||
class QuantizationPattern(ABC): | ||
@abstractmethod | ||
def partition_types( | ||
self, | ||
) -> Union[List[Type[torch.nn.Module]], List[Callable[..., torch.Tensor]]]: | ||
def partition_types(self) -> list[OpOverload]: | ||
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. I think we need to support Python 3.8 here, which doesn't support |
||
""" | ||
List of types to be passed to find_sequential_partitions. | ||
List of types to be passed to find_sequential_partitions_aten. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
self, gm: torch.fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
) -> Optional[PartitionAnchors]: | ||
pass | ||
|
||
|
@@ -71,8 +69,8 @@ def replacement_op(self) -> OpOverload: | |
|
||
|
||
class AddmmPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Type[torch.nn.Module]]: | ||
return [torch.addmm] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.addmm.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
@@ -103,8 +101,8 @@ def replacement_op(self) -> OpOverload: | |
|
||
|
||
class BmmPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Callable[..., torch.Tensor]]: | ||
return [torch.bmm] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.bmm.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
@@ -123,8 +121,8 @@ def replacement_op(self) -> OpOverload: | |
|
||
|
||
class Conv1dPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Type[torch.nn.Module]]: | ||
return [torch.nn.Conv1d] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.conv1d.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
@@ -161,8 +159,8 @@ def replacement_op(self) -> OpOverload: | |
|
||
|
||
class Conv2dPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Type[torch.nn.Module]]: | ||
return [torch.nn.Conv2d] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.conv2d.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
@@ -199,32 +197,8 @@ def replacement_op(self) -> OpOverload: | |
|
||
|
||
class LayerNormPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Type[torch.nn.Module]]: | ||
return [torch.nn.LayerNorm] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
) -> PartitionAnchors: | ||
layer_norm_node = fused_partition[0].nodes[-1] | ||
|
||
# Weights and biases are used as fp32 by our kernel, so they are | ||
# passed in as others here along with the normalized shape. | ||
return PartitionAnchors( | ||
inputs=[(layer_norm_node, 0)], | ||
weights=[], | ||
biases=[], | ||
# Ordering: normalized_shape, weights, bias | ||
others=[(layer_norm_node, 1), (layer_norm_node, 2), (layer_norm_node, 3)], | ||
output=[(layer_norm_node,)], | ||
) | ||
|
||
def replacement_op(self) -> OpOverload: | ||
return torch.ops.cadence.quantized_layer_norm.default | ||
|
||
|
||
class LayerNormFunctionalPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Callable[..., torch.Tensor]]: | ||
return [torch.nn.functional.layer_norm] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.layer_norm.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
@@ -257,8 +231,8 @@ def replacement_op(self) -> OpOverload: | |
|
||
|
||
class LinearPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Type[torch.nn.Module]]: | ||
return [torch.nn.Linear] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.linear.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
@@ -294,47 +268,9 @@ def replacement_op(self) -> OpOverload: | |
return torch.ops.cadence.quantized_linear.default | ||
|
||
|
||
class LinearFunctionalPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Callable[..., torch.Tensor]]: | ||
return [torch.nn.functional.linear] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
) -> PartitionAnchors: | ||
linear_node = fused_partition[0].nodes[-1] | ||
|
||
bias_qspec = DerivedQuantizationSpec( | ||
derived_from=[ | ||
(linear_node.args[0], linear_node), | ||
(linear_node.args[1], linear_node), | ||
], | ||
derive_qparams_fn=get_bias_qparams, | ||
dtype=torch.int32, | ||
quant_min=-(2**31), | ||
quant_max=2**31 - 1, | ||
qscheme=torch.per_tensor_affine, | ||
) | ||
|
||
# Keep bias empty if not supplied | ||
bias = [] | ||
if len(linear_node.args) > 2 and linear_node.args[2] is not None: | ||
bias = [(linear_node, 2, bias_qspec)] | ||
|
||
return PartitionAnchors( | ||
inputs=[(linear_node, 0)], | ||
weights=[(linear_node, 1)], | ||
# pyre-fixme[6]: Incompatible parameter type | ||
biases=bias, | ||
output=[(linear_node,)], | ||
) | ||
|
||
def replacement_op(self) -> OpOverload: | ||
return torch.ops.cadence.quantized_linear.default | ||
|
||
|
||
class MatmulPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Callable[..., torch.Tensor]]: | ||
return [torch.matmul] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.matmul.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
@@ -353,8 +289,8 @@ def replacement_op(self) -> OpOverload: | |
|
||
|
||
class ReluPattern(QuantizationPattern): | ||
def partition_types(self) -> List[Type[torch.nn.Module]]: | ||
return [torch.nn.ReLU] | ||
def partition_types(self) -> List[OpOverload]: | ||
return [torch.ops.aten.relu.default] | ||
|
||
def get_anchors( | ||
self, gm: fx.GraphModule, fused_partition: List[fx.GraphModule] | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Is this a duplicate and not properly stacked with
#4047?
You can use "gh-stack" to help with this in the future