Skip to content

Commit

Permalink
Add z-threshold part for network editor tutorial (#922)
Browse files Browse the repository at this point in the history
Co-authored-by: reuvenp <[email protected]>
  • Loading branch information
reuvenperetz and reuvenp authored Jan 9, 2024
1 parent 19a1301 commit fdc5520
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tutorials/notebooks/example_keras_network_editor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,46 @@
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Edit Z-Threshold for Activation Quantization\n",
"\n",
"In the context of model quantization, the Z-Threshold helps in handling outliers in the activation data. Outliers in the data can hurt the quantization process, leading to less efficient and potentially less accurate models.\n",
"\n",
"The Z-Threshold is used to set a boundary, beyond which extreme values in the activation data are considered outliers and are not used to determine the quantization parameters. This approach effectively filters out extreme values, ensuring a more robust and representative quantization.\n",
"\n",
"Adjusting the Z-Threshold can be particularly useful during the debugging and optimization of model quantization. By tweaking this parameter, you can fine-tune the balance between model accuracy and robustness against outliers in your specific use case.\n",
"\n",
"A higher Z-Threshold means more data is considered during quantization, including some outliers, which might be necessary for certain models or datasets.\n",
"\n",
"The following code demonstrates how you can customize the Z-Threshold for a specific layer type (Conv2D) in a Keras model using MCT's network editor functionality. This feature allows you to set different Z-Threshold values for different layers. By default, all layers use threshold of infinity (thus, no outlier-removal occurs)."
],
"metadata": {
"id": "2TqXTB48jKHx"
}
},
{
"cell_type": "code",
"source": [
"z_threshold_target = 5\n",
"edit_rules_list = [\n",
" mct.network_editor.EditRule(\n",
" filter=mct.network_editor.NodeTypeFilter(Conv2D),\n",
" action=mct.network_editor.ChangeCandidatesActivationQuantConfigAttr(z_threshold=z_threshold_target)\n",
" )\n",
"]\n",
"\n",
"debug_config = mct.DebugConfig(network_editor=edit_rules_list)\n",
"core_config = mct.core.CoreConfig(debug_config=debug_config)\n",
"quantized_model = quantize_keras_mct(model, representative_data_gen, core_config)"
],
"metadata": {
"id": "VBRfQqZVjN3J"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
Expand Down

0 comments on commit fdc5520

Please sign in to comment.