Skip to content

Commit 5a55a7d

Browse files
Added try for backends (#3138)
### Changes - Added `try-catch` blocks for `isinstance` during backend calculation. ### Reason for changes - Corrupted frameworks may lead to exceptions even if these frameworks were not intended to be used. ### Related tickets - 158806 ### Tests - Manual
1 parent 3775503 commit 5a55a7d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

nncf/common/utils/backend.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import importlib
1212
from copy import deepcopy
1313
from enum import Enum
14-
from typing import List, TypeVar
14+
from typing import Any, Callable, List, TypeVar
1515

1616
import nncf
1717

@@ -26,6 +26,16 @@ class BackendType(Enum):
2626
OPENVINO = "OpenVINO"
2727

2828

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+
2939
def get_available_backends() -> List[BackendType]:
3040
"""
3141
Returns a list of available backends.
@@ -51,6 +61,7 @@ def get_available_backends() -> List[BackendType]:
5161
return available_backends
5262

5363

64+
@result_verifier
5465
def is_torch_model(model: TModel) -> bool:
5566
"""
5667
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:
6475
return not isinstance(model, torch.fx.GraphModule) and isinstance(model, torch.nn.Module)
6576

6677

78+
@result_verifier
6779
def is_torch_fx_model(model: TModel) -> bool:
6880
"""
6981
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:
7688
return isinstance(model, torch.fx.GraphModule)
7789

7890

91+
@result_verifier
7992
def is_tensorflow_model(model: TModel) -> bool:
8093
"""
8194
Returns True if the model is an instance of tensorflow.Module, otherwise False.
@@ -88,6 +101,7 @@ def is_tensorflow_model(model: TModel) -> bool:
88101
return isinstance(model, tensorflow.Module)
89102

90103

104+
@result_verifier
91105
def is_onnx_model(model: TModel) -> bool:
92106
"""
93107
Returns True if the model is an instance of onnx.ModelProto, otherwise False.
@@ -100,6 +114,7 @@ def is_onnx_model(model: TModel) -> bool:
100114
return isinstance(model, onnx.ModelProto)
101115

102116

117+
@result_verifier
103118
def is_openvino_model(model: TModel) -> bool:
104119
"""
105120
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:
112127
return isinstance(model, ov.Model)
113128

114129

130+
@result_verifier
115131
def is_openvino_compiled_model(model: TModel) -> bool:
116132
"""
117133
Returns True if the model is an instance of openvino.runtime.CompiledModel, otherwise False.
@@ -150,7 +166,7 @@ def get_backend(model: TModel) -> BackendType:
150166

151167
raise nncf.UnsupportedBackendError(
152168
"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. "
154170
"The available frameworks found: {}.".format(", ".join([b.value for b in available_backends]))
155171
)
156172

0 commit comments

Comments
 (0)