11
11
import importlib
12
12
from copy import deepcopy
13
13
from enum import Enum
14
- from typing import List , TypeVar
14
+ from typing import Any , Callable , List , TypeVar
15
15
16
16
import nncf
17
17
@@ -26,6 +26,16 @@ class BackendType(Enum):
26
26
OPENVINO = "OpenVINO"
27
27
28
28
29
+ def result_verifier (func : Callable [[TModel ], bool ]) -> Callable [..., None ]:
30
+ def verify_result (* args : Any , ** kwargs : Any ): # type: ignore
31
+ try :
32
+ return func (* args , ** kwargs )
33
+ except AttributeError :
34
+ return False
35
+
36
+ return verify_result
37
+
38
+
29
39
def get_available_backends () -> List [BackendType ]:
30
40
"""
31
41
Returns a list of available backends.
@@ -51,6 +61,7 @@ def get_available_backends() -> List[BackendType]:
51
61
return available_backends
52
62
53
63
64
+ @result_verifier
54
65
def is_torch_model (model : TModel ) -> bool :
55
66
"""
56
67
Returns True if the model is an instance of torch.nn.Module and not a torch.fx.GraphModule, otherwise False.
@@ -64,6 +75,7 @@ def is_torch_model(model: TModel) -> bool:
64
75
return not isinstance (model , torch .fx .GraphModule ) and isinstance (model , torch .nn .Module )
65
76
66
77
78
+ @result_verifier
67
79
def is_torch_fx_model (model : TModel ) -> bool :
68
80
"""
69
81
Returns True if the model is an instance of torch.fx.GraphModule, otherwise False.
@@ -76,6 +88,7 @@ def is_torch_fx_model(model: TModel) -> bool:
76
88
return isinstance (model , torch .fx .GraphModule )
77
89
78
90
91
+ @result_verifier
79
92
def is_tensorflow_model (model : TModel ) -> bool :
80
93
"""
81
94
Returns True if the model is an instance of tensorflow.Module, otherwise False.
@@ -88,6 +101,7 @@ def is_tensorflow_model(model: TModel) -> bool:
88
101
return isinstance (model , tensorflow .Module )
89
102
90
103
104
+ @result_verifier
91
105
def is_onnx_model (model : TModel ) -> bool :
92
106
"""
93
107
Returns True if the model is an instance of onnx.ModelProto, otherwise False.
@@ -100,6 +114,7 @@ def is_onnx_model(model: TModel) -> bool:
100
114
return isinstance (model , onnx .ModelProto )
101
115
102
116
117
+ @result_verifier
103
118
def is_openvino_model (model : TModel ) -> bool :
104
119
"""
105
120
Returns True if the model is an instance of openvino.runtime.Model, otherwise False.
@@ -112,6 +127,7 @@ def is_openvino_model(model: TModel) -> bool:
112
127
return isinstance (model , ov .Model )
113
128
114
129
130
+ @result_verifier
115
131
def is_openvino_compiled_model (model : TModel ) -> bool :
116
132
"""
117
133
Returns True if the model is an instance of openvino.runtime.CompiledModel, otherwise False.
@@ -150,7 +166,7 @@ def get_backend(model: TModel) -> BackendType:
150
166
151
167
raise nncf .UnsupportedBackendError (
152
168
"Could not infer the backend framework from the model type because "
153
- "the framework is not available or the model type is unsupported. "
169
+ "the framework is not available or corrupted, or the model type is unsupported. "
154
170
"The available frameworks found: {}." .format (", " .join ([b .value for b in available_backends ]))
155
171
)
156
172
0 commit comments