Skip to content

Commit

Permalink
Add "identity" to NoQuantization in TPCs
Browse files Browse the repository at this point in the history
Remove BatchNorm from NoQuantization
  • Loading branch information
elad-c committed Apr 11, 2024
1 parent e295990 commit 883ec83
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
if version.parse(tf.__version__) >= version.parse("2.12"):
from keras.src.layers import Identity
else:
from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
Conv2DTranspose, Identity

from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1.tp_model import get_tp_model
import model_compression_toolkit as mct
Expand Down Expand Up @@ -61,7 +63,8 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
no_quant_list = [tf.identity,
Reshape,
tf.reshape,
Permute,
tf.transpose,
Expand All @@ -84,6 +87,9 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if version.parse(tf.__version__) >= version.parse("2.12"):
no_quant_list.append(Identity)

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
gather, equal, transpose, permute, argmax, squeeze
from torch.nn import Conv2d, Linear, BatchNorm2d, ConvTranspose2d
from torch.nn import Conv2d, Linear, ConvTranspose2d, Identity
from torch.nn import Dropout, Flatten, Hardtanh
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
Expand Down Expand Up @@ -64,15 +64,15 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
BIAS_ATTR: DefaultDict(default_value=BIAS)}

with pytorch_tpc:
tp.OperationsSetToLayers("NoQuantization", [Dropout,
tp.OperationsSetToLayers("NoQuantization", [Identity,
Dropout,
Flatten,
dropout,
flatten,
split,
operator.getitem,
reshape,
unsqueeze,
BatchNorm2d,
chunk,
unbind,
torch.Tensor.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
if version.parse(tf.__version__) >= version.parse("2.12"):
from keras.src.layers import Identity
else:
from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Expand Down Expand Up @@ -61,7 +63,8 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
no_quant_list = [tf.identity,
Reshape,
tf.reshape,
Permute,
tf.transpose,
Expand All @@ -84,6 +87,9 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if version.parse(tf.__version__) >= version.parse("2.12"):
no_quant_list.append(Identity)

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
gather, equal, transpose, permute, argmax, squeeze
from torch.nn import Conv2d, Linear, BatchNorm2d, ConvTranspose2d
from torch.nn import Conv2d, Linear, ConvTranspose2d, Identity
from torch.nn import Dropout, Flatten, Hardtanh
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
Expand Down Expand Up @@ -64,15 +64,15 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
BIAS_ATTR: DefaultDict(default_value=BIAS)}

with pytorch_tpc:
tp.OperationsSetToLayers("NoQuantization", [Dropout,
tp.OperationsSetToLayers("NoQuantization", [Identity,
Dropout,
Flatten,
dropout,
flatten,
split,
operator.getitem,
reshape,
unsqueeze,
BatchNorm2d,
chunk,
unbind,
torch.Tensor.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
if version.parse(tf.__version__) >= version.parse("2.12"):
from keras.src.layers import Identity
else:
from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
Conv2DTranspose, Identity

from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_pot.tp_model import get_tp_model
import model_compression_toolkit as mct
Expand Down Expand Up @@ -61,7 +63,8 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
no_quant_list = [tf.identity,
Reshape,
tf.reshape,
Permute,
tf.transpose,
Expand All @@ -84,6 +87,9 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if version.parse(tf.__version__) >= version.parse("2.12"):
no_quant_list.append(Identity)

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
gather, equal, transpose, permute, argmax, squeeze
from torch.nn import Conv2d, Linear, BatchNorm2d, ConvTranspose2d
from torch.nn import Conv2d, Linear, ConvTranspose2d, Identity
from torch.nn import Dropout, Flatten, Hardtanh
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
Expand Down Expand Up @@ -65,15 +65,15 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
BIAS_ATTR: DefaultDict(default_value=BIAS)}

with pytorch_tpc:
tp.OperationsSetToLayers("NoQuantization", [Dropout,
tp.OperationsSetToLayers("NoQuantization", [Identity,
Dropout,
Flatten,
dropout,
flatten,
split,
operator.getitem,
reshape,
unsqueeze,
BatchNorm2d,
chunk,
unbind,
torch.Tensor.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
if version.parse(tf.__version__) >= version.parse("2.12"):
from keras.src.layers import Identity
else:
from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
Conv2DTranspose, Identity

from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2.tp_model import get_tp_model
import model_compression_toolkit as mct
Expand Down Expand Up @@ -62,7 +64,8 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
no_quant_list = [tf.identity,
Reshape,
tf.reshape,
Permute,
tf.transpose,
Expand All @@ -85,6 +88,9 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if version.parse(tf.__version__) >= version.parse("2.12"):
no_quant_list.append(Identity)

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
gather, equal, transpose, permute, argmax, squeeze
from torch.nn import Conv2d, Linear, BatchNorm2d, ConvTranspose2d
from torch.nn import Conv2d, Linear, ConvTranspose2d
from torch.nn import Dropout, Flatten, Hardtanh
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
Expand Down Expand Up @@ -73,7 +73,6 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
operator.getitem,
reshape,
unsqueeze,
BatchNorm2d,
chunk,
unbind,
torch.Tensor.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
if version.parse(tf.__version__) >= version.parse("2.12"):
from keras.src.layers import Identity
else:
from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
Conv2DTranspose
Conv2DTranspose, Identity

from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2_lut.tp_model import get_tp_model
import model_compression_toolkit as mct
Expand Down Expand Up @@ -61,7 +63,8 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
no_quant_list = [tf.identity,
Reshape,
tf.reshape,
Permute,
tf.transpose,
Expand All @@ -84,6 +87,9 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if version.parse(tf.__version__) >= version.parse("2.12"):
no_quant_list.append(Identity)

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
gather, equal, transpose, permute, argmax, squeeze
from torch.nn import Conv2d, Linear, BatchNorm2d, ConvTranspose2d
from torch.nn import Conv2d, Linear, ConvTranspose2d
from torch.nn import Dropout, Flatten, Hardtanh
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
Expand Down Expand Up @@ -72,7 +72,6 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
operator.getitem,
reshape,
unsqueeze,
BatchNorm2d,
chunk,
unbind,
torch.Tensor.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
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
if version.parse(tf.__version__) >= version.parse("2.12"):
from keras.src.layers import Identity
else:
from keras.layers import Conv2D, Dense, Reshape, ZeroPadding2D, AveragePooling2D, Activation, DepthwiseConv2D, \
MaxPooling2D, ReLU, Add, Softmax, Concatenate, Multiply, Maximum, Minimum, BatchNormalization
MaxPooling2D, ReLU, Add, Softmax, Concatenate, Multiply, Maximum, Minimum, BatchNormalization, Identity

from tensorflow.python.keras.layers.core import SlicingOpLambda
from tensorflow.python.ops.image_ops_impl import ResizeMethod
Expand Down Expand Up @@ -62,31 +64,37 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
version=TPC_VERSION)

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])
no_quant_list = [tf.identity,
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]

if version.parse(tf.__version__) >= version.parse("2.12"):
no_quant_list.append(Identity)

tp.OperationsSetToLayers("NoQuantization", no_quant_list)

tp.OperationsSetToLayers("FullyConnected", [Dense],
# we provide attributes mapping that maps each layer type in the operations set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
# ==============================================================================
import torch
from torch.nn import AvgPool2d, MaxPool2d
from torch.nn import AvgPool2d, MaxPool2d, Identity
from torch.nn.functional import avg_pool2d, max_pool2d, interpolate

from model_compression_toolkit.defaultdict import DefaultDict
Expand Down Expand Up @@ -51,7 +51,8 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
version=TPC_VERSION)

with pytorch_tpc:
tp.OperationsSetToLayers("NoQuantization", [AvgPool2d,
tp.OperationsSetToLayers("NoQuantization", [Identity,
AvgPool2d,
avg_pool2d,
torch.cat,
torch.concat,
Expand Down
Loading

0 comments on commit 883ec83

Please sign in to comment.