From e6e653650382ea277b5fb0054f5b7079c48b8b25 Mon Sep 17 00:00:00 2001 From: Krzysztof Bruniecki Date: Fri, 30 Apr 2021 17:14:37 +0200 Subject: [PATCH] Use gna execution target as consistent device --- .../src/gna_plugin/gna_device.cpp | 32 ++++++++++++++++--- .../src/gna_plugin/gna_device.hpp | 26 +++++++++------ .../src/gna_plugin/gna_plugin.cpp | 4 ++- .../src/gna_plugin/gna_plugin_config.cpp | 30 ++++++++--------- .../src/gna_plugin/gna_plugin_config.hpp | 4 +-- .../tests/unit/gna/gna_plugin_config_test.cpp | 8 ++--- 6 files changed, 68 insertions(+), 36 deletions(-) diff --git a/inference-engine/src/gna_plugin/gna_device.cpp b/inference-engine/src/gna_plugin/gna_device.cpp index 95899982ca3164..9e3fcfe5c19283 100644 --- a/inference-engine/src/gna_plugin/gna_device.cpp +++ b/inference-engine/src/gna_plugin/gna_device.cpp @@ -24,6 +24,7 @@ #include "gna-api.h" #endif +#include "gna/gna_config.hpp" #include "gna_plugin_log.hpp" //#define MODEL_DUMP @@ -130,7 +131,7 @@ void GNADeviceHelper::enforceLegacyCnns(Gna2Model& gnaModel) { uint32_t GNADeviceHelper::createModel(Gna2Model& gnaModel) const { std::unique_lock lockGnaCalls{ acrossPluginsSync }; uint32_t modelId; - if (isUpTo20GnaHwDevice() && isGnaLibVersion2_1) { + if (enforceLegacyCnnNeeded()) { enforceLegacyCnns(gnaModel); } #if GNA_LIB_VER == 2 && defined MODEL_DUMP @@ -154,15 +155,36 @@ void GNADeviceHelper::releaseModel(const uint32_t model_id) { checkGna2Status(status, "Gna2ModelRelease"); } +bool GNADeviceHelper::enforceLegacyCnnNeeded() const { + auto devVersion = getExecutionTargetDevice(); + return isGnaLibVersion2_1 && isUpTo20HwGnaDevice(devVersion); +} + +Gna2DeviceVersion GNADeviceHelper::getExecutionTargetDevice() const { + const volatile uint32_t executionTarget0x30 = 0x30; + if (executionTarget.empty()) { + if (detectedGnaDevVersion == Gna2DeviceVersionSoftwareEmulation) + return isGnaLibVersion2_1 ? static_cast(executionTarget0x30) : Gna2DeviceVersion2_0; + return detectedGnaDevVersion; + } else if (executionTarget == InferenceEngine::GNAConfigParams::GNA_TARGET_3_0) { + if (!isGnaLibVersion2_1) + THROW_GNA_EXCEPTION << "Unsupported GNA execution target " << executionTarget << " when GNA Library version is 2.0.X.X"; + return static_cast(executionTarget0x30); + } else if (executionTarget == InferenceEngine::GNAConfigParams::GNA_TARGET_2_0) { + return Gna2DeviceVersion2_0; + } + THROW_GNA_EXCEPTION << "Unknown execution target: \"" << executionTarget << "\""; +} + uint32_t GNADeviceHelper::createRequestConfig(const uint32_t model_id) { std::unique_lock lockGnaCalls{ acrossPluginsSync }; uint32_t reqConfId; auto status = Gna2RequestConfigCreate(model_id, &reqConfId); checkGna2Status(status, "Gna2RequestConfigCreate"); - if (gna2HwConsistency != Gna2DeviceVersionSoftwareEmulation && !isGnaLibVersion2_1) { - status = Gna2RequestConfigEnableHardwareConsistency(reqConfId, - isUpTo20GnaDevice() ? gna2HwConsistency : detectedGnaDevVersion); - checkGna2Status(status, "Gna2RequestConfigEnableHardwareConsistency"); + if (swExactMode) { + const auto consistentDevice = getExecutionTargetDevice(); + status = Gna2RequestConfigEnableHardwareConsistency(reqConfId, consistentDevice); + checkGna2Status(status, "Gna2RequestConfigEnableHardwareConsistency(" + std::to_string(static_cast(consistentDevice)) + ")"); } status = Gna2InstrumentationConfigAssignToRequestConfig(instrumentationConfigId, reqConfId); checkGna2Status(status, "Gna2InstrumentationConfigAssignToRequestConfig"); diff --git a/inference-engine/src/gna_plugin/gna_device.hpp b/inference-engine/src/gna_plugin/gna_device.hpp index ad639291e3db0c..831b9fde517352 100644 --- a/inference-engine/src/gna_plugin/gna_device.hpp +++ b/inference-engine/src/gna_plugin/gna_device.hpp @@ -51,8 +51,10 @@ class GNADeviceHelper { intel_gna_perf_t nGNAPerfResultsTotal; #else uint32_t nGnaDeviceIndex = 0; - Gna2DeviceVersion gna2HwConsistency = Gna2DeviceVersionSoftwareEmulation; + bool swExactMode = false; Gna2DeviceVersion detectedGnaDevVersion = Gna2DeviceVersionSoftwareEmulation; + std::string executionTarget; + std::string compileTarget; bool isGnaLibVersion2_1 = false; static const uint32_t TotalGna2InstrumentationPoints = 2; @@ -75,11 +77,15 @@ class GNADeviceHelper { bool isPerformanceMeasuring = false) : isPerformanceMeasuring(isPerformanceMeasuring) { #else - explicit GNADeviceHelper(Gna2DeviceVersion gna2HwConsistency = Gna2DeviceVersionSoftwareEmulation, + explicit GNADeviceHelper(std::string executionTargetIn = "", + std::string compileTargetIn = "", + bool swExactModeIn = false, uint8_t lib_async_n_threads = 1, bool use_openmp = false, bool isPerformanceMeasuring = false) : - gna2HwConsistency(gna2HwConsistency), + swExactMode(swExactModeIn), + executionTarget(executionTargetIn), + compileTarget(compileTargetIn), isPerformanceMeasuring(isPerformanceMeasuring), nGnaDeviceIndex{selectGnaDevice()} { #endif @@ -129,15 +135,17 @@ class GNADeviceHelper { uint32_t createRequestConfig(const uint32_t model_id); static uint32_t getNumberOfGnaDevices(); static uint32_t selectGnaDevice(); - bool hasGnaHw() const { - return Gna2DeviceVersionSoftwareEmulation != detectedGnaDevVersion; + static bool isGnaHw(const Gna2DeviceVersion dev) { + return Gna2DeviceVersionSoftwareEmulation != dev; } - bool isUpTo20GnaDevice() const { - return detectedGnaDevVersion <= Gna2DeviceVersion2_0; + bool hasGnaHw() const { + return isGnaHw(detectedGnaDevVersion); } - bool isUpTo20GnaHwDevice() const { - return isUpTo20GnaDevice() && detectedGnaDevVersion != Gna2DeviceVersionSoftwareEmulation; + static bool isUpTo20HwGnaDevice(const Gna2DeviceVersion dev) { + return dev <= Gna2DeviceVersion2_0 && isGnaHw(dev); } + bool enforceLegacyCnnNeeded() const; + Gna2DeviceVersion getExecutionTargetDevice() const; static void checkGna2Status(Gna2Status status, const std::string& from); static void checkGna2Status(Gna2Status status, const Gna2Model& gnaModel); #endif diff --git a/inference-engine/src/gna_plugin/gna_plugin.cpp b/inference-engine/src/gna_plugin/gna_plugin.cpp index e997e49cf91c46..9832d59d527a70 100644 --- a/inference-engine/src/gna_plugin/gna_plugin.cpp +++ b/inference-engine/src/gna_plugin/gna_plugin.cpp @@ -397,7 +397,9 @@ void GNAPlugin::InitGNADevice() { gnaFlags->gna_openmp_multithreading, gnaFlags->performance_counting); #else - gnadevice = std::make_shared(config.pluginGna2DeviceConsistent, + gnadevice = std::make_shared(config.gnaExecTarget, + config.gnaCompileTarget, + config.swExactMode, gnaFlags->gna_lib_async_threads_num, gnaFlags->gna_openmp_multithreading, gnaFlags->performance_counting); diff --git a/inference-engine/src/gna_plugin/gna_plugin_config.cpp b/inference-engine/src/gna_plugin/gna_plugin_config.cpp index 325df192d6f17e..2dcb05d6ab8a3f 100644 --- a/inference-engine/src/gna_plugin/gna_plugin_config.cpp +++ b/inference-engine/src/gna_plugin/gna_plugin_config.cpp @@ -33,19 +33,19 @@ static const std::vector supported_values_on_gna2 = { GNAConfigParams::GNA_AVX2_EXACT }; #else -static const caseless_unordered_map > supported_values = { - {GNAConfigParams::GNA_AUTO, {Gna2AccelerationModeAuto, Gna2DeviceVersionSoftwareEmulation}}, - {GNAConfigParams::GNA_HW, {Gna2AccelerationModeHardware, Gna2DeviceVersionSoftwareEmulation}}, - {GNAConfigParams::GNA_SW, {Gna2AccelerationModeSoftware, Gna2DeviceVersionSoftwareEmulation}}, - {GNAConfigParams::GNA_SW_EXACT, {Gna2AccelerationModeSoftware, Gna2DeviceVersion1_0}}, - {GNAConfigParams::GNA_GEN, {Gna2AccelerationModeGeneric, Gna2DeviceVersionSoftwareEmulation}}, - {GNAConfigParams::GNA_GEN_EXACT, {Gna2AccelerationModeGeneric, Gna2DeviceVersion1_0}}, - {GNAConfigParams::GNA_SSE, {Gna2AccelerationModeSse4x2, Gna2DeviceVersionSoftwareEmulation}}, - {GNAConfigParams::GNA_SSE_EXACT, {Gna2AccelerationModeSse4x2, Gna2DeviceVersion1_0}}, - {GNAConfigParams::GNA_AVX1, {Gna2AccelerationModeAvx1, Gna2DeviceVersionSoftwareEmulation}}, - {GNAConfigParams::GNA_AVX1_EXACT, {Gna2AccelerationModeAvx1, Gna2DeviceVersion1_0}}, - {GNAConfigParams::GNA_AVX2, {Gna2AccelerationModeAvx2, Gna2DeviceVersionSoftwareEmulation}}, - {GNAConfigParams::GNA_AVX2_EXACT, {Gna2AccelerationModeAvx2, Gna2DeviceVersion1_0}}, +static const caseless_unordered_map > supported_values = { + {GNAConfigParams::GNA_AUTO, {Gna2AccelerationModeAuto, false}}, + {GNAConfigParams::GNA_HW, {Gna2AccelerationModeHardware, false}}, + {GNAConfigParams::GNA_SW, {Gna2AccelerationModeSoftware, false}}, + {GNAConfigParams::GNA_SW_EXACT, {Gna2AccelerationModeSoftware, true}}, + {GNAConfigParams::GNA_GEN, {Gna2AccelerationModeGeneric, false}}, + {GNAConfigParams::GNA_GEN_EXACT, {Gna2AccelerationModeGeneric, true}}, + {GNAConfigParams::GNA_SSE, {Gna2AccelerationModeSse4x2, false}}, + {GNAConfigParams::GNA_SSE_EXACT, {Gna2AccelerationModeSse4x2, true}}, + {GNAConfigParams::GNA_AVX1, {Gna2AccelerationModeAvx1, false}}, + {GNAConfigParams::GNA_AVX1_EXACT, {Gna2AccelerationModeAvx1, true}}, + {GNAConfigParams::GNA_AVX2, {Gna2AccelerationModeAvx2, false}}, + {GNAConfigParams::GNA_AVX2_EXACT, {Gna2AccelerationModeAvx2, true}}, }; #endif @@ -122,7 +122,7 @@ void Config::UpdateFromMap(const std::map& config) { gna_proc_type = static_cast(procType->second); #else pluginGna2AccMode = procType->second.first; - pluginGna2DeviceConsistent = procType->second.second; + swExactMode = procType->second.second; #endif } } else if (key == GNA_CONFIG_KEY(EXEC_TARGET) || key == GNA_CONFIG_KEY(COMPILE_TARGET)) { @@ -266,7 +266,7 @@ void Config::AdjustKeyMapValues() { } #else if (value.second.first == pluginGna2AccMode && - value.second.second == pluginGna2DeviceConsistent) { + value.second.second == swExactMode) { device_mode = value.first; break; } diff --git a/inference-engine/src/gna_plugin/gna_plugin_config.hpp b/inference-engine/src/gna_plugin/gna_plugin_config.hpp index 80e0b1b80c699a..850c61c5a6eea5 100644 --- a/inference-engine/src/gna_plugin/gna_plugin_config.hpp +++ b/inference-engine/src/gna_plugin/gna_plugin_config.hpp @@ -37,7 +37,7 @@ struct Config { gna_proc_type = r.gna_proc_type; #else pluginGna2AccMode = r.pluginGna2AccMode; - pluginGna2DeviceConsistent = r.pluginGna2DeviceConsistent; + swExactMode = r.swExactMode; #endif inputScaleFactors = r.inputScaleFactors; gnaFlags = r.gnaFlags; @@ -62,7 +62,7 @@ struct Config { intel_gna_proc_t gna_proc_type = static_cast(GNA_SOFTWARE & GNA_HARDWARE); #else Gna2AccelerationMode pluginGna2AccMode = Gna2AccelerationModeSoftware; - Gna2DeviceVersion pluginGna2DeviceConsistent = Gna2DeviceVersion1_0; + bool swExactMode = true; #endif std::vector inputScaleFactors; diff --git a/inference-engine/tests/unit/gna/gna_plugin_config_test.cpp b/inference-engine/tests/unit/gna/gna_plugin_config_test.cpp index 8d464d0ee29f88..bdfd50ba03700d 100644 --- a/inference-engine/tests/unit/gna/gna_plugin_config_test.cpp +++ b/inference-engine/tests/unit/gna/gna_plugin_config_test.cpp @@ -106,28 +106,28 @@ TEST_F(GNAPluginConfigTest, GnaConfigDeviceModeTest) { EXPECT_EQ(config.gna_proc_type, static_cast(GNA_HARDWARE)); #else EXPECT_EQ(config.pluginGna2AccMode, Gna2AccelerationModeHardware); - EXPECT_EQ(config.pluginGna2DeviceConsistent, Gna2DeviceVersionSoftwareEmulation); + EXPECT_EQ(config.swExactMode, false); #endif SetAndCompare(GNA_CONFIG_KEY(DEVICE_MODE), GNAConfigParams::GNA_SW); #if GNA_LIB_VER == 1 EXPECT_EQ(config.gna_proc_type, static_cast(GNA_SOFTWARE)); #else EXPECT_EQ(config.pluginGna2AccMode, Gna2AccelerationModeSoftware); - EXPECT_EQ(config.pluginGna2DeviceConsistent, Gna2DeviceVersionSoftwareEmulation); + EXPECT_EQ(config.swExactMode, false); #endif SetAndCompare(GNA_CONFIG_KEY(DEVICE_MODE), GNAConfigParams::GNA_SW_EXACT); #if GNA_LIB_VER == 1 EXPECT_EQ(config.gna_proc_type, static_cast(GNA_SOFTWARE & GNA_HARDWARE)); #else EXPECT_EQ(config.pluginGna2AccMode, Gna2AccelerationModeSoftware); - EXPECT_EQ(config.pluginGna2DeviceConsistent, Gna2DeviceVersion1_0); + EXPECT_EQ(config.swExactMode, true); #endif SetAndCompare(GNA_CONFIG_KEY(DEVICE_MODE), GNAConfigParams::GNA_AUTO); #if GNA_LIB_VER == 1 EXPECT_EQ(config.gna_proc_type, static_cast(GNA_AUTO)); #else EXPECT_EQ(config.pluginGna2AccMode, Gna2AccelerationModeAuto); - EXPECT_EQ(config.pluginGna2DeviceConsistent, Gna2DeviceVersionSoftwareEmulation); + EXPECT_EQ(config.swExactMode, false); #endif ExpectThrow(GNA_CONFIG_KEY(DEVICE_MODE), ""); ExpectThrow(GNA_CONFIG_KEY(DEVICE_MODE), "abc");