Skip to content

Commit

Permalink
PR fixes:
Browse files Browse the repository at this point in the history
1. Fixed imports from long strings to alias "schema".
2. Fixed some failing tests.
3. Removed unused files.
  • Loading branch information
liord committed Nov 27, 2024
1 parent 536dc92 commit 6e3bfb5
Show file tree
Hide file tree
Showing 54 changed files with 920 additions and 930 deletions.
2 changes: 1 addition & 1 deletion model_compression_toolkit/qat/keras/quantizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Several training methods may be applied by the user to train the QAT ready model
created by `keras_quantization_aware_training_init` method in [`keras/quantization_facade`](../quantization_facade.py).
Each `TrainingMethod` (an enum defined in the [`qat_config`](../../common/qat_config.py))
and [`QuantizationMethod`](../../../target_platform_capabilities/target_platform/op_quantization_config.py)
and `QuantizationMethod`
selects a quantizer for weights and a quantizer for activations.

Currently, only the STE (straight through estimator) training method is implemented by the MCT.
Expand Down
2 changes: 1 addition & 1 deletion model_compression_toolkit/qat/pytorch/quantizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Several training methods may be applied by the user to train the QAT ready model
created by `pytorch_quantization_aware_training_init` method in [`pytorch/quantization_facade`](../quantization_facade.py).
Each [`TrainingMethod`](../../../trainable_infrastructure/common/training_method.py)
and [`QuantizationMethod`](../../../target_platform_capabilities/target_platform/op_quantization_config.py)
and `QuantizationMethod`
selects a quantizer for weights and a quantizer for activations.

## Make your own training method
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ class TargetPlatformCapabilities(ImmutableClass):
"""
Attach framework information to a modeled hardware.
"""
def __init__(self, tp_model: TargetPlatformModel):
def __init__(self,
tp_model: TargetPlatformModel,
name: str = "base"):
"""
Args:
tp_model (TargetPlatformModel): Modeled hardware to attach framework information to.
name (str): Name of the TargetPlatformCapabilities.
"""

super().__init__()
self.name = name
assert isinstance(tp_model, TargetPlatformModel), f'Target platform model that was passed to TargetPlatformCapabilities must be of type TargetPlatformModel, but has type of {type(tp_model)}'
self.tp_model = tp_model
self.op_sets_to_layers = OperationsToLayers() # Init an empty OperationsToLayers
Expand Down Expand Up @@ -107,7 +111,9 @@ def get_info(self) -> Dict[str, Any]:
"""
return {"Target Platform Capabilities": self.name,
"Version": self.version,
"Minor version": self.tp_model.tpc_minor_version,
"Patch version": self.tp_model.tpc_patch_version,
"Platform type": self.tp_model.tpc_platform_type,
"Target Platform Model": self.tp_model.get_info(),
"Operations to layers": {op2layer.name:[l.__name__ for l in op2layer.layers] for op2layer in self.op_sets_to_layers.op_sets_to_layers}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import List, Tuple

import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
import model_compression_toolkit.target_platform_capabilities.schema.v1 as schema
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
Expand Down Expand Up @@ -63,7 +63,8 @@ def get_op_quantization_configs() -> Tuple[OpQuantizationConfig, List[OpQuantiza
weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
weights_n_bits=8,
weights_per_channel_threshold=False,
enable_weights_quantization=False, # TODO: this will changed to True once implementing multi-attributes quantization
enable_weights_quantization=False,
# TODO: this will changed to True once implementing multi-attributes quantization
lut_values_bitwidth=None)

# define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
Expand All @@ -88,7 +89,7 @@ def get_op_quantization_configs() -> Tuple[OpQuantizationConfig, List[OpQuantiza

# We define a default config for operation without kernel attribute.
# This is the default config that should be used for non-linear operations.
eight_bits_default = model_compression_toolkit.target_platform_capabilities.schema.v1.OpQuantizationConfig(
eight_bits_default = schema.OpQuantizationConfig(
default_weight_attr_config=default_weight_attr_config,
attr_weights_configs_mapping={},
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
Expand All @@ -102,7 +103,7 @@ def get_op_quantization_configs() -> Tuple[OpQuantizationConfig, List[OpQuantiza
signedness=Signedness.AUTO)

# We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
linear_eight_bits = model_compression_toolkit.target_platform_capabilities.schema.v1.OpQuantizationConfig(
linear_eight_bits = schema.OpQuantizationConfig(
default_weight_attr_config=default_weight_attr_config,
attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
Expand Down Expand Up @@ -152,12 +153,12 @@ def generate_tp_model(default_config: OpQuantizationConfig,
# of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
# If the QuantizationConfigOptions contains only one configuration,
# this configuration will be used for the operation quantization:
default_configuration_options = model_compression_toolkit.target_platform_capabilities.schema.v1.QuantizationConfigOptions([default_config])
default_configuration_options = schema.QuantizationConfigOptions([default_config])

# Create a TargetPlatformModel and set its default quantization config.
# This default configuration will be used for all operations
# unless specified otherwise (see OperatorsSet, for example):
generated_tpc = model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel(
generated_tpc = schema.TargetPlatformModel(
default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
Expand All @@ -178,42 +179,42 @@ def generate_tp_model(default_config: OpQuantizationConfig,

# May suit for operations like: Dropout, Reshape, etc.
default_qco = tp.get_default_quantization_config_options()
model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("NoQuantization",
default_qco.clone_and_edit(enable_activation_quantization=False)
.clone_and_edit_weight_attribute(enable_weights_quantization=False))
schema.OperatorsSet("NoQuantization",
default_qco.clone_and_edit(enable_activation_quantization=False)
.clone_and_edit_weight_attribute(enable_weights_quantization=False))

# Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
mixed_precision_configuration_options = model_compression_toolkit.target_platform_capabilities.schema.v1.QuantizationConfigOptions(mixed_precision_cfg_list,
base_config=base_config)
mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
base_config=base_config)

# Define operator sets that use mixed_precision_configuration_options:
conv = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Conv", mixed_precision_configuration_options)
fc = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)

# Define operations sets without quantization configuration
# options (useful for creating fusing patterns, for example):
any_relu = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("AnyReLU")
add = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Add")
sub = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Sub")
mul = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Mul")
div = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Div")
prelu = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("PReLU")
swish = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Swish")
sigmoid = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Sigmoid")
tanh = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("Tanh")
any_relu = schema.OperatorsSet("AnyReLU")
add = schema.OperatorsSet("Add")
sub = schema.OperatorsSet("Sub")
mul = schema.OperatorsSet("Mul")
div = schema.OperatorsSet("Div")
prelu = schema.OperatorsSet("PReLU")
swish = schema.OperatorsSet("Swish")
sigmoid = schema.OperatorsSet("Sigmoid")
tanh = schema.OperatorsSet("Tanh")

# Combine multiple operators into a single operator to avoid quantization between
# them. To do this we define fusing patterns using the OperatorsSets that were created.
# To group multiple sets with regard to fusing, an OperatorSetConcat can be created
activations_after_conv_to_fuse = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorSetConcat(any_relu, swish, prelu, sigmoid, tanh)
activations_after_fc_to_fuse = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorSetConcat(any_relu, swish, sigmoid)
any_binary = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorSetConcat(add, sub, mul, div)
activations_after_conv_to_fuse = schema.OperatorSetConcat(any_relu, swish, prelu, sigmoid, tanh)
activations_after_fc_to_fuse = schema.OperatorSetConcat(any_relu, swish, sigmoid)
any_binary = schema.OperatorSetConcat(add, sub, mul, div)

# ------------------- #
# Fusions
# ------------------- #
model_compression_toolkit.target_platform_capabilities.schema.v1.Fusing([conv, activations_after_conv_to_fuse])
model_compression_toolkit.target_platform_capabilities.schema.v1.Fusing([fc, activations_after_fc_to_fuse])
model_compression_toolkit.target_platform_capabilities.schema.v1.Fusing([any_binary, any_relu])
schema.Fusing([conv, activations_after_conv_to_fuse])
schema.Fusing([fc, activations_after_fc_to_fuse])
schema.Fusing([any_binary, any_relu])

return generated_tpc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import tensorflow as tf
from packaging import version

import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel
from model_compression_toolkit.defaultdict import DefaultDict
from model_compression_toolkit.verify_packages import FOUND_SONY_CUSTOM_LAYERS
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_DEPTHWISE_KERNEL, \
Expand Down Expand Up @@ -49,7 +49,7 @@ def get_keras_tpc() -> tp.TargetPlatformCapabilities:
return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)


def generate_keras_tpc(name: str, tp_model: model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel):
def generate_keras_tpc(name: str, tp_model: TargetPlatformModel):
"""
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu

import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel
from model_compression_toolkit.defaultdict import DefaultDict
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, PYTORCH_KERNEL, \
BIAS
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1.tp_model import get_tp_model
import model_compression_toolkit as mct
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1 import __version__ as TPC_VERSION

tp = mct.target_platform

Expand All @@ -43,7 +42,7 @@ def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)


def generate_pytorch_tpc(name: str, tp_model: model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel):
def generate_pytorch_tpc(name: str, tp_model: TargetPlatformModel):
"""
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
Args:
Expand Down
Loading

0 comments on commit 6e3bfb5

Please sign in to comment.