@@ -133,55 +133,11 @@ def make_generate_function(self):
133
133
134
134
self .generate_function = self .generate_step
135
135
if keras .config .backend () == "openvino" :
136
- import numpy as np
137
136
import openvino as ov
138
137
import openvino .runtime .opset14 as ov_opset
139
- from keras .src .backend .openvino .core import OPENVINO_DTYPES
140
- from keras .src .backend .openvino .core import OpenVINOKerasTensor
141
-
142
- def unpack_singleton (x ):
143
- if isinstance (x , (list , tuple )) and len (x ) == 1 :
144
- return x [0 ]
145
- return x
146
-
147
- def parameterize_inputs (inputs ):
148
- if isinstance (inputs , (list , tuple )):
149
- return [parameterize_inputs (e ) for e in inputs ]
150
- elif isinstance (inputs , dict ):
151
- return {
152
- k : parameterize_inputs (v ) for k , v in inputs .items ()
153
- }
154
- elif isinstance (inputs , np .ndarray ):
155
- ov_type = OPENVINO_DTYPES [str (inputs .dtype )]
156
- ov_shape = list (inputs .shape )
157
- param = ov_opset .parameter (shape = ov_shape , dtype = ov_type )
158
- return OpenVINOKerasTensor (param .output (0 ))
159
- elif isinstance (inputs , (int , np .integer )):
160
- param = ov_opset .parameter (shape = [], dtype = ov .Type .i32 )
161
- return OpenVINOKerasTensor (param .output (0 ))
162
- elif isinstance (inputs , (float , np .floating )):
163
- param = ov_opset .parameter (shape = [], dtype = ov .Type .f32 )
164
- return OpenVINOKerasTensor (param .output (0 ))
165
- else :
166
- raise TypeError (f"Unknown input type: { type (inputs )} " )
167
-
168
- def get_struct_outputs (inputs , stop_token_ids , fn ):
169
- struct_params = parameterize_inputs (inputs )
170
- struct_outputs = fn (struct_params , stop_token_ids )
171
- return struct_params , struct_outputs
172
-
173
- def get_outputs_from_model (
174
- inputs , struct_outputs , compile_ov_model
175
- ):
176
- flatten_inputs = tree .flatten (inputs )
177
- assert OpenVINOKerasTensor not in inputs , (
178
- "inputs should be numpy arrays"
179
- )
180
- outputs = compile_ov_model (flatten_inputs )
181
- outputs = unpack_singleton (
182
- tree .pack_sequence_as (struct_outputs , outputs .to_tuple ())
183
- )
184
- return outputs
138
+
139
+ from keras_hub .src .utils .openvino_utils import get_outputs
140
+ from keras_hub .src .utils .openvino_utils import get_struct_outputs
185
141
186
142
def ov_infer (inputs , stop_token_ids , fn ):
187
143
struct_params , struct_outputs = get_struct_outputs (
@@ -220,14 +176,11 @@ def ov_infer(inputs, stop_token_ids, fn):
220
176
)
221
177
222
178
compile_ov_model = ov_infer ._compiled_models [model_hash ]
223
- return get_outputs_from_model (
224
- inputs , struct_outputs , compile_ov_model
225
- )
179
+ return get_outputs (inputs , struct_outputs , compile_ov_model )
226
180
227
181
def wrapped_generate_function (inputs , stop_token_ids = None ):
228
182
for k , v in inputs .items ():
229
- if isinstance (v , OpenVINOKerasTensor ):
230
- inputs [k ] = ops .array (v )
183
+ inputs [k ] = ops .array (v )
231
184
return ov_infer (inputs , stop_token_ids , self .generate_step )
232
185
233
186
self .generate_function = wrapped_generate_function
0 commit comments