Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Fix the logic for QuantizeConvert MatcherPass (#33271)

Anoob9c4a2eb9ad3b
diff --git a/src/frontends/tensorflow_lite/src/tflite_transformations/tflite_quantize_resolver.cpp b/src/frontends/tensorflow_lite/src/tflite_transformations/tflite_quantize_resolver.cpp
index c8267bfd340..6304fceeaf9 100644
--- a/src/frontends/tensorflow_lite/src/tflite_transformations/tflite_quantize_resolver.cpp
+++ b/src/frontends/tensorflow_lite/src/tflite_transformations/tflite_quantize_resolver.cpp
@@ -6,6 +6,7 @@
#include <memory>
+#include "openvino/core/rt_info.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
@@ -45,13 +46,53 @@ pass::TFLQuantizeConvert::TFLQuantizeConvert() {
auto tfl_quantize = ov::as_type_ptr<TFLQuantize>(tfl_quantize_node);
if (!tfl_quantize)
return false;
- tfl_quantize->set_type(type);
- convert->output(0).replace(tfl_quantize->output(0));
- return true;
+
+ const auto replace_f32_convert = [](const std::shared_ptr<v0::Convert>& consumer_convert,
+ const std::shared_ptr<TFLQuantize>& producer) -> bool {
+ if (!consumer_convert || consumer_convert->get_destination_type() != element::f32)
+ return false;
+ consumer_convert->input(0).replace_source_output(producer->output(0));
+ consumer_convert->output(0).replace(producer->output(0));
+ ov::copy_runtime_info(consumer_convert, producer);
+ return true;
+ };
+
+ auto output_targets = tfl_quantize->get_output_target_inputs(0);
+
+ // Matcher guarantees at least one Convert consumer for this TFLQuantize.
+ if (output_targets.size() == 1) {
+ tfl_quantize->set_type(type);
+ if (!replace_f32_convert(convert, tfl_quantize))
+ return false;
+ tfl_quantize->set_friendly_name(convert->get_friendly_name());
+ return true;
+ }
+
+ // If there are multiple consumers of TFLQuantize, we cannot just change its output type to f32.
+ auto new_tfl_quantize = std::make_shared<TFLQuantize>(tfl_quantize->input_value(0),
+ tfl_quantize->get_info(),
+ tfl_quantize->get_original_type());
+ new_tfl_quantize->set_type(type);
+ // Seed new node with original TFLQuantize RT info before replacing consumers.
+ ov::copy_runtime_info(tfl_quantize, new_tfl_quantize);
+ bool replaced = false;
+ std::shared_ptr<v0::Convert> first_replaced_convert;
+ for (const auto& target : output_targets) {
+ auto consumer_convert = ov::as_type_ptr<v0::Convert>(target.get_node()->shared_from_this());
+ if (replace_f32_convert(consumer_convert, new_tfl_quantize)) {
+ if (!first_replaced_convert)
+ first_replaced_convert = consumer_convert;
+ replaced = true;
+ }
+ }
+ if (first_replaced_convert) {
+ new_tfl_quantize->set_friendly_name(first_replaced_convert->get_friendly_name());
+ }
+ return replaced;
};
auto m =
- std::make_shared<pattern::Matcher>(convert_label, "ov::frontend::tensorflow_lite::pass::TFLQuantizeResolver");
+ std::make_shared<pattern::Matcher>(convert_label, "ov::frontend::tensorflow_lite::pass::TFLQuantizeConvert");
register_matcher(m, callback);
}
diff --git a/tests/layer_tests/tensorflow_lite_tests/test_tfl_QuantizeMultiOutputs.py b/tests/layer_tests/tensorflow_lite_tests/test_tfl_QuantizeMultiOutputs.py
new file mode 100644
index 00000000000..c7c834ece38
--- /dev/null
+++ b/tests/layer_tests/tensorflow_lite_tests/test_tfl_QuantizeMultiOutputs.py
@@ -0,0 +1,183 @@
+# Copyright (C) 2018-2026 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import os
+import copy
+import pytest
+import numpy as np
+import tensorflow as tf
+from tensorflow.lite.python import schema_py_generated as schema_fb
+from tensorflow.lite.tools import flatbuffer_utils as utils
+
+from common.tflite_layer_test_class import TFLiteLayerTest
+
+SCALE = 0.0039
+ZERO_POINT = 0
+
+
+class TestTFLiteQuantizeMultiOutputs(TFLiteLayerTest):
+ """Test that TFLQuantizeConvert pass correctly handles quantize with multiple Convert consumers"""
+
+ inputs = ["Input_0", "Input_1"]
+ outputs = ["Result_0", "Result_1"]
+ allowed_ops = ['ADD'] # After _strip_quantize_wrappers(), only ADD ops remain in the final model
+
+ def make_model(self, params):
+ tf.compat.v1.reset_default_graph()
+ with tf.compat.v1.Session() as sess:
+ input_0 = tf.compat.v1.placeholder(tf.float32, [1, 8], name=self.inputs[0])
+ input_1 = tf.compat.v1.placeholder(tf.float32, [1, 8], name=self.inputs[1])
+
+ add_out = tf.add(input_0, input_1, name="add_inputs")
+ add_const_out = tf.add(add_out, tf.constant(10.0, shape=[1, 8], dtype=tf.float32), name="add_const_10")
+
+ tf.identity(add_out, name=self.outputs[0])
+ tf.identity(add_const_out, name=self.outputs[1])
+ net = sess.graph_def
+ return net
+
+ def produce_tflite_model(self, framework_model, save_path):
+ graph = tf.Graph()
+ with graph.as_default():
+ tf.import_graph_def(framework_model, name='')
+
+ with tf.compat.v1.Session(graph=graph) as sess:
+ input_tensors = [graph.get_tensor_by_name(f'{name}:0') for name in self.inputs]
+ output_tensors = [graph.get_tensor_by_name(f'{name}:0') for name in self.outputs]
+
+ converter = tf.compat.v1.lite.TFLiteConverter.from_session(sess, input_tensors, output_tensors)
+ converter.optimizations = [tf.lite.Optimize.DEFAULT]
+ converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
+ converter.inference_input_type = tf.uint8
+ converter.inference_output_type = tf.uint8
+
+ def representative_dataset():
+ for _ in range(128):
+ input_0 = np.random.uniform(0.0, 0.995, [1, 8]).astype(np.float32)
+ input_1 = np.random.uniform(0.0, 0.995, [1, 8]).astype(np.float32)
+ yield [input_0, input_1]
+
+ converter.representative_dataset = representative_dataset
+ tflite_model = converter.convert()
+
+ # Post-process to strip QUANTIZE wrapper ops and keep only ADD ops with quantized tensors
+ model_obj = utils.read_model_from_bytearray(tflite_model)
+ self._strip_quantize_wrappers(model_obj)
+ self._force_u8_quant_params(model_obj)
+
+ tflite_model_path = os.path.join(save_path, 'quantize_multi_outs.tflite')
+ utils.write_model(model_obj, tflite_model_path)
+
+ return tflite_model_path
+
+ def _force_u8_quant_params(self, model_obj):
+ """Force uint8 quantization parameters on all uint8 tensors."""
+ subgraph = model_obj.subgraphs[0]
+ for tensor in subgraph.tensors:
+ if tensor.type != schema_fb.TensorType.UINT8:
+ continue
+ if tensor.quantization is None:
+ tensor.quantization = schema_fb.QuantizationParametersT()
+ tensor.quantization.scale = [float(SCALE)]
+ tensor.quantization.zeroPoint = [int(ZERO_POINT)]
+ tensor.quantization.min = []
+ tensor.quantization.max = []
+
+ def _strip_quantize_wrappers(self, model_obj):
+ """Remove QUANTIZE ops, keeping only ADD operations with quantized tensors."""
+ subgraph = model_obj.subgraphs[0]
+ # Find all ADD operators
+ add_indices = []
+ for i, op in enumerate(subgraph.operators):
+ # Handle both deprecated and normal opcode fields
+ op_code = model_obj.operatorCodes[op.opcodeIndex]
+ code = op_code.builtinCode
+ if (code == schema_fb.BuiltinOperator.PLACEHOLDER_FOR_GREATER_OP_CODES and
+ op_code.deprecatedBuiltinCode != schema_fb.BuiltinOperator.PLACEHOLDER_FOR_GREATER_OP_CODES):
+ code = op_code.deprecatedBuiltinCode
+ if code == schema_fb.BuiltinOperator.ADD:
+ add_indices.append(i)
+
+ if len(add_indices) < 2:
+ raise RuntimeError("Expected at least two ADD operators")
+
+ # Keep the two ADD operations
+ add_inputs_op = copy.deepcopy(subgraph.operators[add_indices[0]])
+ add_const_op = copy.deepcopy(subgraph.operators[add_indices[1]])
+
+ # Wire them directly: first ADD uses model inputs, second ADD uses first's output
+ add_inputs_op.inputs = [subgraph.inputs[0], subgraph.inputs[1]]
+ add_const_op.inputs = [add_inputs_op.outputs[0], add_const_op.inputs[1]]
+
+ subgraph.operators = [add_inputs_op, add_const_op]
+
+ # Set model outputs to the ADD operation outputs
+ add_out_idx = add_inputs_op.outputs[0]
+ add_const_out_idx = add_const_op.outputs[0]
+ const_idx = add_const_op.inputs[1]
+ subgraph.outputs = [add_out_idx, add_const_out_idx]
+
+ # Name and quantize all relevant tensors
+ for idx, name in [
+ (subgraph.inputs[0], b"Input_0"),
+ (subgraph.inputs[1], b"Input_1"),
+ (add_out_idx, b"Result_0"),
+ (add_const_out_idx, b"Result_1"),
+ (const_idx, b"const_10"),
+ ]:
+ tensor = subgraph.tensors[idx]
+ tensor.name = name
+ tensor.type = schema_fb.TensorType.UINT8
+
+ # Set constant tensor as uint8 with value 10
+ const_tensor = subgraph.tensors[const_idx]
+ const_tensor.shape = [1, 8]
+ const_buffer = model_obj.buffers[const_tensor.buffer]
+ const_buffer.data = bytearray([10] * 8)
+
+ def _prepare_input(self, inputs_dict, generator=None):
+ """Generate uint8 input data matching model input type."""
+ for input_name, input_shape in inputs_dict.items():
+ inputs_dict[input_name] = np.random.randint(0, 256, size=input_shape, dtype=np.uint8)
+ return inputs_dict
+
+ def check_tflite_model_has_only_allowed_ops(self):
+ """
+ Validate that the modified subgraph contains only allowed operators.
+ This override is needed because _strip_quantize_wrappers() rewrites subgraph.operators
+ but does not prune operatorCodes, so the base validation would incorrectly check
+ all original opcodes instead of only the remaining operators.
+ This checks that all operators are in the allowed set, not exact count/order.
+ """
+ if self.allowed_ops is None:
+ return
+ BO = utils.schema_fb.BuiltinOperator
+ builtin_operators = {getattr(BO, name): name for name in dir(BO) if not name.startswith("_")}
+ model = utils.read_model(self.model_path)
+ subgraph = model.subgraphs[0]
+
+ # Collect opcodes actually used in subgraph.operators
+ op_names = []
+ for op in subgraph.operators:
+ op_code = model.operatorCodes[op.opcodeIndex]
+ code = op_code.builtinCode
+ if (code == BO.PLACEHOLDER_FOR_GREATER_OP_CODES and
+ op_code.deprecatedBuiltinCode != BO.PLACEHOLDER_FOR_GREATER_OP_CODES):
+ code = op_code.deprecatedBuiltinCode
+ op_names.append(builtin_operators[code])
+
+ # Check that all operators are in the allowed set (order/count agnostic)
+ allowed_set = set(self.allowed_ops) if not isinstance(self.allowed_ops, tuple) else set(self.allowed_ops[0])
+ actual_set = set(op_names)
+ assert actual_set == allowed_set, f"TFLite model contains unexpected ops. Expected only {allowed_set}, got {actual_set}"
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ def test_quantize_multi_outputs(self, ie_device, precision, temp_dir):
+ """
+ Test that quantized model with multiple outputs produces correct results.
+ This verifies that TFLQuantizeConvert pass correctly optimizes when quantize has
+ multiple Convert consumers, preserving output types and runtime info.
+ """
+ params = {}
+ self._test(ie_device, precision, temp_dir, params)