mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Fixes #112988 For files __init__.py _correct_bias.py _equalize.py _learnable_fake_quantize.py backend_config experimental fake_quantize.py fuse_modules.py fuser_method_mappings.py Correct the following __init__.py:1 at module level: D104: Missing docstring in public package __init__.py:144 in public function `default_eval_fn`: D205: 1 blank line required between summary line and description (found 0) __init__.py:144 in public function `default_eval_fn`: D400: First line should end with a period (not 'f') __init__.py:144 in public function `default_eval_fn`: D401: First line should be in imperative mood; try rephrasing (found 'Default') __init__.py:152 in private class `_DerivedObserverOrFakeQuantize`: D204: 1 blank line required after class docstring (found 0) __init__.py:152 in private class `_DerivedObserverOrFakeQuantize`: D205: 1 blank line required between summary line and description (found 0) __init__.py:152 in private class `_DerivedObserverOrFakeQuantize`: D210: No whitespaces allowed surrounding docstring text __init__.py:152 in private class `_DerivedObserverOrFakeQuantize`: D400: First line should end with a period (not 's') _correct_bias.py:20 in public function `get_module`: D200: One-line docstring should fit on one line with quotes (found 2) _correct_bias.py:20 in public function `get_module`: D210: No whitespaces allowed surrounding docstring text _correct_bias.py:20 in public function `get_module`: D300: Use """triple double quotes""" (found '''-quotes) _correct_bias.py:20 in public function `get_module`: D400: First line should end with a period (not 'l') _correct_bias.py:25 in public function `parent_child_names`: D200: One-line docstring should fit on one line with quotes (found 2) _correct_bias.py:25 in public function `parent_child_names`: D300: Use """triple double quotes""" (found '''-quotes) _correct_bias.py:25 in public function `parent_child_names`: D400: First line should end with a period (not 'e') _correct_bias.py:25 in public function `parent_child_names`: D401: First line should be in imperative mood (perhaps 'Split', not 'Splits') _correct_bias.py:34 in public function `get_param`: D205: 1 blank line required between summary line and description (found 0) _correct_bias.py:34 in public function `get_param`: D210: No whitespaces allowed surrounding docstring text _correct_bias.py:34 in public function `get_param`: D300: Use """triple double quotes""" (found '''-quotes) _correct_bias.py:34 in public function `get_param`: D400: First line should end with a period (not 's') _correct_bias.py:44 in public class `MeanShadowLogger`: D204: 1 blank line required after class docstring (found 0) _correct_bias.py:44 in public class `MeanShadowLogger`: D205: 1 blank line required between summary line and description (found 0) _correct_bias.py:44 in public class `MeanShadowLogger`: D400: First line should end with a period (not 'n') _correct_bias.py:47 in public method `__init__`: D107: Missing docstring in __init__ _correct_bias.py:56 in public method `forward`: D205: 1 blank line required between summary line and description (found 0) _correct_bias.py:56 in public method `forward`: D210: No whitespaces allowed surrounding docstring text _correct_bias.py:56 in public method `forward`: D300: Use """triple double quotes""" (found '''-quotes) _correct_bias.py:56 in public method `forward`: D401: First line should be in imperative mood; try rephrasing (found 'The') _correct_bias.py:77 in public method `clear`: D102: Missing docstring in public method _correct_bias.py:85 in public function `bias_correction`: D205: 1 blank line required between summary line and description (found 0) _correct_bias.py:85 in public function `bias_correction`: D210: No whitespaces allowed surrounding docstring text _correct_bias.py:85 in public function `bias_correction`: D300: Use """triple double quotes""" (found '''-quotes) _correct_bias.py:85 in public function `bias_correction`: D400: First line should end with a period (not 's') _correct_bias.py:85 in public function `bias_correction`: D401: First line should be in imperative mood (perhaps 'Use', not 'Using') _equalize.py:22 in public function `set_module_weight`: D103: Missing docstring in public function _equalize.py:28 in public function `set_module_bias`: D103: Missing docstring in public function _equalize.py:34 in public function `get_module_weight`: D103: Missing docstring in public function _equalize.py:40 in public function `get_module_bias`: D103: Missing docstring in public function _equalize.py:47 in public function `max_over_ndim`: D200: One-line docstring should fit on one line with quotes (found 2) _equalize.py:47 in public function `max_over_ndim`: D210: No whitespaces allowed surrounding docstring text _equalize.py:47 in public function `max_over_ndim`: D300: Use """triple double quotes""" (found '''-quotes) _equalize.py:47 in public function `max_over_ndim`: D400: First line should end with a period (not 's') _equalize.py:47 in public function `max_over_ndim`: D401: First line should be in imperative mood (perhaps 'Apply', not 'Applies') _equalize.py:55 in public function `min_over_ndim`: D200: One-line docstring should fit on one line with quotes (found 2) _equalize.py:55 in public function `min_over_ndim`: D210: No whitespaces allowed surrounding docstring text _equalize.py:55 in public function `min_over_ndim`: D300: Use """triple double quotes""" (found '''-quotes) _equalize.py:55 in public function `min_over_ndim`: D400: First line should end with a period (not 's') _equalize.py:55 in public function `min_over_ndim`: D401: First line should be in imperative mood (perhaps 'Apply', not 'Applies') _equalize.py:63 in public function `channel_range`: D200: One-line docstring should fit on one line with quotes (found 2) _equalize.py:63 in public function `channel_range`: D210: No whitespaces allowed surrounding docstring text _equalize.py:63 in public function `channel_range`: D300: Use """triple double quotes""" (found '''-quotes) _equalize.py:63 in public function `channel_range`: D400: First line should end with a period (not 'l') _equalize.py:63 in public function `channel_range`: D401: First line should be in imperative mood (perhaps 'Find', not 'finds') _equalize.py:63 in public function `channel_range`: D403: First word of the first line should be properly capitalized ('Finds', not 'finds') _equalize.py:76 in public function `cross_layer_equalization`: D205: 1 blank line required between summary line and description (found 0) _equalize.py:76 in public function `cross_layer_equalization`: D210: No whitespaces allowed surrounding docstring text _equalize.py:76 in public function `cross_layer_equalization`: D300: Use """triple double quotes""" (found '''-quotes) _equalize.py:76 in public function `cross_layer_equalization`: D400: First line should end with a period (not 't') _equalize.py:120 in public function `equalize`: D205: 1 blank line required between summary line and description (found 0) _equalize.py:120 in public function `equalize`: D210: No whitespaces allowed surrounding docstring text _equalize.py:120 in public function `equalize`: D300: Use """triple double quotes""" (found '''-quotes) _equalize.py:120 in public function `equalize`: D400: First line should end with a period (not 'l') _equalize.py:159 in public function `converged`: D205: 1 blank line required between summary line and description (found 0) _equalize.py:159 in public function `converged`: D210: No whitespaces allowed surrounding docstring text _equalize.py:159 in public function `converged`: D300: Use """triple double quotes""" (found '''-quotes) _equalize.py:159 in public function `converged`: D400: First line should end with a period (not 's') _equalize.py:159 in public function `converged`: D401: First line should be in imperative mood (perhaps 'Test', not 'Tests') _learnable_fake_quantize.py:8 in private class `_LearnableFakeQuantize`: D204: 1 blank line required after class docstring (found 0) _learnable_fake_quantize.py:8 in private class `_LearnableFakeQuantize`: D205: 1 blank line required between summary line and description (found 0) _learnable_fake_quantize.py:8 in private class `_LearnableFakeQuantize`: D210: No whitespaces allowed surrounding docstring text _learnable_fake_quantize.py:8 in private class `_LearnableFakeQuantize`: D400: First line should end with a period (not 'h') _learnable_fake_quantize.py:68 in private method `enable_param_learning`: D205: 1 blank line required between summary line and description (found 0) _learnable_fake_quantize.py:68 in private method `enable_param_learning`: D400: First line should end with a period (not 'd') _learnable_fake_quantize.py:68 in private method `enable_param_learning`: D401: First line should be in imperative mood (perhaps 'Enable', not 'Enables') _learnable_fake_quantize.py:78 in private method `enable_static_estimate`: D205: 1 blank line required between summary line and description (found 0) _learnable_fake_quantize.py:78 in private method `enable_static_estimate`: D400: First line should end with a period (not 'f') _learnable_fake_quantize.py:78 in private method `enable_static_estimate`: D401: First line should be in imperative mood (perhaps 'Enable', not 'Enables') _learnable_fake_quantize.py:87 in private method `enable_static_observation`: D205: 1 blank line required between summary line and description (found 0) _learnable_fake_quantize.py:87 in private method `enable_static_observation`: D400: First line should end with a period (not 't') _learnable_fake_quantize.py:87 in private method `enable_static_observation`: D401: First line should be in imperative mood (perhaps 'Enable', not 'Enables') fake_quantize.py:1 at module level: D205: 1 blank line required between summary line and description (found 0) fake_quantize.py:1 at module level: D400: First line should end with a period (not 'n') fake_quantize.py:61 in public class `FakeQuantizeBase`: D205: 1 blank line required between summary line and description (found 0) fake_quantize.py:61 in public class `FakeQuantizeBase`: D210: No whitespaces allowed surrounding docstring text fake_quantize.py:61 in public class `FakeQuantizeBase`: D400: First line should end with a period (not 'e') fake_quantize.py:74 in public method `__init__`: D107: Missing docstring in __init__ fake_quantize.py:83 in public method `forward`: D102: Missing docstring in public method fake_quantize.py:87 in public method `calculate_qparams`: D102: Missing docstring in public method fake_quantize.py:91 in public method `enable_fake_quant`: D102: Missing docstring in public method fake_quantize.py:95 in public method `disable_fake_quant`: D102: Missing docstring in public method fake_quantize.py:99 in public method `enable_observer`: D102: Missing docstring in public method fake_quantize.py:103 in public method `disable_observer`: D102: Missing docstring in public method fake_quantize.py:107 in public method `with_args`: D102: Missing docstring in public method fake_quantize.py:115 in public class `FakeQuantize`: D205: 1 blank line required between summary line and description (found 0) fake_quantize.py:115 in public class `FakeQuantize`: D210: No whitespaces allowed surrounding docstring text fake_quantize.py:115 in public class `FakeQuantize`: D412: No blank lines allowed between a section header and its content ('Attributes') fake_quantize.py:150 in public method `__init__`: D107: Missing docstring in __init__ fake_quantize.py:188 in public method `calculate_qparams`: D102: Missing docstring in public method fake_quantize.py:191 in public method `forward`: D102: Missing docstring in public method fake_quantize.py:214 in public method `extra_repr`: D102: Missing docstring in public method fake_quantize.py:262 in public class `FixedQParamsFakeQuantize`: D205: 1 blank line required between summary line and description (found 0) fake_quantize.py:262 in public class `FixedQParamsFakeQuantize`: D210: No whitespaces allowed surrounding docstring text fake_quantize.py:262 in public class `FixedQParamsFakeQuantize`: D400: First line should end with a period (not 'n') fake_quantize.py:268 in public method `__init__`: D107: Missing docstring in __init__ fake_quantize.py:279 in public method `calculate_qparams`: D102: Missing docstring in public method fake_quantize.py:283 in public method `extra_repr`: D102: Missing docstring in public method fake_quantize.py:292 in public class `FusedMovingAvgObsFakeQuantize`: D205: 1 blank line required between summary line and description (found 0) fake_quantize.py:292 in public class `FusedMovingAvgObsFakeQuantize`: D400: First line should end with a period (not 'e') fake_quantize.py:307 in public method `__init__`: D107: Missing docstring in __init__ fake_quantize.py:322 in public method `calculate_qparams`: D102: Missing docstring in public method fake_quantize.py:326 in public method `extra_repr`: D102: Missing docstring in public method fake_quantize.py:342 in public method `forward`: D102: Missing docstring in public method fake_quantize.py:480 in private function `_is_fake_quant_script_module`: D200: One-line docstring should fit on one line with quotes (found 2) fake_quantize.py:480 in private function `_is_fake_quant_script_module`: D210: No whitespaces allowed surrounding docstring text fake_quantize.py:480 in private function `_is_fake_quant_script_module`: D300: Use """triple double quotes""" (found '''-quotes) fake_quantize.py:480 in private function `_is_fake_quant_script_module`: D401: First line should be in imperative mood (perhaps 'Return', not 'Returns') fake_quantize.py:491 in public function `disable_fake_quant`: D400: First line should end with a period (not ':') fake_quantize.py:502 in public function `enable_fake_quant`: D400: First line should end with a period (not ':') fake_quantize.py:513 in public function `disable_observer`: D400: First line should end with a period (not ':') fake_quantize.py:524 in public function `enable_observer`: D400: First line should end with a period (not ':') fuse_modules.py:1 at module level: D100: Missing docstring in public module fuse_modules.py:39 in public function `fuse_known_modules`: D205: 1 blank line required between summary line and description (found 0) fuse_modules.py:39 in public function `fuse_known_modules`: D400: First line should end with a period (not 'd') fuse_modules.py:39 in public function `fuse_known_modules`: D401: First line should be in imperative mood (perhaps 'Return', not 'Returns') fuse_modules.py:104 in public function `fuse_modules`: D400: First line should end with a period (not 'e') fuse_modules.py:167 in public function `fuse_modules_qat`: D200: One-line docstring should fit on one line with quotes (found 2) fuse_modules.py:167 in public function `fuse_modules_qat`: D210: No whitespaces allowed surrounding docstring text fuse_modules.py:167 in public function `fuse_modules_qat`: D400: First line should end with a period (not '`') fuser_method_mappings.py:1 at module level: D100: Missing docstring in public module fuser_method_mappings.py:18 in public function `fuse_conv_bn`: D400: First line should end with a period (not 'e') fuser_method_mappings.py:55 in public function `fuse_conv_bn_relu`: D400: First line should end with a period (not 'e') fuser_method_mappings.py:102 in public function `fuse_linear_bn`: D400: First line should end with a period (not 'e') fuser_method_mappings.py:131 in public function `fuse_convtranspose_bn`: D400: First line should end with a period (not 'e') fuser_method_mappings.py:154 in private function `_sequential_wrapper2`: D205: 1 blank line required between summary line and description (found 0) fuser_method_mappings.py:154 in private function `_sequential_wrapper2`: D210: No whitespaces allowed surrounding docstring text fuser_method_mappings.py:154 in private function `_sequential_wrapper2`: D400: First line should end with a period (not 's') fuser_method_mappings.py:182 in public function `get_fuser_method`: D205: 1 blank line required between summary line and description (found 0) fuser_method_mappings.py:182 in public function `get_fuser_method`: D210: No whitespaces allowed surrounding docstring text fuser_method_mappings.py:182 in public function `get_fuser_method`: D300: Use """triple double quotes""" (found '''-quotes) fuser_method_mappings.py:182 in public function `get_fuser_method`: D400: First line should end with a period (not ',') fuser_method_mappings.py:205 in private function `_get_valid_patterns`: D205: 1 blank line required between summary line and description (found 0) fuser_method_mappings.py:205 in private function `_get_valid_patterns`: D400: First line should end with a period (not ',') fuser_method_mappings.py:205 in private function `_get_valid_patterns`: D401: First line should be in imperative mood (perhaps 'Return', not 'Returns') fuser_method_mappings.py:238 in public function `get_fuser_method_new`: D205: 1 blank line required between summary line and description (found 0) fuser_method_mappings.py:238 in public function `get_fuser_method_new`: D210: No whitespaces allowed surrounding docstring text fuser_method_mappings.py:238 in public function `get_fuser_method_new`: D400: First line should end with a period (not 'd') fuser_method_mappings.py:238 in public function `get_fuser_method_new`: D401: First line should be in imperative mood; try rephrasing (found 'This') Pull Request resolved: https://github.com/pytorch/pytorch/pull/112992 Approved by: https://github.com/kit1980
260 lines
10 KiB
Python
260 lines
10 KiB
Python
import torch.nn as nn
|
|
import torch.ao.nn.intrinsic as nni
|
|
|
|
from typing import Union, Callable, Tuple, Dict, Optional, Type
|
|
from torch.ao.quantization.utils import Pattern, get_combined_dict, MatchAllNode
|
|
import itertools
|
|
|
|
__all__ = [
|
|
"fuse_conv_bn",
|
|
"fuse_conv_bn_relu",
|
|
"fuse_linear_bn",
|
|
"fuse_convtranspose_bn",
|
|
"get_fuser_method",
|
|
"get_fuser_method_new",
|
|
]
|
|
|
|
def fuse_conv_bn(is_qat, conv, bn):
|
|
r"""Return the fused the conv and bn modules.
|
|
Given the conv and bn modules, fuses them and returns the fused module
|
|
|
|
Args:
|
|
is_qat: a flag for whether we are using quantization aware training fusion
|
|
or post training quantization fusion
|
|
conv: Module instance of type conv2d/conv3d
|
|
bn: Spatial BN instance that needs to be fused with the conv
|
|
|
|
Examples::
|
|
|
|
>>> m1 = nn.Conv2d(10, 20, 3)
|
|
>>> b1 = nn.BatchNorm2d(20)
|
|
>>> # xdoctest: +SKIP
|
|
>>> m2 = fuse_conv_bn(m1, b1)
|
|
"""
|
|
assert(conv.training == bn.training),\
|
|
"Conv and BN both must be in the same mode (train or eval)."
|
|
|
|
fused_module_class_map = {
|
|
nn.Conv1d: nni.ConvBn1d,
|
|
nn.Conv2d: nni.ConvBn2d,
|
|
nn.Conv3d: nni.ConvBn3d,
|
|
}
|
|
|
|
if is_qat:
|
|
assert bn.num_features == conv.out_channels, 'Output channel of Conv2d must match num_features of BatchNorm2d'
|
|
assert bn.affine, 'Only support fusing BatchNorm2d with affine set to True'
|
|
assert bn.track_running_stats, 'Only support fusing BatchNorm2d with tracking_running_stats set to True'
|
|
fused_module_class = fused_module_class_map.get((type(conv)), None)
|
|
if fused_module_class is not None:
|
|
return fused_module_class(conv, bn)
|
|
else:
|
|
raise NotImplementedError(f"Cannot fuse train modules: {(conv, bn)}")
|
|
else:
|
|
return nn.utils.fuse_conv_bn_eval(conv, bn)
|
|
|
|
def fuse_conv_bn_relu(is_qat, conv, bn, relu):
|
|
r"""Return the fused conv and bv modules.
|
|
|
|
Given the conv and bn modules, fuses them and returns the fused module
|
|
|
|
Args:
|
|
is_qat: a flag for whether we are using quantization aware training fusion
|
|
or post training quantization fusion
|
|
conv: Module instance of type conv2d/conv3d
|
|
bn: Spatial BN instance that needs to be fused with the conv
|
|
|
|
Examples::
|
|
|
|
>>> m1 = nn.Conv2d(10, 20, 3)
|
|
>>> b1 = nn.BatchNorm2d(20)
|
|
>>> r1 = nn.ReLU(inplace=False)
|
|
>>> # xdoctest: +SKIP
|
|
>>> m2 = fuse_conv_bn_relu(m1, b1, r1)
|
|
"""
|
|
assert(conv.training == bn.training == relu.training),\
|
|
"Conv and BN both must be in the same mode (train or eval)."
|
|
fused_module : Optional[Type[nn.Sequential]] = None
|
|
if is_qat:
|
|
map_to_fused_module_train = {
|
|
nn.Conv1d: nni.ConvBnReLU1d,
|
|
nn.Conv2d: nni.ConvBnReLU2d,
|
|
nn.Conv3d: nni.ConvBnReLU3d,
|
|
}
|
|
assert bn.num_features == conv.out_channels, 'Output channel of Conv must match num_features of BatchNorm'
|
|
assert bn.affine, 'Only support fusing BatchNorm with affine set to True'
|
|
assert bn.track_running_stats, 'Only support fusing BatchNorm with tracking_running_stats set to True'
|
|
fused_module = map_to_fused_module_train.get(type(conv), None)
|
|
if fused_module is not None:
|
|
return fused_module(conv, bn, relu)
|
|
else:
|
|
raise NotImplementedError(f"Cannot fuse train modules: {(conv, bn, relu)}")
|
|
else:
|
|
map_to_fused_module_eval = {
|
|
nn.Conv1d: nni.ConvReLU1d,
|
|
nn.Conv2d: nni.ConvReLU2d,
|
|
nn.Conv3d: nni.ConvReLU3d,
|
|
}
|
|
fused_module = map_to_fused_module_eval.get(type(conv), None)
|
|
if fused_module is not None:
|
|
fused_conv = nn.utils.fusion.fuse_conv_bn_eval(conv, bn)
|
|
return fused_module(fused_conv, relu)
|
|
else:
|
|
raise NotImplementedError(f"Cannot fuse eval modules: {(conv, bn, relu)}")
|
|
|
|
def fuse_linear_bn(is_qat, linear, bn):
|
|
r"""Return the fused linear and bn modules.
|
|
Given the linear and bn modules, fuses them and returns the fused module
|
|
|
|
Args:
|
|
is_qat: a flag for whether we are using quantization aware training fusion
|
|
or post training quantization fusion
|
|
linear: Module instance of type Linear
|
|
bn: BatchNorm1d instance that needs to be fused with the linear layer
|
|
|
|
Examples::
|
|
|
|
>>> m1 = nn.Linear(20, 10)
|
|
>>> b1 = nn.BatchNorm1d(10)
|
|
>>> # xdoctest: +SKIP
|
|
>>> m2 = fuse_linear_bn(m1, b1)
|
|
"""
|
|
assert(linear.training == bn.training),\
|
|
"Linear and BN both must be in the same mode (train or eval)."
|
|
|
|
if is_qat:
|
|
assert bn.num_features == linear.out_features,\
|
|
"Output features of Linear must match num_features of BatchNorm1d"
|
|
assert bn.affine, "Only support fusing BatchNorm1d with affine set to True"
|
|
assert bn.track_running_stats,\
|
|
"Only support fusing BatchNorm1d with tracking_running_stats set to True"
|
|
return nni.LinearBn1d(linear, bn)
|
|
else:
|
|
return nn.utils.fusion.fuse_linear_bn_eval(linear, bn)
|
|
|
|
def fuse_convtranspose_bn(is_qat, convt, bn):
|
|
r"""Return the fused ConvTranspose and bn modules.
|
|
Given ConvTranspose and bn modules, fuses them and returns the fused module
|
|
|
|
Args:
|
|
convt: Module instance of type ConvTransposeNd
|
|
bn: BatchNormNd instance that needs to be fused with the linear layer.
|
|
batch norm N should match the ConvTranspose N
|
|
|
|
Examples::
|
|
|
|
>>> m1 = nn.ConvTranspose2d(10, 20, 3)
|
|
>>> b1 = nn.BatchNorm2d(20)
|
|
>>> # xdoctest: +SKIP
|
|
>>> m2 = fuse_convtranspose_bn(m1, b1)
|
|
"""
|
|
assert(convt.training == bn.training),\
|
|
"ConvTranspose and BN both must be in the same mode (train or eval)."
|
|
|
|
if is_qat:
|
|
raise Exception("Fusing ConvTranspose+BatchNorm not yet supported in QAT.")
|
|
else:
|
|
return nn.utils.fusion.fuse_conv_bn_eval(convt, bn, transpose=True)
|
|
|
|
def _sequential_wrapper2(sequential):
|
|
"""Return a sequential wrapped that for is_qat and two modules.
|
|
Given a sequential class for two modules, return a function that takes
|
|
is_qat, and then two modules as argument, that ignores the is_qat flag
|
|
and always returns the sequential that combines the two input modules
|
|
"""
|
|
def fuser_method(is_qat, m1, m2):
|
|
return sequential(m1, m2)
|
|
return fuser_method
|
|
|
|
_DEFAULT_OP_LIST_TO_FUSER_METHOD: Dict[Tuple, Union[nn.Sequential, Callable]] = {
|
|
(nn.Conv1d, nn.BatchNorm1d): fuse_conv_bn,
|
|
(nn.Conv1d, nn.BatchNorm1d, nn.ReLU): fuse_conv_bn_relu,
|
|
(nn.Conv2d, nn.BatchNorm2d): fuse_conv_bn,
|
|
(nn.Conv2d, nn.BatchNorm2d, nn.ReLU): fuse_conv_bn_relu,
|
|
(nn.Conv3d, nn.BatchNorm3d): fuse_conv_bn,
|
|
(nn.Conv3d, nn.BatchNorm3d, nn.ReLU): fuse_conv_bn_relu,
|
|
(nn.Conv1d, nn.ReLU): _sequential_wrapper2(nni.ConvReLU1d),
|
|
(nn.Conv2d, nn.ReLU): _sequential_wrapper2(nni.ConvReLU2d),
|
|
(nn.Conv3d, nn.ReLU): _sequential_wrapper2(nni.ConvReLU3d),
|
|
(nn.Linear, nn.BatchNorm1d): fuse_linear_bn,
|
|
(nn.Linear, nn.ReLU): _sequential_wrapper2(nni.LinearReLU),
|
|
(nn.BatchNorm2d, nn.ReLU): _sequential_wrapper2(nni.BNReLU2d),
|
|
(nn.BatchNorm3d, nn.ReLU): _sequential_wrapper2(nni.BNReLU3d),
|
|
(nn.ConvTranspose1d, nn.BatchNorm1d): fuse_convtranspose_bn,
|
|
(nn.ConvTranspose2d, nn.BatchNorm2d): fuse_convtranspose_bn,
|
|
(nn.ConvTranspose3d, nn.BatchNorm3d): fuse_convtranspose_bn,
|
|
}
|
|
|
|
def get_fuser_method(op_list, additional_fuser_method_mapping=None):
|
|
"""Get fuser method for the given list of module types.
|
|
|
|
Get fuser method for the given list of module types,
|
|
return None if fuser method does not exist
|
|
"""
|
|
if additional_fuser_method_mapping is None:
|
|
additional_fuser_method_mapping = {}
|
|
all_mappings = get_combined_dict(_DEFAULT_OP_LIST_TO_FUSER_METHOD,
|
|
additional_fuser_method_mapping)
|
|
fuser_method = all_mappings.get(op_list, None)
|
|
assert fuser_method is not None, f"did not find fuser method for: {op_list} "
|
|
return fuser_method
|
|
|
|
def _reverse2(f):
|
|
def reversed(is_qat, x, y):
|
|
return f(is_qat, y, x)
|
|
return reversed
|
|
|
|
def _reverse3(f):
|
|
def reversed(is_qat, x, w):
|
|
y, z = w
|
|
return f(is_qat, z, y, x)
|
|
return reversed
|
|
|
|
def _get_valid_patterns(op_pattern):
|
|
"""Return a list of valid patterns generated from the op_pattern.
|
|
|
|
Returns a list of valid patterns generated from the op_pattern,
|
|
since MatchAllNode can match all types of nodes,
|
|
e.g. pattern (torch.nn.Conv2d, torch.add) should also be able to match keys like
|
|
(MatchAllNode, torch.add) and (torch.nn.Conv2d, MatchAllNode)
|
|
|
|
Example Input:
|
|
(torch.add, (torch.nn.ReLU, torch.nn.Conv2d))
|
|
|
|
Example Output:
|
|
[(torch.add, (torch.nn.ReLU, torch.nn.Conv2d)),
|
|
(torch.add, (torch.nn.ReLU, MatchAllNode)),
|
|
(torch.add, (MatchAllNode, torch.nn.Conv2d)),
|
|
(torch.add, (MatchAllNode, MatchAllNode)),
|
|
(MatchAllNode, (torch.nn.ReLU, torch.nn.Conv2d)),
|
|
(MatchAllNode, (torch.nn.ReLU, MatchAllNode)),
|
|
(MatchAllNode, (MatchAllNode, torch.nn.Conv2d)),
|
|
(MatchAllNode, (MatchAllNode, MatchAllNode)),
|
|
]
|
|
"""
|
|
result = []
|
|
if isinstance(op_pattern, (tuple, list)):
|
|
sub_combs = []
|
|
for sub_pattern in op_pattern:
|
|
sub_combs.append(_get_valid_patterns(sub_pattern))
|
|
result = list(itertools.product(*sub_combs))
|
|
else:
|
|
result = [op_pattern, MatchAllNode]
|
|
return result
|
|
|
|
def get_fuser_method_new(
|
|
op_pattern: Pattern,
|
|
fuser_method_mapping: Dict[Pattern, Union[nn.Sequential, Callable]]):
|
|
"""Get fuser method.
|
|
|
|
This will be made default after we deprecate the get_fuser_method
|
|
Would like to implement this first and have a separate PR for deprecation
|
|
"""
|
|
op_patterns = _get_valid_patterns(op_pattern)
|
|
fuser_method = None
|
|
for op_pattern in op_patterns:
|
|
fuser_method = fuser_method_mapping.get(op_pattern, None)
|
|
if fuser_method is not None:
|
|
break
|
|
assert fuser_method is not None, f"did not find fuser method for: {op_pattern} "
|
|
return fuser_method
|