@@ -133,9 +133,14 @@ def make_generate_function(self):
133
133
134
134
self .generate_function = self .generate_step
135
135
if keras .config .backend () == "openvino" :
136
+ import os
137
+
138
+ os .environ ["OV_ENABLE_EINSUM_DECOMPOSITION" ] = "1"
136
139
import openvino as ov
137
140
import openvino .runtime .opset14 as ov_opset
138
141
142
+ from keras_hub .src .utils .keras_utils import print_msg
143
+
139
144
def ov_infer (inputs , stop_token_ids , fn ):
140
145
def get_outputs (inputs , struct_outputs , compiled_ov_model ):
141
146
flatten_inputs = tree .flatten (inputs )
@@ -147,28 +152,33 @@ def get_outputs(inputs, struct_outputs, compiled_ov_model):
147
152
)
148
153
return outputs
149
154
150
- struct_params = self ._parameterize_data (inputs )
151
- struct_outputs = fn (struct_params , stop_token_ids )
152
-
153
155
# Try using the existing compiled model
154
156
if self .ov_compiled_model is not None :
155
157
try :
156
158
return get_outputs (
157
- inputs , struct_outputs , self .ov_compiled_model
159
+ inputs , self . struct_outputs , self .ov_compiled_model
158
160
)
159
- except Exception :
160
- # Delete previous model, then
161
+ except RuntimeError as e :
162
+ # Delete previous model and struct outputs , then
161
163
# Fall through to recompilation if inference fails
164
+ print_msg (
165
+ "WARNING: OpenVINO inference \033 [1mFAILED\033 [0m, "
166
+ "so we'll Rebuild and compile the model then "
167
+ f"try again.\n { e } "
168
+ )
162
169
del self .ov_compiled_model
170
+ del self .struct_outputs
163
171
pass
164
172
165
173
# Rebuild and compile the OpenVINO model
174
+ struct_params = self ._parameterize_data (inputs )
175
+ self .struct_outputs = fn (struct_params , stop_token_ids )
166
176
parameters = [
167
177
p .output .get_node () for p in tree .flatten (struct_params )
168
178
]
169
179
results = [
170
180
ov_opset .result (r .output )
171
- for r in tree .flatten (struct_outputs )
181
+ for r in tree .flatten (self . struct_outputs )
172
182
]
173
183
ov_model = ov .Model (results = results , parameters = parameters )
174
184
for ov_input in ov_model .inputs :
@@ -182,10 +192,11 @@ def get_outputs(inputs, struct_outputs, compiled_ov_model):
182
192
# OpenVINO supports only compiling with 'CPU' devices.
183
193
self .ov_compiled_model = core .compile_model (ov_model , device )
184
194
return get_outputs (
185
- inputs , struct_outputs , self .ov_compiled_model
195
+ inputs , self . struct_outputs , self .ov_compiled_model
186
196
)
187
197
188
198
def wrapped_generate_function (inputs , stop_token_ids = None ):
199
+ # ops.array converts yo numpy in openvino backend
189
200
inputs = tree .map_structure (ops .array , inputs )
190
201
return ov_infer (inputs , stop_token_ids , self .generate_step )
191
202
0 commit comments