Skip to content

Commit

Permalink
Add Keras Quant Analyzer document
Browse files Browse the repository at this point in the history
Signed-off-by: Geunho Lee <quic_geunlee@quicinc.com>
  • Loading branch information
quic-geunlee authored and neoci committed Nov 28, 2023
1 parent 3b9589d commit 616d44a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 12 deletions.
67 changes: 67 additions & 0 deletions Docs/api_docs/keras_quant_analyzer.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
:orphan:

.. _api-keras-quant-analyzer:

================================
AIMET Keras Quant Analyzer API
================================

AIMET Keras Quant Analyzer analyzes the Keras model and points out sensitive layers to quantization in the model.
It checks model sensitivity to weight and activation quantization, performs per layer sensitivity and MSE analysis.
It also exports per layer encodings min and max ranges and statistics histogram for every layer.

Top-level API
=============
.. autoclass:: aimet_tensorflow.keras.quant_analyzer.QuantAnalyzer
:members: analyze

Code Examples
=============

**Required imports**

.. literalinclude:: ../keras_code_examples/quant_analyzer_code_example.py
:language: python
:lines: 39-47

**Prepare toy dataset to run example code**

.. literalinclude:: ../keras_code_examples/quant_analyzer_code_example.py
:language: python
:start-after: # Step 0. Prepare toy dataset to run example code
:end-before: # End step 0

**Prepare forward pass callback**

.. literalinclude:: ../keras_code_examples/quant_analyzer_code_example.py
:language: python
:start-after: # Step 1. Prepare forward pass callback
:end-before: # End step 1

**Prepare eval callback**

.. literalinclude:: ../keras_code_examples/quant_analyzer_code_example.py
:language: python
:start-after: # Step 2. Prepare eval callback
:end-before: # End step 2

**Prepare model**

.. literalinclude:: ../keras_code_examples/quant_analyzer_code_example.py
:language: python
:start-after: # Step 3. Prepare model
:end-before: # End step 3

**Create QuantAnalyzer object**

.. literalinclude:: ../keras_code_examples/quant_analyzer_code_example.py
:language: python
:start-after: # Step 4. Create QuantAnalyzer object
:end-before: # End step 4

**Run QuantAnalyzer**

.. literalinclude:: ../keras_code_examples/quant_analyzer_code_example.py
:language: python
:start-after: # Step 5. Run QuantAnalyzer
:end-before: # End step 5
21 changes: 17 additions & 4 deletions Docs/api_docs/keras_quantization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@ In order to make full use of AIMET Quantization features, there are several guid
when defining Keras models. AIMET provides APIs which can automate some of the model definition changes and checks
whether AIMET Quantization features can be applied on Keras model.

.. toctree::
:titlesonly:
:hidden:

Model Guidelines <keras_model_guidelines>
Model Preparer API<keras_model_preparer>
Quant Analyzer API<keras_quant_analyzer>
Quantization Simulation API<keras_quantsim>
Adaptive Rounding API<keras_adaround>
Cross-Layer Equalization API<keras_cross_layer_equalization>
BN Re-estimation APIs<keras_batchnorm_re_estimation>

Users should first invoke Model Preparer API before using any of the AIMET Quantization features.
- :ref:`Model Guidelines<api-keras-model-guidelines>`: Guidelines for defining Keras models
- :ref:`Model Preparer API<api-keras-model-preparer>`: Allows user to automate model definition changes

AIMET Quantization for Keras provides the following functionality
- :ref:`Quantization Simulation<api-keras-quantsim>`: Allows ability to simulate inference and training on quantized hardware
- :ref:`Adaptive Rounding<api-keras-adaround>`: Post-training quantization technique to optimize rounding of weight tensors
- :ref:`Cross-Layer Equalization<api-keras-cle>`: Post-training quantization technique to equalize layer parameters
- :ref:`BatchNorm Re-estimation<api-keras-bn-reestimation>`: Quantization-aware training technique to counter potential instability of batchnorm statistics (i.e. running mean and variance)
- :ref:`Quant Analyzer API<api-keras-quant-analyzer>`: Analyzes the model and points out sensitive layers to quantization
- :ref:`Quantization Simulation API<api-keras-quantsim>`: Allows ability to simulate inference and training on quantized hardware
- :ref:`Adaptive Rounding API<api-keras-adaround>`: Post-training quantization technique to optimize rounding of weight tensors
- :ref:`Cross-Layer Equalization API<api-keras-cle>`: Post-training quantization technique to equalize layer parameters
- :ref:`BatchNorm Re-estimation API<api-keras-bn-reestimation>`: Quantization-aware training technique to counter potential instability of batchnorm statistics (i.e. running mean and variance)
6 changes: 3 additions & 3 deletions Docs/keras_code_examples/quant_analyzer_code_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

from aimet_common.defs import QuantScheme
from aimet_common.utils import CallbackFunc
from aimet_tensorflow.keras.model_preparer import prepare_model
from aimet_tensorflow.keras.quant_analyzer import QuantAnalyzer

# Step 0. Prepare toy dataset to run example code
Expand Down Expand Up @@ -83,7 +84,6 @@ def forward_pass_callback(model: tf.keras.Model, _: Any = None) -> None:
# User should create data loader/iterable using representative dataset and simply run
# forward passes on the model.
_ = model.predict(unlabeled_dataset)

# End step 1


Expand Down Expand Up @@ -116,13 +116,13 @@ def eval_callback(model: tf.keras.Model, _: Any = None) -> float:

_, acc = model.evaluate(eval_dataset)
return acc

# End step 2


def quant_analyzer_example():
# Step 3. Prepare model
model = tf.keras.applications.ResNet50()
prepared_model = prepare_model(model)
# End step 3

# User action required
Expand All @@ -131,7 +131,7 @@ def quant_analyzer_example():
eval_callback_fn = CallbackFunc(eval_callback, func_callback_args=None)

# Step 4. Create QuantAnalyzer object
quant_analyzer = QuantAnalyzer(model=model,
quant_analyzer = QuantAnalyzer(model=prepared_model,
forward_pass_callback=forward_pass_callback_fn,
eval_callback=eval_callback_fn)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ class QuantAnalyzer:
"""
QuantAnalyzer tool provides
1) model sensitivity to weight and activation quantization
2) per layer sensitivity analysis
3) per layer encoding (min - max range)
4) per PDF analysis and
4) per layer MSE analysis
1) model sensitivity to weight and activation quantization
2) per layer sensitivity analysis
3) per layer encoding (min - max range)
4) per PDF analysis and
5) per layer MSE analysis
"""

def __init__(self,
Expand Down

0 comments on commit 616d44a

Please sign in to comment.