Skip to content

Commit

Permalink
Remove experimental from keras kpi data name (#955)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: reuvenp <reuvenp@altair-semi.com>
  • Loading branch information
reuvenperetz and reuvenp authored Feb 28, 2024
1 parent fbd858b commit ce5b747
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 86 deletions.
2 changes: 1 addition & 1 deletion model_compression_toolkit/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
from model_compression_toolkit.core.common.quantization.core_config import CoreConfig
from model_compression_toolkit.core.common.mixed_precision.kpi_tools.kpi import KPI
from model_compression_toolkit.core.common.mixed_precision.mixed_precision_quantization_config import MixedPrecisionQuantizationConfig, MixedPrecisionQuantizationConfigV2
from model_compression_toolkit.core.keras.kpi_data_facade import keras_kpi_data, keras_kpi_data_experimental
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
74 changes: 2 additions & 72 deletions model_compression_toolkit/core/keras/kpi_data_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@

from typing import Callable

from model_compression_toolkit.core import MixedPrecisionQuantizationConfig, CoreConfig, MixedPrecisionQuantizationConfigV2
from model_compression_toolkit.core import CoreConfig, MixedPrecisionQuantizationConfigV2
from model_compression_toolkit.core.common.mixed_precision.kpi_tools.kpi import KPI
from model_compression_toolkit.logger import Logger
from model_compression_toolkit.constants import TENSORFLOW
from model_compression_toolkit.target_platform_capabilities.target_platform import TargetPlatformCapabilities
from model_compression_toolkit.core.common.mixed_precision.kpi_tools.kpi_data import compute_kpi_data
from model_compression_toolkit.core.common.framework_info import FrameworkInfo
from model_compression_toolkit.core.common.mixed_precision.mixed_precision_quantization_config import \
DEFAULT_MIXEDPRECISION_CONFIG
from model_compression_toolkit.constants import FOUND_TF

if FOUND_TF:
Expand All @@ -36,75 +34,13 @@

KERAS_DEFAULT_TPC = get_target_platform_capabilities(TENSORFLOW, DEFAULT_TP_MODEL)


def keras_kpi_data(in_model: Model,
representative_data_gen: Callable,
quant_config: MixedPrecisionQuantizationConfig = DEFAULT_MIXEDPRECISION_CONFIG,
core_config: CoreConfig,
fw_info: FrameworkInfo = DEFAULT_KERAS_INFO,
target_platform_capabilities: TargetPlatformCapabilities = KERAS_DEFAULT_TPC) -> KPI:
"""
Computes KPI data that can be used to calculate the desired target KPI for mixed-precision quantization.
Builds the computation graph from the given model and target platform modeling, and uses it to compute the KPI data.
Args:
in_model (Model): Keras model to quantize.
representative_data_gen (Callable): Dataset used for calibration.
quant_config (MixedPrecisionQuantizationConfig): MixedPrecisionQuantizationConfig containing parameters of how the model should be quantized.
fw_info (FrameworkInfo): Information needed for quantization about the specific framework (e.g., kernel channels indices, groups of layers by how they should be quantized, etc.). `Default Keras info <https://github.com/sony/model_optimization/blob/main/model_compression_toolkit/core/keras/default_framework_info.py>`_
target_platform_capabilities (TargetPlatformCapabilities): TargetPlatformCapabilities to optimize the Keras model according to.
Returns:
A KPI object with total weights parameters sum, max activation tensor and total kpi.
Examples:
Import a Keras model:
>>> from tensorflow.keras.applications.mobilenet import MobileNet
>>> model = MobileNet()
Create a random dataset generator, for required number of calibration iterations (num_calibration_batches):
In this example a random dataset of 10 batches each containing 4 images is used.
>>> import numpy as np
>>> num_calibration_batches = 10
>>> def repr_datagen():
>>> for _ in range(num_calibration_batches):
>>> yield [np.random.random((4, 224, 224, 3))]
Import MCT and call for KPI data calculation:
>>> import model_compression_toolkit as mct
>>> kpi_data = mct.core.keras_kpi_data(model, repr_datagen)
"""

if not isinstance(quant_config, MixedPrecisionQuantizationConfig):
Logger.error("KPI data computation can't be executed without MixedPrecisionQuantizationConfig object."
"Given quant_config is not of type MixedPrecisionQuantizationConfig.")

fw_impl = KerasImplementation()

quantization_config, mp_config = quant_config.separate_configs()
core_config = CoreConfig(quantization_config=quantization_config,
mixed_precision_config=mp_config)

return compute_kpi_data(in_model,
representative_data_gen,
core_config,
target_platform_capabilities,
fw_info,
fw_impl)


def keras_kpi_data_experimental(in_model: Model,
representative_data_gen: Callable,
core_config: CoreConfig,
fw_info: FrameworkInfo = DEFAULT_KERAS_INFO,
target_platform_capabilities: TargetPlatformCapabilities = KERAS_DEFAULT_TPC) -> KPI:
"""
Computes KPI data that can be used to calculate the desired target KPI for mixed-precision quantization.
Builds the computation graph from the given model and hw modeling, and uses it to compute the KPI data.
Args:
Expand Down Expand Up @@ -154,12 +90,6 @@ def keras_kpi_data_experimental(in_model: Model,
# If tensorflow is not installed,
# we raise an exception when trying to use this function.
def keras_kpi_data(*args, **kwargs):
Logger.critical('Installing tensorflow and is mandatory '
'when using keras_kpi_data. '
'Could not find Tensorflow package.') # pragma: no cover


def keras_kpi_data_experimental(*args, **kwargs):
Logger.critical('Installing tensorflow is mandatory '
'when using keras_kpi_data. '
'Could not find Tensorflow package.') # pragma: no cover
1 change: 1 addition & 0 deletions tests/keras_tests/function_tests/test_kpi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def prep_test(model, mp_bitwidth_candidates_list, random_datagen):

kpi_data = mct.core.keras_kpi_data(in_model=model,
representative_data_gen=random_datagen,
core_config=mct.core.CoreConfig(mixed_precision_config=mct.core.MixedPrecisionQuantizationConfigV2()),
target_platform_capabilities=tpc)

return kpi_data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ def representative_data_gen() -> list:
# Get KPI information to constraint your model's memory size.
# Retrieve a KPI object with helpful information of each KPI metric,
# to constraint the quantized model to the desired memory size.
kpi_data = mct.core.keras_kpi_data_experimental(model,
representative_data_gen,
config,
target_platform_capabilities=target_platform_cap)
kpi_data = mct.core.keras_kpi_data(model,
representative_data_gen,
config,
target_platform_capabilities=target_platform_cap)

# Set a constraint for each of the KPI metrics.
# Create a KPI object to limit our returned model's size. Note that this values affects only layers and attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def representative_data_gen() -> list:
# Get KPI information to constraint your model's memory size.
# Retrieve a KPI object with helpful information of each KPI metric,
# to constraint the quantized model to the desired memory size.
kpi_data = mct.core.keras_kpi_data_experimental(model,
representative_data_gen,
configuration,
target_platform_capabilities=target_platform_cap)
kpi_data = mct.core.keras_kpi_data(model,
representative_data_gen,
configuration,
target_platform_capabilities=target_platform_cap)

# Set a constraint for each of the KPI metrics.
# Create a KPI object to limit our returned model's size. Note that this values affects only layers and attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ def representative_data_gen() -> list:
# Get KPI information to constraint your model's memory size.
# Retrieve a KPI object with helpful information of each KPI metric,
# to constraint the quantized model to the desired memory size.
kpi_data = mct.core.keras_kpi_data_experimental(model,
representative_data_gen,
configuration,
target_platform_capabilities=target_platform_cap)
kpi_data = mct.core.keras_kpi_data(model,
representative_data_gen,
configuration,
target_platform_capabilities=target_platform_cap)

# Set a constraint for each of the KPI metrics.
# Create a KPI object to limit our returned model's size. Note that this values affects only layers and attributes
Expand Down
2 changes: 1 addition & 1 deletion tutorials/quick_start/keras_fw/quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_target_kpi(model, weights_compression, representative_data_gen, core_con
A KPI object computed from MCT and contains info about the target model size.
"""
kpi_data = mct.core.keras_kpi_data_experimental(model, representative_data_gen, core_config=core_config, target_platform_capabilities=tpc)
kpi_data = mct.core.keras_kpi_data(model, representative_data_gen, core_config=core_config, target_platform_capabilities=tpc)
weights_kpi = BYTES_TO_FP32 * kpi_data.weights_memory / weights_compression # (4 bytes for fp32) * weights memory(in Bytes) / compression rate
return KPI(weights_memory=weights_kpi)

Expand Down

0 comments on commit ce5b747

Please sign in to comment.