fuser_method_mappings.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import torch.nn as nn
  2. import torch.ao.nn.intrinsic as nni
  3. from typing import Union, Callable, Tuple, Dict, Optional, Type
  4. from torch.ao.quantization.utils import Pattern, get_combined_dict, MatchAllNode
  5. import itertools
  6. __all__ = [
  7. "fuse_conv_bn",
  8. "fuse_conv_bn_relu",
  9. "fuse_linear_bn",
  10. "fuse_convtranspose_bn",
  11. "get_fuser_method",
  12. "get_fuser_method_new",
  13. ]
  14. def fuse_conv_bn(is_qat, conv, bn):
  15. r"""Given the conv and bn modules, fuses them and returns the fused module
  16. Args:
  17. is_qat: a flag for whether we are using quantization aware training fusion
  18. or post training quantization fusion
  19. conv: Module instance of type conv2d/conv3d
  20. bn: Spatial BN instance that needs to be fused with the conv
  21. Examples::
  22. >>> m1 = nn.Conv2d(10, 20, 3)
  23. >>> b1 = nn.BatchNorm2d(20)
  24. >>> # xdoctest: +SKIP
  25. >>> m2 = fuse_conv_bn(m1, b1)
  26. """
  27. assert(conv.training == bn.training),\
  28. "Conv and BN both must be in the same mode (train or eval)."
  29. fused_module_class_map = {
  30. nn.Conv1d: nni.ConvBn1d,
  31. nn.Conv2d: nni.ConvBn2d,
  32. nn.Conv3d: nni.ConvBn3d,
  33. }
  34. if is_qat:
  35. assert bn.num_features == conv.out_channels, 'Output channel of Conv2d must match num_features of BatchNorm2d'
  36. assert bn.affine, 'Only support fusing BatchNorm2d with affine set to True'
  37. assert bn.track_running_stats, 'Only support fusing BatchNorm2d with tracking_running_stats set to True'
  38. fused_module_class = fused_module_class_map.get((type(conv)), None)
  39. if fused_module_class is not None:
  40. return fused_module_class(conv, bn)
  41. else:
  42. raise NotImplementedError("Cannot fuse train modules: {}".format((conv, bn)))
  43. else:
  44. return nn.utils.fuse_conv_bn_eval(conv, bn)
  45. def fuse_conv_bn_relu(is_qat, conv, bn, relu):
  46. r"""Given the conv and bn modules, fuses them and returns the fused module
  47. Args:
  48. is_qat: a flag for whether we are using quantization aware training fusion
  49. or post training quantization fusion
  50. conv: Module instance of type conv2d/conv3d
  51. bn: Spatial BN instance that needs to be fused with the conv
  52. Examples::
  53. >>> m1 = nn.Conv2d(10, 20, 3)
  54. >>> b1 = nn.BatchNorm2d(20)
  55. >>> r1 = nn.ReLU(inplace=False)
  56. >>> # xdoctest: +SKIP
  57. >>> m2 = fuse_conv_bn_relu(m1, b1, r1)
  58. """
  59. assert(conv.training == bn.training == relu.training),\
  60. "Conv and BN both must be in the same mode (train or eval)."
  61. fused_module : Optional[Type[nn.Sequential]] = None
  62. if is_qat:
  63. map_to_fused_module_train = {
  64. nn.Conv1d: nni.ConvBnReLU1d,
  65. nn.Conv2d: nni.ConvBnReLU2d,
  66. nn.Conv3d: nni.ConvBnReLU3d,
  67. }
  68. assert bn.num_features == conv.out_channels, 'Output channel of Conv must match num_features of BatchNorm'
  69. assert bn.affine, 'Only support fusing BatchNorm with affine set to True'
  70. assert bn.track_running_stats, 'Only support fusing BatchNorm with tracking_running_stats set to True'
  71. fused_module = map_to_fused_module_train.get(type(conv), None)
  72. if fused_module is not None:
  73. return fused_module(conv, bn, relu)
  74. else:
  75. raise NotImplementedError("Cannot fuse train modules: {}".format((conv, bn, relu)))
  76. else:
  77. map_to_fused_module_eval = {
  78. nn.Conv1d: nni.ConvReLU1d,
  79. nn.Conv2d: nni.ConvReLU2d,
  80. nn.Conv3d: nni.ConvReLU3d,
  81. }
  82. fused_module = map_to_fused_module_eval.get(type(conv), None)
  83. if fused_module is not None:
  84. fused_conv = nn.utils.fusion.fuse_conv_bn_eval(conv, bn)
  85. return fused_module(fused_conv, relu)
  86. else:
  87. raise NotImplementedError("Cannot fuse eval modules: {}".format((conv, bn, relu)))
  88. def fuse_linear_bn(is_qat, linear, bn):
  89. r"""Given the linear and bn modules, fuses them and returns the fused module
  90. Args:
  91. is_qat: a flag for whether we are using quantization aware training fusion
  92. or post training quantization fusion
  93. linear: Module instance of type Linear
  94. bn: BatchNorm1d instance that needs to be fused with the linear layer
  95. Examples::
  96. >>> m1 = nn.Linear(20, 10)
  97. >>> b1 = nn.BatchNorm1d(10)
  98. >>> # xdoctest: +SKIP
  99. >>> m2 = fuse_linear_bn(m1, b1)
  100. """
  101. assert(linear.training == bn.training),\
  102. "Linear and BN both must be in the same mode (train or eval)."
  103. if is_qat:
  104. assert bn.num_features == linear.out_features,\
  105. "Output features of Linear must match num_features of BatchNorm1d"
  106. assert bn.affine, "Only support fusing BatchNorm1d with affine set to True"
  107. assert bn.track_running_stats,\
  108. "Only support fusing BatchNorm1d with tracking_running_stats set to True"
  109. return nni.LinearBn1d(linear, bn)
  110. else:
  111. return nn.utils.fusion.fuse_linear_bn_eval(linear, bn)
  112. def fuse_convtranspose_bn(is_qat, convt, bn):
  113. r"""Given ConvTranspose and bn modules, fuses them and returns the fused module
  114. Args:
  115. convt: Module instance of type ConvTransposeNd
  116. bn: BatchNormNd instance that needs to be fused with the linear layer.
  117. batch norm N should match the ConvTranspose N
  118. Examples::
  119. >>> m1 = nn.ConvTranspose2d(10, 20, 3)
  120. >>> b1 = nn.BatchNorm2d(20)
  121. >>> # xdoctest: +SKIP
  122. >>> m2 = fuse_convtranspose_bn(m1, b1)
  123. """
  124. assert(convt.training == bn.training),\
  125. "ConvTranspose and BN both must be in the same mode (train or eval)."
  126. if is_qat:
  127. raise Exception("Fusing ConvTranspose+BatchNorm not yet supported in QAT.")
  128. else:
  129. return nn.utils.fusion.fuse_conv_bn_eval(convt, bn, transpose=True)
  130. def _sequential_wrapper2(sequential):
  131. """ Given a sequential class for two modules, return a function that takes
  132. is_qat, and then two modules as argument, that ignores the is_qat flag
  133. and always returns the sequential that combines the two input modules
  134. """
  135. def fuser_method(is_qat, m1, m2):
  136. return sequential(m1, m2)
  137. return fuser_method
  138. _DEFAULT_OP_LIST_TO_FUSER_METHOD: Dict[Tuple, Union[nn.Sequential, Callable]] = {
  139. (nn.Conv1d, nn.BatchNorm1d): fuse_conv_bn,
  140. (nn.Conv1d, nn.BatchNorm1d, nn.ReLU): fuse_conv_bn_relu,
  141. (nn.Conv2d, nn.BatchNorm2d): fuse_conv_bn,
  142. (nn.Conv2d, nn.BatchNorm2d, nn.ReLU): fuse_conv_bn_relu,
  143. (nn.Conv3d, nn.BatchNorm3d): fuse_conv_bn,
  144. (nn.Conv3d, nn.BatchNorm3d, nn.ReLU): fuse_conv_bn_relu,
  145. (nn.Conv1d, nn.ReLU): _sequential_wrapper2(nni.ConvReLU1d),
  146. (nn.Conv2d, nn.ReLU): _sequential_wrapper2(nni.ConvReLU2d),
  147. (nn.Conv3d, nn.ReLU): _sequential_wrapper2(nni.ConvReLU3d),
  148. (nn.Linear, nn.BatchNorm1d): fuse_linear_bn,
  149. (nn.Linear, nn.ReLU): _sequential_wrapper2(nni.LinearReLU),
  150. (nn.BatchNorm2d, nn.ReLU): _sequential_wrapper2(nni.BNReLU2d),
  151. (nn.BatchNorm3d, nn.ReLU): _sequential_wrapper2(nni.BNReLU3d),
  152. (nn.ConvTranspose1d, nn.BatchNorm1d): fuse_convtranspose_bn,
  153. (nn.ConvTranspose2d, nn.BatchNorm2d): fuse_convtranspose_bn,
  154. (nn.ConvTranspose3d, nn.BatchNorm3d): fuse_convtranspose_bn,
  155. }
  156. def get_fuser_method(op_list, additional_fuser_method_mapping=None):
  157. ''' Get fuser method for the given list of module types,
  158. return None if fuser method does not exist
  159. '''
  160. if additional_fuser_method_mapping is None:
  161. additional_fuser_method_mapping = {}
  162. all_mappings = get_combined_dict(_DEFAULT_OP_LIST_TO_FUSER_METHOD,
  163. additional_fuser_method_mapping)
  164. fuser_method = all_mappings.get(op_list, None)
  165. assert fuser_method is not None, "did not find fuser method for: {} ".format(op_list)
  166. return fuser_method
  167. def _reverse2(f):
  168. def reversed(is_qat, x, y):
  169. return f(is_qat, y, x)
  170. return reversed
  171. def _reverse3(f):
  172. def reversed(is_qat, x, w):
  173. y, z = w
  174. return f(is_qat, z, y, x)
  175. return reversed
  176. def _get_valid_patterns(op_pattern):
  177. """
  178. Returns a list of valid patterns generated from the op_pattern,
  179. since MatchAllNode can match all types of nodes,
  180. e.g. pattern (torch.nn.Conv2d, torch.add) should also be able to match keys like
  181. (MatchAllNode, torch.add) and (torch.nn.Conv2d, MatchAllNode)
  182. Example Input:
  183. (torch.add, (torch.nn.ReLU, torch.nn.Conv2d))
  184. Example Output:
  185. [(torch.add, (torch.nn.ReLU, torch.nn.Conv2d)),
  186. (torch.add, (torch.nn.ReLU, MatchAllNode)),
  187. (torch.add, (MatchAllNode, torch.nn.Conv2d)),
  188. (torch.add, (MatchAllNode, MatchAllNode)),
  189. (MatchAllNode, (torch.nn.ReLU, torch.nn.Conv2d)),
  190. (MatchAllNode, (torch.nn.ReLU, MatchAllNode)),
  191. (MatchAllNode, (MatchAllNode, torch.nn.Conv2d)),
  192. (MatchAllNode, (MatchAllNode, MatchAllNode)),
  193. ]
  194. """
  195. result = []
  196. if isinstance(op_pattern, (tuple, list)):
  197. sub_combs = []
  198. for sub_pattern in op_pattern:
  199. sub_combs.append(_get_valid_patterns(sub_pattern))
  200. result = list(itertools.product(*sub_combs))
  201. else:
  202. result = [op_pattern, MatchAllNode]
  203. return result
  204. def get_fuser_method_new(
  205. op_pattern: Pattern,
  206. fuser_method_mapping: Dict[Pattern, Union[nn.Sequential, Callable]]):
  207. """ This will be made defult after we deprecate the get_fuser_method
  208. Would like to implement this first and have a separate PR for deprecation
  209. """
  210. op_patterns = _get_valid_patterns(op_pattern)
  211. fuser_method = None
  212. for op_pattern in op_patterns:
  213. fuser_method = fuser_method_mapping.get(op_pattern, None)
  214. if fuser_method is not None:
  215. break
  216. assert fuser_method is not None, "did not find fuser method for: {} ".format(op_pattern)
  217. return fuser_method