|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "utils.h" |
| 4 | + |
| 5 | +#include <onnxruntime/core/graph/constants.h> |
| 6 | +#include <onnxruntime/core/session/onnxruntime_cxx_api.h> |
| 7 | + |
| 8 | +#include <filesystem> |
| 9 | +#include <functional> |
| 10 | +#include <string> |
| 11 | +#include <string_view> |
| 12 | + |
| 13 | +struct Opts { |
| 14 | + std::string input_image; |
| 15 | + std::string output_image; |
| 16 | + std::string select_vendor; |
| 17 | + std::string select_ep; |
| 18 | + bool enableEpContext{true}; |
| 19 | + OrtExecutionProviderDevicePolicy ep_device_policy = |
| 20 | + OrtExecutionProviderDevicePolicy_PREFER_GPU; |
| 21 | +}; |
| 22 | + |
| 23 | +struct ArgumentSpec { |
| 24 | + const char *name; |
| 25 | + const char *short_name; |
| 26 | + const char *help; |
| 27 | + int num_args; |
| 28 | + std::function<bool(int)> lambda; |
| 29 | +}; |
| 30 | + |
| 31 | +static Opts parse_args(int argc, char **argv) { |
| 32 | + using namespace std::string_view_literals; |
| 33 | + Opts opts; |
| 34 | + auto arg_specs = std::array{ |
| 35 | + // clang-format off |
| 36 | + ArgumentSpec{ |
| 37 | + "--input", "-i", "Path to input image (*.png)", 1, [&](int i) { |
| 38 | + opts.input_image = argv[i + 1]; |
| 39 | + if (opts.input_image.starts_with("-")) { |
| 40 | + LOG("Path to input image can't start with -: \"{}\"", |
| 41 | + opts.input_image.c_str()); |
| 42 | + return false; |
| 43 | + } |
| 44 | + return true; |
| 45 | + }}, |
| 46 | + ArgumentSpec{ |
| 47 | + "--output", "-o", "Path where to save output image (*.png)", 1, [&](int i) { |
| 48 | + opts.output_image = argv[i + 1]; |
| 49 | + if (opts.output_image.starts_with("-")) { |
| 50 | + LOG("Path to output image can't start with -: \"{}\"", |
| 51 | + opts.output_image.c_str()); |
| 52 | + return false; |
| 53 | + } |
| 54 | + return true; |
| 55 | + }}, |
| 56 | + ArgumentSpec{ |
| 57 | + "--select-vendor", "-f", "Select device of provided vendor.", 1, [&](int i) { |
| 58 | + opts.select_vendor = argv[i + 1]; |
| 59 | + if (opts.select_vendor.starts_with("-")) { |
| 60 | + LOG("Vendor can't start with -: \"{}\"", |
| 61 | + opts.select_vendor.c_str()); |
| 62 | + return false; |
| 63 | + } |
| 64 | + return true; |
| 65 | + }}, |
| 66 | + ArgumentSpec{ |
| 67 | + "--select-ep", "-f", "Select devices that support a specific execution provider. " |
| 68 | + "See https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/graph/constants.h for EP names." |
| 69 | + , 1, [&](int i) { |
| 70 | + opts.select_vendor = argv[i + 1]; |
| 71 | + if (opts.select_vendor.starts_with("-")) { |
| 72 | + LOG("Execution provider can't start with -: \"{}\"", |
| 73 | + opts.select_vendor.c_str()); |
| 74 | + return false; |
| 75 | + } |
| 76 | + return true; |
| 77 | + }}, |
| 78 | + ArgumentSpec{ |
| 79 | + "--ep-device-policy", "-p", "Set a EP device policy: e.g. prefer-cpu, prefer-gpu, prefer-npu, max-performance, max-efficiency, min-overall-power", 1, [&](int i) { |
| 80 | + if(argv[i+1] == "prefer-cpu"sv) { |
| 81 | + opts.ep_device_policy = OrtExecutionProviderDevicePolicy_PREFER_CPU; |
| 82 | + } else if(argv[i+1] == "prefer-gpu"sv) { |
| 83 | + opts.ep_device_policy = OrtExecutionProviderDevicePolicy_PREFER_GPU; |
| 84 | + } else if(argv[i+1] == "prefer-npu"sv) { |
| 85 | + opts.ep_device_policy = OrtExecutionProviderDevicePolicy_PREFER_NPU; |
| 86 | + } else if(argv[i+1] == "max-performance"sv) { |
| 87 | + opts.ep_device_policy = OrtExecutionProviderDevicePolicy_MAX_PERFORMANCE; |
| 88 | + } else if(argv[i+1] == "max-efficiency"sv) { |
| 89 | + opts.ep_device_policy = OrtExecutionProviderDevicePolicy_MAX_EFFICIENCY; |
| 90 | + } else if(argv[i+1] == "min-overall-power"sv) { |
| 91 | + opts.ep_device_policy = OrtExecutionProviderDevicePolicy_MIN_OVERALL_POWER; |
| 92 | + } else { |
| 93 | + LOG("Invalid execution provider policy: \"{}\"! Choose among prefer-cpu, prefer-gpu, prefer-npu, max-performance, max-efficiency, min-overall-power", argv[i+1]); |
| 94 | + return false; |
| 95 | + } |
| 96 | + return true; |
| 97 | + }} |
| 98 | + // clang-format on |
| 99 | + }; |
| 100 | + auto print_usage = [&] { |
| 101 | + LOG(""); |
| 102 | + LOG("Usage:"); |
| 103 | + LOG("{} <options>", argv[0]); |
| 104 | + for (auto &spec : arg_specs) { |
| 105 | + if (spec.short_name) { |
| 106 | + LOG("\t{} {} {}", spec.name, spec.short_name, spec.help); |
| 107 | + } else { |
| 108 | + LOG("\t{} {}", spec.name, spec.help); |
| 109 | + } |
| 110 | + } |
| 111 | + }; |
| 112 | + for (int i = 1; i < argc; i++) { |
| 113 | + bool arg_found = false; |
| 114 | + for (auto &spec : arg_specs) { |
| 115 | + if (std::strcmp(spec.name, argv[i]) == 0 || |
| 116 | + (spec.short_name && std::strcmp(spec.short_name, argv[i]) == 0)) { |
| 117 | + if (i + spec.num_args < argc) { |
| 118 | + bool ok = spec.lambda(i); |
| 119 | + if (!ok) { |
| 120 | + LOG("Failed to parse arguments for {}!", spec.name); |
| 121 | + exit(EXIT_FAILURE); |
| 122 | + } |
| 123 | + arg_found = true; |
| 124 | + i += spec.num_args; |
| 125 | + break; |
| 126 | + } else { |
| 127 | + LOG("Not enough arguments for {} specified!", spec.name); |
| 128 | + exit(EXIT_FAILURE); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + if (!arg_found) { |
| 133 | + auto arg = argv[i]; |
| 134 | + LOG("Unknown argument: {}", arg); |
| 135 | + print_usage(); |
| 136 | + exit(EXIT_FAILURE); |
| 137 | + } |
| 138 | + } |
| 139 | + if (opts.input_image.empty()) { |
| 140 | + opts.input_image = (get_executable_path().parent_path() / "Input.png").string(); |
| 141 | + } |
| 142 | + if (opts.output_image.empty()) { |
| 143 | + opts.output_image = (get_executable_path().parent_path() / "output.png").string(); |
| 144 | + } |
| 145 | + if (!std::filesystem::is_regular_file(opts.input_image)) { |
| 146 | + LOG("Please make sure that provided input image path exists: \"{}\"!", |
| 147 | + opts.input_image.c_str()); |
| 148 | + print_usage(); |
| 149 | + exit(EXIT_FAILURE); |
| 150 | + } |
| 151 | + if (!std::filesystem::is_directory( |
| 152 | + std::filesystem::path(opts.output_image).parent_path())) { |
| 153 | + LOG("Please make sure that the parent directory of the provided output " |
| 154 | + "path exists: \"{}\"!", |
| 155 | + opts.output_image.c_str()); |
| 156 | + print_usage(); |
| 157 | + exit(EXIT_FAILURE); |
| 158 | + } |
| 159 | + return opts; |
| 160 | +} |
0 commit comments