[GNA] clear input scale factor from configuration for imported model (#2172)
* [GNA] clear input scale factor from configuration for imported model * add test * [GNA] add warning for using imported scale factors
This commit is contained in:
parent
2be6032c8d
commit
1cfa47cde8
@ -602,41 +602,50 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (FLAGS_q.compare("user") == 0) {
|
if (FLAGS_q.compare("user") == 0) {
|
||||||
auto scaleFactorInput = ParseScaleFactors(FLAGS_sf);
|
if (!FLAGS_rg.empty()) {
|
||||||
if (numInputArkFiles != scaleFactorInput.size()) {
|
slog::warn << "Custom scale factor will be ignored - using scale factor from provided imported gna model: "
|
||||||
std::string errMessage("Incorrect command line for multiple inputs: "
|
<< FLAGS_rg << slog::endl;
|
||||||
+ std::to_string(scaleFactorInput.size()) + " scale factors provided for "
|
} else {
|
||||||
+ std::to_string(numInputArkFiles) + " input files.");
|
auto scaleFactorInput = ParseScaleFactors(FLAGS_sf);
|
||||||
throw std::logic_error(errMessage);
|
if (numInputArkFiles != scaleFactorInput.size()) {
|
||||||
}
|
std::string errMessage("Incorrect command line for multiple inputs: "
|
||||||
|
+ std::to_string(scaleFactorInput.size()) + " scale factors provided for "
|
||||||
|
+ std::to_string(numInputArkFiles) + " input files.");
|
||||||
|
throw std::logic_error(errMessage);
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < scaleFactorInput.size(); ++i) {
|
for (size_t i = 0; i < scaleFactorInput.size(); ++i) {
|
||||||
slog::info << "For input " << i << " using scale factor of " << scaleFactorInput[i] << slog::endl;
|
slog::info << "For input " << i << " using scale factor of " << scaleFactorInput[i] << slog::endl;
|
||||||
std::string scaleFactorConfigKey = GNA_CONFIG_KEY(SCALE_FACTOR) + std::string("_") + std::to_string(i);
|
std::string scaleFactorConfigKey = GNA_CONFIG_KEY(SCALE_FACTOR) + std::string("_") + std::to_string(i);
|
||||||
gnaPluginConfig[scaleFactorConfigKey] = scaleFactorInput[i];
|
gnaPluginConfig[scaleFactorConfigKey] = scaleFactorInput[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// "static" quantization with calculated scale factor
|
// "static" quantization with calculated scale factor
|
||||||
for (size_t i = 0; i < numInputArkFiles; i++) {
|
if (!FLAGS_rg.empty()) {
|
||||||
auto inputArkName = inputArkFiles[i].c_str();
|
slog::info << "Using scale factor from provided imported gna model: " << FLAGS_rg << slog::endl;
|
||||||
std::string name;
|
} else {
|
||||||
std::vector<uint8_t> ptrFeatures;
|
for (size_t i = 0; i < numInputArkFiles; i++) {
|
||||||
uint32_t numArrays(0), numBytes(0), numFrames(0), numFrameElements(0), numBytesPerElement(0);
|
auto inputArkName = inputArkFiles[i].c_str();
|
||||||
GetKaldiArkInfo(inputArkName, 0, &numArrays, &numBytes);
|
std::string name;
|
||||||
ptrFeatures.resize(numBytes);
|
std::vector<uint8_t> ptrFeatures;
|
||||||
LoadKaldiArkArray(inputArkName,
|
uint32_t numArrays(0), numBytes(0), numFrames(0), numFrameElements(0), numBytesPerElement(0);
|
||||||
0,
|
GetKaldiArkInfo(inputArkName, 0, &numArrays, &numBytes);
|
||||||
name,
|
ptrFeatures.resize(numBytes);
|
||||||
ptrFeatures,
|
LoadKaldiArkArray(inputArkName,
|
||||||
&numFrames,
|
0,
|
||||||
&numFrameElements,
|
name,
|
||||||
&numBytesPerElement);
|
ptrFeatures,
|
||||||
auto floatScaleFactor =
|
&numFrames,
|
||||||
|
&numFrameElements,
|
||||||
|
&numBytesPerElement);
|
||||||
|
auto floatScaleFactor =
|
||||||
ScaleFactorForQuantization(ptrFeatures.data(), MAX_VAL_2B_FEAT, numFrames * numFrameElements);
|
ScaleFactorForQuantization(ptrFeatures.data(), MAX_VAL_2B_FEAT, numFrames * numFrameElements);
|
||||||
slog::info << "Using scale factor of " << floatScaleFactor << " calculated from first utterance."
|
slog::info << "Using scale factor of " << floatScaleFactor << " calculated from first utterance."
|
||||||
<< slog::endl;
|
<< slog::endl;
|
||||||
std::string scaleFactorConfigKey = GNA_CONFIG_KEY(SCALE_FACTOR) + std::string("_") + std::to_string(i);
|
std::string scaleFactorConfigKey = GNA_CONFIG_KEY(SCALE_FACTOR) + std::string("_") + std::to_string(i);
|
||||||
gnaPluginConfig[scaleFactorConfigKey] = std::to_string(floatScaleFactor);
|
gnaPluginConfig[scaleFactorConfigKey] = std::to_string(floatScaleFactor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1180,6 +1180,12 @@ InferenceEngine::IExecutableNetwork::Ptr GNAPlugin::ImportNetwork(std::istream&
|
|||||||
auto serial = GNAModelSerial(&std::get<0>(nnets.back())->obj, mt);
|
auto serial = GNAModelSerial(&std::get<0>(nnets.back())->obj, mt);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (!inputsDesc->inputScaleFactors.empty()) {
|
||||||
|
gnalog() << "[Import Network] Clearing input scale factors defined in configuration. "
|
||||||
|
<< "Scale factors provided in imported model will be used\n";
|
||||||
|
inputsDesc->inputScaleFactors.clear();
|
||||||
|
}
|
||||||
|
|
||||||
serial.setHeader(header);
|
serial.setHeader(header);
|
||||||
serial.Import(basePtr,
|
serial.Import(basePtr,
|
||||||
header.gnaMemSize,
|
header.gnaMemSize,
|
||||||
|
@ -23,7 +23,8 @@
|
|||||||
typedef std::tuple<
|
typedef std::tuple<
|
||||||
InferenceEngine::Precision, // Network Precision
|
InferenceEngine::Precision, // Network Precision
|
||||||
std::string, // Target Device
|
std::string, // Target Device
|
||||||
std::map<std::string, std::string> //Configuration
|
std::map<std::string, std::string>, // Export Configuration
|
||||||
|
std::map<std::string, std::string> // Import Configuration
|
||||||
> exportImportNetworkParams;
|
> exportImportNetworkParams;
|
||||||
|
|
||||||
namespace LayerTestsDefinitions {
|
namespace LayerTestsDefinitions {
|
||||||
@ -34,14 +35,18 @@ class ImportNetworkTest : public testing::WithParamInterface<exportImportNetwork
|
|||||||
static std::string getTestCaseName(testing::TestParamInfo<exportImportNetworkParams> obj) {
|
static std::string getTestCaseName(testing::TestParamInfo<exportImportNetworkParams> obj) {
|
||||||
InferenceEngine::Precision netPrecision;
|
InferenceEngine::Precision netPrecision;
|
||||||
std::string targetDevice;
|
std::string targetDevice;
|
||||||
std::map<std::string, std::string> configuration;
|
std::map<std::string, std::string> exportConfiguration;
|
||||||
std::tie(netPrecision, targetDevice, configuration) = obj.param;
|
std::map<std::string, std::string> importConfiguration;
|
||||||
|
std::tie(netPrecision, targetDevice, exportConfiguration, importConfiguration) = obj.param;
|
||||||
|
|
||||||
std::ostringstream result;
|
std::ostringstream result;
|
||||||
result << "netPRC=" << netPrecision.name() << "_";
|
result << "netPRC=" << netPrecision.name() << "_";
|
||||||
result << "targetDevice=" << targetDevice << "_";
|
result << "targetDevice=" << targetDevice << "_";
|
||||||
for (auto const& configItem : configuration) {
|
for (auto const& configItem : exportConfiguration) {
|
||||||
result << "_configItem=" << configItem.first << "_" << configItem.second;
|
result << "_exportConfigItem=" << configItem.first << "_" << configItem.second;
|
||||||
|
}
|
||||||
|
for (auto const& configItem : importConfiguration) {
|
||||||
|
result << "_importConfigItem=" << configItem.first << "_" << configItem.second;
|
||||||
}
|
}
|
||||||
return result.str();
|
return result.str();
|
||||||
}
|
}
|
||||||
@ -49,6 +54,7 @@ class ImportNetworkTest : public testing::WithParamInterface<exportImportNetwork
|
|||||||
void Run() override {
|
void Run() override {
|
||||||
SKIP_IF_CURRENT_TEST_IS_DISABLED()
|
SKIP_IF_CURRENT_TEST_IS_DISABLED()
|
||||||
|
|
||||||
|
configuration.insert(exportConfiguration.begin(), exportConfiguration.end());
|
||||||
LoadNetwork();
|
LoadNetwork();
|
||||||
Infer();
|
Infer();
|
||||||
executableNetwork.Export("exported_model.blob");
|
executableNetwork.Export("exported_model.blob");
|
||||||
@ -57,6 +63,9 @@ class ImportNetworkTest : public testing::WithParamInterface<exportImportNetwork
|
|||||||
auto referenceOutputs = CalculateRefs();
|
auto referenceOutputs = CalculateRefs();
|
||||||
Compare(referenceOutputs, actualOutputs);
|
Compare(referenceOutputs, actualOutputs);
|
||||||
|
|
||||||
|
for (auto const& configItem : importConfiguration) {
|
||||||
|
configuration[configItem.first] = configItem.second;
|
||||||
|
}
|
||||||
std::fstream inputStream("exported_model.blob", std::ios_base::in | std::ios_base::binary);
|
std::fstream inputStream("exported_model.blob", std::ios_base::in | std::ios_base::binary);
|
||||||
if (inputStream.fail()) {
|
if (inputStream.fail()) {
|
||||||
FAIL() << "Cannot open file to import model: exported_model.blob";
|
FAIL() << "Cannot open file to import model: exported_model.blob";
|
||||||
@ -68,7 +77,7 @@ class ImportNetworkTest : public testing::WithParamInterface<exportImportNetwork
|
|||||||
protected:
|
protected:
|
||||||
void SetUp() override {
|
void SetUp() override {
|
||||||
InferenceEngine::Precision netPrecision;
|
InferenceEngine::Precision netPrecision;
|
||||||
std::tie(netPrecision, targetDevice, configuration) = this->GetParam();
|
std::tie(netPrecision, targetDevice, exportConfiguration, importConfiguration) = this->GetParam();
|
||||||
auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision);
|
auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision);
|
||||||
|
|
||||||
auto params = ngraph::builder::makeParams(ngPrc, { {1, 336} });
|
auto params = ngraph::builder::makeParams(ngPrc, { {1, 336} });
|
||||||
@ -95,6 +104,9 @@ class ImportNetworkTest : public testing::WithParamInterface<exportImportNetwork
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::map<std::string, std::string> exportConfiguration;
|
||||||
|
std::map<std::string, std::string> importConfiguration;
|
||||||
|
|
||||||
std::vector<std::vector<std::uint8_t>> CalculateImportedNetwork(std::istream& networkModel) {
|
std::vector<std::vector<std::uint8_t>> CalculateImportedNetwork(std::istream& networkModel) {
|
||||||
auto importedNetwork = core->ImportNetwork(networkModel, targetDevice, configuration);
|
auto importedNetwork = core->ImportNetwork(networkModel, targetDevice, configuration);
|
||||||
|
|
||||||
@ -149,18 +161,30 @@ class ImportNetworkTest : public testing::WithParamInterface<exportImportNetwork
|
|||||||
InferenceEngine::Precision::FP16
|
InferenceEngine::Precision::FP16
|
||||||
};
|
};
|
||||||
|
|
||||||
const std::vector<std::map<std::string, std::string>> configs = {
|
const std::vector<std::map<std::string, std::string>> exportConfigs = {
|
||||||
{
|
{
|
||||||
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
|
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
|
||||||
{"GNA_SCALE_FACTOR_0", "327.67"}
|
{"GNA_SCALE_FACTOR_0", "327.67"}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const std::vector<std::map<std::string, std::string>> importConfigs = {
|
||||||
|
{
|
||||||
|
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
|
||||||
|
{"GNA_SCALE_FACTOR_0", "32767"}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
|
||||||
|
{"GNA_SCALE_FACTOR_0", "327.67"}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
INSTANTIATE_TEST_CASE_P(ImportNetworkCase, ImportNetworkTest,
|
INSTANTIATE_TEST_CASE_P(ImportNetworkCase, ImportNetworkTest,
|
||||||
::testing::Combine(
|
::testing::Combine(
|
||||||
::testing::ValuesIn(netPrecisions),
|
::testing::ValuesIn(netPrecisions),
|
||||||
::testing::Values(CommonTestUtils::DEVICE_GNA),
|
::testing::Values(CommonTestUtils::DEVICE_GNA),
|
||||||
::testing::ValuesIn(configs)),
|
::testing::ValuesIn(exportConfigs),
|
||||||
|
::testing::ValuesIn(importConfigs)),
|
||||||
ImportNetworkTest::getTestCaseName);
|
ImportNetworkTest::getTestCaseName);
|
||||||
|
|
||||||
} // namespace LayerTestsDefinitions
|
} // namespace LayerTestsDefinitions
|
||||||
|
Loading…
Reference in New Issue
Block a user