| 120 | |
| 121 | |
| 122 | class VulkanQuantizer(Quantizer): |
| 123 | |
| 124 | def __init__(self) -> None: |
| 125 | super().__init__() |
| 126 | self.global_config: Optional[QuantizationConfig] = None |
| 127 | # If specified, only quantize nodes that return true for the filter |
| 128 | # function. |
| 129 | self.filter_fn: Optional[Callable[[Node], bool]] = None |
| 130 | |
| 131 | def set_global(self, quantization_config: QuantizationConfig) -> VulkanQuantizer: |
| 132 | self.global_config = quantization_config |
| 133 | return self |
| 134 | |
| 135 | def set_filter_function(self, filter_fn: Callable[[Node], bool]): |
| 136 | """ |
| 137 | Set the filter function. We only quantize nodes that return True for |
| 138 | the filter function. |
| 139 | """ |
| 140 | self.filter_fn = filter_fn |
| 141 | return self |
| 142 | |
| 143 | def transform_for_annotation( |
| 144 | self, model: torch.fx.GraphModule |
| 145 | ) -> torch.fx.GraphModule: |
| 146 | """Transforms scalar values to tensor attributes""" |
| 147 | return _convert_scalars_to_attrs(model) |
| 148 | |
| 149 | def annotate(self, model: torch.fx.GraphModule) -> torch.fx.GraphModule: |
| 150 | model = self._annotate_for_quantization_config(model) |
| 151 | propagate_annotation(model) |
| 152 | return model |
| 153 | |
| 154 | def _annotate_all_patterns( |
| 155 | self, |
| 156 | model: torch.fx.GraphModule, |
| 157 | quantization_config: Optional[QuantizationConfig], |
| 158 | filter_fn: Optional[Callable[[Node], bool]] = None, |
| 159 | ) -> torch.fx.GraphModule: |
| 160 | if quantization_config is None: |
| 161 | return model |
| 162 | |
| 163 | # Create a combined filter function, which returns True only when |
| 164 | # both filter_fn and self.filter_fn return True. |
| 165 | def combined_filter_fn(n: Node) -> bool: |
| 166 | combined_filter = [self.filter_fn, filter_fn] |
| 167 | return all(f(n) for f in combined_filter if f is not None) |
| 168 | |
| 169 | for op in _SUPPORTED_OPS: |
| 170 | OP_TO_ANNOTATOR[op](model, quantization_config, combined_filter_fn) |
| 171 | return model |
| 172 | |
| 173 | def _annotate_for_quantization_config( |
| 174 | self, model: torch.fx.GraphModule |
| 175 | ) -> torch.fx.GraphModule: |
| 176 | self._annotate_all_patterns( |
| 177 | model, |
| 178 | self.global_config, |
| 179 | ) |
no outgoing calls