Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/lite/python/analyzer.py: 38%

26 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2021 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""This tool analyzes a TensorFlow Lite graph.""" 

16 

17import os 

18 

19# pylint: disable=g-import-not-at-top 

20if not os.path.splitext(__file__)[0].endswith( 

21 os.path.join("tflite_runtime", "analyzer")): 

22 # This file is part of tensorflow package. 

23 from tensorflow.lite.python import wrap_toco 

24 from tensorflow.lite.python.analyzer_wrapper import _pywrap_analyzer_wrapper as _analyzer_wrapper 

25 from tensorflow.python.util.tf_export import tf_export as _tf_export 

26else: 

27 # This file is part of tflite_runtime package. 

28 from tflite_runtime import _pywrap_analyzer_wrapper as _analyzer_wrapper 

29 

30 def _tf_export(*x, **kwargs): 

31 del x, kwargs 

32 return lambda x: x 

33 

34 

35@_tf_export("lite.experimental.Analyzer") 

36class ModelAnalyzer(): 

37 """Provides a collection of TFLite model analyzer tools. 

38 

39 Example: 

40 

41 ```python 

42 model = tf.keras.applications.MobileNetV3Large() 

43 fb_model = tf.lite.TFLiteConverterV2.from_keras_model(model).convert() 

44 tf.lite.experimental.Analyzer.analyze(model_content=fb_model) 

45 # === TFLite ModelAnalyzer === 

46 # 

47 # Your TFLite model has ‘1’ subgraph(s). In the subgraph description below, 

48 # T# represents the Tensor numbers. For example, in Subgraph#0, the MUL op 

49 # takes tensor #0 and tensor #19 as input and produces tensor #136 as output. 

50 # 

51 # Subgraph#0 main(T#0) -> [T#263] 

52 # Op#0 MUL(T#0, T#19) -> [T#136] 

53 # Op#1 ADD(T#136, T#18) -> [T#137] 

54 # Op#2 CONV_2D(T#137, T#44, T#93) -> [T#138] 

55 # Op#3 HARD_SWISH(T#138) -> [T#139] 

56 # Op#4 DEPTHWISE_CONV_2D(T#139, T#94, T#24) -> [T#140] 

57 # ... 

58 ``` 

59 

60 WARNING: Experimental interface, subject to change. 

61 """ 

62 

63 @staticmethod 

64 def analyze(model_path=None, 

65 model_content=None, 

66 gpu_compatibility=False, 

67 **kwargs): 

68 """Analyzes the given tflite_model with dumping model structure. 

69 

70 This tool provides a way to understand users' TFLite flatbuffer model by 

71 dumping internal graph structure. It also provides additional features 

72 like checking GPU delegate compatibility. 

73 

74 WARNING: Experimental interface, subject to change. 

75 The output format is not guaranteed to stay stable, so don't 

76 write scripts to this. 

77 

78 Args: 

79 model_path: TFLite flatbuffer model path. 

80 model_content: TFLite flatbuffer model object. 

81 gpu_compatibility: Whether to check GPU delegate compatibility. 

82 **kwargs: Experimental keyword arguments to analyze API. 

83 

84 Returns: 

85 Print analyzed report via console output. 

86 """ 

87 if not model_path and not model_content: 

88 raise ValueError("neither `model_path` nor `model_content` is provided") 

89 if model_path: 

90 print(f"=== {model_path} ===\n") 

91 tflite_model = model_path 

92 input_is_filepath = True 

93 else: 

94 print("=== TFLite ModelAnalyzer ===\n") 

95 tflite_model = model_content 

96 input_is_filepath = False 

97 

98 if kwargs.get("experimental_use_mlir", False): 

99 print( 

100 wrap_toco.wrapped_flat_buffer_file_to_mlir(tflite_model, 

101 input_is_filepath)) 

102 else: 

103 print( 

104 _analyzer_wrapper.ModelAnalyzer(tflite_model, input_is_filepath, 

105 gpu_compatibility))