|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +from typing import Any, Optional, Sequence |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +from executorch.backends.xnnpack.partition.config.xnnpack_config import ( |
| 14 | + ConfigPrecisionType, |
| 15 | +) |
| 16 | +from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
| 17 | +from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import ( |
| 18 | + get_symmetric_quantization_config, |
| 19 | + XNNPACKQuantizer, |
| 20 | +) |
| 21 | + |
| 22 | +from executorch.backends.xnnpack.recipes.xnnpack_recipe_types import XNNPackRecipeType |
| 23 | +from executorch.backends.xnnpack.utils.configs import ( |
| 24 | + get_xnnpack_edge_compile_config, |
| 25 | + get_xnnpack_executorch_backend_config, |
| 26 | +) |
| 27 | +from executorch.export import ( |
| 28 | + BackendRecipeProvider, |
| 29 | + ExportRecipe, |
| 30 | + QuantizationRecipe, |
| 31 | + RecipeType, |
| 32 | +) |
| 33 | +from torchao.quantization.granularity import PerAxis, PerGroup |
| 34 | +from torchao.quantization.quant_api import Int8DynamicActivationIntxWeightConfig |
| 35 | + |
| 36 | + |
| 37 | +class XNNPACKRecipeProvider(BackendRecipeProvider): |
| 38 | + @property |
| 39 | + def backend_name(self) -> str: |
| 40 | + return "xnnpack" |
| 41 | + |
| 42 | + def get_supported_recipes(self) -> Sequence[RecipeType]: |
| 43 | + return list(XNNPackRecipeType) |
| 44 | + |
| 45 | + def create_recipe( |
| 46 | + self, recipe_type: RecipeType, **kwargs: Any |
| 47 | + ) -> Optional[ExportRecipe]: |
| 48 | + """Create XNNPACK recipe""" |
| 49 | + |
| 50 | + if recipe_type not in self.get_supported_recipes(): |
| 51 | + return None |
| 52 | + |
| 53 | + # Validate kwargs |
| 54 | + self._validate_recipe_kwargs(recipe_type, **kwargs) |
| 55 | + |
| 56 | + if recipe_type == XNNPackRecipeType.FP32: |
| 57 | + return self._build_fp32_recipe(recipe_type) |
| 58 | + |
| 59 | + elif recipe_type == XNNPackRecipeType.INT8_DYNAMIC_PER_CHANNEL: |
| 60 | + return self._build_quantized_recipe( |
| 61 | + recipe_type, is_per_channel=True, is_dynamic=True |
| 62 | + ) |
| 63 | + |
| 64 | + elif recipe_type == XNNPackRecipeType.INT8_DYNAMIC_PER_TENSOR: |
| 65 | + return self._build_quantized_recipe( |
| 66 | + recipe_type, is_per_channel=False, is_dynamic=True |
| 67 | + ) |
| 68 | + |
| 69 | + elif recipe_type == XNNPackRecipeType.INT8_STATIC_PER_CHANNEL: |
| 70 | + return self._build_quantized_recipe( |
| 71 | + recipe_type, is_per_channel=True, is_dynamic=False |
| 72 | + ) |
| 73 | + |
| 74 | + elif recipe_type == XNNPackRecipeType.INT8_STATIC_PER_TENSOR: |
| 75 | + return self._build_quantized_recipe( |
| 76 | + recipe_type, is_per_channel=False, is_dynamic=False |
| 77 | + ) |
| 78 | + |
| 79 | + elif recipe_type == XNNPackRecipeType.INT8_DYNAMIC_ACT_INT4_WEIGHT_PER_CHANNEL: |
| 80 | + return self._build_int8da_intx_weight_recipe( |
| 81 | + recipe_type=recipe_type, |
| 82 | + is_per_channel=True, |
| 83 | + weight_dtype=torch.int4, |
| 84 | + ) |
| 85 | + |
| 86 | + elif recipe_type == XNNPackRecipeType.INT8_DYNAMIC_ACT_INT4_WEIGHT_PER_TENSOR: |
| 87 | + group_size = kwargs.get("group_size", 32) |
| 88 | + return self._build_int8da_intx_weight_recipe( |
| 89 | + recipe_type=recipe_type, |
| 90 | + is_per_channel=False, |
| 91 | + weight_dtype=torch.int4, |
| 92 | + group_size=group_size, |
| 93 | + ) |
| 94 | + return None |
| 95 | + |
| 96 | + def _build_fp32_recipe(self, recipe_type: RecipeType) -> ExportRecipe: |
| 97 | + return ExportRecipe( |
| 98 | + name=recipe_type.value, |
| 99 | + edge_compile_config=get_xnnpack_edge_compile_config(), |
| 100 | + executorch_backend_config=get_xnnpack_executorch_backend_config(), |
| 101 | + partitioners=[XnnpackPartitioner()], |
| 102 | + ) |
| 103 | + |
| 104 | + def _build_quantized_recipe( |
| 105 | + self, |
| 106 | + recipe_type: RecipeType, |
| 107 | + is_per_channel: bool = True, |
| 108 | + is_dynamic: bool = True, |
| 109 | + is_qat: bool = False, |
| 110 | + ) -> ExportRecipe: |
| 111 | + quantizer = XNNPACKQuantizer() |
| 112 | + operator_config = get_symmetric_quantization_config( |
| 113 | + is_per_channel=is_per_channel, is_dynamic=is_dynamic, is_qat=is_qat |
| 114 | + ) |
| 115 | + quantizer.set_global(operator_config) |
| 116 | + |
| 117 | + quant_recipe = QuantizationRecipe(quantizers=[quantizer]) |
| 118 | + |
| 119 | + precision_type = ( |
| 120 | + ConfigPrecisionType.DYNAMIC_QUANT |
| 121 | + if is_dynamic |
| 122 | + else ConfigPrecisionType.STATIC_QUANT |
| 123 | + ) |
| 124 | + |
| 125 | + return ExportRecipe( |
| 126 | + name=recipe_type.value, |
| 127 | + quantization_recipe=quant_recipe, |
| 128 | + edge_compile_config=get_xnnpack_edge_compile_config(), |
| 129 | + executorch_backend_config=get_xnnpack_executorch_backend_config(), |
| 130 | + partitioners=[XnnpackPartitioner(config_precision=precision_type)], |
| 131 | + ) |
| 132 | + |
| 133 | + def _build_int8da_intx_weight_recipe( |
| 134 | + self, |
| 135 | + recipe_type: RecipeType, |
| 136 | + is_per_channel: bool = True, |
| 137 | + weight_dtype: torch.dtype = torch.int4, |
| 138 | + group_size: int = 32, |
| 139 | + ) -> ExportRecipe: |
| 140 | + if is_per_channel: |
| 141 | + weight_granularity = PerAxis(axis=0) |
| 142 | + else: |
| 143 | + weight_granularity = PerGroup(group_size=group_size) |
| 144 | + |
| 145 | + config = Int8DynamicActivationIntxWeightConfig( |
| 146 | + weight_dtype=weight_dtype, |
| 147 | + weight_granularity=weight_granularity, |
| 148 | + ) |
| 149 | + |
| 150 | + quant_recipe = QuantizationRecipe( |
| 151 | + quantizers=None, |
| 152 | + ao_base_config=[config], |
| 153 | + ) |
| 154 | + |
| 155 | + return ExportRecipe( |
| 156 | + name=recipe_type.value, |
| 157 | + quantization_recipe=quant_recipe, |
| 158 | + edge_compile_config=get_xnnpack_edge_compile_config(), |
| 159 | + executorch_backend_config=get_xnnpack_executorch_backend_config(), |
| 160 | + partitioners=[XnnpackPartitioner()], |
| 161 | + ) |
| 162 | + |
| 163 | + def _validate_recipe_kwargs(self, recipe_type: RecipeType, **kwargs: Any) -> None: |
| 164 | + if recipe_type == XNNPackRecipeType.INT8_DYNAMIC_ACT_INT4_WEIGHT_PER_TENSOR: |
| 165 | + expected_keys = {"group_size"} |
| 166 | + unexpected = set(kwargs.keys()) - expected_keys |
| 167 | + if unexpected: |
| 168 | + raise ValueError( |
| 169 | + f"Recipe '{recipe_type.value}' only accepts 'group_size' parameter. " |
| 170 | + f"Unexpected parameters: {list(unexpected)}" |
| 171 | + ) |
| 172 | + if "group_size" in kwargs: |
| 173 | + group_size = kwargs["group_size"] |
| 174 | + if not isinstance(group_size, int): |
| 175 | + raise ValueError( |
| 176 | + f"Parameter 'group_size' must be an integer, got {type(group_size).__name__}: {group_size}" |
| 177 | + ) |
| 178 | + elif kwargs: |
| 179 | + # All other recipes don't expect any kwargs |
| 180 | + unexpected = list(kwargs.keys()) |
| 181 | + raise ValueError( |
| 182 | + f"Recipe '{recipe_type.value}' does not accept any parameters. " |
| 183 | + f"Unexpected parameters: {unexpected}" |
| 184 | + ) |
0 commit comments