Skip to content

Commit

Permalink
align tests and use CustomOpsetLayers
Browse files Browse the repository at this point in the history
  • Loading branch information
ofirgo committed Jan 5, 2025
1 parent 26ae10e commit c280dd8
Show file tree
Hide file tree
Showing 26 changed files with 48 additions and 75 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 @@ -18,7 +18,7 @@
from model_compression_toolkit.core.common.quantization.debug_config import DebugConfig
from model_compression_toolkit.core.common.quantization import quantization_config
from model_compression_toolkit.core.common.mixed_precision import mixed_precision_quantization_config
from model_compression_toolkit.core.common.quantization.quantization_config import QuantizationConfig, QuantizationErrorMethod, DEFAULTCONFIG
from model_compression_toolkit.core.common.quantization.quantization_config import QuantizationConfig, QuantizationErrorMethod, DEFAULTCONFIG, CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.bit_width_config import BitWidthConfig
from model_compression_toolkit.core.common.quantization.core_config import CoreConfig
from model_compression_toolkit.core.common.mixed_precision.resource_utilization_tools.resource_utilization import ResourceUtilization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@
from dataclasses import dataclass
import math
from enum import Enum
from typing import Optional, Dict
from typing import Optional, Dict, NamedTuple, List

from model_compression_toolkit import DefaultDict
from model_compression_toolkit.constants import MIN_THRESHOLD
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers


class CustomOpsetLayers(NamedTuple):
"""
This struct is used to define a set of operators to map to a custom operator set defined in the TPC.
Args:
operators: a list of framework operators to map to a certain custom opset name.
attr_mapping: a mapping between an opset name to a mapping between its attributes' general names and names in
the framework.
"""

operators: List
attr_mapping: Optional[Dict[str, DefaultDict]] = None


class QuantizationErrorMethod(Enum):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
from typing import Dict, NamedTuple, List, Any, Optional
from typing import Dict, Optional

from model_compression_toolkit import DefaultDict
from model_compression_toolkit.logger import Logger
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, \
OperatorsSet
from model_compression_toolkit.target_platform_capabilities.target_platform import TargetPlatformCapabilities, \
OperationsSetToLayers


class CustomOpsetLayers(NamedTuple):
"""
This struct is used to define a set of operators to map to a custom operator set defined in the TPC.
Args:
operators: a list of framework operators to map to a certain custom opset name.
attr_mapping: a mapping between an opset name to a mapping between its attributes' general names and names in
the framework.
"""

operators: List
attr_mapping: Optional[Dict[str, DefaultDict]] = None
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers


class AttachTpcToFramework:
Expand All @@ -33,7 +20,7 @@ def __init__(self):
self._opset2attr_mapping = None # Mapping of operation sets to their corresponding framework-specific layers

def attach(self, tpc_model: TargetPlatformModel,
custom_opset2layer: Optional[Dict[str, CustomOpsetLayers]] = None
custom_opset2layer: Optional[Dict[str, 'CustomOpsetLayers']] = None
) -> TargetPlatformCapabilities:
"""
Attaching a TargetPlatformModel which includes a platform capabilities description to specific
Expand All @@ -57,15 +44,11 @@ def attach(self, tpc_model: TargetPlatformModel,
for opset in tpc_model.operator_set:
if isinstance(opset, OperatorsSet): # filter out OperatorsSetConcat
if opset.name in custom_opset2layer:
operators = custom_opset2layer[opset.name]
if len(operators) == 1:
OperationsSetToLayers(opset.name, operators[0])
elif len(operators) == 2:
OperationsSetToLayers(opset.name, operators[0], attr_mapping=operators[1])
else:
raise ValueError(f"Custom operator set to layer mapping should include up to 2 elements - "
f"a list of layers to attach to the operator and an optional mapping of "
f"attributes names, but given a mapping contains {len(operators)} elements.")
custom_opset_layers = custom_opset2layer[opset.name]
OperationsSetToLayers(opset.name,
layers=custom_opset_layers.operators,
attr_mapping=custom_opset_layers.attr_mapping)

elif opset.name in self._opset2layer:
# Note that if the user provided a custom operator set with a name that exists in our
# pre-defined set of operator sets, we prioritize the user's custom opset definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from model_compression_toolkit.core.keras.constants import GAMMA, BETA
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import Signedness
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS, BIAS_ATTR
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.generate_test_tp_model import generate_test_attr_configs, \
DEFAULT_WEIGHT_ATTR_CONFIG, KERNEL_BASE_CONFIG, generate_test_op_qc, BIAS_CONFIG
from tests.keras_tests.feature_networks_tests.base_keras_feature_test import BaseKerasFeatureNetworkTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
QuantizationConfigOptions
from model_compression_toolkit.target_platform_capabilities.schema.schema_functions import \
get_config_options_by_operators_set
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.generate_test_tp_model import generate_test_op_qc, generate_test_attr_configs, \
generate_custom_test_tp_model
from tests.common_tests.helpers.tpcs_for_tests.v3.tp_model import get_tp_model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

from packaging import version

from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2keras import \
AttachTpcToKeras

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
QuantizationConfig
from keras.layers import Conv2D, Conv2DTranspose, DepthwiseConv2D, Dense, BatchNormalization, ReLU, Input, Add

from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.keras_tests.feature_networks_tests.base_keras_feature_test import BaseKerasFeatureNetworkTest
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import get_op_quantization_configs
import tensorflow as tf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from model_compression_toolkit.core import QuantizationConfig, CoreConfig
from model_compression_toolkit.core.keras.constants import SIGMOID, SOFTMAX, BIAS
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, KERAS_KERNEL
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.generate_test_tp_model import generate_test_op_qc, generate_test_attr_configs
from tests.keras_tests.exporter_tests.tflite_int8.imx500_int8_tp_model import get_op_quantization_configs
from tests.keras_tests.feature_networks_tests.base_keras_feature_test import BaseKerasFeatureNetworkTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from model_compression_toolkit.defaultdict import DefaultDict
from model_compression_toolkit.core.common.mixed_precision.distance_weighting import MpDistanceWeighting
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, BIAS
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import \
get_op_quantization_configs, generate_keras_tpc
from tests.common_tests.helpers.generate_test_tp_model import generate_test_op_qc, generate_test_attr_configs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
from packaging import version
import tensorflow as tf

from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2keras import \
AttachTpcToKeras
from tests.common_tests.helpers.generate_test_tp_model import generate_test_op_qc, generate_test_attr_configs
Expand Down
3 changes: 1 addition & 2 deletions tests/keras_tests/function_tests/test_custom_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import Signedness
from model_compression_toolkit.target_platform_capabilities.constants import BIAS_ATTR, KERNEL_ATTR
from model_compression_toolkit.target_platform_capabilities.target_platform import LayerFilterParams
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.generate_test_tp_model import generate_test_attr_configs, DEFAULT_WEIGHT_ATTR_CONFIG, \
KERNEL_BASE_CONFIG, BIAS_CONFIG

Expand Down
3 changes: 1 addition & 2 deletions tests/keras_tests/function_tests/test_hmse_error_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
from model_compression_toolkit.core.keras.constants import KERNEL, GAMMA
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, KERAS_KERNEL, BIAS
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import AttributeQuantizationConfig
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2keras import \
AttachTpcToKeras
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import generate_keras_tpc
Expand Down
3 changes: 1 addition & 2 deletions tests/keras_tests/function_tests/test_layer_fusing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from model_compression_toolkit.core.common.fusion.layer_fusing import fusion
from model_compression_toolkit.core.keras.default_framework_info import DEFAULT_KERAS_INFO
from model_compression_toolkit.core.keras.keras_implementation import KerasImplementation
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2keras import \
AttachTpcToKeras
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
from model_compression_toolkit.core.keras.constants import DEPTHWISE_KERNEL, KERNEL
from model_compression_toolkit.core.keras.graph_substitutions.substitutions.separableconv_decomposition import \
POINTWISE_KERNEL
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.generate_test_tp_model import generate_test_op_qc, generate_test_attr_configs
from tests.keras_tests.tpc_keras import get_tpc_with_activation_mp_keras

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
from keras import Input
from keras.layers import Conv2D

from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers

if tf.__version__ >= "2.13":
from keras.src.engine.input_layer import InputLayer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import keras
import unittest

from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers

if tf.__version__ >= "2.13":
from keras.src.layers import Conv2D, Conv2DTranspose, DepthwiseConv2D, Dense, BatchNormalization, ReLU, Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
from model_compression_toolkit.core.keras.default_framework_info import DEFAULT_KERAS_INFO
from model_compression_toolkit.core.keras.keras_implementation import KerasImplementation
from model_compression_toolkit.logger import Logger
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2keras import \
AttachTpcToKeras
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import generate_keras_tpc
Expand Down
3 changes: 1 addition & 2 deletions tests/pytorch_tests/function_tests/layer_fusing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from model_compression_toolkit.target_platform_capabilities.target_platform import LayerFilterParams
from model_compression_toolkit.core.pytorch.default_framework_info import DEFAULT_PYTORCH_INFO
from model_compression_toolkit.core.pytorch.pytorch_implementation import PytorchImplementation
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2pytorch import \
AttachTpcToPytorch
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

from model_compression_toolkit.core import QuantizationConfig
from model_compression_toolkit.core.pytorch.reader.node_holders import DummyPlaceHolder
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import \
get_op_quantization_configs
from model_compression_toolkit.core.pytorch.constants import KERNEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from model_compression_toolkit.core.pytorch.constants import GAMMA, BETA
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, PYTORCH_KERNEL, BIAS, \
BIAS_ATTR
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.generate_test_tp_model import generate_test_attr_configs, \
DEFAULT_WEIGHT_ATTR_CONFIG, KERNEL_BASE_CONFIG, generate_test_op_qc, BIAS_CONFIG
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import Signedness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import Signedness
from model_compression_toolkit.core import MixedPrecisionQuantizationConfig, CoreConfig, QuantizationConfig
from model_compression_toolkit.core.pytorch.utils import to_torch_tensor, torch_tensor_to_numpy, set_model
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.tpcs_for_tests.v4.tp_model import get_tp_model as get_tp_v4
from tests.common_tests.helpers.tpcs_for_tests.v3.tp_model import get_tp_model as get_tp_v3
from tests.pytorch_tests.model_tests.base_pytorch_feature_test import BasePytorchFeatureNetworkTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
QuantizationConfigOptions
from model_compression_toolkit.target_platform_capabilities.schema.schema_functions import \
get_config_options_by_operators_set
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.common_tests.helpers.generate_test_tp_model import generate_custom_test_tp_model
from tests.common_tests.helpers.tpcs_for_tests.v3.tp_model import get_tp_model
from tests.pytorch_tests.model_tests.feature_models.mixed_precision_activation_test import \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
from model_compression_toolkit.target_platform_capabilities.target_platform import TargetPlatformCapabilities, OperationsSetToLayers
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, OperatorsSet, \
QuantizationConfigOptions
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import get_op_quantization_configs
from tests.common_tests.helpers.generate_test_tp_model import generate_tp_model_with_activation_mp
from tests.pytorch_tests.model_tests.base_pytorch_test import BasePytorchTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import torch.nn
from model_compression_toolkit.core import MixedPrecisionQuantizationConfig, ResourceUtilization, MixedPrecisionQuantizationConfig
from model_compression_toolkit.core.pytorch.reader.node_holders import DummyPlaceHolder
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
CustomOpsetLayers
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from tests.pytorch_tests.model_tests.base_pytorch_test import BasePytorchTest

from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import get_op_quantization_configs
Expand Down
Loading

0 comments on commit c280dd8

Please sign in to comment.