| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "shape_inference.h" |
| 16 | |
| 17 | #include <sstream> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <onnxruntime_c_api.h> |
| 22 | |
| 23 | namespace pnnx { |
| 24 | |
| 25 | namespace onnx2pnnx { |
| 26 | |
| 27 | static ONNXTensorElementDataType get_onnx_tensor_elem_data_type(const std::string& type) |
| 28 | { |
| 29 | if (type == "i8") return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8; |
| 30 | if (type == "u8") return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8; |
| 31 | if (type == "i16") return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16; |
| 32 | if (type == "u16") return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16; |
| 33 | if (type == "i32") return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32; |
| 34 | if (type == "u32") return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32; |
| 35 | if (type == "i64") return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64; |
| 36 | if (type == "u64") return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64; |
| 37 | if (type == "f16") return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16; |
| 38 | if (type == "f32") return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT; |
| 39 | if (type == "f64") return ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE; |
| 40 | if (type == "bf16") return ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16; |
| 41 | if (type == "c64") return ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64; |
| 42 | if (type == "c128") return ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128; |
| 43 | |
| 44 | // unknown |
| 45 | fprintf(stderr, "unsupported tensor elem data type %s\n", type.c_str()); |
| 46 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED; |
| 47 | } |
| 48 | |
| 49 | static void onnx_tensor_fill_random(void* ort_val_data, const std::vector<int64_t>& shape, ONNXTensorElementDataType datatype) |
| 50 | { |
| 51 | if (shape.empty()) |
| 52 | return; |
| 53 | |
| 54 | int64_t n = 1; |
| 55 | for (size_t i = 0; i < shape.size(); i++) |
| 56 | { |
| 57 | n *= shape[i]; |
| 58 | } |
| 59 | |
| 60 | if (datatype == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) |
| 61 | { |
| 62 | float* p = (float*)ort_val_data; |
| 63 | for (int64_t i = 0; i < n; i++) |
| 64 | { |
| 65 | p[i] = 0.1f; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (datatype == ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64) |
| 70 | { |
| 71 | int64_t* p = (int64_t*)ort_val_data; |
| 72 | for (int64_t i = 0; i < n; i++) |
| 73 | { |
| 74 | p[i] = 7; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static bool string_starts_with(const std::string& s, const std::string& s2) |
| 80 | { |
| 81 | return strncmp(s.c_str(), s2.c_str(), s2.size()) == 0; |
| 82 | } |
| 83 | |
| 84 | void shape_inference(onnx::ModelProto& model, |
| 85 | const std::vector<std::vector<int64_t> >& input_shapes, |
| 86 | const std::vector<std::string>& input_types, |
| 87 | const std::vector<std::vector<int64_t> >& input_shapes2, |
| 88 | const std::vector<std::string>& input_types2) |
| 89 | { |
| 90 | onnx::GraphProto* graph = model.mutable_graph(); |
| 91 | |
| 92 | // set graph input shape |
| 93 | if (!input_shapes.empty()) |
| 94 | { |
| 95 | for (int i = 0; i < graph->input_size(); i++) |
| 96 | { |
| 97 | onnx::ValueInfoProto* value = graph->mutable_input(i); |
| 98 | |
| 99 | ONNXTensorElementDataType datatype = get_onnx_tensor_elem_data_type(input_types[i]); |
| 100 | const std::vector<int64_t>& in_shape = input_shapes[i]; |
| 101 | const size_t in_dims = in_shape.size(); |
| 102 | |
| 103 | value->mutable_type()->mutable_tensor_type()->set_elem_type((int32_t)datatype); |
| 104 | |
| 105 | onnx::TensorShapeProto* tsp = value->mutable_type()->mutable_tensor_type()->mutable_shape(); |
| 106 | |
| 107 | tsp->clear_dim(); |
| 108 | for (size_t j = 0; j < in_dims; j++) |
| 109 | { |
| 110 | tsp->add_dim()->set_dim_value(in_shape[j]); |
| 111 | } |
| 112 | |
| 113 | if (!input_shapes2.empty()) |
| 114 | { |
| 115 | const std::vector<int64_t>& in_shape2 = input_shapes2[i]; |
| 116 | |
| 117 | for (size_t j = 0; j < in_dims; j++) |
| 118 | { |
| 119 | if (tsp->dim(j).dim_value() == in_shape2[j]) |
| 120 | continue; |
| 121 | |
| 122 | // dynamic dim size |
| 123 | tsp->mutable_dim(j)->clear_dim_value(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // save original outputs |
| 130 | std::vector<std::string> orig_outputs; |
| 131 | { |
| 132 | for (int i = 0; i < graph->output_size(); i++) |
| 133 | { |
| 134 | orig_outputs.push_back(graph->output(i).name()); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // collect intermediates |
| 139 | std::vector<std::string> intermediates; |
| 140 | { |
| 141 | for (int i = 0; i < graph->node_size(); i++) |
| 142 | { |
| 143 | const onnx::NodeProto& node = graph->node(i); |
| 144 | |
| 145 | const std::string& op_type = node.op_type(); |
| 146 | |
| 147 | // blacklist some glues |
| 148 | if (op_type == "Constant") |
| 149 | continue; |
| 150 | |
| 151 | // TODO fuse cat |
| 152 | if (op_type == "SequenceConstruct") |
| 153 | continue; |
| 154 | |
| 155 | // TODO fuse chunk/tensor_split |
| 156 | if (op_type == "aten_split") |
| 157 | continue; |
| 158 | |
| 159 | if (node.domain().empty() || string_starts_with(op_type, "nn_") || string_starts_with(op_type, "aten_") || string_starts_with(op_type, "_aten_")) |
| 160 | { |
| 161 | for (int j = 0; j < node.output_size(); j++) |
| 162 | { |
| 163 | // some input/output may have empty name, it causes trouble, skip it |
| 164 | if (node.output(j).empty()) |
| 165 | continue; |
| 166 | |
| 167 | intermediates.push_back(node.output(j)); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // add intermediates to onnx output |
| 174 | { |
| 175 | graph->clear_output(); |
| 176 | |
| 177 | for (size_t i = 0; i < intermediates.size(); i++) |
| 178 | { |
| 179 | graph->add_output()->set_name(intermediates[i]); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // generate temp onnx graph |
| 184 | std::string tmp_onnx_data; |
| 185 | { |
| 186 | std::stringstream tmp_onnx_data_ss; |
| 187 | if (!model.SerializeToOstream(&tmp_onnx_data_ss)) |
| 188 | { |
| 189 | fprintf(stderr, "write onnx failed\n"); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | tmp_onnx_data = tmp_onnx_data_ss.str(); |
| 194 | } |
| 195 | |
| 196 | // onnxrt inference |
| 197 | std::vector<std::string> new_outputs; |
| 198 | { |
| 199 | const OrtApi* ort_api = OrtGetApiBase()->GetApi(ORT_API_VERSION); |
| 200 | |
| 201 | OrtStatus* ort_status = 0; |
| 202 | |
| 203 | OrtEnv* ort_env = 0; |
| 204 | ort_status = ort_api->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "pnnx", &ort_env); |
| 205 | if (ort_status) |
| 206 | { |
| 207 | fprintf(stderr, "ort CreateEnv failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 208 | } |
| 209 | |
| 210 | OrtSessionOptions* ort_session_opt = 0; |
| 211 | ort_status = ort_api->CreateSessionOptions(&ort_session_opt); |
| 212 | if (ort_status) |
| 213 | { |
| 214 | fprintf(stderr, "ort CreateSessionOptions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 215 | } |
| 216 | |
| 217 | ort_status = ort_api->SetSessionGraphOptimizationLevel(ort_session_opt, ORT_DISABLE_ALL); |
| 218 | if (ort_status) |
| 219 | { |
| 220 | fprintf(stderr, "ort SetSessionGraphOptimizationLevel failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 221 | } |
| 222 | |
| 223 | // ort_status = ort_api->SetIntraOpNumThreads(ort_session_opt, 4); |
| 224 | // if (ort_status) |
| 225 | // { |
| 226 | // fprintf(stderr, "ort SetIntraOpNumThreads failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 227 | // } |
| 228 | // |
| 229 | // ort_status = ort_api->SetInterOpNumThreads(ort_session_opt, 4); |
| 230 | // if (ort_status) |
| 231 | // { |
| 232 | // fprintf(stderr, "ort SetInterOpNumThreads failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 233 | // } |
| 234 | |
| 235 | OrtSession* ort_session = 0; |
| 236 | ort_status = ort_api->CreateSessionFromArray(ort_env, (const void*)tmp_onnx_data.data(), tmp_onnx_data.size(), ort_session_opt, &ort_session); |
| 237 | if (ort_status) |
| 238 | { |
| 239 | fprintf(stderr, "ort CreateSession failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 240 | } |
| 241 | |
| 242 | OrtRunOptions* ort_run_opt = 0; |
| 243 | ort_status = ort_api->CreateRunOptions(&ort_run_opt); |
| 244 | if (ort_status) |
| 245 | { |
| 246 | fprintf(stderr, "ort CreateRunOptions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 247 | } |
| 248 | |
| 249 | OrtAllocator* ort_allocator = 0; |
| 250 | ort_status = ort_api->GetAllocatorWithDefaultOptions(&ort_allocator); |
| 251 | if (ort_status) |
| 252 | { |
| 253 | fprintf(stderr, "ort GetAllocatorWithDefaultOptions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 254 | } |
| 255 | |
| 256 | std::vector<const char*> input_names; |
| 257 | std::vector<OrtValue*> inputs; |
| 258 | for (int i = 0; i < graph->input_size(); i++) |
| 259 | { |
| 260 | const onnx::ValueInfoProto& value = graph->input(i); |
| 261 | |
| 262 | std::vector<int64_t> shape; |
| 263 | ONNXTensorElementDataType datatype; |
| 264 | if (!input_shapes.empty()) |
| 265 | { |
| 266 | shape = input_shapes[i]; |
| 267 | datatype = get_onnx_tensor_elem_data_type(input_types[i]); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | const onnx::TensorShapeProto& tsp = value.type().tensor_type().shape(); |
| 272 | for (int k = 0; k < tsp.dim_size(); k++) |
| 273 | { |
| 274 | shape.push_back(tsp.dim(k).dim_value()); |
| 275 | } |
| 276 | |
| 277 | datatype = (ONNXTensorElementDataType)value.type().tensor_type().elem_type(); |
| 278 | } |
| 279 | |
| 280 | OrtValue* ort_val = 0; |
| 281 | ort_status = ort_api->CreateTensorAsOrtValue(ort_allocator, (const int64_t*)shape.data(), shape.size(), datatype, &ort_val); |
| 282 | if (ort_status) |
| 283 | { |
| 284 | fprintf(stderr, "ort CreateTensorAsOrtValue failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 285 | } |
| 286 | |
| 287 | void* ort_val_data = 0; |
| 288 | ort_status = ort_api->GetTensorMutableData(ort_val, &ort_val_data); |
| 289 | if (ort_status) |
| 290 | { |
| 291 | fprintf(stderr, "ort GetTensorMutableData failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 292 | } |
| 293 | onnx_tensor_fill_random(ort_val_data, shape, datatype); |
| 294 | |
| 295 | input_names.push_back(value.name().c_str()); |
| 296 | inputs.push_back(ort_val); |
| 297 | } |
| 298 | |
| 299 | std::vector<const char*> output_names; |
| 300 | std::vector<OrtValue*> outputs; |
| 301 | for (size_t i = 0; i < intermediates.size(); i++) |
| 302 | { |
| 303 | output_names.push_back(intermediates[i].c_str()); |
| 304 | outputs.push_back(0); |
| 305 | } |
| 306 | |
| 307 | ort_status = ort_api->Run(ort_session, ort_run_opt, |
| 308 | input_names.data(), inputs.data(), input_names.size(), |
| 309 | output_names.data(), output_names.size(), outputs.data()); |
| 310 | if (ort_status) |
| 311 | { |
| 312 | fprintf(stderr, "ort Run failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 313 | } |
| 314 | |
| 315 | // TODO get output data |
| 316 | |
| 317 | graph->clear_output(); |
| 318 | |
| 319 | for (size_t i = 0; i < output_names.size(); i++) |
| 320 | { |
| 321 | OrtTypeInfo* type_info = 0; |
| 322 | ort_status = ort_api->GetTypeInfo(outputs[i], &type_info); |
| 323 | if (ort_status) |
| 324 | { |
| 325 | fprintf(stderr, "ort GetTypeInfo failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 326 | } |
| 327 | |
| 328 | ONNXType type = ONNX_TYPE_UNKNOWN; |
| 329 | if (type_info) |
| 330 | { |
| 331 | ort_status = ort_api->GetOnnxTypeFromTypeInfo(type_info, &type); |
| 332 | if (ort_status) |
| 333 | { |
| 334 | fprintf(stderr, "ort GetOnnxTypeFromTypeInfo failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (type == ONNX_TYPE_TENSOR) |
| 339 | { |
| 340 | OrtTensorTypeAndShapeInfo* info = 0; |
| 341 | ort_status = ort_api->GetTensorTypeAndShape(outputs[i], &info); |
| 342 | if (ort_status) |
| 343 | { |
| 344 | fprintf(stderr, "ort GetTensorTypeAndShape failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 345 | } |
| 346 | |
| 347 | ONNXTensorElementDataType datatype = ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED; |
| 348 | ort_status = ort_api->GetTensorElementType(info, &datatype); |
| 349 | if (ort_status) |
| 350 | { |
| 351 | fprintf(stderr, "ort GetTensorElementType failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 352 | } |
| 353 | |
| 354 | size_t out_dims = 0; |
| 355 | ort_status = ort_api->GetDimensionsCount(info, &out_dims); |
| 356 | if (ort_status) |
| 357 | { |
| 358 | fprintf(stderr, "ort GetDimensionsCount failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 359 | } |
| 360 | |
| 361 | // fprintf(stderr, " out_dims = %lu\n", out_dims); |
| 362 | |
| 363 | std::vector<int64_t> out_shape; |
| 364 | out_shape.resize(out_dims); |
| 365 | ort_status = ort_api->GetDimensions(info, out_shape.data(), out_dims); |
| 366 | if (ort_status) |
| 367 | { |
| 368 | fprintf(stderr, "ort GetDimensions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 369 | } |
| 370 | |
| 371 | // fprintf(stderr, "%16s = ", output_names[i]); |
| 372 | // for (size_t j = 0; j < out_dims; j++) |
| 373 | // { |
| 374 | // fprintf(stderr, "%lu ", out_shape[j]); |
| 375 | // } |
| 376 | // fprintf(stderr, "\n"); |
| 377 | |
| 378 | // assign value info |
| 379 | { |
| 380 | onnx::ValueInfoProto* value = 0; |
| 381 | |
| 382 | // maybe output |
| 383 | for (size_t j = 0; j < orig_outputs.size(); j++) |
| 384 | { |
| 385 | if (orig_outputs[j] == output_names[i]) |
| 386 | { |
| 387 | value = graph->add_output(); |
| 388 | value->set_name(output_names[i]); |
| 389 | new_outputs.push_back(output_names[i]); |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | if (!value) |
| 394 | { |
| 395 | for (int j = 0; j < graph->value_info_size(); j++) |
| 396 | { |
| 397 | if (graph->mutable_value_info(j)->name() == output_names[i]) |
| 398 | { |
| 399 | value = graph->mutable_value_info(j); |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | if (!value) |
| 404 | { |
| 405 | value = graph->add_value_info(); |
| 406 | value->set_name(output_names[i]); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // fprintf(stderr, "assign value info %s\n", value->name().c_str()); |
| 411 | |
| 412 | value->mutable_type()->mutable_tensor_type()->set_elem_type((int32_t)datatype); |
| 413 | |
| 414 | onnx::TensorShapeProto* tsp = value->mutable_type()->mutable_tensor_type()->mutable_shape(); |
| 415 | |
| 416 | tsp->clear_dim(); |
| 417 | for (size_t j = 0; j < out_dims; j++) |
| 418 | { |
| 419 | tsp->add_dim()->set_dim_value(out_shape[j]); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | ort_api->ReleaseTensorTypeAndShapeInfo(info); |
| 424 | } |
| 425 | |
| 426 | if (type_info) |
| 427 | { |
| 428 | ort_api->ReleaseTypeInfo(type_info); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | for (size_t i = 0; i < input_names.size(); i++) |
| 433 | { |
| 434 | ort_api->ReleaseValue(inputs[i]); |
| 435 | } |
| 436 | |
| 437 | for (size_t i = 0; i < output_names.size(); i++) |
| 438 | { |
| 439 | ort_api->ReleaseValue(outputs[i]); |
| 440 | } |
| 441 | |
| 442 | ort_api->ReleaseRunOptions(ort_run_opt); |
| 443 | ort_api->ReleaseSession(ort_session); |
| 444 | ort_api->ReleaseSessionOptions(ort_session_opt); |
| 445 | ort_api->ReleaseEnv(ort_env); |
| 446 | } |
| 447 | |
| 448 | // onnxrt inference for input_shapes2 |
| 449 | if (!input_shapes2.empty()) |
| 450 | { |
| 451 | const OrtApi* ort_api = OrtGetApiBase()->GetApi(ORT_API_VERSION); |
| 452 | |
| 453 | OrtStatus* ort_status = 0; |
| 454 | |
| 455 | OrtEnv* ort_env = 0; |
| 456 | ort_status = ort_api->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "pnnx", &ort_env); |
| 457 | if (ort_status) |
| 458 | { |
| 459 | fprintf(stderr, "ort CreateEnv failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 460 | } |
| 461 | |
| 462 | OrtSessionOptions* ort_session_opt = 0; |
| 463 | ort_status = ort_api->CreateSessionOptions(&ort_session_opt); |
| 464 | if (ort_status) |
| 465 | { |
| 466 | fprintf(stderr, "ort CreateSessionOptions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 467 | } |
| 468 | |
| 469 | ort_status = ort_api->SetSessionGraphOptimizationLevel(ort_session_opt, ORT_DISABLE_ALL); |
| 470 | if (ort_status) |
| 471 | { |
| 472 | fprintf(stderr, "ort SetSessionGraphOptimizationLevel failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 473 | } |
| 474 | |
| 475 | // ort_status = ort_api->SetIntraOpNumThreads(ort_session_opt, 4); |
| 476 | // if (ort_status) |
| 477 | // { |
| 478 | // fprintf(stderr, "ort SetIntraOpNumThreads failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 479 | // } |
| 480 | // |
| 481 | // ort_status = ort_api->SetInterOpNumThreads(ort_session_opt, 4); |
| 482 | // if (ort_status) |
| 483 | // { |
| 484 | // fprintf(stderr, "ort SetInterOpNumThreads failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 485 | // } |
| 486 | |
| 487 | OrtSession* ort_session = 0; |
| 488 | ort_status = ort_api->CreateSessionFromArray(ort_env, (const void*)tmp_onnx_data.data(), tmp_onnx_data.size(), ort_session_opt, &ort_session); |
| 489 | if (ort_status) |
| 490 | { |
| 491 | fprintf(stderr, "ort CreateSession failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 492 | } |
| 493 | |
| 494 | OrtRunOptions* ort_run_opt = 0; |
| 495 | ort_status = ort_api->CreateRunOptions(&ort_run_opt); |
| 496 | if (ort_status) |
| 497 | { |
| 498 | fprintf(stderr, "ort CreateRunOptions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 499 | } |
| 500 | |
| 501 | OrtAllocator* ort_allocator = 0; |
| 502 | ort_status = ort_api->GetAllocatorWithDefaultOptions(&ort_allocator); |
| 503 | if (ort_status) |
| 504 | { |
| 505 | fprintf(stderr, "ort GetAllocatorWithDefaultOptions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 506 | } |
| 507 | |
| 508 | std::vector<const char*> input_names; |
| 509 | std::vector<OrtValue*> inputs; |
| 510 | for (int i = 0; i < graph->input_size(); i++) |
| 511 | { |
| 512 | const onnx::ValueInfoProto& value = graph->input(i); |
| 513 | |
| 514 | std::vector<int64_t> shape = input_shapes2[i]; |
| 515 | ONNXTensorElementDataType datatype = get_onnx_tensor_elem_data_type(input_types2[i]); |
| 516 | |
| 517 | OrtValue* ort_val = 0; |
| 518 | ort_status = ort_api->CreateTensorAsOrtValue(ort_allocator, (const int64_t*)shape.data(), shape.size(), datatype, &ort_val); |
| 519 | if (ort_status) |
| 520 | { |
| 521 | fprintf(stderr, "ort CreateTensorAsOrtValue failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 522 | } |
| 523 | |
| 524 | void* ort_val_data = 0; |
| 525 | ort_status = ort_api->GetTensorMutableData(ort_val, &ort_val_data); |
| 526 | if (ort_status) |
| 527 | { |
| 528 | fprintf(stderr, "ort GetTensorMutableData failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 529 | } |
| 530 | onnx_tensor_fill_random(ort_val_data, shape, datatype); |
| 531 | |
| 532 | input_names.push_back(value.name().c_str()); |
| 533 | inputs.push_back(ort_val); |
| 534 | } |
| 535 | |
| 536 | std::vector<const char*> output_names; |
| 537 | std::vector<OrtValue*> outputs; |
| 538 | for (size_t i = 0; i < intermediates.size(); i++) |
| 539 | { |
| 540 | output_names.push_back(intermediates[i].c_str()); |
| 541 | outputs.push_back(0); |
| 542 | } |
| 543 | |
| 544 | ort_status = ort_api->Run(ort_session, ort_run_opt, |
| 545 | input_names.data(), inputs.data(), input_names.size(), |
| 546 | output_names.data(), output_names.size(), outputs.data()); |
| 547 | if (ort_status) |
| 548 | { |
| 549 | fprintf(stderr, "ort Run failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 550 | } |
| 551 | |
| 552 | // TODO get output data |
| 553 | |
| 554 | // graph->clear_output(); |
| 555 | |
| 556 | for (size_t i = 0; i < output_names.size(); i++) |
| 557 | { |
| 558 | OrtTypeInfo* type_info = 0; |
| 559 | ort_status = ort_api->GetTypeInfo(outputs[i], &type_info); |
| 560 | if (ort_status) |
| 561 | { |
| 562 | fprintf(stderr, "ort GetTypeInfo failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 563 | } |
| 564 | |
| 565 | ONNXType type = ONNX_TYPE_UNKNOWN; |
| 566 | if (type_info) |
| 567 | { |
| 568 | ort_status = ort_api->GetOnnxTypeFromTypeInfo(type_info, &type); |
| 569 | if (ort_status) |
| 570 | { |
| 571 | fprintf(stderr, "ort GetOnnxTypeFromTypeInfo failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (type == ONNX_TYPE_TENSOR) |
| 576 | { |
| 577 | OrtTensorTypeAndShapeInfo* info = 0; |
| 578 | ort_status = ort_api->GetTensorTypeAndShape(outputs[i], &info); |
| 579 | if (ort_status) |
| 580 | { |
| 581 | fprintf(stderr, "ort GetTensorTypeAndShape failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 582 | } |
| 583 | |
| 584 | ONNXTensorElementDataType datatype = ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED; |
| 585 | ort_status = ort_api->GetTensorElementType(info, &datatype); |
| 586 | if (ort_status) |
| 587 | { |
| 588 | fprintf(stderr, "ort GetTensorElementType failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 589 | } |
| 590 | |
| 591 | size_t out_dims = 0; |
| 592 | ort_status = ort_api->GetDimensionsCount(info, &out_dims); |
| 593 | if (ort_status) |
| 594 | { |
| 595 | fprintf(stderr, "ort GetDimensionsCount failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 596 | } |
| 597 | |
| 598 | // fprintf(stderr, " out_dims = %lu\n", out_dims); |
| 599 | |
| 600 | std::vector<int64_t> out_shape; |
| 601 | out_shape.resize(out_dims); |
| 602 | ort_status = ort_api->GetDimensions(info, out_shape.data(), out_dims); |
| 603 | if (ort_status) |
| 604 | { |
| 605 | fprintf(stderr, "ort GetDimensions failed %s\n", ort_api->GetErrorMessage(ort_status)); |
| 606 | } |
| 607 | |
| 608 | // fprintf(stderr, "%16s = ", output_names[i]); |
| 609 | // for (size_t j = 0; j < out_dims; j++) |
| 610 | // { |
| 611 | // fprintf(stderr, "%lu ", out_shape[j]); |
| 612 | // } |
| 613 | // fprintf(stderr, "\n"); |
| 614 | |
| 615 | // assign value info |
| 616 | { |
| 617 | onnx::ValueInfoProto* value = 0; |
| 618 | |
| 619 | // maybe output |
| 620 | for (size_t j = 0; j < new_outputs.size(); j++) |
| 621 | { |
| 622 | if (new_outputs[j] == output_names[i]) |
| 623 | { |
| 624 | value = graph->mutable_output(j); |
| 625 | break; |
| 626 | } |
| 627 | } |
| 628 | if (!value) |
| 629 | { |
| 630 | for (int j = 0; j < graph->value_info_size(); j++) |
| 631 | { |
| 632 | if (graph->mutable_value_info(j)->name() == output_names[i]) |
| 633 | { |
| 634 | value = graph->mutable_value_info(j); |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | // fprintf(stderr, "assign value info2 %s\n", value->name().c_str()); |
| 641 | |
| 642 | value->mutable_type()->mutable_tensor_type()->set_elem_type((int32_t)datatype); |
| 643 | |
| 644 | onnx::TensorShapeProto* tsp = value->mutable_type()->mutable_tensor_type()->mutable_shape(); |
| 645 | |
| 646 | // tsp->clear_dim(); |
| 647 | for (size_t j = 0; j < out_dims; j++) |
| 648 | { |
| 649 | if (tsp->dim(j).dim_value() == out_shape[j]) |
| 650 | continue; |
| 651 | |
| 652 | // dynamic dim size |
| 653 | tsp->mutable_dim(j)->clear_dim_value(); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | ort_api->ReleaseTensorTypeAndShapeInfo(info); |
| 658 | } |
| 659 | |
| 660 | if (type_info) |
| 661 | { |
| 662 | ort_api->ReleaseTypeInfo(type_info); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | for (size_t i = 0; i < input_names.size(); i++) |
| 667 | { |
| 668 | ort_api->ReleaseValue(inputs[i]); |
| 669 | } |
| 670 | |
| 671 | for (size_t i = 0; i < output_names.size(); i++) |
| 672 | { |
| 673 | ort_api->ReleaseValue(outputs[i]); |
| 674 | } |
| 675 | |
| 676 | ort_api->ReleaseRunOptions(ort_run_opt); |
| 677 | ort_api->ReleaseSession(ort_session); |
| 678 | ort_api->ReleaseSessionOptions(ort_session_opt); |
| 679 | ort_api->ReleaseEnv(ort_env); |
| 680 | } |
| 681 | |
| 682 | // new_outputs order may differ from orig_outputs |
| 683 | { |
| 684 | for (size_t i = 0; i < orig_outputs.size(); i++) |
| 685 | { |
| 686 | if (orig_outputs[i] == new_outputs[i]) |
| 687 | continue; |
| 688 | |
| 689 | for (size_t j = 0; j < new_outputs.size(); j++) |
| 690 | { |
| 691 | if (orig_outputs[i] == new_outputs[j]) |
| 692 | { |
| 693 | graph->mutable_output()->SwapElements((int)i, (int)j); |
| 694 | std::swap(new_outputs[i], new_outputs[j]); |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | } // namespace onnx2pnnx |
| 703 | |
| 704 | } // namespace pnnx |