|
| 1 | +# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 2 | + |
| 3 | +# pyre-strict |
| 4 | +from typing import Any, Callable |
| 5 | + |
| 6 | +from executorch.backends.transforms.duplicate_dynamic_quant_chain import ( |
| 7 | + duplicate_dynamic_quant_chain_pass, |
| 8 | + DuplicateDynamicQuantChainPass, |
| 9 | +) |
| 10 | + |
| 11 | +from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
| 12 | + |
| 13 | +from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import ( |
| 14 | + get_symmetric_quantization_config, |
| 15 | + XNNPACKQuantizer, |
| 16 | +) |
| 17 | +from executorch.exir import ExportRecipe |
| 18 | + |
| 19 | +def get_generic_fp32_cpu_recipe() -> ExportRecipe: |
| 20 | + quantizer = XNNPACKQuantizer() |
| 21 | + operator_config = get_symmetric_quantization_config(is_per_channel=False) |
| 22 | + quantizer.set_global(operator_config) |
| 23 | + return ExportRecipe( |
| 24 | + name = "fp32_recipe", |
| 25 | + quantizer = None, |
| 26 | + partitioners=[XnnpackPartitioner()], |
| 27 | + |
| 28 | + ) |
| 29 | + |
| 30 | +def get_dynamic_quant_recipe() -> ExportRecipe: |
| 31 | + quantizer = XNNPACKQuantizer() |
| 32 | + operator_config = get_symmetric_quantization_config( |
| 33 | + is_per_channel=True, is_dynamic=True |
| 34 | + ) |
| 35 | + quantizer.set_global(operator_config) |
| 36 | + DuplicateDynamicQuantChainPass |
| 37 | + return ExportRecipe( |
| 38 | + name = "dynamic_quant_recipe", |
| 39 | + quantizer = quantizer, |
| 40 | + partitioners=[XnnpackPartitioner()], |
| 41 | + pre_edge_transform_passes=duplicate_dynamic_quant_chain_pass, |
| 42 | + ) |
| 43 | + |
| 44 | +RECIPE_MAP: dict[str, Callable[[], ExportRecipe]] = { |
| 45 | + "FP32_CPU_ACCELERATED_RECIPE": get_generic_fp32_cpu_recipe, |
| 46 | + "DYNAMIC_QUANT_CPU_ACCELERATED_RECIPE": get_dynamic_quant_recipe, |
| 47 | +} |
| 48 | + |
| 49 | +def get_xnnpack_recipe(recipe_name:str, **kwargs: Any) -> ExportRecipe: |
| 50 | + assert recipe_name in RECIPE_MAP, f"Recipe {recipe_name} not found." |
| 51 | + return RECIPE_MAP[recipe_name](**kwargs) |
0 commit comments