x86.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import torch
  2. from ._common_operator_config_utils import (
  3. _get_binary_op_configs,
  4. _get_bn_configs,
  5. _get_cat_config,
  6. _get_conv_configs,
  7. _get_default_op_configs,
  8. _get_embedding_op_configs,
  9. _get_fixed_qparams_op_configs,
  10. _get_linear_configs,
  11. _get_rnn_op_configs,
  12. _get_share_qparams_op_configs,
  13. _get_tensor_info_op_configs,
  14. )
  15. from .backend_config import BackendConfig, DTypeConfig
  16. __all__ = [
  17. "get_x86_backend_config",
  18. ]
  19. # ===================
  20. # | DTYPE CONFIGS |
  21. # ===================
  22. # X86 aligns with FBGEMM for now
  23. x86_weighted_op_int8_dtype_config = DTypeConfig(
  24. input_dtype=torch.quint8,
  25. output_dtype=torch.quint8,
  26. weight_dtype=torch.qint8,
  27. bias_dtype=torch.float,
  28. )
  29. x86_default_op_quint8_dtype_config = DTypeConfig(
  30. input_dtype=torch.quint8,
  31. output_dtype=torch.quint8,
  32. )
  33. x86_default_op_fp16_dtype_config = DTypeConfig(
  34. input_dtype=torch.float16,
  35. output_dtype=torch.float16,
  36. weight_dtype=torch.float16,
  37. bias_dtype=torch.float16,
  38. )
  39. x86_default_dynamic_int8_dtype_config = DTypeConfig(
  40. input_dtype=torch.quint8,
  41. output_dtype=torch.float,
  42. weight_dtype=torch.qint8,
  43. bias_dtype=torch.float,
  44. is_dynamic=True,
  45. )
  46. x86_default_dynamic_float16_dtype_config = DTypeConfig(
  47. input_dtype=torch.float16,
  48. output_dtype=torch.float,
  49. weight_dtype=torch.float16,
  50. bias_dtype=torch.float,
  51. is_dynamic=True,
  52. )
  53. x86_weight_only_quint8_dtype_config = DTypeConfig(
  54. input_dtype=torch.float,
  55. output_dtype=torch.float,
  56. weight_dtype=torch.quint8,
  57. )
  58. x86_weight_only_quint4x2_dtype_config = DTypeConfig(
  59. input_dtype=torch.float,
  60. output_dtype=torch.float,
  61. weight_dtype=torch.quint4x2,
  62. )
  63. # =====================
  64. # | BACKEND CONFIGS |
  65. # =====================
  66. def get_x86_backend_config() -> BackendConfig:
  67. """
  68. Return the `BackendConfig` for PyTorch's native x86 backend.
  69. """
  70. conv_dtype_configs = [x86_weighted_op_int8_dtype_config]
  71. linear_dtype_configs = [
  72. x86_weighted_op_int8_dtype_config,
  73. x86_default_dynamic_int8_dtype_config,
  74. x86_default_dynamic_float16_dtype_config,
  75. ]
  76. binary_op_dtype_configs = [x86_weighted_op_int8_dtype_config]
  77. default_op_dtype_configs = [x86_default_op_quint8_dtype_config]
  78. fixed_qparams_op_dtype_configs = [x86_weighted_op_int8_dtype_config]
  79. share_qparams_op_dtype_configs = [x86_default_op_quint8_dtype_config]
  80. tensor_info_op_dtype_configs = [x86_default_op_quint8_dtype_config]
  81. rnn_op_dtype_configs = [
  82. x86_default_dynamic_int8_dtype_config,
  83. x86_default_dynamic_float16_dtype_config,
  84. ]
  85. embedding_op_dtype_configs = [
  86. x86_weight_only_quint8_dtype_config,
  87. x86_weight_only_quint4x2_dtype_config,
  88. ]
  89. return BackendConfig("x86") \
  90. .set_backend_pattern_configs(_get_conv_configs(conv_dtype_configs)) \
  91. .set_backend_pattern_configs(_get_linear_configs(linear_dtype_configs)) \
  92. .set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs)) \
  93. .set_backend_pattern_config(_get_cat_config(default_op_dtype_configs)) \
  94. .set_backend_pattern_configs(_get_default_op_configs(default_op_dtype_configs)) \
  95. .set_backend_pattern_configs(_get_fixed_qparams_op_configs(fixed_qparams_op_dtype_configs)) \
  96. .set_backend_pattern_configs(_get_share_qparams_op_configs(share_qparams_op_dtype_configs)) \
  97. .set_backend_pattern_configs(_get_tensor_info_op_configs(tensor_info_op_dtype_configs)) \
  98. .set_backend_pattern_configs(_get_bn_configs(default_op_dtype_configs)) \
  99. .set_backend_pattern_configs(_get_rnn_op_configs(rnn_op_dtype_configs)) \
  100. .set_backend_pattern_configs(_get_embedding_op_configs(embedding_op_dtype_configs))