@@ -60,12 +60,17 @@ def create(
60
60
)
61
61
62
62
weights = model_description .weights
63
- errors : List [str ] = []
63
+ errors : List [Tuple [ WeightsFormat , Exception ] ] = []
64
64
weight_format_priority_order = (
65
65
DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER
66
66
if weight_format_priority_order is None
67
67
else weight_format_priority_order
68
68
)
69
+ # limit weight formats to the ones present
70
+ weight_format_priority_order = [
71
+ w for w in weight_format_priority_order if getattr (weights , w ) is not None
72
+ ]
73
+
69
74
for wf in weight_format_priority_order :
70
75
if wf == "pytorch_state_dict" and weights .pytorch_state_dict is not None :
71
76
try :
@@ -77,7 +82,7 @@ def create(
77
82
devices = devices ,
78
83
)
79
84
except Exception as e :
80
- errors .append (f" { wf } : { e } " )
85
+ errors .append (( wf , e ) )
81
86
elif (
82
87
wf == "tensorflow_saved_model_bundle"
83
88
and weights .tensorflow_saved_model_bundle is not None
@@ -89,7 +94,7 @@ def create(
89
94
model_description = model_description , devices = devices
90
95
)
91
96
except Exception as e :
92
- errors .append (f" { wf } : { e } " )
97
+ errors .append (( wf , e ) )
93
98
elif wf == "onnx" and weights .onnx is not None :
94
99
try :
95
100
from ._onnx_model_adapter import ONNXModelAdapter
@@ -98,7 +103,7 @@ def create(
98
103
model_description = model_description , devices = devices
99
104
)
100
105
except Exception as e :
101
- errors .append (f" { wf } : { e } " )
106
+ errors .append (( wf , e ) )
102
107
elif wf == "torchscript" and weights .torchscript is not None :
103
108
try :
104
109
from ._torchscript_model_adapter import TorchscriptModelAdapter
@@ -107,7 +112,7 @@ def create(
107
112
model_description = model_description , devices = devices
108
113
)
109
114
except Exception as e :
110
- errors .append (f" { wf } : { e } " )
115
+ errors .append (( wf , e ) )
111
116
elif wf == "keras_hdf5" and weights .keras_hdf5 is not None :
112
117
# keras can either be installed as a separate package or used as part of tensorflow
113
118
# we try to first import the keras model adapter using the separate package and,
@@ -125,15 +130,24 @@ def create(
125
130
model_description = model_description , devices = devices
126
131
)
127
132
except Exception as e :
128
- errors .append (f" { wf } : { e } " )
133
+ errors .append (( wf , e ) )
129
134
130
135
assert errors
131
- error_list = "\n - " .join (errors )
132
- raise ValueError (
133
- "None of the weight format specific model adapters could be created for"
134
- + f" '{ model_description .id or model_description .name } '"
135
- + f" in this environment. Errors are:\n \n { error_list } .\n \n "
136
- )
136
+ if len (weight_format_priority_order ) == 1 :
137
+ assert len (errors ) == 1
138
+ raise ValueError (
139
+ f"The '{ weight_format_priority_order [0 ]} ' model adapter could not be created"
140
+ + f" in this environment:\n { errors [0 ][1 ].__class__ .__name__ } ({ errors [0 ][1 ]} ).\n \n "
141
+ )
142
+
143
+ else :
144
+ error_list = "\n - " .join (
145
+ f"{ wf } : { e .__class__ .__name__ } ({ e } )" for wf , e in errors
146
+ )
147
+ raise ValueError (
148
+ "None of the weight format specific model adapters could be created"
149
+ + f" in this environment. Errors are:\n \n { error_list } .\n \n "
150
+ )
137
151
138
152
@final
139
153
def load (self , * , devices : Optional [Sequence [str ]] = None ) -> None :
0 commit comments