Skip to content

Commit

Permalink
Use gna execution target as consistent device
Browse files Browse the repository at this point in the history
  • Loading branch information
kbruniec committed May 5, 2021
1 parent 5a236e3 commit e6e6536
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 36 deletions.
32 changes: 27 additions & 5 deletions inference-engine/src/gna_plugin/gna_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gna-api.h"
#endif

#include "gna/gna_config.hpp"
#include "gna_plugin_log.hpp"

//#define MODEL_DUMP
Expand Down Expand Up @@ -130,7 +131,7 @@ void GNADeviceHelper::enforceLegacyCnns(Gna2Model& gnaModel) {
uint32_t GNADeviceHelper::createModel(Gna2Model& gnaModel) const {
std::unique_lock<std::mutex> lockGnaCalls{ acrossPluginsSync };
uint32_t modelId;
if (isUpTo20GnaHwDevice() && isGnaLibVersion2_1) {
if (enforceLegacyCnnNeeded()) {
enforceLegacyCnns(gnaModel);
}
#if GNA_LIB_VER == 2 && defined MODEL_DUMP
Expand All @@ -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<Gna2DeviceVersion>(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<Gna2DeviceVersion>(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<std::mutex> 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<long>(consistentDevice)) + ")");
}
status = Gna2InstrumentationConfigAssignToRequestConfig(instrumentationConfigId, reqConfId);
checkGna2Status(status, "Gna2InstrumentationConfigAssignToRequestConfig");
Expand Down
26 changes: 17 additions & 9 deletions inference-engine/src/gna_plugin/gna_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion inference-engine/src/gna_plugin/gna_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ void GNAPlugin::InitGNADevice() {
gnaFlags->gna_openmp_multithreading,
gnaFlags->performance_counting);
#else
gnadevice = std::make_shared<GNADeviceHelper>(config.pluginGna2DeviceConsistent,
gnadevice = std::make_shared<GNADeviceHelper>(config.gnaExecTarget,
config.gnaCompileTarget,
config.swExactMode,
gnaFlags->gna_lib_async_threads_num,
gnaFlags->gna_openmp_multithreading,
gnaFlags->performance_counting);
Expand Down
30 changes: 15 additions & 15 deletions inference-engine/src/gna_plugin/gna_plugin_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ static const std::vector<std::string> supported_values_on_gna2 = {
GNAConfigParams::GNA_AVX2_EXACT
};
#else
static const caseless_unordered_map <std::string, std::pair<Gna2AccelerationMode, Gna2DeviceVersion>> 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 <std::string, std::pair<Gna2AccelerationMode, bool>> 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

Expand Down Expand Up @@ -122,7 +122,7 @@ void Config::UpdateFromMap(const std::map<std::string, std::string>& config) {
gna_proc_type = static_cast<intel_gna_proc_t>(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)) {
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions inference-engine/src/gna_plugin/gna_plugin_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -62,7 +62,7 @@ struct Config {
intel_gna_proc_t gna_proc_type = static_cast<intel_gna_proc_t>(GNA_SOFTWARE & GNA_HARDWARE);
#else
Gna2AccelerationMode pluginGna2AccMode = Gna2AccelerationModeSoftware;
Gna2DeviceVersion pluginGna2DeviceConsistent = Gna2DeviceVersion1_0;
bool swExactMode = true;
#endif

std::vector<float> inputScaleFactors;
Expand Down
8 changes: 4 additions & 4 deletions inference-engine/tests/unit/gna/gna_plugin_config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,28 @@ TEST_F(GNAPluginConfigTest, GnaConfigDeviceModeTest) {
EXPECT_EQ(config.gna_proc_type, static_cast<intel_gna_proc_t>(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<intel_gna_proc_t>(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<intel_gna_proc_t>(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<intel_gna_proc_t>(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");
Expand Down

0 comments on commit e6e6536

Please sign in to comment.