Skip to content

Commit

Permalink
edit tpc in qat tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenp committed Jan 14, 2025
1 parent 37c2e1e commit 47f5298
Showing 1 changed file with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@
"from model_compression_toolkit import DefaultDict\n",
"from model_compression_toolkit.constants import FLOAT_BITWIDTH\n",
"from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, BIAS\n",
"\n",
"tp = mct.target_platform\n",
"\n",
"from mct_quantizers import QuantizationMethod\n",
"from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import schema, TargetPlatformCapabilities, Signedness, \\\n",
" AttributeQuantizationConfig, OpQuantizationConfig, QuantizationConfigOptions\n",
"\n",
"def get_tpc():\n",
" \"\"\"\n",
Expand All @@ -211,23 +211,23 @@
"\n",
" # define a default quantization config for all non-specified weights attributes.\n",
" default_weight_attr_config = AttributeQuantizationConfig(\n",
" weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,\n",
" weights_quantization_method=QuantizationMethod.POWER_OF_TWO,\n",
" weights_n_bits=8,\n",
" weights_per_channel_threshold=False,\n",
" enable_weights_quantization=False,\n",
" lut_values_bitwidth=None)\n",
"\n",
" # define a quantization config to quantize the kernel (for layers where there is a kernel attribute).\n",
" kernel_base_config = AttributeQuantizationConfig(\n",
" weights_quantization_method=tp.QuantizationMethod.SYMMETRIC,\n",
" weights_quantization_method=QuantizationMethod.SYMMETRIC,\n",
" weights_n_bits=2,\n",
" weights_per_channel_threshold=True,\n",
" enable_weights_quantization=True,\n",
" lut_values_bitwidth=None)\n",
"\n",
" # define a quantization config to quantize the bias (for layers where there is a bias attribute).\n",
" bias_config = AttributeQuantizationConfig(\n",
" weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,\n",
" weights_quantization_method=QuantizationMethod.POWER_OF_TWO,\n",
" weights_n_bits=FLOAT_BITWIDTH,\n",
" weights_per_channel_threshold=False,\n",
" enable_weights_quantization=False,\n",
Expand All @@ -237,11 +237,11 @@
" # AttributeQuantizationConfig for weights with no specific AttributeQuantizationConfig.\n",
" # MCT will compress a layer's kernel and bias according to the configurations that are\n",
" # set in KERNEL_ATTR and BIAS_ATTR that are passed in attr_weights_configs_mapping.\n",
" default_config = tp.OpQuantizationConfig(\n",
" default_config = OpQuantizationConfig(\n",
" default_weight_attr_config=default_weight_attr_config,\n",
" attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config,\n",
" BIAS_ATTR: bias_config},\n",
" activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,\n",
" activation_quantization_method=QuantizationMethod.POWER_OF_TWO,\n",
" activation_n_bits=3,\n",
" supported_input_activation_n_bits=8,\n",
" enable_activation_quantization=True,\n",
Expand All @@ -253,29 +253,21 @@
"\n",
" # Set default QuantizationConfigOptions in new TargetPlatformCapabilities to be used when no other\n",
" # QuantizationConfigOptions is set for an OperatorsSet.\n",
" default_configuration_options = tp.QuantizationConfigOptions([default_config])\n",
" tp_model = tp.TargetPlatformCapabilities(default_configuration_options,\n",
" tpc_minor_version=1,\n",
" tpc_patch_version=0,\n",
" tpc_platform_type=\"custom_qat_notebook_tpc\")\n",
" with tp_model:\n",
" default_qco = tp.get_default_quantization_config_options()\n",
" # Group of OperatorsSets that should not be quantized.\n",
" tp.OperatorsSet(\"NoQuantization\",\n",
" default_qco.clone_and_edit(enable_activation_quantization=False)\n",
" .clone_and_edit_weight_attribute(enable_weights_quantization=False))\n",
" # Group of linear OperatorsSets such as convolution and matmul.\n",
" tp.OperatorsSet(\"LinearOp\")\n",
" default_configuration_options = QuantizationConfigOptions(quantization_configurations=[default_config])\n",
" no_quantization_config = (default_configuration_options.clone_and_edit(enable_activation_quantization=False)\n",
" .clone_and_edit_weight_attribute(enable_weights_quantization=False))\n",
"\n",
" operator_set = []\n",
"\n",
" operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.DROPOUT, qc_options=no_quantization_config))\n",
" operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.FLATTEN, qc_options=no_quantization_config))\n",
"\n",
"\n",
" tpc = tp.FrameworkQuantizationCapabilities(tp_model)\n",
" with tpc:\n",
" # No need to quantize Flatten and Dropout layers\n",
" tp.OperationsSetToLayers(\"NoQuantization\", [layers.Flatten, layers.Dropout])\n",
" # Assign the framework layers' attributes to KERNEL_ATTR and BIAS_ATTR that were used during creation\n",
" # of the default OpQuantizationConfig.\n",
" tp.OperationsSetToLayers(\"LinearOp\", [layers.Dense, layers.Conv2D],\n",
" attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),\n",
" BIAS_ATTR: DefaultDict(default_value=BIAS)})\n",
" tpc = TargetPlatformCapabilities(default_qco=default_configuration_options,\n",
" tpc_minor_version=1,\n",
" tpc_patch_version=0,\n",
" tpc_platform_type=\"custom_qat_notebook_tpc\",\n",
" operator_set=tuple(operator_set))\n",
" return tpc\n"
]
},
Expand Down

0 comments on commit 47f5298

Please sign in to comment.