From 616d44a67eb1772ae4a5d4b772a5350c26d1a68b Mon Sep 17 00:00:00 2001 From: Geunho Lee Date: Thu, 23 Nov 2023 14:23:09 +0900 Subject: [PATCH] Add Keras Quant Analyzer document Signed-off-by: Geunho Lee --- Docs/api_docs/keras_quant_analyzer.rst | 67 +++++++++++++++++++ Docs/api_docs/keras_quantization.rst | 21 ++++-- .../quant_analyzer_code_example.py | 6 +- .../aimet_tensorflow/keras/quant_analyzer.py | 10 +-- 4 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 Docs/api_docs/keras_quant_analyzer.rst diff --git a/Docs/api_docs/keras_quant_analyzer.rst b/Docs/api_docs/keras_quant_analyzer.rst new file mode 100644 index 00000000000..139d44e637a --- /dev/null +++ b/Docs/api_docs/keras_quant_analyzer.rst @@ -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 diff --git a/Docs/api_docs/keras_quantization.rst b/Docs/api_docs/keras_quantization.rst index 0c82b873fed..27d47ae7596 100644 --- a/Docs/api_docs/keras_quantization.rst +++ b/Docs/api_docs/keras_quantization.rst @@ -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 + Model Preparer API + Quant Analyzer API + Quantization Simulation API + Adaptive Rounding API + Cross-Layer Equalization API + BN Re-estimation APIs + Users should first invoke Model Preparer API before using any of the AIMET Quantization features. - :ref:`Model Guidelines`: Guidelines for defining Keras models - :ref:`Model Preparer API`: Allows user to automate model definition changes AIMET Quantization for Keras provides the following functionality - - :ref:`Quantization Simulation`: Allows ability to simulate inference and training on quantized hardware - - :ref:`Adaptive Rounding`: Post-training quantization technique to optimize rounding of weight tensors - - :ref:`Cross-Layer Equalization`: Post-training quantization technique to equalize layer parameters - - :ref:`BatchNorm Re-estimation`: Quantization-aware training technique to counter potential instability of batchnorm statistics (i.e. running mean and variance) + - :ref:`Quant Analyzer API`: Analyzes the model and points out sensitive layers to quantization + - :ref:`Quantization Simulation API`: Allows ability to simulate inference and training on quantized hardware + - :ref:`Adaptive Rounding API`: Post-training quantization technique to optimize rounding of weight tensors + - :ref:`Cross-Layer Equalization API`: Post-training quantization technique to equalize layer parameters + - :ref:`BatchNorm Re-estimation API`: Quantization-aware training technique to counter potential instability of batchnorm statistics (i.e. running mean and variance) diff --git a/Docs/keras_code_examples/quant_analyzer_code_example.py b/Docs/keras_code_examples/quant_analyzer_code_example.py index 4f6fddd23e1..3462984cc11 100644 --- a/Docs/keras_code_examples/quant_analyzer_code_example.py +++ b/Docs/keras_code_examples/quant_analyzer_code_example.py @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/TrainingExtensions/tensorflow/src/python/aimet_tensorflow/keras/quant_analyzer.py b/TrainingExtensions/tensorflow/src/python/aimet_tensorflow/keras/quant_analyzer.py index a6ace0b0dfb..47639d7d49f 100644 --- a/TrainingExtensions/tensorflow/src/python/aimet_tensorflow/keras/quant_analyzer.py +++ b/TrainingExtensions/tensorflow/src/python/aimet_tensorflow/keras/quant_analyzer.py @@ -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,