|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include <onnxruntime_cxx_api.h> |
| 5 | + |
| 6 | +#include <filesystem> // NOLINT |
| 7 | +#include <iostream> |
| 8 | +#include <string> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include "core/platform/path_lib.h" |
| 12 | +#include "include/utils.hpp" |
| 13 | + |
| 14 | +int main(int, char* argv[]) { |
| 15 | + std::string model_dir(argv[1]); |
| 16 | + std::cout << model_dir << std::endl; |
| 17 | + std::basic_string<PATH_CHAR_TYPE> model_path = find_model_path(model_dir); |
| 18 | + if (model_path.size() <= 0) { |
| 19 | + std::cout << ".onnx model should be provided" << std::endl; |
| 20 | + exit(0); |
| 21 | + } |
| 22 | + |
| 23 | + // model |
| 24 | + std::cout << "[Successfully Load Model] " << std::endl; |
| 25 | + const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION); |
| 26 | + std::cout << "[ORT_API_VERSION] " << ORT_API_VERSION << std::endl; |
| 27 | + OrtEnv* env; |
| 28 | + g_ort->CreateEnv(ORT_LOGGING_LEVEL_VERBOSE, "test", &env); |
| 29 | + OrtSessionOptions* session_options; |
| 30 | + g_ort->CreateSessionOptions(&session_options); |
| 31 | + g_ort->SetIntraOpNumThreads(session_options, 1); |
| 32 | + g_ort->SetSessionGraphOptimizationLevel(session_options, ORT_ENABLE_BASIC); |
| 33 | + |
| 34 | + std::string backend_path(argv[2]); |
| 35 | + if (!std::filesystem::exists(std::filesystem::path(backend_path))) { |
| 36 | + std::cout << "Not Found:" << backend_path << std::endl; |
| 37 | + exit(0); |
| 38 | + } |
| 39 | + std::vector<const char*> options_keys = {"backend_path"}; |
| 40 | + std::vector<const char*> options_values = {backend_path.c_str()}; |
| 41 | + |
| 42 | + g_ort->SessionOptionsAppendExecutionProvider( |
| 43 | + session_options, "QNN", |
| 44 | + options_keys.data(), options_values.data(), options_keys.size() |
| 45 | + ); |
| 46 | + |
| 47 | + OrtSession* session; |
| 48 | + g_ort->CreateSession(env, model_path.c_str(), session_options, &session); |
| 49 | + std::cout << "[Successfully CreateSession]" << std::endl; |
| 50 | + |
| 51 | + OrtAllocator* allocator; |
| 52 | + g_ort->GetAllocatorWithDefaultOptions(&allocator); |
| 53 | + |
| 54 | + OnnxModelInfo model_info(g_ort, session, allocator); |
| 55 | + model_info.PrintOnnxModelInfo(); |
| 56 | + |
| 57 | + // Multiple test_data_set_X |
| 58 | + std::vector<std::basic_string<PATH_CHAR_TYPE>> test_data_sets = find_test_data_sets(model_dir); |
| 59 | + std::cout << "test_data_sets.size() " << test_data_sets.size() << std::endl; |
| 60 | + for (size_t idx = 0; idx < test_data_sets.size(); idx++) { |
| 61 | + std::cout << "---- test_data_set_" << idx << " ----" << std::endl; |
| 62 | + std::vector<std::vector<float>> input_data; |
| 63 | + auto test_data_set_dir = std::filesystem::path(test_data_sets[idx]); |
| 64 | + auto data_format = check_data_format(test_data_set_dir); |
| 65 | + if (data_format == "pb") { |
| 66 | + std::cout << "[test_data_sets_" << idx << "] " << "Loading .pb" << std::endl; |
| 67 | + load_input_tensors_from_pbs( |
| 68 | + test_data_set_dir, |
| 69 | + g_ort, |
| 70 | + &model_info, |
| 71 | + &input_data |
| 72 | + ); |
| 73 | + } else if (data_format == "raw") { |
| 74 | + std::cout << "[test_data_sets_" << idx << "] " << "Loading .raw" << std::endl; |
| 75 | + load_input_tensors_from_raws( |
| 76 | + test_data_set_dir, |
| 77 | + g_ort, |
| 78 | + &model_info, |
| 79 | + &input_data |
| 80 | + ); |
| 81 | + } |
| 82 | + std::cout << "[test_data_sets_" << idx << "] " << "Successfully Load Inputs" << std::endl; |
| 83 | + g_ort->Run( |
| 84 | + session, |
| 85 | + nullptr, |
| 86 | + model_info.get_in_tensor_names().data(), |
| 87 | + (const OrtValue* const*)model_info.get_in_tensors().data(), |
| 88 | + model_info.get_in_tensors().size(), |
| 89 | + model_info.get_out_tensor_names().data(), |
| 90 | + model_info.get_out_tensor_names().size(), |
| 91 | + model_info.get_out_tensors().data() |
| 92 | + ); |
| 93 | + std::cout << "[test_data_sets_" << idx << "] " << "Successfully Inference" << std::endl; |
| 94 | + if (data_format == "pb") { |
| 95 | + std::cout << "[test_data_sets_" << idx << "] " << "Dumping .pb" << std::endl; |
| 96 | + dump_output_tensors_to_pbs( |
| 97 | + test_data_set_dir, |
| 98 | + g_ort, |
| 99 | + &model_info |
| 100 | + ); |
| 101 | + } else if (data_format == "raw") { |
| 102 | + std::cout << "[test_data_sets_" << idx << "] " << "Dumping .raw" << std::endl; |
| 103 | + dump_output_tensors_to_raws( |
| 104 | + test_data_set_dir, |
| 105 | + g_ort, |
| 106 | + &model_info |
| 107 | + ); |
| 108 | + } |
| 109 | + std::cout << "[test_data_sets_" << idx << "] " << "Successfully Save Outputs" << std::endl; |
| 110 | + model_info.release_ort_values(g_ort); |
| 111 | + std::cout << "[test_data_sets_" << idx << "] " << "Successfully Release OrtValue" << std::endl; |
| 112 | + } |
| 113 | + return 0; |
| 114 | +} |
0 commit comments