Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit febe097

Browse files
committed
Disable per-channel quantization for tflite
1 parent 4939274 commit febe097

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ usage: tflite2tensorflow
361361
[--optimizing_for_edgetpu]
362362
[--replace_prelu_and_minmax]
363363
[--disable_experimental_new_quantizer]
364+
[--disable_per_channel]
364365
[--optimizing_barracuda]
365366
[--locationids_of_the_terminating_output]
366367
@@ -467,6 +468,8 @@ optional arguments:
467468
--disable_experimental_new_quantizer
468469
Disable MLIRs new quantization feature during INT8 quantization
469470
in TensorFlowLite.
471+
--disable_per_channel
472+
Disable per-channel quantization for tflite.
470473
--optimizing_barracuda
471474
Generates ONNX by replacing Barracuda unsupported layers
472475
with standard layers. For example, GatherND.

schema/schema.fbs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ table Tensor {
220220
// Encodes `shape` with unknown dimensions. Unknown dimensions are
221221
// represented with -1.
222222
shape_signature:[int]; // Optional.
223+
224+
// If false, the rank or the number of tensor dimensions is unknown.
225+
// If false, "shape" must be [].
226+
has_rank: bool = false;
223227
}
224228

225229
// A list of builtin operators. Builtin operators are slightly faster than custom
@@ -386,6 +390,11 @@ enum BuiltinOperator : int32 {
386390
MULTINOMIAL = 149,
387391
GELU = 150,
388392
DYNAMIC_UPDATE_SLICE = 151,
393+
RELU_0_TO_1 = 152,
394+
UNSORTED_SEGMENT_PROD = 153,
395+
UNSORTED_SEGMENT_MAX = 154,
396+
UNSORTED_SEGMENT_SUM = 155,
397+
ATAN2 = 156
389398
}
390399
// LINT.ThenChange(nnapi_linter/linter.proto)
391400

@@ -508,10 +517,17 @@ union BuiltinOptions {
508517
BucketizeOptions,
509518
GeluOptions,
510519
DynamicUpdateSliceOptions,
520+
UnsortedSegmentProdOptions,
521+
UnsortedSegmentMaxOptions,
522+
UnsortedSegmentSumOptions,
523+
ATan2Options
511524
}
512525

526+
// LINT.IfChange
513527
enum Padding : byte { SAME, VALID }
528+
// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td)
514529

530+
// LINT.IfChange
515531
enum ActivationFunctionType : byte {
516532
NONE = 0,
517533
RELU = 1,
@@ -520,6 +536,7 @@ enum ActivationFunctionType : byte {
520536
TANH = 4,
521537
SIGN_BIT = 5,
522538
}
539+
// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td)
523540

524541
table Conv2DOptions {
525542
padding:Padding;
@@ -611,10 +628,12 @@ table BidirectionalSequenceRNNOptions {
611628
asymmetric_quantize_inputs:bool;
612629
}
613630

631+
// LINT.IfChange
614632
enum FullyConnectedOptionsWeightsFormat: byte {
615633
DEFAULT = 0,
616634
SHUFFLED4x16INT8 = 1,
617635
}
636+
// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td)
618637

619638
// An implementation of TensorFlow fully_connected (a.k.a Dense) layer.
620639
table FullyConnectedOptions {
@@ -667,12 +686,14 @@ table LocalResponseNormalizationOptions {
667686
beta:float;
668687
}
669688

689+
// LINT.IfChange
670690
enum LSTMKernelType : byte {
671691
// Full LSTM kernel which supports peephole and projection.
672692
FULL = 0,
673693
// Basic LSTM kernels. Equivalent to TensorFlow BasicLSTMCell.
674694
BASIC = 1,
675695
}
696+
// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td)
676697

677698
// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell
678699
table LSTMOptions {
@@ -972,12 +993,14 @@ table LeakyReluOptions {
972993
table SquaredDifferenceOptions {
973994
}
974995

996+
// LINT.IfChange
975997
enum MirrorPadMode : byte {
976998
// Doesn't include borders.
977999
REFLECT = 0,
9781000
// Includes borders.
9791001
SYMMETRIC = 1,
9801002
}
1003+
// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td)
9811004

9821005
table MirrorPadOptions {
9831006
mode:MirrorPadMode;
@@ -1109,6 +1132,19 @@ table GeluOptions {
11091132
table DynamicUpdateSliceOptions {
11101133
}
11111134

1135+
table UnsortedSegmentProdOptions {
1136+
}
1137+
1138+
table UnsortedSegmentMaxOptions {
1139+
}
1140+
1141+
table UnsortedSegmentSumOptions {
1142+
}
1143+
1144+
table ATan2Options {
1145+
}
1146+
1147+
11121148
// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a
11131149
// builtin, or a string if the operator is custom.
11141150
table OperatorCode {

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
setup(
1212
name="tflite2tensorflow",
1313
scripts=scripts,
14-
version="1.21.2",
14+
version="1.22.0",
1515
description="Generate saved_model, tfjs, tf-trt, EdgeTPU, CoreML, quantized tflite, ONNX, OpenVINO, Myriad Inference Engine blob and .pb from .tflite.",
1616
long_description=long_description,
1717
long_description_content_type="text/markdown",

tflite2tensorflow/tflite2tensorflow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5684,6 +5684,7 @@ def main():
56845684
parser.add_argument('--optimizing_for_edgetpu', action='store_true', help='Optimizing for edgetpu')
56855685
parser.add_argument('--replace_prelu_and_minmax', action='store_true', help='Replace prelu and minimum/maximum with each other')
56865686
parser.add_argument('--disable_experimental_new_quantizer', action='store_true', help='Disable MLIR\'s new quantization feature during INT8 quantization in TensorFlowLite.')
5687+
parser.add_argument('--disable_per_channel', action='store_true', help='Disable per-channel quantization for tflite')
56875688
parser.add_argument('--optimizing_barracuda', action='store_true', help='Generates ONNX by replacing Barracuda\'s unsupported layers with standard layers.')
56885689
parser.add_argument('--locationids_of_the_terminating_output', type=str, default='', help='A comma-separated list of location IDs to be used as output layers. Default: \'\'')
56895690
args = parser.parse_args()
@@ -5734,6 +5735,7 @@ def main():
57345735
optimizing_for_edgetpu = args.optimizing_for_edgetpu
57355736
replace_prelu_and_minmax = args.replace_prelu_and_minmax
57365737
use_experimental_new_quantizer = not args.disable_experimental_new_quantizer
5738+
use_per_channel = not args.disable_per_channel
57375739
optimizing_barracuda = args.optimizing_barracuda
57385740
locationids_of_the_terminating_output_tmp = args.locationids_of_the_terminating_output
57395741
locationids_of_the_terminating_output = None
@@ -6057,6 +6059,7 @@ def main():
60576059
try:
60586060
print(f'{Color.REVERCE}Dynamic Range Quantization started{Color.RESET}', '=' * 50)
60596061
converter = tf.lite.TFLiteConverter.from_saved_model(model_output_path)
6062+
converter._experimental_disable_per_channel = not use_per_channel
60606063
converter.optimizations = [tf.lite.Optimize.DEFAULT]
60616064
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
60626065
tflite_model = converter.convert()
@@ -6073,6 +6076,7 @@ def main():
60736076
try:
60746077
print(f'{Color.REVERCE}Weight Quantization started{Color.RESET}', '=' * 57)
60756078
converter = tf.lite.TFLiteConverter.from_saved_model(model_output_path)
6079+
converter._experimental_disable_per_channel = not use_per_channel
60766080
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
60776081
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
60786082
tflite_model = converter.convert()
@@ -6188,6 +6192,7 @@ def representative_dataset_gen():
61886192
print(f'{Color.REVERCE}Integer Quantization started{Color.RESET}', '=' * 56)
61896193
converter = tf.lite.TFLiteConverter.from_saved_model(model_output_path)
61906194
converter.experimental_new_quantizer = use_experimental_new_quantizer
6195+
converter._experimental_disable_per_channel = not use_per_channel
61916196
converter.optimizations = [tf.lite.Optimize.DEFAULT]
61926197
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8, tf.lite.OpsSet.SELECT_TF_OPS]
61936198
tflite_model = None
@@ -6217,6 +6222,7 @@ def representative_dataset_gen():
62176222
print(f'{Color.REVERCE}Full Integer Quantization started{Color.RESET}', '=' * 51)
62186223
converter = tf.lite.TFLiteConverter.from_saved_model(model_output_path)
62196224
converter.experimental_new_quantizer = use_experimental_new_quantizer
6225+
converter._experimental_disable_per_channel = not use_per_channel
62206226
converter.optimizations = [tf.lite.Optimize.DEFAULT]
62216227
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8, tf.lite.OpsSet.SELECT_TF_OPS]
62226228
inf_type = None

0 commit comments

Comments
 (0)