Skip to content

Commit

Permalink
Added tpc_platfrom_type to meta data for tp model
Browse files Browse the repository at this point in the history
  • Loading branch information
liord committed Nov 25, 2024
1 parent 2f794ad commit 536dc92
Show file tree
Hide file tree
Showing 30 changed files with 88 additions and 22 deletions.
4 changes: 3 additions & 1 deletion model_compression_toolkit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

# Metadata fields
MCT_VERSION = 'mct_version'
TPC_VERSION = 'tpc_version'
TPC_MINOR_VERSION = 'tpc_minor_version'
TPC_PATCH_VERSION = 'tpc_patch_version'
TPC_PLATFORM_TYPE = 'tpc_platform_type'
TPC_SCHEMA = 'tpc_schema'

WEIGHTS_SIGNED = True
Expand Down
14 changes: 10 additions & 4 deletions model_compression_toolkit/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
# ==============================================================================

from typing import Dict, Any
from model_compression_toolkit.constants import MCT_VERSION, TPC_VERSION, OPERATORS_SCHEDULING, FUSED_NODES_MAPPING, \
CUTS, MAX_CUT, OP_ORDER, OP_RECORD, SHAPE, NODE_OUTPUT_INDEX, NODE_NAME, TOTAL_SIZE, MEM_ELEMENTS, TPC_SCHEMA
from model_compression_toolkit.constants import MCT_VERSION, TPC_MINOR_VERSION, OPERATORS_SCHEDULING, \
FUSED_NODES_MAPPING, \
CUTS, MAX_CUT, OP_ORDER, OP_RECORD, SHAPE, NODE_OUTPUT_INDEX, NODE_NAME, TOTAL_SIZE, MEM_ELEMENTS, TPC_SCHEMA, \
TPC_PATCH_VERSION, TPC_PLATFORM_TYPE
from model_compression_toolkit.core.common.graph.memory_graph.compute_graph_max_cut import SchedulerInfo
from model_compression_toolkit.target_platform_capabilities.target_platform import TargetPlatformCapabilities

Expand Down Expand Up @@ -48,10 +50,14 @@ def get_versions_dict(tpc) -> Dict:
"""
# imported inside to avoid circular import error
from model_compression_toolkit import __version__ as mct_version
tpc_version = f'{tpc.tp_model.tpc_minor_version}.{tpc.tp_model.tpc_patch_version}'
tpc_minor_version = f'{tpc.tp_model.tpc_minor_version}'
tpc_patch_version = f'{tpc.tp_model.tpc_patch_version}'
tpc_platform_type = f'{tpc.tp_model.tpc_platform_type}'
tpc_schema = f'{tpc.tp_model.SCHEMA_VERSION}'
return {MCT_VERSION: mct_version,
TPC_VERSION: tpc_version,
TPC_MINOR_VERSION: tpc_minor_version,
TPC_PATCH_VERSION: tpc_patch_version,
TPC_PLATFORM_TYPE: tpc_platform_type,
TPC_SCHEMA: tpc_schema}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ def __init__(self,
default_qco: QuantizationConfigOptions,
tpc_minor_version: Optional[int],
tpc_patch_version: Optional[int],
tpc_platform_type: Optional[str],
add_metadata: bool = True,
name="default_tp_model"):
"""
Expand All @@ -544,6 +545,7 @@ def __init__(self,
default_qco (QuantizationConfigOptions): Default QuantizationConfigOptions to use for operators that their QuantizationConfigOptions are not defined in the model.
tpc_minor_version (Optional[int]): The minor version of the target platform capabilities.
tpc_patch_version (Optional[int]): The patch version of the target platform capabilities.
tpc_platform_type (Optional[str]): The platform type of the target platform capabilities.
add_metadata (bool): Whether to add metadata to the model or not.
name (str): Name of the model.
Expand All @@ -554,6 +556,7 @@ def __init__(self,
super().__init__()
self.tpc_minor_version = tpc_minor_version
self.tpc_patch_version = tpc_patch_version
self.tpc_platform_type = tpc_platform_type
self.add_metadata = add_metadata
self.name = name
self.operator_set = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, OperatorsSetBase, \
OpQuantizationConfig, QuantizationConfigOptions
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.current_tpc import _current_tpc
from model_compression_toolkit.constants import MCT_VERSION, TPC_VERSION


class TargetPlatformCapabilities(ImmutableClass):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -160,6 +161,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
name=name,
add_metadata=False)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
WEIGHTS_QUANTIZATION_METHOD
WEIGHTS_QUANTIZATION_METHOD, IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -159,6 +159,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
add_metadata=False,
name=name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -154,6 +155,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
add_metadata=False,
name=name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -162,6 +163,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=2,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
add_metadata=True,
name=name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
WEIGHTS_QUANTIZATION_METHOD
WEIGHTS_QUANTIZATION_METHOD, IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -161,6 +161,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=2,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
add_metadata=True,
name=name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -183,6 +184,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=3,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
add_metadata=True,
name=name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
WEIGHTS_QUANTIZATION_METHOD
WEIGHTS_QUANTIZATION_METHOD, IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -172,6 +172,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=3,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
add_metadata=True,
name=name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -231,6 +232,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=4,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
add_metadata=True, name=name)

# To start defining the model's components (such as operator sets, and fusing patterns),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, QNNPACK_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -147,6 +147,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=QNNPACK_TP_MODEL,
add_metadata=False,
name=name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.v1
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import BIAS_ATTR, KERNEL_ATTR
from model_compression_toolkit.target_platform_capabilities.constants import BIAS_ATTR, KERNEL_ATTR, TFLITE_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.v1 import TargetPlatformModel, Signedness, \
AttributeQuantizationConfig, OpQuantizationConfig

Expand Down Expand Up @@ -145,6 +145,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=TFLITE_TP_MODEL,
add_metadata=False,
name=name)

Expand Down
1 change: 1 addition & 0 deletions tests/common_tests/helpers/generate_test_tp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def generate_custom_test_tp_model(name: str,
default_configuration_options,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False,
name=name)

Expand Down
9 changes: 9 additions & 0 deletions tests/common_tests/test_tp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ def test_get_default_options(self):
with model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel(TEST_QCO,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False):
self.assertEqual(tp.get_default_quantization_config_options(), TEST_QCO)

def test_immutable_tp(self):
model = model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel(TEST_QCO,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False)
with self.assertRaises(Exception) as e:
with model:
Expand All @@ -60,13 +62,15 @@ def test_default_options_more_than_single_qc(self):
model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel(test_qco,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False)
self.assertEqual('Default QuantizationConfigOptions must contain only one option', str(e.exception))

def test_tp_model_show(self):
tpm = model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel(TEST_QCO,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False)
with tpm:
a = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("opA")
Expand All @@ -80,6 +84,7 @@ def test_opset_qco(self):
hm = model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel(TEST_QCO,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False,
name='test')
opset_name = "ops_3bit"
Expand All @@ -100,6 +105,7 @@ def test_opset_concat(self):
hm = model_compression_toolkit.target_platform_capabilities.schema.v1.TargetPlatformModel(TEST_QCO,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False,
name='test')
with hm:
Expand All @@ -117,6 +123,7 @@ def test_non_unique_opset(self):
model_compression_toolkit.target_platform_capabilities.schema.v1.QuantizationConfigOptions([TEST_QC]),
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False)
with self.assertRaises(Exception) as e:
with hm:
Expand Down Expand Up @@ -181,6 +188,7 @@ def test_fusing_contains(self):
model_compression_toolkit.target_platform_capabilities.schema.v1.QuantizationConfigOptions([TEST_QC]),
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False)
with hm:
conv = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("conv")
Expand All @@ -201,6 +209,7 @@ def test_fusing_contains_with_opset_concat(self):
model_compression_toolkit.target_platform_capabilities.schema.v1.QuantizationConfigOptions([TEST_QC]),
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False)
with hm:
conv = model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("conv")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
default_configuration_options,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False, name=name)
with generated_tpc:
model_compression_toolkit.target_platform_capabilities.schema.v1.OperatorsSet("NoQuantization",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def _generate_bn_quantized_tpm(quantize_linear):
default_configuration_options,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False, name='bn_quantized_tpm')

with generated_tpm:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ def get_tpc(self):
model_compression_toolkit.target_platform_capabilities.schema.v1.QuantizationConfigOptions([cfg], cfg),
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False,
name="mp_activation_conf_weights_test")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def get_tpc(self):
weight_fixed_cfg,
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False,
name="mp_part_weights_layers_test")
with tp_model:
Expand Down Expand Up @@ -525,6 +526,7 @@ def get_tpc(self):
model_compression_toolkit.target_platform_capabilities.schema.v1.QuantizationConfigOptions([cfg], cfg),
tpc_minor_version=None,
tpc_patch_version=None,
tpc_platform_type=None,
add_metadata=False,
name="mp_weights_conf_act_test")

Expand Down
Loading

0 comments on commit 536dc92

Please sign in to comment.