From 78f2f4d998254ddf7918d35607cb93f2909dfacf Mon Sep 17 00:00:00 2001 From: Reuven <44209964+reuvenperetz@users.noreply.github.com> Date: Sun, 17 Mar 2024 17:18:57 +0200 Subject: [PATCH] Update docsrc (#989) * Automate manual parts of creating pages in docsrc Makefile. * Remove the old API dir and use the experimental old dir as the current API in docsrs. * Update documentation in classes that are auto-generated. * Adapt docsrc to new API. --------- Co-authored-by: reuvenp --- docsrc/Makefile | 10 +- .../api_docs/classes/DataGenerationConfig.rst | 68 ++++++ .../classes/DefaultDict.rst | 0 .../classes/FolderImageLoader.rst | 2 +- .../classes/FrameworkInfo.rst | 4 +- .../classes/GradientPTQConfig.rst | 17 +- docsrc/source/api/api_docs/classes/KPI.rst | 11 + .../MixedPrecisionQuantizationConfig.rst | 11 + .../api_docs/classes/MpDistanceWeighting.rst | 12 ++ .../api/api_docs/classes/PruningConfig.rst | 30 +++ .../api/api_docs/classes/PruningInfo.rst | 11 + .../api_docs/classes/QuantizationConfig.rst | 10 + .../classes/QuantizationErrorMethod.rst | 11 + docsrc/source/api/api_docs/index.rst | 106 ++++++++-- .../get_keras_data_generation_config.rst | 0 .../methods/get_keras_gptq_config.rst | 0 .../get_pytorch_data_generation_config.rst | 0 .../methods/get_pytroch_gptq_config.rst | 0 .../get_target_platform_capabilities.rst | 0 .../keras_data_generation_experimental.rst | 10 + ...s_gradient_post_training_quantization.rst} | 4 +- .../api/api_docs/methods/keras_kpi_data.rst | 2 +- .../methods/keras_load_quantizad_model.rst | 0 .../keras_post_training_quantization.rst | 2 +- ..._training_quantization_mixed_precision.rst | 10 - .../methods/keras_pruning_experimental.rst | 12 ++ ..._aware_training_finalize_experimental.rst} | 4 +- ...tion_aware_training_init_experimental.rst} | 4 +- .../pytorch_data_generation_experimental.rst | 0 ...h_gradient_post_training_quantization.rst} | 4 +- .../api/api_docs/methods/pytorch_kpi_data.rst | 2 +- .../pytorch_post_training_quantization.rst | 2 +- ..._training_quantization_mixed_precision.rst | 10 - .../methods/pytorch_pruning_experimental.rst | 15 ++ ..._aware_training_finalize_experimental.rst} | 4 +- ...tion_aware_training_init_experimental.rst} | 4 +- .../methods/set_logger_path.rst | 2 +- .../modules/core_config.rst | 6 +- .../modules/debug_config.rst | 2 +- .../source/api/api_docs/modules/exporter.rst | 199 ++++++++++++++++++ .../modules/layer_filters.rst | 0 .../mixed_precision_quantization_config.rst | 27 --- .../api/api_docs/modules/network_editor.rst | 62 ++++++ .../modules/qat_config.rst | 0 .../modules/target_platform.rst | 0 .../modules/trainable_infrastructure.rst | 0 .../notes/tpc_note.rst | 0 .../api/experimental_api_docs/index.rst | 70 ------ .../keras_data_generation_experimental.rst | 10 - .../methods/keras_kpi_data_experimental.rst | 10 - ...ost_training_quantization_experimental.rst | 10 - .../methods/keras_pruning_experimental.rst | 25 --- .../methods/pytorch_kpi_data_experimental.rst | 10 - ...ost_training_quantization_experimental.rst | 10 - .../methods/pytorch_pruning_experimental.rst | 25 --- .../modules/exporter.rst | 41 ---- .../mixed_precision_quantization_config.rst | 26 --- .../modules/network_editor.rst | 62 ------ .../modules/quantization_config.rst | 25 --- .../notes/experimental_api_note.rst | 7 - docsrc/source/guidelines/quickstart_keras.rst | 107 ---------- .../source/guidelines/quickstart_pytorch.rst | 103 --------- docsrc/source/guidelines/visualization.rst | 2 +- docsrc/source/index.rst | 44 ++-- model_compression_toolkit/core/__init__.py | 2 + .../core/common/pruning/pruning_config.py | 10 +- .../core/common/pruning/pruning_info.py | 13 +- .../data_generation/__init__.py | 3 + .../data_generation/common/enums.py | 83 +++++--- .../keras/keras_data_generation.py | 29 +++ .../pytorch/pytorch_data_generation.py | 31 ++- .../fw_agonstic/quantization_format.py | 10 + .../keras/keras_export_facade.py | 14 +- .../gptq/common/gptq_config.py | 9 +- .../pruning/keras/pruning_facade.py | 9 +- .../operations_to_layers.py | 6 +- 76 files changed, 760 insertions(+), 736 deletions(-) create mode 100644 docsrc/source/api/api_docs/classes/DataGenerationConfig.rst rename docsrc/source/api/{experimental_api_docs => api_docs}/classes/DefaultDict.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/classes/FolderImageLoader.rst (87%) rename docsrc/source/api/{experimental_api_docs => api_docs}/classes/FrameworkInfo.rst (74%) rename docsrc/source/api/{experimental_api_docs => api_docs}/classes/GradientPTQConfig.rst (71%) create mode 100644 docsrc/source/api/api_docs/classes/KPI.rst create mode 100644 docsrc/source/api/api_docs/classes/MixedPrecisionQuantizationConfig.rst create mode 100644 docsrc/source/api/api_docs/classes/MpDistanceWeighting.rst create mode 100644 docsrc/source/api/api_docs/classes/PruningConfig.rst create mode 100644 docsrc/source/api/api_docs/classes/PruningInfo.rst create mode 100644 docsrc/source/api/api_docs/classes/QuantizationConfig.rst create mode 100644 docsrc/source/api/api_docs/classes/QuantizationErrorMethod.rst rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/get_keras_data_generation_config.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/get_keras_gptq_config.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/get_pytorch_data_generation_config.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/get_pytroch_gptq_config.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/get_target_platform_capabilities.rst (100%) create mode 100644 docsrc/source/api/api_docs/methods/keras_data_generation_experimental.rst rename docsrc/source/api/{experimental_api_docs/methods/keras_gradient_post_training_quantization_experimental.rst => api_docs/methods/keras_gradient_post_training_quantization.rst} (68%) rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/keras_load_quantizad_model.rst (100%) delete mode 100644 docsrc/source/api/api_docs/methods/keras_post_training_quantization_mixed_precision.rst create mode 100644 docsrc/source/api/api_docs/methods/keras_pruning_experimental.rst rename docsrc/source/api/{experimental_api_docs/methods/keras_quantization_aware_training_finalize.rst => api_docs/methods/keras_quantization_aware_training_finalize_experimental.rst} (68%) rename docsrc/source/api/{experimental_api_docs/methods/keras_quantization_aware_training_init.rst => api_docs/methods/keras_quantization_aware_training_init_experimental.rst} (69%) rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/pytorch_data_generation_experimental.rst (100%) rename docsrc/source/api/{experimental_api_docs/methods/pytorch_gradient_post_training_quantization_experimental.rst => api_docs/methods/pytorch_gradient_post_training_quantization.rst} (68%) delete mode 100644 docsrc/source/api/api_docs/methods/pytorch_post_training_quantization_mixed_precision.rst create mode 100644 docsrc/source/api/api_docs/methods/pytorch_pruning_experimental.rst rename docsrc/source/api/{experimental_api_docs/methods/pytorch_quantization_aware_training_finalize.rst => api_docs/methods/pytorch_quantization_aware_training_finalize_experimental.rst} (68%) rename docsrc/source/api/{experimental_api_docs/methods/pytorch_quantization_aware_training_init.rst => api_docs/methods/pytorch_quantization_aware_training_init_experimental.rst} (68%) rename docsrc/source/api/{experimental_api_docs => api_docs}/methods/set_logger_path.rst (86%) rename docsrc/source/api/{experimental_api_docs => api_docs}/modules/core_config.rst (61%) rename docsrc/source/api/{experimental_api_docs => api_docs}/modules/debug_config.rst (79%) create mode 100644 docsrc/source/api/api_docs/modules/exporter.rst rename docsrc/source/api/{experimental_api_docs => api_docs}/modules/layer_filters.rst (100%) delete mode 100644 docsrc/source/api/api_docs/modules/mixed_precision_quantization_config.rst create mode 100644 docsrc/source/api/api_docs/modules/network_editor.rst rename docsrc/source/api/{experimental_api_docs => api_docs}/modules/qat_config.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/modules/target_platform.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/modules/trainable_infrastructure.rst (100%) rename docsrc/source/api/{experimental_api_docs => api_docs}/notes/tpc_note.rst (100%) delete mode 100644 docsrc/source/api/experimental_api_docs/index.rst delete mode 100644 docsrc/source/api/experimental_api_docs/methods/keras_data_generation_experimental.rst delete mode 100644 docsrc/source/api/experimental_api_docs/methods/keras_kpi_data_experimental.rst delete mode 100644 docsrc/source/api/experimental_api_docs/methods/keras_post_training_quantization_experimental.rst delete mode 100644 docsrc/source/api/experimental_api_docs/methods/keras_pruning_experimental.rst delete mode 100644 docsrc/source/api/experimental_api_docs/methods/pytorch_kpi_data_experimental.rst delete mode 100644 docsrc/source/api/experimental_api_docs/methods/pytorch_post_training_quantization_experimental.rst delete mode 100644 docsrc/source/api/experimental_api_docs/methods/pytorch_pruning_experimental.rst delete mode 100644 docsrc/source/api/experimental_api_docs/modules/exporter.rst delete mode 100644 docsrc/source/api/experimental_api_docs/modules/mixed_precision_quantization_config.rst delete mode 100644 docsrc/source/api/experimental_api_docs/modules/network_editor.rst delete mode 100644 docsrc/source/api/experimental_api_docs/modules/quantization_config.rst delete mode 100644 docsrc/source/api/experimental_api_docs/notes/experimental_api_note.rst delete mode 100644 docsrc/source/guidelines/quickstart_keras.rst delete mode 100644 docsrc/source/guidelines/quickstart_pytorch.rst diff --git a/docsrc/Makefile b/docsrc/Makefile index a31637a91..7d5921aeb 100644 --- a/docsrc/Makefile +++ b/docsrc/Makefile @@ -6,11 +6,13 @@ github: @cp -a ../docs/html/. ../docs @rm -rf ../docs/html/ @rm -rf ../docs/doctrees/ + @find ../docs -type f -exec sed -i 's|_images/|images/|g' {} + + @find ../docs -type f -exec sed -i 's|_static/|static/|g' {} + + @rm -rf ../docs/images + @rm -rf ../docs/static + @mv ../docs/_images/ ../docs/images + @mv ../docs/_static/ ../docs/static -# rm -rf ../docs/images/ -# rm -rf ../docs/statis/ -# mv ../docs/_images/ ../docs/images -# mv ../docs/_static/ ../docs/static # You can set these variables from the command line, and also diff --git a/docsrc/source/api/api_docs/classes/DataGenerationConfig.rst b/docsrc/source/api/api_docs/classes/DataGenerationConfig.rst new file mode 100644 index 000000000..694183ea7 --- /dev/null +++ b/docsrc/source/api/api_docs/classes/DataGenerationConfig.rst @@ -0,0 +1,68 @@ +:orphan: + +.. _ug-DataGenerationConfig: + +================================================ +Data Generation Configuration +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.DataGenerationConfig + :members: + + +ImageGranularity +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.ImageGranularity + :members: + + +SchedulerType +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.SchedulerType + :members: + + + +BatchNormAlignemntLossType +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.BatchNormAlignemntLossType + :members: + + +OutputLossType +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.OutputLossType + :members: + + +DataInitType +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.DataInitType + :members: + + +BNLayerWeightingType +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.BNLayerWeightingType + :members: + + +ImagePipelineType +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.ImagePipelineType + :members: + + +ImageNormalizationType +================================================ + +.. autoclass:: model_compression_toolkit.data_generation.ImageNormalizationType + :members: + diff --git a/docsrc/source/api/experimental_api_docs/classes/DefaultDict.rst b/docsrc/source/api/api_docs/classes/DefaultDict.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/classes/DefaultDict.rst rename to docsrc/source/api/api_docs/classes/DefaultDict.rst diff --git a/docsrc/source/api/experimental_api_docs/classes/FolderImageLoader.rst b/docsrc/source/api/api_docs/classes/FolderImageLoader.rst similarity index 87% rename from docsrc/source/api/experimental_api_docs/classes/FolderImageLoader.rst rename to docsrc/source/api/api_docs/classes/FolderImageLoader.rst index e9fda9d43..6897e353f 100644 --- a/docsrc/source/api/experimental_api_docs/classes/FolderImageLoader.rst +++ b/docsrc/source/api/api_docs/classes/FolderImageLoader.rst @@ -8,7 +8,7 @@ Folder Image Loader API **The following API can be used to load a folder of images to create a representative dataset for PTQ calibration** -.. autoclass:: model_compression_toolkit.FolderImageLoader +.. autoclass:: model_compression_toolkit.core.FolderImageLoader :members: diff --git a/docsrc/source/api/experimental_api_docs/classes/FrameworkInfo.rst b/docsrc/source/api/api_docs/classes/FrameworkInfo.rst similarity index 74% rename from docsrc/source/api/experimental_api_docs/classes/FrameworkInfo.rst rename to docsrc/source/api/api_docs/classes/FrameworkInfo.rst index 8aa3bcd52..2338a7dd4 100644 --- a/docsrc/source/api/experimental_api_docs/classes/FrameworkInfo.rst +++ b/docsrc/source/api/api_docs/classes/FrameworkInfo.rst @@ -9,7 +9,7 @@ FrameworkInfo Class **The following API can be used to pass MCT framework-related information to use when optimizing the network** -.. autoclass:: model_compression_toolkit.FrameworkInfo +.. autoclass:: model_compression_toolkit.core.FrameworkInfo | @@ -19,5 +19,5 @@ ChannelAxis ========================== **Enum to select the output channels format in the model:** -.. autoclass:: model_compression_toolkit.ChannelAxis +.. autoclass:: model_compression_toolkit.core.ChannelAxis diff --git a/docsrc/source/api/experimental_api_docs/classes/GradientPTQConfig.rst b/docsrc/source/api/api_docs/classes/GradientPTQConfig.rst similarity index 71% rename from docsrc/source/api/experimental_api_docs/classes/GradientPTQConfig.rst rename to docsrc/source/api/api_docs/classes/GradientPTQConfig.rst index caea133e0..c14ec1e7c 100644 --- a/docsrc/source/api/experimental_api_docs/classes/GradientPTQConfig.rst +++ b/docsrc/source/api/api_docs/classes/GradientPTQConfig.rst @@ -2,15 +2,6 @@ .. _ug-GradientPTQConfig: -================================= -GradientPTQConfigV2 Class -================================= - -**The following API can be used to create a GradientPTQConfigV2 instance which can be used for post training quantization using knowledge distillation from a teacher (float model) to a student (the quantized model). This is experimental and subject to future changes.** - -.. autoclass:: model_compression_toolkit.gptq.GradientPTQConfigV2 - :members: - ================================= GradientPTQConfig Class @@ -31,3 +22,11 @@ GPTQHessianScoresConfig Class .. autoclass:: model_compression_toolkit.gptq.GPTQHessianScoresConfig :members: + + +================================= +RoundingType +================================= + +.. autoclass:: model_compression_toolkit.gptq.RoundingType + :members: diff --git a/docsrc/source/api/api_docs/classes/KPI.rst b/docsrc/source/api/api_docs/classes/KPI.rst new file mode 100644 index 000000000..a25c98289 --- /dev/null +++ b/docsrc/source/api/api_docs/classes/KPI.rst @@ -0,0 +1,11 @@ +:orphan: + +.. _ug-KPI: + + +KPI +================================ +**Object to configure resources to use when searching for a configuration for the optimized model (such as in mixed-precision, pruning, etc.):** + +.. autoclass:: model_compression_toolkit.core.KPI + diff --git a/docsrc/source/api/api_docs/classes/MixedPrecisionQuantizationConfig.rst b/docsrc/source/api/api_docs/classes/MixedPrecisionQuantizationConfig.rst new file mode 100644 index 000000000..eba91660f --- /dev/null +++ b/docsrc/source/api/api_docs/classes/MixedPrecisionQuantizationConfig.rst @@ -0,0 +1,11 @@ +:orphan: + +.. _ug-MixedPrecisionQuantizationConfig: + + +MixedPrecisionQuantizationConfig +=================================== +**Class to configure the quantization process of the model when quantizing in mixed-precision:** + +.. autoclass:: model_compression_toolkit.core.MixedPrecisionQuantizationConfig + diff --git a/docsrc/source/api/api_docs/classes/MpDistanceWeighting.rst b/docsrc/source/api/api_docs/classes/MpDistanceWeighting.rst new file mode 100644 index 000000000..56aad7c1a --- /dev/null +++ b/docsrc/source/api/api_docs/classes/MpDistanceWeighting.rst @@ -0,0 +1,12 @@ +:orphan: + +.. _ug-MpDistanceWeighting: + + +================================= +MpDistanceWeighting +================================= + +.. autoclass:: model_compression_toolkit.core.MpDistanceWeighting + :members: + diff --git a/docsrc/source/api/api_docs/classes/PruningConfig.rst b/docsrc/source/api/api_docs/classes/PruningConfig.rst new file mode 100644 index 000000000..bbf2e8eaa --- /dev/null +++ b/docsrc/source/api/api_docs/classes/PruningConfig.rst @@ -0,0 +1,30 @@ +:orphan: + +.. _ug-PruningConfig: + +================================================ +Pruning Configuration +================================================ + +.. autoclass:: model_compression_toolkit.pruning.PruningConfig + :members: + + +ImportanceMetric +================================================ + +.. autoclass:: model_compression_toolkit.pruning.ImportanceMetric + :members: + + +ChannelsFilteringStrategy +================================================ + +.. autoclass:: model_compression_toolkit.pruning.ChannelsFilteringStrategy + :members: + + + + + + diff --git a/docsrc/source/api/api_docs/classes/PruningInfo.rst b/docsrc/source/api/api_docs/classes/PruningInfo.rst new file mode 100644 index 000000000..9b8efea4d --- /dev/null +++ b/docsrc/source/api/api_docs/classes/PruningInfo.rst @@ -0,0 +1,11 @@ +:orphan: + +.. _ug-PruningInfo: + +================================================ +Pruning Information +================================================ + +.. autoclass:: model_compression_toolkit.pruning.PruningInfo + :members: + diff --git a/docsrc/source/api/api_docs/classes/QuantizationConfig.rst b/docsrc/source/api/api_docs/classes/QuantizationConfig.rst new file mode 100644 index 000000000..4759e44ee --- /dev/null +++ b/docsrc/source/api/api_docs/classes/QuantizationConfig.rst @@ -0,0 +1,10 @@ +:orphan: + +.. _ug-QuantizationConfig: + + +QuantizationConfig +========================== +**Class to configure the quantization process of the model:** + +.. autoclass:: model_compression_toolkit.core.QuantizationConfig diff --git a/docsrc/source/api/api_docs/classes/QuantizationErrorMethod.rst b/docsrc/source/api/api_docs/classes/QuantizationErrorMethod.rst new file mode 100644 index 000000000..25097f87c --- /dev/null +++ b/docsrc/source/api/api_docs/classes/QuantizationErrorMethod.rst @@ -0,0 +1,11 @@ +:orphan: + +.. _ug-QuantizationErrorMethod: + + +QuantizationErrorMethod +========================== +**Enum to select a method for quantization parameters' selection:** + +.. autoclass:: model_compression_toolkit.core.QuantizationErrorMethod + diff --git a/docsrc/source/api/api_docs/index.rst b/docsrc/source/api/api_docs/index.rst index 28f078466..fd5bb6e01 100644 --- a/docsrc/source/api/api_docs/index.rst +++ b/docsrc/source/api/api_docs/index.rst @@ -7,8 +7,6 @@ API Docs ========= -.. note:: This API will be removed in future releases. Please switch to the :ref:`new API` - **Init module for MCT API.** .. code-block:: python @@ -17,30 +15,96 @@ API Docs | -Functions -========= -- :ref:`pytorch_post_training_quantization`: Function to use for post training quantization of Pytorch models. -- :ref:`pytorch_post_training_quantization_mixed_precision`: Function to use for mixed-precision post training quantization of Pytorch models (experimental). -- :ref:`keras_post_training_quantization`: Function to use for post training quantization of Keras models. -- :ref:`keras_post_training_quantization_mixed_precision`: Function to use for mixed-precision post training quantization of Keras models (experimental). -- :ref:`get_keras_gptq_config`: Function to create a GradientPTQConfig instance to use for Keras models when using GPTQ (experimental). -- :ref:`get_target_platform_capabilities`: Function to get a target platform model for Tensorflow and Pytorch. -- :ref:`keras_kpi_data`: Function to compute KPI data that can be used to calculate the desired target KPI for mixed-precision quantization of Keras models. -- :ref:`pytorch_kpi_data`: Function to compute KPI data that can be used to calculate the desired target KPI for mixed-precision quantization of PyTorch models. + +ptq +==== + +- :ref:`pytorch_post_training_quantization`: A function to use for post training quantization of PyTorch models. +- :ref:`keras_post_training_quantization`: A function to use for post training quantization of Keras models. + +gptq +==== + +- :ref:`pytorch_gradient_post_training_quantization`: A function to use for gradient-based post training quantization of Pytorch models. +- :ref:`get_pytorch_gptq_config`: A function to create a GradientPTQConfig instance to use for Pytorch models when using GPTQ. + +- :ref:`keras_gradient_post_training_quantization`: A function to use for gradient-based post training quantization of Keras models. +- :ref:`get_keras_gptq_config`: A function to create a GradientPTQConfig instance to use for Keras models when using GPTQ. + +- :ref:`GradientPTQConfig`: Class to configure GradientPTQ options for gradient based post training quantization. + +qat +==== + +- :ref:`pytorch_quantization_aware_training_init_experimental`: A function to use for preparing a Pytorch model for Quantization Aware Training (experimental). +- :ref:`pytorch_quantization_aware_training_finalize_experimental`: A function to finalize a Pytorch model after Quantization Aware Training to a model without QuantizeWrappers (experimental). +- :ref:`keras_quantization_aware_training_init_experimental`: A function to use for preparing a Keras model for Quantization Aware Training (experimental). +- :ref:`keras_quantization_aware_training_finalize_experimental`: A function to finalize a Keras model after Quantization Aware Training to a model without QuantizeWrappers (experimental). +- :ref:`qat_config`: Module to create quantization configuration for Quantization-aware Training (experimental). + +core +===== + +- :ref:`CoreConfig`: Module to contain configurations of the entire optimization process. +- :ref:`QuantizationConfig`: Module to configure the quantization process. +- :ref:`QuantizationErrorMethod`: Select a method for quantization parameters' selection. +- :ref:`MixedPrecisionQuantizationConfig`: Module to configure the quantization process when using mixed-precision PTQ. +- :ref:`KPI`: Module to configure resources to use when searching for a configuration for the optimized model. +- :ref:`MpDistanceWeighting`: Mixed precision distance metric weighting methods. +- :ref:`network_editor`: Module to modify the optimization process for troubleshooting. +- :ref:`FolderImageLoader`: Class to use an images directory as a representative dataset. +- :ref:`pytorch_kpi_data`: A function to compute KPI data that can be used to calculate the desired target KPI for PyTorch models. +- :ref:`keras_kpi_data`: A function to compute KPI data that can be used to calculate the desired target KPI for Keras models. + + +data_generation +================= + +- :ref:`pytorch_data_generation_experimental`: A function to generate data for a Pytorch model (experimental). +- :ref:`get_pytorch_data_generation_config`: A function to load a DataGenerationConfig for Pytorch data generation (experimental). +- :ref:`keras_data_generation_experimental`: A function to generate data for a Keras model (experimental). +- :ref:`get_keras_data_generation_config`: A function to generate a DataGenerationConfig for Tensorflow data generation (experimental). +- :ref:`DataGenerationConfig`: A configuration class for the data generation process (experimental). -Modules +pruning +=========== + +- :ref:`pytorch_pruning_experimental`: A function to apply structured pruning for Pytorch models (experimental). +- :ref:`keras_pruning_experimental`: A function to apply structured pruning for Keras models (experimental). + +- :ref:`PruningConfig`: Configuration for the pruning process (experimental). +- :ref:`PruningInfo`: Information about the pruned model such as pruned channel indices, etc. (experimental). + + +exporter ========= -- :ref:`quantization_config`: Module to configure the quantization process. -- :ref:`mixed_precision_quantization_config`: Module to configure the quantization process when using mixed-precision PTQ. -- :ref:`network_editor`: Module to edit your model during the quantization process. + +- :ref:`exporter`: Module that enables to export a quantized model in different serialization formats. + + +trainable_infrastructure +========================= + +- :ref:`trainable_infrastructure`: Module that contains quantization abstraction and quantizers for hardware-oriented model optimization tools. + + + +set_log_folder +================ +- :ref:`set_log_folder`: Function to set the logger path directory and to enable logging. + +keras_load_quantized_model +============================ +- :ref:`keras_load_quantized_model`: A function to load a quantized keras model. + + +target_platform +================ - :ref:`target_platform`: Module to create and model hardware-related settings to optimize the model according to, by the hardware the optimized model will use during inference. +- :ref:`get_target_platform_capabilities`: A function to get a target platform model for Tensorflow and Pytorch. +- :ref:`DefaultDict`: Util class for creating a TargetPlatformCapabilities. -Classes -========= -- :ref:`GradientPTQConfig`: Class to configure GradientPTQC options for gradient based post training quantization. -- :ref:`FolderImageLoader`: Class to use an images directory as a representative dataset. -- :ref:`FrameworkInfo`: Class to wrap framework information to be used by MCT when optimizing models. Indices and tables ================== diff --git a/docsrc/source/api/experimental_api_docs/methods/get_keras_data_generation_config.rst b/docsrc/source/api/api_docs/methods/get_keras_data_generation_config.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/methods/get_keras_data_generation_config.rst rename to docsrc/source/api/api_docs/methods/get_keras_data_generation_config.rst diff --git a/docsrc/source/api/experimental_api_docs/methods/get_keras_gptq_config.rst b/docsrc/source/api/api_docs/methods/get_keras_gptq_config.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/methods/get_keras_gptq_config.rst rename to docsrc/source/api/api_docs/methods/get_keras_gptq_config.rst diff --git a/docsrc/source/api/experimental_api_docs/methods/get_pytorch_data_generation_config.rst b/docsrc/source/api/api_docs/methods/get_pytorch_data_generation_config.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/methods/get_pytorch_data_generation_config.rst rename to docsrc/source/api/api_docs/methods/get_pytorch_data_generation_config.rst diff --git a/docsrc/source/api/experimental_api_docs/methods/get_pytroch_gptq_config.rst b/docsrc/source/api/api_docs/methods/get_pytroch_gptq_config.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/methods/get_pytroch_gptq_config.rst rename to docsrc/source/api/api_docs/methods/get_pytroch_gptq_config.rst diff --git a/docsrc/source/api/experimental_api_docs/methods/get_target_platform_capabilities.rst b/docsrc/source/api/api_docs/methods/get_target_platform_capabilities.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/methods/get_target_platform_capabilities.rst rename to docsrc/source/api/api_docs/methods/get_target_platform_capabilities.rst diff --git a/docsrc/source/api/api_docs/methods/keras_data_generation_experimental.rst b/docsrc/source/api/api_docs/methods/keras_data_generation_experimental.rst new file mode 100644 index 000000000..66dfa8dcf --- /dev/null +++ b/docsrc/source/api/api_docs/methods/keras_data_generation_experimental.rst @@ -0,0 +1,10 @@ +:orphan: + +.. _ug-keras_data_generation_experimental: + + +===================================== +Keras Data Generation +===================================== + +.. autofunction:: model_compression_toolkit.data_generation.keras_data_generation_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_gradient_post_training_quantization_experimental.rst b/docsrc/source/api/api_docs/methods/keras_gradient_post_training_quantization.rst similarity index 68% rename from docsrc/source/api/experimental_api_docs/methods/keras_gradient_post_training_quantization_experimental.rst rename to docsrc/source/api/api_docs/methods/keras_gradient_post_training_quantization.rst index fa99048f1..afd2eabf6 100644 --- a/docsrc/source/api/experimental_api_docs/methods/keras_gradient_post_training_quantization_experimental.rst +++ b/docsrc/source/api/api_docs/methods/keras_gradient_post_training_quantization.rst @@ -1,10 +1,10 @@ :orphan: -.. _ug-keras_gradient_post_training_quantization_experimental: +.. _ug-keras_gradient_post_training_quantization: ================================================ Keras Gradient Based Post Training Quantization ================================================ -.. autofunction:: model_compression_toolkit.gptq.keras_gradient_post_training_quantization_experimental +.. autofunction:: model_compression_toolkit.gptq.keras_gradient_post_training_quantization diff --git a/docsrc/source/api/api_docs/methods/keras_kpi_data.rst b/docsrc/source/api/api_docs/methods/keras_kpi_data.rst index d1af078dc..a0205814f 100644 --- a/docsrc/source/api/api_docs/methods/keras_kpi_data.rst +++ b/docsrc/source/api/api_docs/methods/keras_kpi_data.rst @@ -7,4 +7,4 @@ Get KPI information for Keras Models ======================================= -.. autofunction:: model_compression_toolkit.keras_kpi_data +.. autofunction:: model_compression_toolkit.core.keras_kpi_data diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_load_quantizad_model.rst b/docsrc/source/api/api_docs/methods/keras_load_quantizad_model.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/methods/keras_load_quantizad_model.rst rename to docsrc/source/api/api_docs/methods/keras_load_quantizad_model.rst diff --git a/docsrc/source/api/api_docs/methods/keras_post_training_quantization.rst b/docsrc/source/api/api_docs/methods/keras_post_training_quantization.rst index 9a14b4510..82a351716 100644 --- a/docsrc/source/api/api_docs/methods/keras_post_training_quantization.rst +++ b/docsrc/source/api/api_docs/methods/keras_post_training_quantization.rst @@ -7,4 +7,4 @@ Keras Post Training Quantization ================================= -.. autofunction:: model_compression_toolkit.keras_post_training_quantization +.. autofunction:: model_compression_toolkit.ptq.keras_post_training_quantization diff --git a/docsrc/source/api/api_docs/methods/keras_post_training_quantization_mixed_precision.rst b/docsrc/source/api/api_docs/methods/keras_post_training_quantization_mixed_precision.rst deleted file mode 100644 index 650cc4c70..000000000 --- a/docsrc/source/api/api_docs/methods/keras_post_training_quantization_mixed_precision.rst +++ /dev/null @@ -1,10 +0,0 @@ -:orphan: - -.. _ug-keras_post_training_quantization_mixed_precision: - - -================================================================== -Keras Post Training Mixed Precision Quantization -================================================================== - -.. autofunction:: model_compression_toolkit.keras_post_training_quantization_mixed_precision diff --git a/docsrc/source/api/api_docs/methods/keras_pruning_experimental.rst b/docsrc/source/api/api_docs/methods/keras_pruning_experimental.rst new file mode 100644 index 000000000..508a11e47 --- /dev/null +++ b/docsrc/source/api/api_docs/methods/keras_pruning_experimental.rst @@ -0,0 +1,12 @@ +:orphan: + +.. _ug-keras_pruning_experimental: + + +================================================ +Keras Structured Pruning +================================================ + +.. autofunction:: model_compression_toolkit.pruning.keras_pruning_experimental + + diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_quantization_aware_training_finalize.rst b/docsrc/source/api/api_docs/methods/keras_quantization_aware_training_finalize_experimental.rst similarity index 68% rename from docsrc/source/api/experimental_api_docs/methods/keras_quantization_aware_training_finalize.rst rename to docsrc/source/api/api_docs/methods/keras_quantization_aware_training_finalize_experimental.rst index 1e2bac84a..54ff74a3f 100644 --- a/docsrc/source/api/experimental_api_docs/methods/keras_quantization_aware_training_finalize.rst +++ b/docsrc/source/api/api_docs/methods/keras_quantization_aware_training_finalize_experimental.rst @@ -1,10 +1,10 @@ :orphan: -.. _ug-keras_quantization_aware_training_finalize: +.. _ug-keras_quantization_aware_training_finalize_experimental: ================================================ Keras Quantization Aware Training Model Finalize ================================================ -.. autofunction:: model_compression_toolkit.qat.keras_quantization_aware_training_finalize +.. autofunction:: model_compression_toolkit.qat.keras_quantization_aware_training_finalize_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_quantization_aware_training_init.rst b/docsrc/source/api/api_docs/methods/keras_quantization_aware_training_init_experimental.rst similarity index 69% rename from docsrc/source/api/experimental_api_docs/methods/keras_quantization_aware_training_init.rst rename to docsrc/source/api/api_docs/methods/keras_quantization_aware_training_init_experimental.rst index f055e1621..b89c3367e 100644 --- a/docsrc/source/api/experimental_api_docs/methods/keras_quantization_aware_training_init.rst +++ b/docsrc/source/api/api_docs/methods/keras_quantization_aware_training_init_experimental.rst @@ -1,10 +1,10 @@ :orphan: -.. _ug-keras_quantization_aware_training_init: +.. _ug-keras_quantization_aware_training_init_experimental: ================================================ Keras Quantization Aware Training Model Init ================================================ -.. autofunction:: model_compression_toolkit.qat.keras_quantization_aware_training_init +.. autofunction:: model_compression_toolkit.qat.keras_quantization_aware_training_init_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/pytorch_data_generation_experimental.rst b/docsrc/source/api/api_docs/methods/pytorch_data_generation_experimental.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/methods/pytorch_data_generation_experimental.rst rename to docsrc/source/api/api_docs/methods/pytorch_data_generation_experimental.rst diff --git a/docsrc/source/api/experimental_api_docs/methods/pytorch_gradient_post_training_quantization_experimental.rst b/docsrc/source/api/api_docs/methods/pytorch_gradient_post_training_quantization.rst similarity index 68% rename from docsrc/source/api/experimental_api_docs/methods/pytorch_gradient_post_training_quantization_experimental.rst rename to docsrc/source/api/api_docs/methods/pytorch_gradient_post_training_quantization.rst index 4c30d210b..95dcf10f0 100644 --- a/docsrc/source/api/experimental_api_docs/methods/pytorch_gradient_post_training_quantization_experimental.rst +++ b/docsrc/source/api/api_docs/methods/pytorch_gradient_post_training_quantization.rst @@ -1,10 +1,10 @@ :orphan: -.. _ug-pytorch_gradient_post_training_quantization_experimental: +.. _ug-pytorch_gradient_post_training_quantization: ==================================================== Pytorch Gradient Based Post Training Quantization ==================================================== -.. autofunction:: model_compression_toolkit.gptq.pytorch_gradient_post_training_quantization_experimental +.. autofunction:: model_compression_toolkit.gptq.pytorch_gradient_post_training_quantization diff --git a/docsrc/source/api/api_docs/methods/pytorch_kpi_data.rst b/docsrc/source/api/api_docs/methods/pytorch_kpi_data.rst index be83da8d2..86f512eae 100644 --- a/docsrc/source/api/api_docs/methods/pytorch_kpi_data.rst +++ b/docsrc/source/api/api_docs/methods/pytorch_kpi_data.rst @@ -7,4 +7,4 @@ Get KPI information for PyTorch Models ======================================= -.. autofunction:: model_compression_toolkit.pytorch_kpi_data +.. autofunction:: model_compression_toolkit.core.pytorch_kpi_data diff --git a/docsrc/source/api/api_docs/methods/pytorch_post_training_quantization.rst b/docsrc/source/api/api_docs/methods/pytorch_post_training_quantization.rst index e3ddfb201..46feca7ba 100644 --- a/docsrc/source/api/api_docs/methods/pytorch_post_training_quantization.rst +++ b/docsrc/source/api/api_docs/methods/pytorch_post_training_quantization.rst @@ -7,4 +7,4 @@ Pytorch Post Training Quantization ===================================== -.. autofunction:: model_compression_toolkit.pytorch_post_training_quantization +.. autofunction:: model_compression_toolkit.ptq.pytorch_post_training_quantization diff --git a/docsrc/source/api/api_docs/methods/pytorch_post_training_quantization_mixed_precision.rst b/docsrc/source/api/api_docs/methods/pytorch_post_training_quantization_mixed_precision.rst deleted file mode 100644 index 72d318522..000000000 --- a/docsrc/source/api/api_docs/methods/pytorch_post_training_quantization_mixed_precision.rst +++ /dev/null @@ -1,10 +0,0 @@ -:orphan: - -.. _ug-pytorch_post_training_quantization_mixed_precision: - - -================================================================== -PyTorch Post Training Mixed Precision Quantization -================================================================== - -.. autofunction:: model_compression_toolkit.pytorch_post_training_quantization_mixed_precision diff --git a/docsrc/source/api/api_docs/methods/pytorch_pruning_experimental.rst b/docsrc/source/api/api_docs/methods/pytorch_pruning_experimental.rst new file mode 100644 index 000000000..4f050cfe4 --- /dev/null +++ b/docsrc/source/api/api_docs/methods/pytorch_pruning_experimental.rst @@ -0,0 +1,15 @@ +:orphan: + +.. _ug-pytorch_pruning_experimental: + + +================================================ +Pytorch Structured Pruning +================================================ + +.. autofunction:: model_compression_toolkit.pruning.pytorch_pruning_experimental + + + + + diff --git a/docsrc/source/api/experimental_api_docs/methods/pytorch_quantization_aware_training_finalize.rst b/docsrc/source/api/api_docs/methods/pytorch_quantization_aware_training_finalize_experimental.rst similarity index 68% rename from docsrc/source/api/experimental_api_docs/methods/pytorch_quantization_aware_training_finalize.rst rename to docsrc/source/api/api_docs/methods/pytorch_quantization_aware_training_finalize_experimental.rst index b1e5dcd8c..e391be8d7 100644 --- a/docsrc/source/api/experimental_api_docs/methods/pytorch_quantization_aware_training_finalize.rst +++ b/docsrc/source/api/api_docs/methods/pytorch_quantization_aware_training_finalize_experimental.rst @@ -1,10 +1,10 @@ :orphan: -.. _ug-pytorch_quantization_aware_training_finalize: +.. _ug-pytorch_quantization_aware_training_finalize_experimental: =================================================== PyTorch Quantization Aware Training Model Finalize =================================================== -.. autofunction:: model_compression_toolkit.qat.pytorch_quantization_aware_training_finalize +.. autofunction:: model_compression_toolkit.qat.pytorch_quantization_aware_training_finalize_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/pytorch_quantization_aware_training_init.rst b/docsrc/source/api/api_docs/methods/pytorch_quantization_aware_training_init_experimental.rst similarity index 68% rename from docsrc/source/api/experimental_api_docs/methods/pytorch_quantization_aware_training_init.rst rename to docsrc/source/api/api_docs/methods/pytorch_quantization_aware_training_init_experimental.rst index 899379d6b..4e14c7478 100644 --- a/docsrc/source/api/experimental_api_docs/methods/pytorch_quantization_aware_training_init.rst +++ b/docsrc/source/api/api_docs/methods/pytorch_quantization_aware_training_init_experimental.rst @@ -1,10 +1,10 @@ :orphan: -.. _ug-pytorch_quantization_aware_training_init: +.. _ug-pytorch_quantization_aware_training_init_experimental: ================================================ PyTorch Quantization Aware Training Model Init ================================================ -.. autofunction:: model_compression_toolkit.qat.pytorch_quantization_aware_training_init +.. autofunction:: model_compression_toolkit.qat.pytorch_quantization_aware_training_init_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/set_logger_path.rst b/docsrc/source/api/api_docs/methods/set_logger_path.rst similarity index 86% rename from docsrc/source/api/experimental_api_docs/methods/set_logger_path.rst rename to docsrc/source/api/api_docs/methods/set_logger_path.rst index d449869aa..88130b98a 100644 --- a/docsrc/source/api/experimental_api_docs/methods/set_logger_path.rst +++ b/docsrc/source/api/api_docs/methods/set_logger_path.rst @@ -1,6 +1,6 @@ :orphan: -.. _ug-set_logger_path: +.. _ug-set_log_folder: ================================= Enable a Logger diff --git a/docsrc/source/api/experimental_api_docs/modules/core_config.rst b/docsrc/source/api/api_docs/modules/core_config.rst similarity index 61% rename from docsrc/source/api/experimental_api_docs/modules/core_config.rst rename to docsrc/source/api/api_docs/modules/core_config.rst index 5601ffabd..8b7a85e26 100644 --- a/docsrc/source/api/experimental_api_docs/modules/core_config.rst +++ b/docsrc/source/api/api_docs/modules/core_config.rst @@ -1,12 +1,8 @@ :orphan: -.. _ug-core_config: +.. _ug-CoreConfig: -================================= -core_config Module -================================= - CoreConfig ========================== diff --git a/docsrc/source/api/experimental_api_docs/modules/debug_config.rst b/docsrc/source/api/api_docs/modules/debug_config.rst similarity index 79% rename from docsrc/source/api/experimental_api_docs/modules/debug_config.rst rename to docsrc/source/api/api_docs/modules/debug_config.rst index f1da2735b..16654f433 100644 --- a/docsrc/source/api/experimental_api_docs/modules/debug_config.rst +++ b/docsrc/source/api/api_docs/modules/debug_config.rst @@ -12,6 +12,6 @@ DebugConfig ========================== **Class to configure params for debugging purposes:** -.. autoclass:: model_compression_toolkit.DebugConfig +.. autoclass:: model_compression_toolkit.core.DebugConfig diff --git a/docsrc/source/api/api_docs/modules/exporter.rst b/docsrc/source/api/api_docs/modules/exporter.rst new file mode 100644 index 000000000..259fc7a27 --- /dev/null +++ b/docsrc/source/api/api_docs/modules/exporter.rst @@ -0,0 +1,199 @@ +:orphan: + +.. _ug-exporter: + + +================================= +exporter Module +================================= + +Allows to export a quantized model in different serialization formats and quantization formats. +For more details about the export formats and options, please refer to the project's GitHub `README file `_. +If you have any questions or issues, please open an issue in this GitHub repository. + + +QuantizationFormat +========================== + +.. autoclass:: model_compression_toolkit.exporter.QuantizationFormat + + +KerasExportSerializationFormat +================================ +Select the serialization format for exporting a quantized Keras model. + +.. autoclass:: model_compression_toolkit.exporter.KerasExportSerializationFormat + + +keras_export_model +========================== +Allows to export a Keras model that was quantized via MCT. + +.. autoclass:: model_compression_toolkit.exporter.keras_export_model + + +Keras Tutorial +========================== +To export a TensorFlow model as a quantized model, it is necessary to first apply quantization +to the model using MCT: + +.. code-block:: python + + import numpy as np + from keras.applications import ResNet50 + import model_compression_toolkit as mct + + # Create a model + float_model = ResNet50() + # Quantize the model. + # Notice that here the representative dataset is random for demonstration only. + quantized_exportable_model, _ = mct.ptq.keras_post_training_quantization(float_model, + representative_data_gen=lambda: [np.random.random((1, 224, 224, 3))]) + + + +++++++++++++++++++++++++++++ +keras serialization format +++++++++++++++++++++++++++++ +The model will be exported as a tensorflow `.keras` model where weights and activations are quantized but represented using a float32 dtype. +Two optional quantization formats are available: MCTQ and FAKELY_QUANT. + +++++ +MCTQ +++++ + +By default, `mct.exporter.keras_export_model` will export the quantized Keras model to +a .keras model with custom quantizers from mct_quantizers module. + +.. code-block:: python + + import tempfile + + # Path of exported model + _, keras_file_path = tempfile.mkstemp('.keras') + + # Export a keras model with mctq custom quantizers. + mct.exporter.keras_export_model(model=quantized_exportable_model, + save_model_path=keras_file_path) + +Notice that the model has the same size as the quantized exportable model as weights data types are float. + + + +PytorchExportSerializationFormat +================================== +Select the serialization format for exporting a quantized Pytorch model. + +.. autoclass:: model_compression_toolkit.exporter.PytorchExportSerializationFormat + + + + + +pytorch_export_model +========================== +Allows to export a Pytorch model that was quantized via MCT. + +.. autoclass:: model_compression_toolkit.exporter.pytorch_export_model + + + +Pytorch Tutorial +========================== + +In order to export your quantized model to ONNX format, and use it for inference, some additional packages are needed. Notice, this is needed only for models exported to ONNX format, so this part can be skipped if this is not planned: + +.. code-block:: shell + + ! pip install -q onnx onnxruntime onnxruntime-extensions + +Now, let's start the export demonstration by quantizing the model using MCT: + +.. code-block:: python + + import model_compression_toolkit as mct + import numpy as np + import torch + from torchvision.models.mobilenetv2 import mobilenet_v2 + + # Create a model + float_model = mobilenet_v2() + + + # Notice that here the representative dataset is random for demonstration only. + def representative_data_gen(): + yield [np.random.random((1, 3, 224, 224))] + + + quantized_exportable_model, _ = mct.ptq.pytorch_post_training_quantization(float_model, representative_data_gen=representative_data_gen) + + + ++++++++++ +ONNX ++++++++++ + +The model will be exported in ONNX format where weights and activations are represented as float. Notice that `onnx` should be installed in order to export the model to an ONNX model. + +There are two optional formats to choose: MCTQ or FAKELY_QUANT. + ++++++++++++++++++++++++++++ +MCTQ Quantization Format ++++++++++++++++++++++++++++ + +By default, `mct.exporter.pytorch_export_model` will export the quantized pytorch model to +an ONNX model with custom quantizers from mct_quantizers module. + + +.. code-block:: python + + # Path of exported model + onnx_file_path = 'model_format_onnx_mctq.onnx' + + # Export ONNX model with mctq quantizers. + mct.exporter.pytorch_export_model(model=quantized_exportable_model, + save_model_path=onnx_file_path, + repr_dataset=representative_data_gen) + +Notice that the model has the same size as the quantized exportable model as weights data types are float. + ++++++++++++++++++++++++++++ +ONNX opset version ++++++++++++++++++++++++++++ + +By default, the used ONNX opset version is 15, but this can be changed using `onnx_opset_version`: + +.. code-block:: python + + # Export ONNX model with mctq quantizers. + mct.exporter.pytorch_export_model(model=quantized_exportable_model, + save_model_path=onnx_file_path, + repr_dataset=representative_data_gen, + onnx_opset_version=16) + +| + +++++++++++++++++++++++++++++++++++++ +Use exported model for inference +++++++++++++++++++++++++++++++++++++ + +To load and infer using the exported model, which was exported to an ONNX file in MCTQ format, we will use `mct_quantizers` method `get_ort_session_options` during onnxruntime session creation. **Notice**, inference on models that are exported in this format are slowly and suffers from longer latency. However, inference of these models on IMX500 will not suffer from this issue. + +.. code-block:: python + + import mct_quantizers as mctq + import onnxruntime as ort + + sess = ort.InferenceSession(onnx_file_path, + mctq.get_ort_session_options(), + providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) + + _input_data = next(representative_data_gen())[0].astype(np.float32) + _model_output_name = sess.get_outputs()[0].name + _model_input_name = sess.get_inputs()[0].name + + # Run inference + predictions = sess.run([_model_output_name], {_model_input_name: _input_data}) + +| + diff --git a/docsrc/source/api/experimental_api_docs/modules/layer_filters.rst b/docsrc/source/api/api_docs/modules/layer_filters.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/modules/layer_filters.rst rename to docsrc/source/api/api_docs/modules/layer_filters.rst diff --git a/docsrc/source/api/api_docs/modules/mixed_precision_quantization_config.rst b/docsrc/source/api/api_docs/modules/mixed_precision_quantization_config.rst deleted file mode 100644 index e48b66381..000000000 --- a/docsrc/source/api/api_docs/modules/mixed_precision_quantization_config.rst +++ /dev/null @@ -1,27 +0,0 @@ -:orphan: - -.. _ug-mixed_precision_quantization_config: - - -=========================================== -mixed_precision_quantization_config Module -=========================================== - - -KPI -================================ -**Object to configure resources to use when searching for a mixed-precision configuration for a model:** - -.. autoclass:: model_compression_toolkit.KPI - :noindex: - - -| - - -MixedPrecisionQuantizationConfig -================================= -**Class to configure the quantization process of the model when quantizing in mixed-precision:** - -.. autoclass:: model_compression_toolkit.MixedPrecisionQuantizationConfig - diff --git a/docsrc/source/api/api_docs/modules/network_editor.rst b/docsrc/source/api/api_docs/modules/network_editor.rst new file mode 100644 index 000000000..8df62a390 --- /dev/null +++ b/docsrc/source/api/api_docs/modules/network_editor.rst @@ -0,0 +1,62 @@ +:orphan: + +.. _ug-network_editor: + + +================================= +network_editor Module +================================= + +**The model can be edited by a list of EditRules to apply on nodes in a graph that represents the model during the model quantization. Each EditRule is a tuple of a filter and an action, where we apply the action on each node the filter matches** + +EditRule +========== +.. autoclass:: model_compression_toolkit.core.network_editor.EditRule + +Filters +========== + +.. autoclass:: model_compression_toolkit.core.network_editor.NodeTypeFilter + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.NodeNameFilter + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.NodeNameScopeFilter + + +Actions +========== + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeFinalWeightsQuantConfigAttr + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeCandidatesWeightsQuantConfigAttr + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeFinalActivationQuantConfigAttr + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeCandidatesActivationQuantConfigAttr + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeQuantizationParamFunction + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeFinalWeightsQuantizationMethod + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeCandidatesWeightsQuantizationMethod + +| + +.. autoclass:: model_compression_toolkit.core.network_editor.ChangeCandidatesActivationQuantizationMethod + diff --git a/docsrc/source/api/experimental_api_docs/modules/qat_config.rst b/docsrc/source/api/api_docs/modules/qat_config.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/modules/qat_config.rst rename to docsrc/source/api/api_docs/modules/qat_config.rst diff --git a/docsrc/source/api/experimental_api_docs/modules/target_platform.rst b/docsrc/source/api/api_docs/modules/target_platform.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/modules/target_platform.rst rename to docsrc/source/api/api_docs/modules/target_platform.rst diff --git a/docsrc/source/api/experimental_api_docs/modules/trainable_infrastructure.rst b/docsrc/source/api/api_docs/modules/trainable_infrastructure.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/modules/trainable_infrastructure.rst rename to docsrc/source/api/api_docs/modules/trainable_infrastructure.rst diff --git a/docsrc/source/api/experimental_api_docs/notes/tpc_note.rst b/docsrc/source/api/api_docs/notes/tpc_note.rst similarity index 100% rename from docsrc/source/api/experimental_api_docs/notes/tpc_note.rst rename to docsrc/source/api/api_docs/notes/tpc_note.rst diff --git a/docsrc/source/api/experimental_api_docs/index.rst b/docsrc/source/api/experimental_api_docs/index.rst deleted file mode 100644 index 12e023554..000000000 --- a/docsrc/source/api/experimental_api_docs/index.rst +++ /dev/null @@ -1,70 +0,0 @@ -:orphan: - -.. _ug-experimental_api_docs: - - -========= -API Docs -========= - -**Init module for MCT API.** - -.. code-block:: python - - import model_compression_toolkit as mct - -| - -.. include:: ./notes/experimental_api_note.rst - -| - - -Functions -========= -- :ref:`pytorch_post_training_quantization_experimental`: A function to use for post training quantization of PyTorch models (experimental). -- :ref:`keras_post_training_quantization_experimental`: A function to use for post training quantization of Keras models (experimental). -- :ref:`keras_gradient_post_training_quantization_experimental`: A function to use for gradient-based post training quantization of Keras models (experimental). -- :ref:`pytorch_gradient_post_training_quantization_experimental`: A function to use for gradient-based post training quantization of Pytorch models (experimental). -- :ref:`keras_quantization_aware_training_init`: A function to use for preparing a model for Quantization Aware Training (Experimental) -- :ref:`keras_quantization_aware_training_finalize`: A function to finalize a model after Quantization Aware Training to a model without QuantizeWrappers(Experimental) -- :ref:`get_keras_gptq_config`: A function to create a GradientPTQConfig instance to use for Keras models when using GPTQ (experimental). -- :ref:`get_pytorch_gptq_config`: A function to create a GradientPTQConfig instance to use for Pytorch models when using GPTQ (experimental). -- :ref:`get_target_platform_capabilities`: A function to get a target platform model for Tensorflow and Pytorch. -- :ref:`keras_kpi_data_experimental`: A function to compute KPI data that can be used to calculate the desired target KPI for mixed-precision quantization of Keras models (experimental). -- :ref:`pytorch_kpi_data_experimental`: A function to compute KPI data that can be used to calculate the desired target KPI for mixed-precision quantization of PyTorch models (experimental). -- :ref:`keras_load_quantized_model`: A function to load a quantized keras model (experimental). -- :ref:`tensorflow_data_generation_experimental`: A function to generate data for a Keras model (experimental). -- :ref:`get_keras_data_generation_config`: A function to generate a DataGenerationConfig for Tensorflow data generation(experimental). -- :ref:`pytorch_data_generation_experimental`: A function to generate data for a Pytorch model (experimental). -- :ref:`get_pytorch_data_generation_config`: A function to load a DataGenerationConfig for Pytorch data generation (experimental). -- :ref:`keras_pruning_experimental`: A function to apply structured pruning for Keras models (experimental). - - -Modules -========= -- :ref:`core_config`: Module to contain configurations of the optimization process. -- :ref:`quantization_config`: Module to configure the quantization process. -- :ref:`mixed_precision_quantization_config`: Module to configure the quantization process when using mixed-precision PTQ. -- :ref:`debug_config`: Module to configure options for debugging the optimization process. -- :ref:`target_platform`: Module to create and model hardware-related settings to optimize the model according to, by the hardware the optimized model will use during inference. -- :ref:`qat_config`: Module to create quantization configuration for Quantization-aware Training. -- :ref:`exporter`: Module that enables to export a quantized model in different serialization formats. -- :ref:`trainable_infrastructure`: Module that contains quantization abstraction and quantizers for hardware-oriented model optimization tools. - -Classes -========= -- :ref:`GradientPTQConfig`: Class to configure GradientPTQ options for gradient based post training quantization. -- :ref:`FolderImageLoader`: Class to use an images directory as a representative dataset. -- :ref:`FrameworkInfo`: Class to wrap framework information to be used by MCT when optimizing models. - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` - -.. note:: This documentation is auto-generated using Sphinx - diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_data_generation_experimental.rst b/docsrc/source/api/experimental_api_docs/methods/keras_data_generation_experimental.rst deleted file mode 100644 index 20865d616..000000000 --- a/docsrc/source/api/experimental_api_docs/methods/keras_data_generation_experimental.rst +++ /dev/null @@ -1,10 +0,0 @@ -:orphan: - -.. _ug-tensorflow_data_generation_experimental: - - -===================================== -Keras Data Generation -===================================== - -.. autofunction:: model_compression_toolkit.data_generation.tensorflow_data_generation_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_kpi_data_experimental.rst b/docsrc/source/api/experimental_api_docs/methods/keras_kpi_data_experimental.rst deleted file mode 100644 index 608808bca..000000000 --- a/docsrc/source/api/experimental_api_docs/methods/keras_kpi_data_experimental.rst +++ /dev/null @@ -1,10 +0,0 @@ -:orphan: - -.. _ug-keras_kpi_data_experimental: - - -======================================= -Get KPI information for Keras Models -======================================= - -.. autofunction:: model_compression_toolkit.core.keras_kpi_data_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_post_training_quantization_experimental.rst b/docsrc/source/api/experimental_api_docs/methods/keras_post_training_quantization_experimental.rst deleted file mode 100644 index ca1e912ee..000000000 --- a/docsrc/source/api/experimental_api_docs/methods/keras_post_training_quantization_experimental.rst +++ /dev/null @@ -1,10 +0,0 @@ -:orphan: - -.. _ug-keras_post_training_quantization_experimental: - - -================================= -Keras Post Training Quantization -================================= - -.. autofunction:: model_compression_toolkit.ptq.keras_post_training_quantization_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/keras_pruning_experimental.rst b/docsrc/source/api/experimental_api_docs/methods/keras_pruning_experimental.rst deleted file mode 100644 index 787bb2452..000000000 --- a/docsrc/source/api/experimental_api_docs/methods/keras_pruning_experimental.rst +++ /dev/null @@ -1,25 +0,0 @@ -:orphan: - -.. _ug-keras_pruning_experimental: - - -================================================ -Keras Structured Pruning -================================================ - -.. autofunction:: model_compression_toolkit.pruning.keras_pruning_experimental - -================================================ -Pruning Configuration -================================================ - -.. autofunction:: model_compression_toolkit.pruning.PruningConfig - - - -================================================ -Pruning Information -================================================ - -.. autofunction:: model_compression_toolkit.pruning.PruningInfo - diff --git a/docsrc/source/api/experimental_api_docs/methods/pytorch_kpi_data_experimental.rst b/docsrc/source/api/experimental_api_docs/methods/pytorch_kpi_data_experimental.rst deleted file mode 100644 index 155a7e1c0..000000000 --- a/docsrc/source/api/experimental_api_docs/methods/pytorch_kpi_data_experimental.rst +++ /dev/null @@ -1,10 +0,0 @@ -:orphan: - -.. _ug-pytorch_kpi_data_experimental: - - -======================================= -Get KPI information for PyTorch Models -======================================= - -.. autofunction:: model_compression_toolkit.core.pytorch_kpi_data_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/pytorch_post_training_quantization_experimental.rst b/docsrc/source/api/experimental_api_docs/methods/pytorch_post_training_quantization_experimental.rst deleted file mode 100644 index 48fcdfa6f..000000000 --- a/docsrc/source/api/experimental_api_docs/methods/pytorch_post_training_quantization_experimental.rst +++ /dev/null @@ -1,10 +0,0 @@ -:orphan: - -.. _ug-pytorch_post_training_quantization_experimental: - - -===================================== -Pytorch Post Training Quantization -===================================== - -.. autofunction:: model_compression_toolkit.ptq.pytorch_post_training_quantization_experimental diff --git a/docsrc/source/api/experimental_api_docs/methods/pytorch_pruning_experimental.rst b/docsrc/source/api/experimental_api_docs/methods/pytorch_pruning_experimental.rst deleted file mode 100644 index f52e130f5..000000000 --- a/docsrc/source/api/experimental_api_docs/methods/pytorch_pruning_experimental.rst +++ /dev/null @@ -1,25 +0,0 @@ -:orphan: - -.. _ug-pytorch_pruning_experimental: - - -================================================ -Pytorch Structured Pruning -================================================ - -.. autofunction:: model_compression_toolkit.pruning.pytorch_pruning_experimental - -================================================ -Pruning Configuration -================================================ - -.. autofunction:: model_compression_toolkit.pruning.PruningConfig - - - -================================================ -Pruning Information -================================================ - -.. autofunction:: model_compression_toolkit.pruning.PruningInfo - diff --git a/docsrc/source/api/experimental_api_docs/modules/exporter.rst b/docsrc/source/api/experimental_api_docs/modules/exporter.rst deleted file mode 100644 index 987cbe483..000000000 --- a/docsrc/source/api/experimental_api_docs/modules/exporter.rst +++ /dev/null @@ -1,41 +0,0 @@ -:orphan: - -.. _ug-exporter: - - -================================= -exporter Module -================================= - -Allows to export a quantized model in different serialization formats and quantization formats. -For more details about the export formats and options, please refer to the project's GitHub `README file `_. -Note that this feature is experimental and subject to future changes. If you have any questions or issues, please open an issue in this GitHub repository. - - -KerasExportSerializationFormat -================================ -Select the serialization format for exporting a quantized Keras model. - -.. autoclass:: model_compression_toolkit.exporter.KerasExportSerializationFormat - - -PytorchExportSerializationFormat -================================== -Select the serialization format for exporting a quantized Pytorch model. - -.. autoclass:: model_compression_toolkit.exporter.PytorchExportSerializationFormat - - -keras_export_model -========================== -Allows to export a Keras model that was quantized via MCT. - -.. autoclass:: model_compression_toolkit.exporter.keras_export_model - - -pytorch_export_model -========================== -Allows to export a Pytorch model that was quantized via MCT. - -.. autoclass:: model_compression_toolkit.exporter.pytorch_export_model - diff --git a/docsrc/source/api/experimental_api_docs/modules/mixed_precision_quantization_config.rst b/docsrc/source/api/experimental_api_docs/modules/mixed_precision_quantization_config.rst deleted file mode 100644 index 996479716..000000000 --- a/docsrc/source/api/experimental_api_docs/modules/mixed_precision_quantization_config.rst +++ /dev/null @@ -1,26 +0,0 @@ -:orphan: - -.. _ug-mixed_precision_quantization_config_v2: - - -=========================================================== -mixed_precision_quantization_config Module -=========================================================== - - -KPI -================================ -**Object to configure resources to use when searching for a mixed-precision configuration for a model:** - -.. autoclass:: model_compression_toolkit.KPI - - -| - - -MixedPrecisionQuantizationConfigV2 -=================================== -**Class to configure the quantization process of the model when quantizing in mixed-precision:** - -.. autoclass:: model_compression_toolkit.core.MixedPrecisionQuantizationConfigV2 - diff --git a/docsrc/source/api/experimental_api_docs/modules/network_editor.rst b/docsrc/source/api/experimental_api_docs/modules/network_editor.rst deleted file mode 100644 index a97594e39..000000000 --- a/docsrc/source/api/experimental_api_docs/modules/network_editor.rst +++ /dev/null @@ -1,62 +0,0 @@ -:orphan: - -.. _ug-network_editor: - - -================================= -network_editor Module -================================= - -**The model can be edited by a list of EditRules to apply on nodes in a graph that represents the model during the model quantization. Each EditRule is a tuple of a filter and an action, where we apply the action on each node the filter matches** - -EditRule -========== -.. autoclass:: model_compression_toolkit.network_editor.EditRule - -Filters -========== - -.. autoclass:: model_compression_toolkit.network_editor.NodeTypeFilter - -| - -.. autoclass:: model_compression_toolkit.network_editor.NodeNameFilter - -| - -.. autoclass:: model_compression_toolkit.network_editor.NodeNameScopeFilter - - -Actions -========== - -.. autoclass:: model_compression_toolkit.network_editor.ChangeFinalWeightsQuantConfigAttr - -| - -.. autoclass:: model_compression_toolkit.network_editor.ChangeCandidatesWeightsQuantConfigAttr - -| - -.. autoclass:: model_compression_toolkit.network_editor.ChangeFinalActivationQuantConfigAttr - -| - -.. autoclass:: model_compression_toolkit.network_editor.ChangeCandidatesActivationQuantConfigAttr - -| - -.. autoclass:: model_compression_toolkit.network_editor.ChangeQuantizationParamFunction - -| - -.. autoclass:: model_compression_toolkit.network_editor.ChangeFinalWeightsQuantizationMethod - -| - -.. autoclass:: model_compression_toolkit.network_editor.ChangeCandidatesWeightsQuantizationMethod - -| - -.. autoclass:: model_compression_toolkit.network_editor.ChangeCandidatesActivationQuantizationMethod - diff --git a/docsrc/source/api/experimental_api_docs/modules/quantization_config.rst b/docsrc/source/api/experimental_api_docs/modules/quantization_config.rst deleted file mode 100644 index 68b9599eb..000000000 --- a/docsrc/source/api/experimental_api_docs/modules/quantization_config.rst +++ /dev/null @@ -1,25 +0,0 @@ -:orphan: - -.. _ug-quantization_config: - - -================================= -quantization_config Module -================================= - -QuantizationErrorMethod -========================== -**Enum to select a method for quantization parameters' selection:** - -.. autoclass:: model_compression_toolkit.QuantizationErrorMethod - - -| - - -QuantizationConfig -========================== -**Class to configure the quantization process of the model:** - -.. autoclass:: model_compression_toolkit.QuantizationConfig - diff --git a/docsrc/source/api/experimental_api_docs/notes/experimental_api_note.rst b/docsrc/source/api/experimental_api_docs/notes/experimental_api_note.rst deleted file mode 100644 index 4980a5344..000000000 --- a/docsrc/source/api/experimental_api_docs/notes/experimental_api_note.rst +++ /dev/null @@ -1,7 +0,0 @@ - -.. note:: - This is an experimental new API. The previous API is still available and can be used as before (please - visit :ref:`previous API documentation ` for more information). However, we recommend using the new API as the previous API is deprecated - and will be removed in the future. - - diff --git a/docsrc/source/guidelines/quickstart_keras.rst b/docsrc/source/guidelines/quickstart_keras.rst deleted file mode 100644 index 577ca6f4e..000000000 --- a/docsrc/source/guidelines/quickstart_keras.rst +++ /dev/null @@ -1,107 +0,0 @@ -:orphan: - -.. _ug-quickstart-keras: - -================================================= -MCT Quickstart Guideline for Keras models -================================================= - -Here is an example of a code that shows how to use MCT with Keras models. - - -Import MCT and MobileNetV1 from Keras applications: - -.. code-block:: python - - import model_compression_toolkit as mct - from tensorflow.keras.applications.mobilenet import MobileNet - -| - -Data preprocessing functions: - -.. code-block:: python - - import cv2 - import numpy as np - - MEAN = 127.5 - STD = 127.5 - RESIZE_SCALE = 256 / 224 - SIZE = 224 - - - def resize(x): - resize_side = max(RESIZE_SCALE * SIZE / x.shape[0], RESIZE_SCALE * SIZE / x.shape[1]) - height_tag = int(np.round(resize_side * x.shape[0])) - width_tag = int(np.round(resize_side * x.shape[1])) - resized_img = cv2.resize(x, (width_tag, height_tag)) - offset_height = int((height_tag - SIZE) / 2) - offset_width = int((width_tag - SIZE) / 2) - cropped_img = resized_img[offset_height:offset_height + SIZE, offset_width:offset_width + SIZE] - return cropped_img - - - def normalization(x): - return (x - MEAN) / STD - - -| - -Initialize data loader: - -.. code-block:: python - - # Set the batch size of the images at each calibration iteration. - batch_size = 50 - n_iter=10 - - # Set the path to the folder of images to load and use for the representative dataset. - # Notice that the folder have to contain at least one image. - folder = '/path/to/images/folder' - - # Create a representative data generator, which returns a list of images. - # The images can be preprocessed using a list of preprocessing functions. - from model_compression_toolkit.core import FolderImageLoader - image_data_loader = FolderImageLoader(folder, - preprocessing=[resize, normalization], - batch_size=batch_size) - - # Create a Callable representative dataset for calibration purposes. - # The function should be called without any arguments, and should return a list numpy arrays (array for each - # model's input). - # For example: A model has two input tensors - one with input shape of [32 X 32 X 3] and the second with - # an input shape of [224 X 224 X 3]. We calibrate the model using batches of 20 images. - # Calling representative_data_gen() should return a list - # of two numpy.ndarray objects where the arrays' shapes are [(20, 3, 32, 32), (20, 3, 224, 224)]. - def representative_data_gen() -> list: - for _ in range(n_iter): - yield [image_data_loader.sample()] - -| - -Get a TargetPlatformCapabilities: - -.. code-block:: python - - # Get a TargetPlatformModel object that models the hardware for the quantized model inference. - # The model determines the quantization methods to use during the MCT optimization process. - # Here, for example, we use the default target platform model that is attached to a Tensorflow - # layers representation. - target_platform_cap = mct.get_target_platform_capabilities('tensorflow', 'default') - -| - -Run Post Training Quantization: - -.. code-block:: python - - # Create a model and quantize it using the representative_data_gen as the calibration images. - # Set the number of calibration iterations to 10. - model = MobileNet() - - quantized_model, quantization_info = mct.ptq.keras_post_training_quantization_experimental(model, - representative_data_gen, - target_platform_capabilities=target_platform_cap) - -| diff --git a/docsrc/source/guidelines/quickstart_pytorch.rst b/docsrc/source/guidelines/quickstart_pytorch.rst deleted file mode 100644 index 6862071d8..000000000 --- a/docsrc/source/guidelines/quickstart_pytorch.rst +++ /dev/null @@ -1,103 +0,0 @@ -:orphan: - -.. _ug-quickstart-pytorch: - -================================================= -MCT Quickstart Guideline for Pytorch models -================================================= - -Here is an example of a code that shows how to use MCT with Pytorch models. - - -Import MCT and mobilenet_v2 from torchvision.models: - -.. code-block:: python - - from torchvision.models import mobilenet_v2 - import model_compression_toolkit as mct - -| - -Data preprocessing imports and functions: - -.. code-block:: python - - from PIL import Image - from torchvision import transforms - - def np_to_pil(img): - return Image.fromarray(img) - -| - -Initialize data loader: - -.. code-block:: python - - # Set the batch size of the images at each calibration iteration. - batch_size = 50 - - # Set the path to the folder of images to load and use for the representative dataset. - # Notice that the folder have to contain at least one image. - folder = '/path/to/images/folder' - - # Create a representative data generator, which returns a list of images. - # The images can be preprocessed using a list of preprocessing functions. - from model_compression_toolkit.core import FolderImageLoader - - image_data_loader = FolderImageLoader(folder, - preprocessing=[np_to_pil, - transforms.Compose([ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.ToTensor(), - transforms.Normalize(mean=[0.485, 0.456, 0.406], - std=[0.229, 0.224, 0.225]), - ]) - ], - batch_size=batch_size) - - # Create a Callable representative dataset for calibration purposes. - # The function should be called without any arguments, and should return a list numpy arrays (array for each - # model's input). - # For example: A model has two input tensors - one with input shape of [3 X 32 X 32] and the second with - # an input shape of [3 X 224 X 224]. We calibrate the model using batches of 50 images. - # Calling representative_data_gen() should return a list - # of two numpy.ndarray objects where the arrays' shapes are [(50, 3, 32, 32), (50, 3, 224, 224)]. - n_iter = 20 - def representative_data_gen() -> list: - for _ in range(n_iter): - yield [image_data_loader.sample()] - - -| - -Get a TargetPlatformCapabilities: - -.. code-block:: python - - # Get a TargetPlatformModel object that models the hardware for the quantized model inference. - # The model determines the quantization methods to use during the MCT optimization process. - # Here, for example, we use the default model that is attached to a Pytorch - # layers representation. - target_platform_cap = mct.get_target_platform_capabilities('pytorch', 'default') - -| - - -Run Post Training Quantization: - -.. code-block:: python - - # Create a model and quantize it using the representative_data_gen as the calibration images. - # Set the number of calibration iterations to 20. - model = mobilenet_v2(pretrained=True) - - # set core configuration with z threshold=16 (an algorithm for outlier removal) - core_config = mct.core.CoreConfig(quantization_config=QuantizationConfig(z_threshold=16) - - # run post training quantization on the model to get the quantized model output - quantized_model, quantization_info = mct.ptq.pytorch_post_training_quantization_experimental(model, - representative_data_gen, - core_config=core_config, - target_platform_capabilities=target_platform_cap) diff --git a/docsrc/source/guidelines/visualization.rst b/docsrc/source/guidelines/visualization.rst index 708994629..3061010dc 100644 --- a/docsrc/source/guidelines/visualization.rst +++ b/docsrc/source/guidelines/visualization.rst @@ -7,7 +7,7 @@ Visualization within TensorBoard ================================= One may log various graphs and data collected in different phases of the model quantization and display them within the Tensorboard UI. -To use it, all you have to do is to set a logger path. Setting a path is done by calling :ref:`set_log_folder`. +To use it, all you have to do is to set a logger path. Setting a path is done by calling :ref:`set_log_folder`. .. code-block:: python diff --git a/docsrc/source/index.rst b/docsrc/source/index.rst index bd91fe81f..15fce6e9a 100644 --- a/docsrc/source/index.rst +++ b/docsrc/source/index.rst @@ -11,9 +11,6 @@ Overview Model Compression Toolkit (MCT) is an open source project for neural networks optimization that enables users to compress and quantize models. This project enables researchers, developers and engineers an easily way to optimized and quantized state-of-the-art neural network. -Currently, MCT supports hardware-friendly post training quantization (HPTQ) with Tensorflow 2 [1]. - - MCT project is developed by researchers and engineers working in Sony Semiconductor Israel. Install @@ -41,7 +38,7 @@ A nightly version is also available (unstable): For using with Tensorflow please install the packages: `tensorflow `_ -`tensorflow-model-optimization `_ + For using with Pytorch please install the package: `torch `_ @@ -52,21 +49,21 @@ Supported Features Keras: -* :ref:`Post Training Quantization` [1] -* :ref:`Gradient based post training using knowledge distillation` -* :ref:`Mixed-precision post training quantization` -* :ref:`Init model for Quantization Aware Training` (Experimental) -* :ref:`Finalize model after Quantization Aware Training` (Experimental) -* :ref:`Structured Pruning` (Experimental) +* :ref:`Post Training Quantization` [1] +* :ref:`Gradient based post training using knowledge distillation` [2] +* :ref:`Init model for Quantization Aware Training` (Experimental) +* :ref:`Finalize model after Quantization Aware Training` (Experimental) +* :ref:`Structured pruning` (Experimental) +* :ref:`Data generation` (Experimental) Pytorch: -* :ref:`Post Training Quantization` [1] -* :ref:`Gradient based post training using knowledge distillation` -* :ref:`Mixed-precision post training quantization` -* :ref:`Init model for Quantization Aware Training` (Experimental) -* :ref:`Finalize model after Quantization Aware Training` (Experimental) - +* :ref:`Post Training Quantization` [1] +* :ref:`Gradient based post training using knowledge distillation` [2] +* :ref:`Init model for Quantization Aware Training` (Experimental) +* :ref:`Finalize model after Quantization Aware Training` (Experimental) +* :ref:`Structured pruning` (Experimental) +* :ref:`Data generation` (Experimental) Visualization: @@ -80,17 +77,9 @@ Visualization: Quickstart ==================================== -Take a look of how you can start using MCT in just a few minutes - -.. toctree:: - :titlesonly: - :maxdepth: 1 - - Quick start tutorial for Keras Post Training Quantization<../guidelines/quickstart_keras> - Quick start tutorial for Pytorch Post Training Quantization<../guidelines/quickstart_pytorch> - - +Take a look of how you can start using MCT in just a few minutes! +Visit our `notebooks `_ and `MCT quick start. `_ API Documentation @@ -101,7 +90,7 @@ Please visit the MCT API documentation here :titlesonly: :maxdepth: 1 - API Documentation<../api/experimental_api_docs/index> + API Documentation<../api/api_docs/index> Technical Constraints ========================= @@ -113,3 +102,4 @@ References [1] Habi, H.V., Peretz, R., Cohen, E., Dikstein, L., Dror, O., Diamant, I., Jennings, R.H. and Netzer, A., 2021. `HPTQ: Hardware-Friendly Post Training Quantization. arXiv preprint. `_ +[2] Gordon, O., Habi, H.V., and Netzer, A., 2023. `EPTQ: Enhanced Post-Training Quantization via Label-Free Hessian. arXiv preprint. `_ diff --git a/model_compression_toolkit/core/__init__.py b/model_compression_toolkit/core/__init__.py index 8ad4b28df..7b993610d 100644 --- a/model_compression_toolkit/core/__init__.py +++ b/model_compression_toolkit/core/__init__.py @@ -25,3 +25,5 @@ from model_compression_toolkit.core.common.mixed_precision.mixed_precision_quantization_config import MixedPrecisionQuantizationConfig from model_compression_toolkit.core.keras.kpi_data_facade import keras_kpi_data from model_compression_toolkit.core.pytorch.kpi_data_facade import pytorch_kpi_data +from model_compression_toolkit.core.common.mixed_precision.distance_weighting import MpDistanceWeighting + diff --git a/model_compression_toolkit/core/common/pruning/pruning_config.py b/model_compression_toolkit/core/common/pruning/pruning_config.py index 4476eb41f..b0dbf7b3c 100644 --- a/model_compression_toolkit/core/common/pruning/pruning_config.py +++ b/model_compression_toolkit/core/common/pruning/pruning_config.py @@ -20,14 +20,20 @@ class ImportanceMetric(Enum): """ - Enum for specifying the metric used to determine the importance of channels when pruning. + Enum for specifying the metric used to determine the importance of channels when pruning: + + LFH - Label-Free Hessian uses hessian info for measuring each channel's sensitivity. + """ LFH = 0 # Score based on the Hessian matrix w.r.t. layers weights, to determine channel importance without labels. class ChannelsFilteringStrategy(Enum): """ - Enum for specifying the strategy used for filtering (pruning) channels. + Enum for specifying the strategy used for filtering (pruning) channels: + + GREEDY - Prune the least important channel groups up to allowed resources in the KPI (for now, only weights_memory is considered). + """ GREEDY = 0 # Greedy strategy for pruning channels based on importance metrics. diff --git a/model_compression_toolkit/core/common/pruning/pruning_info.py b/model_compression_toolkit/core/common/pruning/pruning_info.py index 7226aa060..c5a9c7c6e 100644 --- a/model_compression_toolkit/core/common/pruning/pruning_info.py +++ b/model_compression_toolkit/core/common/pruning/pruning_info.py @@ -26,23 +26,16 @@ class PruningInfo: and importance scores for each layer. This class acts as a container for accessing pruning-related metadata. - Attributes: - pruning_masks (Dict[BaseNode, np.ndarray]): Stores the pruning masks for each layer. - A pruning mask is an array where each element indicates whether the corresponding - channel or neuron has been pruned (0) or kept (1). - importance_scores (Dict[BaseNode, np.ndarray]): Stores the importance scores for each layer. - Importance scores quantify the significance of each channel in the layer. """ def __init__(self, pruning_masks: Dict[BaseNode, np.ndarray], importance_scores: Dict[BaseNode, np.ndarray]): """ - Initializes the PruningInfo with pruning masks and importance scores. - Args: - pruning_masks (Dict[BaseNode, np.ndarray]): Pruning masks for each layer. - importance_scores (Dict[BaseNode, np.ndarray]): Importance scores for each layer. + pruning_masks (Dict[BaseNode, np.ndarray]): Stores the pruning masks for each layer. A pruning mask is an array where each element indicates whether the corresponding channel or neuron has been pruned (0) or kept (1). + importance_scores (Dict[BaseNode, np.ndarray]): Stores the importance scores for each layer. Importance scores quantify the significance of each channel in the layer. + """ self._pruning_masks = pruning_masks self._importance_scores = importance_scores diff --git a/model_compression_toolkit/data_generation/__init__.py b/model_compression_toolkit/data_generation/__init__.py index 4586d99f4..d7badc2af 100644 --- a/model_compression_toolkit/data_generation/__init__.py +++ b/model_compression_toolkit/data_generation/__init__.py @@ -12,7 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== + from model_compression_toolkit.constants import FOUND_TORCH, FOUND_TF +from model_compression_toolkit.data_generation.common.data_generation_config import DataGenerationConfig +from model_compression_toolkit.data_generation.common.enums import ImageGranularity, DataInitType, SchedulerType, BNLayerWeightingType, OutputLossType, BatchNormAlignemntLossType, ImagePipelineType, ImageNormalizationType if FOUND_TF: from model_compression_toolkit.data_generation.keras.keras_data_generation import ( diff --git a/model_compression_toolkit/data_generation/common/enums.py b/model_compression_toolkit/data_generation/common/enums.py index e7d181764..7f937975b 100644 --- a/model_compression_toolkit/data_generation/common/enums.py +++ b/model_compression_toolkit/data_generation/common/enums.py @@ -29,10 +29,14 @@ def get_values(cls): class ImageGranularity(EnumBaseClass): """ - An enum for choosing the image dependence granularity when generating images. - 0. ImageWise - 1. BatchWise - 2. AllImages + An enum for choosing the image dependence granularity when generating images: + + ImageWise + + BatchWise + + AllImages + """ ImageWise = 0 @@ -42,9 +46,12 @@ class ImageGranularity(EnumBaseClass): class DataInitType(EnumBaseClass): """ - An enum for choosing the image dependence granularity when generating images. - 0. Gaussian - 1. Diverse + An enum for choosing the image dependence granularity when generating images: + + Gaussian + + Diverse + """ Gaussian = 0 Diverse = 1 @@ -52,9 +59,14 @@ class DataInitType(EnumBaseClass): class ImagePipelineType(EnumBaseClass): """ - An enum for choosing the image pipeline type for image manipulation. - RANDOM_CROP_FLIP: Crop and flip the images. - IDENTITY: Do not apply any manipulation (identity transformation). + An enum for choosing the image pipeline type for image manipulation: + + RANDOM_CROP - Crop the images. + + RANDOM_CROP_FLIP - Crop and flip the images. + + IDENTITY - Do not apply any manipulation (identity transformation). + """ RANDOM_CROP = 'random_crop' RANDOM_CROP_FLIP = 'random_crop_flip' @@ -63,10 +75,14 @@ class ImagePipelineType(EnumBaseClass): class ImageNormalizationType(EnumBaseClass): """ - An enum for choosing the image normalization type. - TORCHVISION: Normalize the images using torchvision normalization. - KERAS_APPLICATIONS: Normalize the images using keras_applications imagenet normalization. - NO_NORMALIZATION: Do not apply any normalization. + An enum for choosing the image normalization type: + + TORCHVISION - Normalize the images using torchvision normalization. + + KERAS_APPLICATIONS - Normalize the images using keras_applications imagenet normalization. + + NO_NORMALIZATION - Do not apply any normalization. + """ TORCHVISION = 'torchvision' KERAS_APPLICATIONS = 'keras_applications' @@ -75,10 +91,14 @@ class ImageNormalizationType(EnumBaseClass): class BNLayerWeightingType(EnumBaseClass): """ - An enum for choosing the layer weighting type. - AVERAGE: Use the same weight per layer. - FIRST_LAYER_MULTIPLIER: Use a multiplier for the first layer, all other layers with the same weight. - GRAD: Use gradient-based layer weighting. + An enum for choosing the layer weighting type: + + AVERAGE - Use the same weight per layer. + + FIRST_LAYER_MULTIPLIER - Use a multiplier for the first layer, all other layers with the same weight. + + GRAD - Use gradient-based layer weighting. + """ AVERAGE = 'average' FIRST_LAYER_MULTIPLIER = 'first_layer_multiplier' @@ -87,18 +107,24 @@ class BNLayerWeightingType(EnumBaseClass): class BatchNormAlignemntLossType(EnumBaseClass): """ - An enum for choosing the BatchNorm alignment loss type. - L2_SQUARE: Use L2 square loss for BatchNorm alignment. + An enum for choosing the BatchNorm alignment loss type: + + L2_SQUARE - Use L2 square loss for BatchNorm alignment. + """ L2_SQUARE = 'l2_square' class OutputLossType(EnumBaseClass): """ - An enum for choosing the output loss type. - NONE: No output loss is applied. - MIN_MAX_DIFF: Use min-max difference as the output loss. - REGULARIZED_MIN_MAX_DIFF: Use regularized min-max difference as the output loss. + An enum for choosing the output loss type: + + NONE - No output loss is applied. + + MIN_MAX_DIFF - Use min-max difference as the output loss. + + REGULARIZED_MIN_MAX_DIFF - Use regularized min-max difference as the output loss. + """ NONE = 'none' MIN_MAX_DIFF = 'min_max_diff' @@ -107,9 +133,12 @@ class OutputLossType(EnumBaseClass): class SchedulerType(EnumBaseClass): """ - An enum for choosing the scheduler type for the optimizer. - REDUCE_ON_PLATEAU: Use the ReduceOnPlateau scheduler. - STEP: Use the Step scheduler. + An enum for choosing the scheduler type for the optimizer: + + REDUCE_ON_PLATEAU - Use the ReduceOnPlateau scheduler. + + STEP - Use the Step scheduler. + """ REDUCE_ON_PLATEAU = 'reduce_on_plateau' STEP = 'step' diff --git a/model_compression_toolkit/data_generation/keras/keras_data_generation.py b/model_compression_toolkit/data_generation/keras/keras_data_generation.py index a84abb51b..92c767ea5 100644 --- a/model_compression_toolkit/data_generation/keras/keras_data_generation.py +++ b/model_compression_toolkit/data_generation/keras/keras_data_generation.py @@ -131,7 +131,36 @@ def keras_data_generation_experimental( Returns: List[tf.Tensor]: Finalized list containing generated images. + + Examples: + + In this example, we'll walk through generating images using a simple Keras model and a data generation configuration. The process involves creating a model, setting up a data generation configuration, and finally generating images with specified parameters. + + Start by importing the Model Compression Toolkit (MCT), TensorFlow, and some layers from `tensorflow.keras`: + + >>> import model_compression_toolkit as mct + >>> from tensorflow.keras.models import Sequential + >>> from tensorflow.keras.layers import Conv2D, BatchNormalization, Flatten, Dense, Reshape + + Next, define a simple Keras model: + + >>> model = Sequential([Conv2D(2, 3, input_shape=(8,8,3)), BatchNormalization(), Flatten(), Dense(10)]) + + Configure the data generation process using `get_keras_data_generation_config`. This function allows customization of the data generation process. For simplicity, this example sets the number of iterations (`n_iter`) to 1 and the batch size (`data_gen_batch_size`) to 2. + + >>> config = mct.data_generation.get_keras_data_generation_config(n_iter=1, data_gen_batch_size=2) + + Finally, use the `keras_data_generation_experimental` function to generate images based on the model and data generation configuration. + Notice that this function is experimental and may change in future versions of MCT. + The `n_images` parameter specifies the number of images to generate, and `output_image_size` sets the size of the generated images. + + >>> generated_images = mct.data_generation.keras_data_generation_experimental(model=model, n_images=4, output_image_size=(8, 8), data_generation_config=config) + + The generated images can then be used for various purposes, such as data-free quantization. + + """ + Logger.warning(f"keras_data_generation_experimental is experimental " f"and is subject to future changes." f"If you encounter an issue, please open an issue in our GitHub " diff --git a/model_compression_toolkit/data_generation/pytorch/pytorch_data_generation.py b/model_compression_toolkit/data_generation/pytorch/pytorch_data_generation.py index bd5d625a7..047a61b1e 100644 --- a/model_compression_toolkit/data_generation/pytorch/pytorch_data_generation.py +++ b/model_compression_toolkit/data_generation/pytorch/pytorch_data_generation.py @@ -129,7 +129,7 @@ def get_pytorch_data_generation_config( def pytorch_data_generation_experimental( model: Module, n_images: int, - output_image_size: Tuple, + output_image_size: int, data_generation_config: DataGenerationConfig) -> List[Tensor]: """ Function to perform data generation using the provided model and data generation configuration. @@ -137,11 +137,38 @@ def pytorch_data_generation_experimental( Args: model (Module): PyTorch model to generate data for. n_images (int): Number of images to generate. - output_image_size (Tuple): Size of the output images. + output_image_size (int): The hight and width size of the output images. data_generation_config (DataGenerationConfig): Configuration for data generation. Returns: List[Tensor]: Finalized list containing generated images. + + Examples: + + In this example, we'll walk through generating images using a simple PyTorch model and a data generation configuration. The process involves creating a model, setting up a data generation configuration, and finally generating images with specified parameters. + + Start by importing the Model Compression Toolkit (MCT), PyTorch, and some modules from `torch.nn`: + + >>> import model_compression_toolkit as mct + >>> import torch.nn as nn + >>> from torch.nn import Conv2d, BatchNorm2d, Flatten, Linear + + Next, define a simple PyTorch model: + + >>> model = nn.Sequential(nn.Conv2d(3, 2, 3), nn.BatchNorm2d(2), nn.Flatten(), nn.Linear(2*6*6, 10)) + + Configure the data generation process using `get_pytorch_data_generation_config`. This function allows customization of the data generation process. For simplicity, this example sets the number of iterations (`n_iter`) to 1 and the batch size (`data_gen_batch_size`) to 2. + + >>> config = mct.data_generation.get_pytorch_data_generation_config(n_iter=1, data_gen_batch_size=2) + + Finally, use the `pytorch_data_generation_experimental` function to generate images based on the model and data generation configuration. + Notice that this function is experimental and may change in future versions of MCT. + The `n_images` parameter specifies the number of images to generate, and `output_image_size` sets the size of the generated images. + + >>> generated_images = mct.data_generation.pytorch_data_generation_experimental(model=model, n_images=4, output_image_size=8, data_generation_config=config) + + The generated images can then be used for various purposes, such as data-free quantization. + """ Logger.warning(f"pytorch_data_generation_experimental is experimental " diff --git a/model_compression_toolkit/exporter/model_exporter/fw_agonstic/quantization_format.py b/model_compression_toolkit/exporter/model_exporter/fw_agonstic/quantization_format.py index dc9c7e965..b6fedc2b2 100644 --- a/model_compression_toolkit/exporter/model_exporter/fw_agonstic/quantization_format.py +++ b/model_compression_toolkit/exporter/model_exporter/fw_agonstic/quantization_format.py @@ -16,6 +16,16 @@ class QuantizationFormat(Enum): + """ + Specify which quantization format to use for exporting a quantized model. + + FAKELY_QUANT - Weights and activations are quantized but represented using float data type. + + INT8 - Weights and activations are represented using 8-bit integer data type. + + MCTQ - Weights and activations are quantized using mct_quantizers custom quantizers. + + """ FAKELY_QUANT = 0 INT8 = 1 MCTQ = 2 diff --git a/model_compression_toolkit/exporter/model_exporter/keras/keras_export_facade.py b/model_compression_toolkit/exporter/model_exporter/keras/keras_export_facade.py index 9995ee356..44c9b52b2 100644 --- a/model_compression_toolkit/exporter/model_exporter/keras/keras_export_facade.py +++ b/model_compression_toolkit/exporter/model_exporter/keras/keras_export_facade.py @@ -42,21 +42,17 @@ def keras_export_model(model: keras.models.Model, serialization_format: KerasExportSerializationFormat = KerasExportSerializationFormat.KERAS, quantization_format : QuantizationFormat = QuantizationFormat.MCTQ) -> Dict[str, type]: """ - Export a Keras quantized model to a h5 or tflite model. + Export a Keras quantized model to a .keras or .tflite format model (according to serialization_format). The model will be saved to the path in save_model_path. - keras_export_model supports the combination of QuantizationFormat.FAKELY_QUANT (where weights - and activations are float fakely-quantized values) and KerasExportSerializationFormat.KERAS_H5 (where the model - will be saved to h5 model) or the combination of KerasExportSerializationFormat.TFLITE (where the model will be - saved to tflite model) with QuantizationFormat.FAKELY_QUANT or QuantizationFormat.INT8 (where weights and - activations are represented using 8bits integers). + Models that are exported to .keras format can use quantization_format of QuantizationFormat.MCTQ or QuantizationFormat.FAKELY_QUANT. + Models that are exported to .tflite format can use quantization_format of QuantizationFormat.INT8 or QuantizationFormat.FAKELY_QUANT. Args: model: Model to export. save_model_path: Path to save the model. is_layer_exportable_fn: Callable to check whether a layer can be exported or not. - serialization_format: Format to export the model according to (by default - KerasExportSerializationFormat.KERAS_H5). - quantization_format: Format of how quantizers are exported (fakely-quant, int8, MCTQ quantizers). + serialization_format: Format to export the model according to (KerasExportSerializationFormat.KERAS, by default). + quantization_format: Format of how quantizers are exported (MCTQ quantizers, by default). Returns: Custom objects dictionary needed to load the model. diff --git a/model_compression_toolkit/gptq/common/gptq_config.py b/model_compression_toolkit/gptq/common/gptq_config.py index b36e1164c..071201f4e 100644 --- a/model_compression_toolkit/gptq/common/gptq_config.py +++ b/model_compression_toolkit/gptq/common/gptq_config.py @@ -19,9 +19,12 @@ class RoundingType(Enum): """ - An enum for choosing the GPTQ rounding methods - 0. STRAIGHT-THROUGH ESTIMATOR - 1. SoftQuantizer + An enum for choosing the GPTQ rounding methods: + + STE - STRAIGHT-THROUGH ESTIMATOR + + SoftQuantizer - SoftQuantizer + """ STE = 0 SoftQuantizer = 1 diff --git a/model_compression_toolkit/pruning/keras/pruning_facade.py b/model_compression_toolkit/pruning/keras/pruning_facade.py index 953426761..bdc7aebec 100644 --- a/model_compression_toolkit/pruning/keras/pruning_facade.py +++ b/model_compression_toolkit/pruning/keras/pruning_facade.py @@ -40,8 +40,7 @@ def keras_pruning_experimental(model: Model, target_kpi: KPI, representative_data_gen: Callable, pruning_config: PruningConfig = PruningConfig(), - target_platform_capabilities: TargetPlatformCapabilities = DEFAULT_KERAS_TPC) -> \ - Tuple[Model, PruningInfo]: + target_platform_capabilities: TargetPlatformCapabilities = DEFAULT_KERAS_TPC) -> Tuple[Model, PruningInfo]: """ Perform structured pruning on a Keras model to meet a specified target KPI. This function prunes the provided model according to the target KPI by grouping and pruning @@ -59,12 +58,14 @@ def keras_pruning_experimental(model: Model, target_kpi (KPI): The target Key Performance Indicators to be achieved through pruning. representative_data_gen (Callable): A function to generate representative data for pruning analysis. pruning_config (PruningConfig): Configuration settings for the pruning process. Defaults to standard config. - target_platform_capabilities (TargetPlatformCapabilities): Platform-specific constraints and capabilities. - Defaults to DEFAULT_KERAS_TPC. + target_platform_capabilities (TargetPlatformCapabilities): Platform-specific constraints and capabilities. Defaults to DEFAULT_KERAS_TPC. Returns: Tuple[Model, PruningInfo]: A tuple containing the pruned Keras model and associated pruning information. + Note: + The pruned model should be fine-tuned or retrained to recover or improve its performance post-pruning. + Examples: Import MCT: diff --git a/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py b/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py index 9fdc38dc7..6a1809f12 100644 --- a/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py +++ b/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py @@ -35,9 +35,9 @@ def __init__(self, Args: op_set_name (str): Name of OperatorsSet to associate with layers. layers (List[Any]): List of layers/FilterLayerParams to associate with OperatorsSet. - attr_mapping (dict): A mapping between a general attribute name to a DefaultDict that maps a layer -+ type to the layer's framework name of this attribute (the dictionary type is not specified to -+ handle circular dependency). + attr_mapping (dict): A mapping between a general attribute name to a DefaultDict that maps a layer type + to the layer's framework name of this attribute (the dictionary type is not specified to handle circular + dependency). """ self.layers = layers self.attr_mapping = attr_mapping