Generic named inference, for tools with different I/O conventions.
362{
363 if (!graphData.
graph) {
365 return StatusCode::FAILURE;
366 }
367 if (graphData.
graph->dataTensor.empty()) {
369 return StatusCode::FAILURE;
370 }
371
372
373
374 graphData.
graph->dataTensor.reserve(inputNames.size() +
outputNames.size());
375 if (graphData.
graph->dataTensor.size() < inputNames.size()) {
377 << " tensors but inference expects " << inputNames.size() << " inputs.");
378 return StatusCode::FAILURE;
379 }
380
381 if (msgLvl(MSG::DEBUG)) {
382
383
385 if (!graphData.
graph->dataTensor.empty()) {
386 const auto& featureTensor = graphData.
graph->dataTensor[0];
387 auto featShape = featureTensor.GetTensorTypeAndShapeInfo().GetShape();
389 << (featShape.size()>1 ? ("," + std::to_string(featShape[1])) : "")
390 << (featShape.size()>2 ? ("," + std::to_string(featShape[2])) : "") << "]");
391
392 float* featData = const_cast<Ort::Value&>(featureTensor).GetTensorMutableData<float>();
393 const size_t totalElements = featureTensor.GetTensorTypeAndShapeInfo().GetElementCount();
394 ATH_MSG_DEBUG(
"Features tensor total elements: " << totalElements);
395
396
397 const size_t nFeat = (featShape.size() > 1 && featShape[1] > 0) ? static_cast<size_t>(featShape[1]) : 1;
398 const size_t nNodes = totalElements / nFeat;
399 const size_t debugNodes = std::min(nNodes, static_cast<size_t>(10));
400
401
402
403 std::vector<std::string> featNames;
404 {
405 Ort::AllocatorWithDefaultOptions allocator;
406 Ort::ModelMetadata meta =
model().GetModelMetadata();
407 auto keys = meta.GetCustomMetadataMapKeysAllocated(allocator);
408 std::vector<std::string> keyNames;
409 keyNames.reserve(
keys.size());
410 for (
const auto& k : keys) keyNames.emplace_back(
k.get());
412 "x_feature_names", "node_feature_names", "feature_names", "input_feature_names"};
413 for (const std::string& key : candidates) {
414 if (std::find(keyNames.begin(), keyNames.end(), key) != keyNames.end()) {
415 std::string
val = meta.LookupCustomMetadataMapAllocated(
key.c_str(), allocator).get();
417 break;
418 }
419 }
420 if (featNames.empty()) {
421 ATH_MSG_DEBUG(
"No usable feature-name metadata key found in model; using generic fN labels.");
422 }
423 }
424 auto featLabel = [&](
size_t f) -> std::string {
425 if (f < featNames.size())
return featNames[
f];
426 return "f" + std::to_string(f);
427 };
428
429
430 {
431 std::ostringstream
legend;
432 legend <<
"Node feature legend (" << nFeat <<
" features):";
433 for (
size_t f = 0;
f < nFeat; ++
f) {
434 legend <<
" f" <<
f <<
"=" << featLabel(f);
435 if (f + 1 < nFeat)
legend <<
",";
436 }
438 }
439
440 for (
size_t n = 0;
n < debugNodes; ++
n) {
441 std::ostringstream
row;
442 row <<
"ONNXNode[" <<
n <<
"]:";
443 for (
size_t f = 0;
f < nFeat; ++
f) {
444 row <<
" f" <<
f <<
"=" << featData[
n * nFeat +
f];
445 if (f + 1 < nFeat)
row <<
",";
446 }
448 }
449 }
451 }
452
453 Ort::RunOptions run_options;
454 run_options.SetRunLogSeverityLevel(ORT_LOGGING_LEVEL_ERROR);
455
457
458 Ort::IoBinding binding(
model());
459 for (std::size_t i = 0;
i < inputNames.size(); ++
i) {
460 binding.BindInput(inputNames[i], graphData.
graph->dataTensor[i]);
461 }
462
463 Ort::MemoryInfo cpuOut = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
464 for (const char* outName : outputNames) {
465 binding.BindOutput(outName, cpuOut);
466 }
467
468 model().Run(run_options, binding);
469 binding.SynchronizeOutputs();
470
471 std::vector<Ort::Value>
outputs = binding.GetOutputValues();
474 return StatusCode::FAILURE;
475 }
476
477 float* outData =
outputs[0].GetTensorMutableData<
float>();
478 const size_t outSize =
outputs[0].GetTensorTypeAndShapeInfo().GetElementCount();
479 ATH_MSG_DEBUG(
"ONNX (IoBinding) raw output elementCount = " << outSize);
480
482 std::span<float> preds(outData, outData + outSize);
483 for (
size_t i = 0;
i < outSize; ++
i) {
484 if (!std::isfinite(preds[i])) {
485 ATH_MSG_WARNING(
"Non-finite prediction detected at " << i <<
" -> set to -100.");
487 }
488 }
489 }
490
491 for (auto& v : outputs) {
492 graphData.
graph->dataTensor.emplace_back(std::move(v));
493 }
494 return StatusCode::SUCCESS;
495 }
496
497
498 std::vector<Ort::Value>
outputs =
499 model().Run(run_options,
500 inputNames.data(),
501 graphData.
graph->dataTensor.data(),
502 inputNames.size(),
505
508 return StatusCode::FAILURE;
509 }
510
511 float* outData =
outputs[0].GetTensorMutableData<
float>();
512 const size_t outSize =
outputs[0].GetTensorTypeAndShapeInfo().GetElementCount();
514
516 std::span<float> preds(outData, outData + outSize);
517 for (
size_t i = 0;
i < outSize; ++
i) {
518 if (!std::isfinite(preds[i])) {
519 ATH_MSG_WARNING(
"Non-finite prediction detected at " << i <<
" -> set to -100.");
521 }
522 }
523 }
524
525 for (auto& v : outputs) {
526 graphData.
graph->dataTensor.emplace_back(std::move(v));
527 }
528 return StatusCode::SUCCESS;
529}
row
Appending html table to final .html summary file.