Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keras QAT Docs update for standalone batchnorms #3332

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/api_docs/keras_quantsim.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Code Examples

.. literalinclude:: ../keras_code_examples/quantization.py
:language: python
:lines: 37-40
:lines: 37-42

**Quantize with Fine tuning**

Expand Down
9 changes: 9 additions & 0 deletions Docs/keras_code_examples/quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import tensorflow as tf

from aimet_tensorflow.keras import quantsim
# Optional import only required for fine-tuning
from aimet_tensorflow.keras.quant_sim.qc_quantize_wrapper import QcQuantizeWrapper

def evaluate(model: tf.keras.Model, forward_pass_callback_args):
"""
Expand Down Expand Up @@ -68,6 +70,13 @@ def quantize_model():
sim.compute_encodings(evaluate, forward_pass_callback_args=(dummy_x, dummy_y))

# Do some fine-tuning
# Note:: For GPU workloads and models with non-trainable BatchNorms is not supported,
# So user need to explicitly set the BatchNorms to trainable.
# Below code snippet sets the BatchNorms to trainable
for layer in sim.model.layers:
if isinstance(layer, QcQuantizeWrapper) and isinstance(layer._layer_to_wrap, tf.keras.layers.BatchNormalization):
layer._layer_to_wrap.trainable = True

sim.model.fit(x=dummy_x, y=dummy_y, epochs=10)

quantize_model()
Loading