|
| 1 | +# mypy: allow-untyped-defs |
| 2 | +import functools |
| 3 | +from typing import Any, Dict, Optional, TYPE_CHECKING |
| 4 | + |
| 5 | +import torch |
| 6 | +from torch.ao.quantization.observer import HistogramObserver, PerChannelMinMaxObserver |
| 7 | +from torch.ao.quantization.quantizer.quantizer import QuantizationSpec |
| 8 | +from torch.ao.quantization.quantizer.x86_inductor_quantizer import ( |
| 9 | + _is_any_annotated, |
| 10 | + FilterFn, |
| 11 | + int8_in_int8_out_ops, |
| 12 | + X86InductorQuantizer, |
| 13 | +) |
| 14 | +from torch.ao.quantization.quantizer.xnnpack_quantizer_utils import QuantizationConfig |
| 15 | +from torch.fx import Node |
| 16 | + |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from torch.ao.quantization.qconfig import _ObserverOrFakeQuantizeConstructor |
| 20 | + |
| 21 | +__all__ = [ |
| 22 | + "XPUInductorQuantizer", |
| 23 | + "get_default_xpu_inductor_quantization_config", |
| 24 | +] |
| 25 | + |
| 26 | + |
| 27 | +@functools.lru_cache |
| 28 | +def get_default_xpu_inductor_quantization_config(): |
| 29 | + extra_args: Dict[str, Any] = {"eps": 2**-12} |
| 30 | + act_observer_or_fake_quant_ctr = HistogramObserver |
| 31 | + act_quantization_spec = QuantizationSpec( |
| 32 | + dtype=torch.int8, |
| 33 | + quant_min=-128, |
| 34 | + quant_max=127, |
| 35 | + qscheme=torch.per_tensor_affine, |
| 36 | + is_dynamic=False, |
| 37 | + observer_or_fake_quant_ctr=act_observer_or_fake_quant_ctr.with_args( |
| 38 | + **extra_args |
| 39 | + ), |
| 40 | + ) |
| 41 | + |
| 42 | + weight_observer_or_fake_quant_ctr: _ObserverOrFakeQuantizeConstructor = ( |
| 43 | + PerChannelMinMaxObserver |
| 44 | + ) |
| 45 | + |
| 46 | + weight_quantization_spec = QuantizationSpec( |
| 47 | + dtype=torch.int8, |
| 48 | + quant_min=-128, |
| 49 | + quant_max=127, |
| 50 | + qscheme=torch.per_channel_symmetric, |
| 51 | + ch_axis=0, # 0 corresponding to weight shape = (oc, ic, kh, kw) of conv |
| 52 | + is_dynamic=False, |
| 53 | + observer_or_fake_quant_ctr=weight_observer_or_fake_quant_ctr.with_args( |
| 54 | + **extra_args |
| 55 | + ), |
| 56 | + ) |
| 57 | + |
| 58 | + bias_quantization_spec = None # will use placeholder observer by default |
| 59 | + quantization_config = QuantizationConfig( |
| 60 | + act_quantization_spec, |
| 61 | + act_quantization_spec, |
| 62 | + weight_quantization_spec, |
| 63 | + bias_quantization_spec, |
| 64 | + False, |
| 65 | + ) |
| 66 | + return quantization_config |
| 67 | + |
| 68 | + |
| 69 | +class XPUInductorQuantizer(X86InductorQuantizer): |
| 70 | + """ |
| 71 | + XPUInductorQuantizer is a class designed to facilitate |
| 72 | + quantization capability at Intel GPU backend. The class |
| 73 | + highly reuses the existing implementation of |
| 74 | + X86InductorQuantizer as both are intended to take advantage |
| 75 | + of the optimized kernels in oneDNN library. |
| 76 | + """ |
| 77 | + |
| 78 | + def __init__(self) -> None: |
| 79 | + super().__init__() |
| 80 | + |
| 81 | + """ |
| 82 | + Following annotate_xx overrides the impls in base class, as |
| 83 | + no XPU implementation for these operators currently. We would |
| 84 | + gradually enable the XPU implementation and remove following |
| 85 | + overrides. We keep the annotate methods but make the function |
| 86 | + body empty, aiming to let `_generate_qdq_quantized_model` |
| 87 | + generate qdq around op and graph execute on fp32 dtype for |
| 88 | + unspported operators. |
| 89 | + """ |
| 90 | + |
| 91 | + def _annotate_qat_conv2d_fusion_pattern( |
| 92 | + self, |
| 93 | + model: torch.fx.GraphModule, |
| 94 | + quantization_config: Optional[QuantizationConfig], |
| 95 | + filter_fn: Optional[FilterFn] = None, |
| 96 | + ): |
| 97 | + pass |
| 98 | + |
| 99 | + def _annotate_conv2d_binary( |
| 100 | + self, |
| 101 | + gm: torch.fx.GraphModule, |
| 102 | + quantization_config: Optional[QuantizationConfig], |
| 103 | + filter_fn: Optional[FilterFn] = None, |
| 104 | + ) -> None: |
| 105 | + pass |
| 106 | + |
| 107 | + def _annotate_conv2d_binary_unary( |
| 108 | + self, |
| 109 | + gm: torch.fx.GraphModule, |
| 110 | + quantization_config: Optional[QuantizationConfig], |
| 111 | + filter_fn: Optional[FilterFn] = None, |
| 112 | + ) -> None: |
| 113 | + pass |
| 114 | + |
| 115 | + def _annotate_linear_fusion_pattern( |
| 116 | + self, |
| 117 | + model: torch.fx.GraphModule, |
| 118 | + quantization_config: Optional[QuantizationConfig], |
| 119 | + filter_fn: Optional[FilterFn] = None, |
| 120 | + ): |
| 121 | + pass |
| 122 | + |
| 123 | + def _annotate_matmul( |
| 124 | + self, |
| 125 | + model: torch.fx.GraphModule, |
| 126 | + quantization_config: Optional[QuantizationConfig], |
| 127 | + filter_fn: Optional[FilterFn] = None, |
| 128 | + ): |
| 129 | + pass |
| 130 | + |
| 131 | + def _annotate_maxpool2d( |
| 132 | + self, |
| 133 | + node: Node, |
| 134 | + quantization_config: Optional[QuantizationConfig], |
| 135 | + ) -> None: |
| 136 | + """ |
| 137 | + Here we skip the annotate logic for maxpool at XPU backend |
| 138 | + as the quantized::max_pool2d is only implemented for CPU. |
| 139 | + """ |
| 140 | + return |
| 141 | + |
| 142 | + def _annotate_output_for_int8_in_int8_out_pattern( |
| 143 | + self, |
| 144 | + node: Node, |
| 145 | + ) -> None: |
| 146 | + if (node.target in int8_in_int8_out_ops) and (_is_any_annotated([node])): |
| 147 | + if node.target == torch.ops.aten.max_pool2d.default: |
| 148 | + return |
| 149 | + else: |
| 150 | + input_node = node.all_input_nodes[0] |
| 151 | + self._annotate_output_share_observer_as_input(input_node, node) |
| 152 | + return |
0 commit comments