From 3dfad7fd3eef4926de5220a4af0106b70e5ee227 Mon Sep 17 00:00:00 2001 From: Ofir Gordon Date: Wed, 1 Jan 2025 12:39:29 +0200 Subject: [PATCH] align tflie and qnnpack --- .../schema/mct_current_schema.py | 2 +- .../target_platform_capabilities/schema/v1.py | 4 + .../targetplatform2framework/attach2keras.py | 12 +- .../attach2pytorch.py | 9 +- .../tpc_models/qnnpack_tpc/v1/tp_model.py | 6 - .../tpc_models/tflite_tpc/latest/__init__.py | 2 +- .../tpc_models/tflite_tpc/v1/tp_model.py | 112 ++++++++--------- .../tpc_models/tflite_tpc/v1/tpc_keras.py | 118 ------------------ 8 files changed, 76 insertions(+), 189 deletions(-) delete mode 100644 model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py diff --git a/model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py b/model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py index 80bf1ce5e..c751049fe 100644 --- a/model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py +++ b/model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py @@ -7,6 +7,6 @@ QuantizationConfigOptions = schema.QuantizationConfigOptions OperatorsSetBase = schema.OperatorsSetBase OperatorsSet = schema.OperatorsSet -OperatorSetConcat= schema.OperatorSetConcat +OperatorSetConcat = schema.OperatorSetConcat Fusing = schema.Fusing TargetPlatformModel = schema.TargetPlatformModel diff --git a/model_compression_toolkit/target_platform_capabilities/schema/v1.py b/model_compression_toolkit/target_platform_capabilities/schema/v1.py index 5e60f4860..ac3fc3516 100644 --- a/model_compression_toolkit/target_platform_capabilities/schema/v1.py +++ b/model_compression_toolkit/target_platform_capabilities/schema/v1.py @@ -66,6 +66,7 @@ class OperatorSetNames(Enum): OPSET_DROPOUT = "Dropout" OPSET_SPLIT_CHUNK = "SplitChunk" OPSET_MAXPOOL = "MaxPool" + OPSET_AVGPOOL = "AvgPool" OPSET_SIZE = "Size" OPSET_SHAPE = "Shape" OPSET_EQUAL = "Equal" @@ -76,6 +77,9 @@ class OperatorSetNames(Enum): OPSET_CROPPING2D = "Cropping2D" OPSET_ZERO_PADDING2d = "ZeroPadding2D" OPSET_CAST = "Cast" + OPSET_RESIZE = "Resize" + OPSET_PAD = "Pad" + OPSET_FOLD = "Fold" OPSET_STRIDED_SLICE = "StridedSlice" OPSET_SSD_POST_PROCESS = "SSDPostProcess" diff --git a/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py b/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py index b56dcfd99..513bf81c7 100644 --- a/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py +++ b/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py @@ -23,11 +23,11 @@ if version.parse(tf.__version__) >= version.parse("2.13"): from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \ - MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \ - Conv2DTranspose, Identity, Concatenate, BatchNormalization, Minimum, Maximum, Softmax + MaxPooling2D, AveragePooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \ + Conv2DTranspose, Concatenate, BatchNormalization, Minimum, Maximum, Softmax else: from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \ - MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \ + MaxPooling2D, AveragePooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \ Conv2DTranspose, Concatenate, BatchNormalization, Minimum, Maximum, Softmax from model_compression_toolkit import DefaultDict @@ -81,8 +81,12 @@ def __init__(self): OperatorSetNames.OPSET_SQUEEZE.value: [tf.squeeze], OperatorSetNames.OPSET_DROPOUT.value: [Dropout], OperatorSetNames.OPSET_SPLIT_CHUNK.value: [tf.split], - OperatorSetNames.OPSET_MAXPOOL.value: [MaxPooling2D], + OperatorSetNames.OPSET_MAXPOOL.value: [MaxPooling2D, tf.nn.avg_pool2d], + OperatorSetNames.OPSET_AVGPOOL.value: [AveragePooling2D], OperatorSetNames.OPSET_SIZE.value: [tf.size], + OperatorSetNames.OPSET_RESIZE.value: [tf.image.resize], + OperatorSetNames.OPSET_PAD.value: [tf.pad], + OperatorSetNames.OPSET_FOLD.value: [tf.space_to_batch_nd], OperatorSetNames.OPSET_SHAPE.value: [tf.shape, tf.compat.v1.shape], OperatorSetNames.OPSET_EQUAL.value: [tf.math.equal], OperatorSetNames.OPSET_ARGMAX.value: [tf.math.argmax], diff --git a/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py b/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py index f64778248..b8d22c6fe 100644 --- a/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py +++ b/model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py @@ -20,8 +20,8 @@ chunk, unbind, topk, gather, equal, transpose, permute, argmax, squeeze, multiply, subtract, minimum, \ maximum, softmax from torch.nn import Conv2d, Linear, ConvTranspose2d, MaxPool2d, BatchNorm2d, Dropout, Flatten, Hardtanh, ReLU, ReLU6, \ - PReLU, SiLU, Sigmoid, Tanh, Hardswish, Hardsigmoid, LeakyReLU, GELU, LogSoftmax, Softmax, ELU -from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, hardsigmoid, leaky_relu, gelu + PReLU, SiLU, Sigmoid, Tanh, Hardswish, Hardsigmoid, LeakyReLU, GELU, LogSoftmax, Softmax, ELU, AvgPool2d +from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, hardsigmoid, leaky_relu, gelu, fold import torch.nn.functional as F from model_compression_toolkit import DefaultDict @@ -31,7 +31,6 @@ from model_compression_toolkit.target_platform_capabilities.target_platform import LayerFilterParams, Eq from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \ AttachTpcToFramework -from model_compression_toolkit.verify_packages import FOUND_SONY_CUSTOM_LAYERS class AttachTpcToPytorch(AttachTpcToFramework): @@ -76,7 +75,11 @@ def __init__(self): OperatorSetNames.OPSET_DROPOUT.value: [Dropout, dropout], OperatorSetNames.OPSET_SPLIT_CHUNK.value: [split, chunk], OperatorSetNames.OPSET_MAXPOOL.value: [MaxPool2d, F.max_pool2d], + OperatorSetNames.OPSET_AVGPOOL.value: [AvgPool2d, F.avg_pool2d], OperatorSetNames.OPSET_SIZE.value: [torch.Tensor.size], + OperatorSetNames.OPSET_RESIZE.value: [torch.Tensor.resize], + OperatorSetNames.OPSET_PAD.value: [F.pad], + OperatorSetNames.OPSET_FOLD.value: [fold, F.fold], OperatorSetNames.OPSET_SHAPE.value: [torch.Tensor.shape], OperatorSetNames.OPSET_EQUAL.value: [equal], OperatorSetNames.OPSET_ARGMAX.value: [argmax], diff --git a/model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py b/model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py index 8045f530b..8b140f6b9 100644 --- a/model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py +++ b/model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py @@ -155,12 +155,6 @@ def generate_tp_model(default_config: OpQuantizationConfig, relu = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_RELU.value) relu6 = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_RELU.value) - # TODO: missing this operator from relu in attach2fw which is part of the old Keras TPC. - # tp.LayerFilterParams(ReLU, negative_slope=0.0), - # need to figure out how to allow to retrive out built-int fw TPCs with a custom layer mapping - # (preferably without changing the existing API for any user who used them). - # consider adding a test for qnnpack and tflite tpcs - hard_tanh = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_HARD_TANH.value) linear = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_FULLY_CONNECTED.value) diff --git a/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/latest/__init__.py b/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/latest/__init__.py index 2ca877a70..6525c6aa6 100644 --- a/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/latest/__init__.py +++ b/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/latest/__init__.py @@ -15,7 +15,7 @@ from model_compression_toolkit.verify_packages import FOUND_TORCH, FOUND_TF from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tp_model import get_tp_model, generate_tp_model, get_op_quantization_configs if FOUND_TF: - from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tpc_keras import get_keras_tpc as get_keras_tpc_latest + from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tp_model import get_keras_tpc as get_keras_tpc_latest from model_compression_toolkit.target_platform_capabilities.tpc_models.get_target_platform_capabilities import \ get_tpc_model as generate_keras_tpc if FOUND_TORCH: diff --git a/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py b/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py index bb05bd2c6..7713415e5 100644 --- a/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py +++ b/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py @@ -144,76 +144,76 @@ def generate_tp_model(default_config: OpQuantizationConfig, operator_set = [] fusing_patterns = [] - operator_set.append(schema.OperatorsSet("NoQuantization", - default_configuration_options.clone_and_edit( - quantization_preserving=True))) - - fc = schema.OperatorsSet(schema.OperatorSetNames.OPSET_FULLY_CONNECTED.value, - default_configuration_options.clone_and_edit_weight_attribute( - weights_per_channel_threshold=False)) - sigmoid = schema.OperatorsSet(schema.OperatorSetNames.OPSET_SIGMOID.value, - default_configuration_options.clone_and_edit_weight_attribute( + quant_preserving = default_configuration_options.clone_and_edit(quantization_preserving=True) + + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_UNSTACK.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_TRANSPOSE.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_GATHER.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_RESHAPE.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_MAXPOOL.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_AVGPOOL.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_STRIDED_SLICE.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_CONCATENATE.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_MUL.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_MIN.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_MAX.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_ZERO_PADDING2d.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_RESIZE.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_PAD.value, qc_options=quant_preserving)) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_FOLD.value, qc_options=quant_preserving)) + + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_L2NORM, + qc_options=default_configuration_options.clone_and_edit( + fixed_zero_point=0, fixed_scale=1 / 128))) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_LOG_SOFTMAX, + qc_options=default_configuration_options.clone_and_edit( + fixed_zero_point=127, fixed_scale=16 / 256))) + operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_SOFTMAX, + qc_options=default_configuration_options.clone_and_edit( + fixed_zero_point=-128, fixed_scale=1 / 256))) + + sigmoid = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_SIGMOID.value, + qc_options=default_configuration_options.clone_and_edit_weight_attribute( + weights_per_channel_threshold=False)) + tanh = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_TANH.value, + qc_options=default_configuration_options.clone_and_edit( + fixed_zero_point=-128, fixed_scale=1 / 256)) + fc = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_FULLY_CONNECTED.value, + qc_options=default_configuration_options.clone_and_edit_weight_attribute( weights_per_channel_threshold=False)) - tanh = schema.OperatorsSet(schema.OperatorSetNames.OPSET_TANH.value, default_configuration_options.clone_and_edit( - fixed_zero_point=-128, fixed_scale=1 / 256)) - squeeze = schema.OperatorsSet("Squeeze", + squeeze = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_SQUEEZE, qc_options=default_configuration_options.clone_and_edit( quantization_preserving=True)) - conv2d = schema.OperatorsSet(schema.OperatorSetNames.OPSET_CONV.value) - relu = schema.OperatorsSet(schema.OperatorSetNames.OPSET_RELU.value) - relu6 = schema.OperatorsSet(schema.OperatorSetNames.OPSET_RELU6.value) - batch_norm = schema.OperatorsSet(schema.OperatorSetNames.OPSET_BATCH_NORM.value) - add = schema.OperatorsSet(schema.OperatorSetNames.OPSET_ADD.value) - - # TODO: in the old TPC the following operator sets were defined in the tflite TPC but it requires custom opset - # implementation in the attach2fw side - # FW: - # tp.OperationsSetToLayers("L2Normalization", [tp.LayerFilterParams(torch.nn.functional.normalize, Eq('p', 2) | Eq('p', None))]) - # tp.OperationsSetToLayers("LogSoftmax", [torch.nn.LogSoftmax]) - # tp.OperationsSetToLayers("Elu", [torch.nn.ELU, torch.nn.functional.elu]) - # tp.OperationsSetToLayers("Softmax", [torch.nn.Softmax, torch.nn.functional.softmax]) - # HardTanh: - # tp.LayerFilterParams(torch.nn.Hardtanh, min_val=0, max_val=6), - # tp.LayerFilterParams(torch.nn.functional.hardtanh, min_val=0, max_val=6) - # CFG: - # operator_set.append(schema.OperatorsSet("L2Normalization", - # default_configuration_options.clone_and_edit( - # fixed_zero_point=0, fixed_scale=1 / 128))) - # operator_set.append(schema.OperatorsSet("LogSoftmax", - # default_configuration_options.clone_and_edit( - # fixed_zero_point=127, fixed_scale=16 / 256))) - # operator_set.append(schema.OperatorsSet("Softmax", - # default_configuration_options.clone_and_edit( - # fixed_zero_point=-128, fixed_scale=1 / 256))) - # elu = schema.OperatorsSet("Elu") - # hardtanh - same as relu and part of relu fuses - # . - # activations_to_fuse = schema.OperatorSetConcat([relu, elu]) - # operator_set.extend([fc, conv2d, relu, elu, batch_norm, bias_add, add, squeeze]) - - activations_to_fuse = schema.OperatorSetConcat([relu, relu6]) - kernel = schema.OperatorSetConcat([conv2d, fc]) - - operator_set.extend([fc, conv2d, relu, relu6, tanh, sigmoid, batch_norm, add, squeeze]) + conv2d = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_CONV.value) + relu = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_RELU.value) + relu6 = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_RELU6.value) + elu = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_ELU.value) + batch_norm = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_BATCH_NORM.value) + add = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_ADD.value) + bias_add = schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_ADD_BIAS.value) + + kernel = schema.OperatorSetConcat(operators_set=[conv2d, fc]) + activations_to_fuse = schema.OperatorSetConcat(operators_set=[relu, elu]) + + operator_set.extend([fc, conv2d, relu, relu6, tanh, sigmoid, batch_norm, add, bias_add, elu, squeeze]) # ------------------- # # Fusions # ------------------- # # Source: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/grappler/optimizers/remapper - # TODO: bias_add was removed since there is no connection to bias-add operator on the fw side - # fusing_patterns.append(schema.Fusing((kernel, bias_add))) - # fusing_patterns.append(schema.Fusing((kernel, bias_add, activations_to_fuse))) - fusing_patterns.append(schema.Fusing((conv2d, batch_norm, activations_to_fuse))) - fusing_patterns.append(schema.Fusing((conv2d, squeeze, activations_to_fuse))) - fusing_patterns.append(schema.Fusing((batch_norm, activations_to_fuse))) - fusing_patterns.append(schema.Fusing((batch_norm, add, activations_to_fuse))) + fusing_patterns.append(schema.Fusing(operator_groups=(kernel, bias_add))) + fusing_patterns.append(schema.Fusing(operator_groups=(kernel, bias_add, activations_to_fuse))) + fusing_patterns.append(schema.Fusing(operator_groups=(conv2d, batch_norm, activations_to_fuse))) + fusing_patterns.append(schema.Fusing(operator_groups=(conv2d, squeeze, activations_to_fuse))) + fusing_patterns.append(schema.Fusing(operator_groups=(batch_norm, activations_to_fuse))) + fusing_patterns.append(schema.Fusing(operator_groups=(batch_norm, add, activations_to_fuse))) # 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, operator_set=tuple(operator_set), diff --git a/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py b/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py deleted file mode 100644 index a5101f819..000000000 --- a/model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py +++ /dev/null @@ -1,118 +0,0 @@ -# Copyright 2022 Sony Semiconductor Israel, Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -import tensorflow as tf -from packaging import version - -import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema -from model_compression_toolkit.defaultdict import DefaultDict -from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, BIAS - -if version.parse(tf.__version__) >= version.parse("2.13"): - from keras.src.layers import Conv2D, Dense, Reshape, ZeroPadding2D, AveragePooling2D, Activation, DepthwiseConv2D, \ - MaxPooling2D, ReLU, Add, Softmax, Concatenate, Multiply, Maximum, Minimum, BatchNormalization -else: - from keras.layers import Conv2D, Dense, Reshape, ZeroPadding2D, AveragePooling2D, Activation, DepthwiseConv2D, \ - MaxPooling2D, ReLU, Add, Softmax, Concatenate, Multiply, Maximum, Minimum, BatchNormalization - -from tensorflow.python.keras.layers.core import SlicingOpLambda -from tensorflow.python.ops.image_ops_impl import ResizeMethod -from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attribute_filter import \ - Eq - -from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tp_model import get_tp_model -import model_compression_toolkit as mct -from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1 import __version__ as TPC_VERSION - -tp = mct.target_platform - - -def get_keras_tpc() -> tp.TargetPlatformCapabilities: - """ - get a Keras TargetPlatformCapabilities object with default operation sets to layers mapping. - Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel. - """ - tflite_tp_model = get_tp_model() - return generate_keras_tpc(name='tflite_keras', tp_model=tflite_tp_model) - - -def generate_keras_tpc(name: str, tp_model: schema.TargetPlatformModel): - """ - Generates a TargetPlatformCapabilities object with default operation sets to layers mapping. - - Args: - name: Name of the TargetPlatformCapabilities. - tp_model: TargetPlatformModel object. - - Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel. - """ - - keras_tpc = tp.TargetPlatformCapabilities(tp_model) - - with keras_tpc: - tp.OperationsSetToLayers("NoQuantization", [AveragePooling2D, - tf.nn.avg_pool2d, - Concatenate, - tf.concat, - MaxPooling2D, - Multiply, - tf.multiply, - Reshape, - tf.reshape, - tp.LayerFilterParams(tf.image.resize, - method=ResizeMethod.BILINEAR), - tf.nn.space_to_depth, - ZeroPadding2D, - tf.unstack, - tf.gather, - tf.compat.v1.batch_to_space_nd, - tf.space_to_batch_nd, - tf.transpose, - tf.maximum, - Maximum, - tf.minimum, - Minimum, - tf.pad, - tf.slice, - SlicingOpLambda]) - - tp.OperationsSetToLayers("FullyConnected", [Dense], - # we provide attributes mapping that maps each layer type in the operations set - # that has weights attributes with provided quantization config (in the tp model) to - # its framework-specific attribute name. - # note that a DefaultDict should be provided if not all the layer types in the - # operation set are provided separately in the mapping. - attr_mapping={ - KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL), - BIAS_ATTR: DefaultDict(default_value=BIAS)}) - tp.OperationsSetToLayers("L2Normalization", [tf.math.l2_normalize]) - tp.OperationsSetToLayers("LogSoftmax", [tf.nn.log_softmax]) - tp.OperationsSetToLayers("Tanh", [tf.nn.tanh, tp.LayerFilterParams(Activation, activation="tanh")]) - tp.OperationsSetToLayers("Softmax", [tf.nn.softmax, - Softmax, - tp.LayerFilterParams(Activation, activation="softmax")]) - tp.OperationsSetToLayers("Logistic", [tf.sigmoid, tp.LayerFilterParams(Activation, activation="sigmoid")]) - - tp.OperationsSetToLayers("Conv2d", [Conv2D, DepthwiseConv2D]) - tp.OperationsSetToLayers("Relu", [tf.nn.relu, - tf.nn.relu6, - tp.LayerFilterParams(ReLU, Eq("max_value", None) | Eq("max_value", 6)), - tp.LayerFilterParams(Activation, activation="relu")]) - tp.OperationsSetToLayers("Elu", [tf.nn.elu, tp.LayerFilterParams(Activation, activation="elu")]) - tp.OperationsSetToLayers("BatchNorm", [BatchNormalization, tf.nn.batch_normalization]) - tp.OperationsSetToLayers("Squeeze", [tf.squeeze]) - tp.OperationsSetToLayers("BiasAdd", [tf.nn.bias_add]) - tp.OperationsSetToLayers("Add", [tf.add, Add]) - - return keras_tpc