From ef73bb773df214f3f33f8e4ca7d276041106cad2 Mon Sep 17 00:00:00 2001 From: Dmitrii Agibov Date: Wed, 9 Nov 2022 11:23:50 +0000 Subject: MLIA-685 Warn about custom operators in SavedModel/Keras models - Add new error types for the TensorFlow Lite compatibility check - Try to detect custom operators in SavedModel/Keras models - Add warning to the advice about models with custom operators Change-Id: I2f65474eecf2788110acc43585fa300eda80e21b --- src/mlia/devices/cortexa/advice_generation.py | 38 ++++++++++++++++++++++++++- src/mlia/devices/cortexa/data_analysis.py | 32 ++++++++++++++-------- 2 files changed, 58 insertions(+), 12 deletions(-) (limited to 'src/mlia/devices') diff --git a/src/mlia/devices/cortexa/advice_generation.py b/src/mlia/devices/cortexa/advice_generation.py index 186f489..3d2f106 100644 --- a/src/mlia/devices/cortexa/advice_generation.py +++ b/src/mlia/devices/cortexa/advice_generation.py @@ -7,9 +7,11 @@ from mlia.core.advice_generation import advice_category from mlia.core.advice_generation import FactBasedAdviceProducer from mlia.core.common import AdviceCategory from mlia.core.common import DataItem +from mlia.devices.cortexa.data_analysis import ModelHasCustomOperators from mlia.devices.cortexa.data_analysis import ModelIsCortexACompatible from mlia.devices.cortexa.data_analysis import ModelIsNotCortexACompatible from mlia.devices.cortexa.data_analysis import ModelIsNotTFLiteCompatible +from mlia.devices.cortexa.data_analysis import TFLiteCompatibilityCheckFailed class CortexAAdviceProducer(FactBasedAdviceProducer): @@ -92,17 +94,25 @@ class CortexAAdviceProducer(FactBasedAdviceProducer): "The following operators are not natively " "supported by TensorFlow Lite: " f"{', '.join(data_item.flex_ops)}.", + "Using select TensorFlow operators in TensorFlow Lite model " + "requires special initialization of TFLiteConverter and " + "TensorFlow Lite run-time.", "Please refer to the TensorFlow documentation for more details.", + "Note, such models are not supported by the ML Inference Advisor.", ] ) if data_item.custom_ops: self.add_advice( [ - "The following operators are custom and not natively " + "The following operators appears to be custom and not natively " "supported by TensorFlow Lite: " f"{', '.join(data_item.custom_ops)}.", + "Using custom operators in TensorFlow Lite model " + "requires special initialization of TFLiteConverter and " + "TensorFlow Lite run-time.", "Please refer to the TensorFlow documentation for more details.", + "Note, such models are not supported by the ML Inference Advisor.", ] ) @@ -113,3 +123,29 @@ class CortexAAdviceProducer(FactBasedAdviceProducer): "Please refer to the table for more details.", ] ) + + @produce_advice.register + @advice_category(AdviceCategory.ALL, AdviceCategory.OPERATORS) + def handle_tflite_check_failed( + self, _data_item: TFLiteCompatibilityCheckFailed + ) -> None: + """Advice for the failed TensorFlow Lite compatibility checks.""" + self.add_advice( + [ + "Model could not be converted into TensorFlow Lite format.", + "Please refer to the table for more details.", + ] + ) + + @produce_advice.register + @advice_category(AdviceCategory.ALL, AdviceCategory.OPERATORS) + def handle_model_has_custom_operators( + self, _data_item: ModelHasCustomOperators + ) -> None: + """Advice for the models with custom operators.""" + self.add_advice( + [ + "Models with custom operators require special initialization " + "and currently are not supported by the ML Inference Advisor.", + ] + ) diff --git a/src/mlia/devices/cortexa/data_analysis.py b/src/mlia/devices/cortexa/data_analysis.py index 6a82dd0..04bc819 100644 --- a/src/mlia/devices/cortexa/data_analysis.py +++ b/src/mlia/devices/cortexa/data_analysis.py @@ -14,7 +14,6 @@ from mlia.core.data_analysis import FactExtractor from mlia.devices.cortexa.operators import CortexACompatibilityInfo from mlia.devices.cortexa.operators import Operator from mlia.nn.tensorflow.tflite_compat import TFLiteCompatibilityInfo -from mlia.nn.tensorflow.tflite_compat import TFLiteConversionErrorCode class CortexADataAnalyzer(FactExtractor): @@ -69,18 +68,19 @@ class CortexADataAnalyzer(FactExtractor): if data_item.compatible: return - custom_ops, flex_ops = [], [] - if data_item.conversion_errors: - custom_ops = data_item.unsupported_ops_by_code( - TFLiteConversionErrorCode.NEEDS_CUSTOM_OPS - ) - flex_ops = data_item.unsupported_ops_by_code( - TFLiteConversionErrorCode.NEEDS_FLEX_OPS + if data_item.conversion_failed_with_errors: + self.add_fact( + ModelIsNotTFLiteCompatible( + custom_ops=data_item.required_custom_ops, + flex_ops=data_item.required_flex_ops, + ) ) - self.add_fact( - ModelIsNotTFLiteCompatible(custom_ops=custom_ops, flex_ops=flex_ops) - ) + if data_item.check_failed_with_unknown_error: + self.add_fact(TFLiteCompatibilityCheckFailed()) + + if data_item.conversion_failed_for_model_with_custom_ops: + self.add_fact(ModelHasCustomOperators()) @dataclass @@ -116,3 +116,13 @@ class ModelIsNotTFLiteCompatible(Fact): custom_ops: list[str] | None = None flex_ops: list[str] | None = None + + +@dataclass +class TFLiteCompatibilityCheckFailed(Fact): + """TensorFlow Lite compatibility check failed by unknown reason.""" + + +@dataclass +class ModelHasCustomOperators(Fact): + """Model could not be loaded because it contains custom ops.""" -- cgit v1.2.1