Skip to content

Commit

Permalink
Refactor Target Platform Capabilities - Phase 4 (#1301)
Browse files Browse the repository at this point in the history
* Refactor Target Platform Capabilities - Phase 4
Replace schema classes from dataclass to pydantic 'BaseModel'.
Fix tests to support pydantic schema classes.
Add test for exporting TargetPlatformModel class to json
---------

Co-authored-by: liord <lior.dikstein@altair-semi.com>
Co-authored-by: Elad Cohen <78862769+elad-c@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 31, 2024
1 parent dcbfc5c commit afed6e3
Show file tree
Hide file tree
Showing 35 changed files with 906 additions and 728 deletions.
481 changes: 308 additions & 173 deletions model_compression_toolkit/target_platform_capabilities/schema/v1.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ 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 = schema.QuantizationConfigOptions(tuple([default_config]))
default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config]))

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

# Create an OperatorsSet to represent a set of operations.
Expand All @@ -167,46 +167,46 @@ def generate_tp_model(default_config: OpQuantizationConfig,
operator_set = []
fusing_patterns = []

operator_set.append(schema.OperatorsSet("NoQuantization",
default_configuration_options.clone_and_edit(enable_activation_quantization=False)
operator_set.append(schema.OperatorsSet(name="NoQuantization",
qc_options=default_configuration_options.clone_and_edit(enable_activation_quantization=False)
.clone_and_edit_weight_attribute(enable_weights_quantization=False)))

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

# Define operations sets without quantization configuration
# options (useful for creating fusing patterns, for example):
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")
any_relu = schema.OperatorsSet(name="AnyReLU")
add = schema.OperatorsSet(name="Add")
sub = schema.OperatorsSet(name="Sub")
mul = schema.OperatorsSet(name="Mul")
div = schema.OperatorsSet(name="Div")
prelu = schema.OperatorsSet(name="PReLU")
swish = schema.OperatorsSet(name="Swish")
sigmoid = schema.OperatorsSet(name="Sigmoid")
tanh = schema.OperatorsSet(name="Tanh")

operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, 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 = 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])
activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, prelu, sigmoid, tanh])
activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, sigmoid])
any_binary = schema.OperatorSetConcat(operators_set=[add, sub, mul, div])

# ------------------- #
# Fusions
# ------------------- #
fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu)))

# 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 = schema.TargetPlatformModel(
default_configuration_options,
default_qco=default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ 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 = schema.QuantizationConfigOptions(tuple([default_config]))
default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config]))

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

# Create an OperatorsSet to represent a set of operations.
Expand All @@ -166,47 +166,47 @@ def generate_tp_model(default_config: OpQuantizationConfig,
fusing_patterns = []

# May suit for operations like: Dropout, Reshape, etc.
operator_set.append(schema.OperatorsSet("NoQuantization",
default_configuration_options.clone_and_edit(
operator_set.append(schema.OperatorsSet(name="NoQuantization",
qc_options=default_configuration_options.clone_and_edit(
enable_activation_quantization=False)
.clone_and_edit_weight_attribute(enable_weights_quantization=False)))

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

# Define operations sets without quantization configuration
# options (useful for creating fusing patterns, for example):
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")
any_relu = schema.OperatorsSet(name="AnyReLU")
add = schema.OperatorsSet(name="Add")
sub = schema.OperatorsSet(name="Sub")
mul = schema.OperatorsSet(name="Mul")
div = schema.OperatorsSet(name="Div")
prelu = schema.OperatorsSet(name="PReLU")
swish = schema.OperatorsSet(name="Swish")
sigmoid = schema.OperatorsSet(name="Sigmoid")
tanh = schema.OperatorsSet(name="Tanh")

operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, 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 = 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])
activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, prelu, sigmoid, tanh])
activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, sigmoid])
any_binary = schema.OperatorSetConcat(operators_set=[add, sub, mul, div])

# ------------------- #
# Fusions
# ------------------- #
fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu)))

# 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 = schema.TargetPlatformModel(
default_configuration_options,
default_qco=default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ 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 = schema.QuantizationConfigOptions(tuple([default_config]))
default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config]))

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

# Create an OperatorsSet to represent a set of operations.
Expand All @@ -162,48 +162,48 @@ def generate_tp_model(default_config: OpQuantizationConfig,
fusing_patterns = []

# May suit for operations like: Dropout, Reshape, etc.
operator_set.append(schema.OperatorsSet("NoQuantization",
default_configuration_options.clone_and_edit(
operator_set.append(schema.OperatorsSet(name="NoQuantization",
qc_options=default_configuration_options.clone_and_edit(
enable_activation_quantization=False)
.clone_and_edit_weight_attribute(enable_weights_quantization=False)))

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

# Define operations sets without quantization configuration
# options (useful for creating fusing patterns, for example):
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")
any_relu = schema.OperatorsSet(name="AnyReLU")
add = schema.OperatorsSet(name="Add")
sub = schema.OperatorsSet(name="Sub")
mul = schema.OperatorsSet(name="Mul")
div = schema.OperatorsSet(name="Div")
prelu = schema.OperatorsSet(name="PReLU")
swish = schema.OperatorsSet(name="Swish")
sigmoid = schema.OperatorsSet(name="Sigmoid")
tanh = schema.OperatorsSet(name="Tanh")

operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, 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 = 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])
activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, prelu, sigmoid, tanh])
activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, sigmoid])
any_binary = schema.OperatorSetConcat(operators_set=[add, sub, mul, div])

# ------------------- #
# Fusions
# ------------------- #
fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu)))

# 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 = schema.TargetPlatformModel(
default_configuration_options,
default_qco=default_configuration_options,
tpc_minor_version=1,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ 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 = schema.QuantizationConfigOptions(tuple([default_config]))
default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config]))

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

# Create an OperatorsSet to represent a set of operations.
Expand All @@ -170,46 +170,46 @@ def generate_tp_model(default_config: OpQuantizationConfig,
operator_set = []
fusing_patterns = []
# May suit for operations like: Dropout, Reshape, etc.
operator_set.append(schema.OperatorsSet("NoQuantization", default_configuration_options.clone_and_edit(
operator_set.append(schema.OperatorsSet(name="NoQuantization", qc_options=default_configuration_options.clone_and_edit(
enable_activation_quantization=False).clone_and_edit_weight_attribute(enable_weights_quantization=False)))

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

# Define operations sets without quantization configuration
# options (useful for creating fusing patterns, for example):
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")
any_relu = schema.OperatorsSet(name="AnyReLU")
add = schema.OperatorsSet(name="Add")
sub = schema.OperatorsSet(name="Sub")
mul = schema.OperatorsSet(name="Mul")
div = schema.OperatorsSet(name="Div")
prelu = schema.OperatorsSet(name="PReLU")
swish = schema.OperatorsSet(name="Swish")
sigmoid = schema.OperatorsSet(name="Sigmoid")
tanh = schema.OperatorsSet(name="Tanh")

operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, 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 = 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])
activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, prelu, sigmoid, tanh])
activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, sigmoid])
any_binary = schema.OperatorSetConcat(operators_set=[add, sub, mul, div])

# ------------------- #
# Fusions
# ------------------- #
fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse)))
fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu)))

# 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_tpm = schema.TargetPlatformModel(
default_configuration_options,
default_qco=default_configuration_options,
tpc_minor_version=2,
tpc_patch_version=0,
tpc_platform_type=IMX500_TP_MODEL,
Expand Down
Loading

0 comments on commit afed6e3

Please sign in to comment.