From 82942a12b298b751881cddb9ee2c4da185122bf8 Mon Sep 17 00:00:00 2001 From: "Wang, Yang" Date: Wed, 26 Oct 2022 14:43:02 +0800 Subject: [PATCH] [AUTO] Benchmark failed to pass nthreads and pin to AUTO/MULTI (#13396) * Create device property for passing nthreads or pin to AUTO or MULTI in benchmark app. Signed-off-by: Wang, Yang * Update the logic of the multi device properties setting for the same target device. * refactor the logic to pass nthreads and pin to AUTO and MULTI. Signed-off-by: Wang, Yang Signed-off-by: Wang, Yang --- samples/cpp/benchmark_app/main.cpp | 55 ++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/samples/cpp/benchmark_app/main.cpp b/samples/cpp/benchmark_app/main.cpp index 85e84c7cdc9..d5aed347d2f 100755 --- a/samples/cpp/benchmark_app/main.cpp +++ b/samples/cpp/benchmark_app/main.cpp @@ -277,18 +277,14 @@ int main(int argc, char* argv[]) { // check if using the virtual device auto if_auto = std::find(devices.begin(), devices.end(), "AUTO") != devices.end(); auto if_multi = std::find(devices.begin(), devices.end(), "MULTI") != devices.end(); + auto hardware_devices = devices; // Remove the hardware devices if AUTO/MULTI appears in the devices list. if (if_auto || if_multi) { devices.clear(); - std::string virtual_device; - if (if_auto) { - virtual_device = "AUTO"; - devices.push_back("AUTO"); - } - if (if_multi) { - virtual_device = "MULTI"; - devices.push_back("MULTI"); - } + std::string virtual_device = if_auto ? "AUTO" : "MULTI"; + auto iter_virtual = std::find(hardware_devices.begin(), hardware_devices.end(), virtual_device); + hardware_devices.erase(iter_virtual); + devices.push_back(virtual_device); parse_value_for_virtual_device(virtual_device, device_nstreams); parse_value_for_virtual_device(virtual_device, device_infer_precision); } @@ -358,9 +354,15 @@ int main(int argc, char* argv[]) { std::stringstream strm(it_device_nstreams->second); std::map devices_property; ov::util::Read>{}(strm, devices_property); - for (auto& it : devices_property) { - device_config.insert( - ov::device::properties(it.first, ov::num_streams(std::stoi(it.second)))); + for (auto it : devices_property) { + if (device_config.find(it.first) == device_config.end()) + device_config.insert( + ov::device::properties(it.first, ov::num_streams(std::stoi(it.second)))); + else { + auto& property = device_config[it.first].as(); + property.emplace(ov::num_streams(std::stoi(it.second))); + device_config.insert(ov::device::properties(it.first, property)); + } } } } else { @@ -419,12 +421,29 @@ int main(int argc, char* argv[]) { return str; }; - if (supported(ov::inference_num_threads.name()) && isFlagSetInCommandLine("nthreads")) { - device_config.emplace(ov::inference_num_threads(FLAGS_nthreads)); - } - if (supported(ov::affinity.name()) && isFlagSetInCommandLine("pin")) { - device_config.emplace(ov::affinity(fix_pin_option(FLAGS_pin))); - } + auto set_nthreads_pin = [&](const std::string& str) { + auto property_name = str == "nthreads" ? ov::inference_num_threads.name() : ov::affinity.name(); + auto property = str == "nthreads" ? ov::inference_num_threads(FLAGS_nthreads) + : ov::affinity(fix_pin_option(FLAGS_pin)); + if (supported(property_name) || if_multi) { + device_config.emplace(property); + } else if (if_auto) { + for (auto& device : hardware_devices) { + if (device_config.find(device) == device_config.end()) { + device_config.insert(ov::device::properties(device, property)); + } else { + auto& properties = device_config[device].as(); + properties.emplace(property); + device_config.insert(ov::device::properties(device, properties)); + } + } + } + }; + if (isFlagSetInCommandLine("nthreads")) + set_nthreads_pin("nthreads"); + + if (isFlagSetInCommandLine("pin")) + set_nthreads_pin("pin"); if (device.find("CPU") != std::string::npos || device.find("GPU") != std::string::npos) { // CPU supports few special performance-oriented keys