@@ -32,6 +32,14 @@ std::vector<int64_t> PartialShapeToVec(const ov::PartialShape& shape) {
32
32
return res;
33
33
}
34
34
35
+ ov::PartialShape VecToPartialShape (const std::vector<int64_t >& shape) {
36
+ std::vector<ov::Dimension> dims;
37
+ for (size_t i = 0 ; i < shape.size (); ++i) {
38
+ dims.emplace_back (ov::Dimension (shape[i]));
39
+ }
40
+ return ov::PartialShape (dims);
41
+ }
42
+
35
43
FDDataType OpenVINODataTypeToFD (const ov::element::Type& type) {
36
44
if (type == ov::element::f32 ) {
37
45
return FDDataType::FP32;
@@ -100,6 +108,26 @@ bool OpenVINOBackend::InitFromPaddle(const std::string& model_file,
100
108
option_ = option;
101
109
102
110
std::shared_ptr<ov::Model> model = core_.read_model (model_file, params_file);
111
+ if (option_.shape_infos .size () > 0 ) {
112
+ std::map<std::string, ov::PartialShape> shape_infos;
113
+ for (const auto & item : option_.shape_infos ) {
114
+ shape_infos[item.first ] = VecToPartialShape (item.second );
115
+ }
116
+ model->reshape (shape_infos);
117
+ }
118
+
119
+ if (option_.device .find (" HETERO" ) != std::string::npos) {
120
+ auto supported_ops = core_.query_model (model, option_.device );
121
+ for (auto && op : model->get_ops ()) {
122
+ auto & affinity = supported_ops[op->get_friendly_name ()];
123
+ if (option_.cpu_operators .find (op->description ()) !=
124
+ option_.cpu_operators .end ()) {
125
+ op->get_rt_info ()[" affinity" ] = " CPU" ;
126
+ } else {
127
+ op->get_rt_info ()[" affinity" ] = affinity;
128
+ }
129
+ }
130
+ }
103
131
104
132
// Get inputs/outputs information from loaded model
105
133
const std::vector<ov::Output<ov::Node>> inputs = model->inputs ();
@@ -151,14 +179,25 @@ bool OpenVINOBackend::InitFromPaddle(const std::string& model_file,
151
179
if (option_.cpu_thread_num > 0 ) {
152
180
properties[" INFERENCE_NUM_THREADS" ] = option_.cpu_thread_num ;
153
181
}
154
- if (option_.ov_num_streams == -1 ) {
155
- properties[" NUM_STREAMS" ] = ov::streams::AUTO;
156
- } else if (option_.ov_num_streams == -2 ) {
157
- properties[" NUM_STREAMS" ] = ov::streams::NUMA;
158
- } else if (option_.ov_num_streams > 0 ) {
159
- properties[" NUM_STREAMS" ] = option_.ov_num_streams ;
182
+ if (option_.device == " CPU" ) {
183
+ if (option_.num_streams == -1 ) {
184
+ properties[" NUM_STREAMS" ] = ov::streams::AUTO;
185
+ } else if (option_.num_streams == -2 ) {
186
+ properties[" NUM_STREAMS" ] = ov::streams::NUMA;
187
+ } else if (option_.num_streams > 0 ) {
188
+ properties[" NUM_STREAMS" ] = option_.num_streams ;
189
+ }
190
+ } else {
191
+ if (option_.num_streams != 0 ) {
192
+ FDWARNING << " NUM_STREAMS only available on device CPU, currently the "
193
+ " device is set as "
194
+ << option_.device << " , the NUM_STREAMS will be ignored."
195
+ << std::endl;
196
+ }
160
197
}
161
- FDINFO << " Compile OpenVINO model on device_name:" << option.device << " ." << std::endl;
198
+
199
+ FDINFO << " Compile OpenVINO model on device_name:" << option.device << " ."
200
+ << std::endl;
162
201
compiled_model_ = core_.compile_model (model, option.device , properties);
163
202
164
203
request_ = compiled_model_.create_infer_request ();
@@ -199,6 +238,27 @@ bool OpenVINOBackend::InitFromOnnx(const std::string& model_file,
199
238
200
239
std::shared_ptr<ov::Model> model = core_.read_model (model_file);
201
240
241
+ if (option_.shape_infos .size () > 0 ) {
242
+ std::map<std::string, ov::PartialShape> shape_infos;
243
+ for (const auto & item : option_.shape_infos ) {
244
+ shape_infos[item.first ] = VecToPartialShape (item.second );
245
+ }
246
+ model->reshape (shape_infos);
247
+ }
248
+
249
+ if (option_.device .find (" HETERO" ) != std::string::npos) {
250
+ auto supported_ops = core_.query_model (model, option_.device );
251
+ for (auto && op : model->get_ops ()) {
252
+ auto & affinity = supported_ops[op->get_friendly_name ()];
253
+ if (option_.cpu_operators .find (op->description ()) !=
254
+ option_.cpu_operators .end ()) {
255
+ op->get_rt_info ()[" affinity" ] = " CPU" ;
256
+ } else {
257
+ op->get_rt_info ()[" affinity" ] = affinity;
258
+ }
259
+ }
260
+ }
261
+
202
262
// Get inputs/outputs information from loaded model
203
263
const std::vector<ov::Output<ov::Node>> inputs = model->inputs ();
204
264
std::map<std::string, TensorInfo> input_infos;
@@ -249,18 +309,29 @@ bool OpenVINOBackend::InitFromOnnx(const std::string& model_file,
249
309
if (option_.cpu_thread_num > 0 ) {
250
310
properties[" INFERENCE_NUM_THREADS" ] = option_.cpu_thread_num ;
251
311
}
252
- if (option_.ov_num_streams == -1 ) {
253
- properties[" NUM_STREAMS" ] = ov::streams::AUTO;
254
- } else if (option_.ov_num_streams == -2 ) {
255
- properties[" NUM_STREAMS" ] = ov::streams::NUMA;
256
- } else if (option_.ov_num_streams > 0 ) {
257
- properties[" NUM_STREAMS" ] = option_.ov_num_streams ;
312
+ if (option_.device == " CPU" ) {
313
+ if (option_.num_streams == -1 ) {
314
+ properties[" NUM_STREAMS" ] = ov::streams::AUTO;
315
+ } else if (option_.num_streams == -2 ) {
316
+ properties[" NUM_STREAMS" ] = ov::streams::NUMA;
317
+ } else if (option_.num_streams > 0 ) {
318
+ properties[" NUM_STREAMS" ] = option_.num_streams ;
319
+ }
320
+ } else {
321
+ if (option_.num_streams != 0 ) {
322
+ FDWARNING << " NUM_STREAMS only available on device CPU, currently the "
323
+ " device is set as "
324
+ << option_.device << " , the NUM_STREAMS will be ignored."
325
+ << std::endl;
326
+ }
258
327
}
259
- FDINFO << " Compile OpenVINO model on device_name:" << option.device << " ." << std::endl;
328
+
329
+ FDINFO << " Compile OpenVINO model on device_name:" << option.device << " ."
330
+ << std::endl;
260
331
compiled_model_ = core_.compile_model (model, option.device , properties);
261
332
262
333
request_ = compiled_model_.create_infer_request ();
263
-
334
+
264
335
initialized_ = true ;
265
336
return true ;
266
337
}
@@ -302,13 +373,16 @@ bool OpenVINOBackend::Infer(std::vector<FDTensor>& inputs,
302
373
return true ;
303
374
}
304
375
305
- std::unique_ptr<BaseBackend> OpenVINOBackend::Clone (void *stream, int device_id) {
306
- std::unique_ptr<BaseBackend> new_backend = utils::make_unique<OpenVINOBackend>();
376
+ std::unique_ptr<BaseBackend> OpenVINOBackend::Clone (void * stream,
377
+ int device_id) {
378
+ std::unique_ptr<BaseBackend> new_backend =
379
+ utils::make_unique<OpenVINOBackend>();
307
380
auto casted_backend = dynamic_cast <OpenVINOBackend*>(new_backend.get ());
308
381
casted_backend->option_ = option_;
309
382
casted_backend->request_ = compiled_model_.create_infer_request ();
310
383
casted_backend->input_infos_ .assign (input_infos_.begin (), input_infos_.end ());
311
- casted_backend->output_infos_ .assign (output_infos_.begin (), output_infos_.end ());
384
+ casted_backend->output_infos_ .assign (output_infos_.begin (),
385
+ output_infos_.end ());
312
386
return new_backend;
313
387
}
314
388
0 commit comments