[GNA] Support of the tensor names for scale factors (#14632)

* Support of the tensor names for scale factors
* Fixed transpose detection pattern
This commit is contained in:
Mikhail Ryzhov
2023-01-10 09:54:38 +01:00
committed by GitHub
parent 36f9d57023
commit d6a4635fd5
7 changed files with 147 additions and 80 deletions

View File

@@ -435,22 +435,11 @@ void GNAPlugin::UpdateInputsAndOutputsInfoFromNetwork(InferenceEngine::CNNNetwor
// update inputs
{
InputsDataMap network_inputs = network.getInputsInfo();
size_t id = 0;
for (const auto& input : network_inputs) {
(*inputs_ptr_)[input.first].Update(input.second);
// update scale factor from config
if (config.inputScaleFactorsPerInput.count(input.first)) {
(*inputs_ptr_)[input.first].scale_factor = config.inputScaleFactorsPerInput[input.first];
} else if (id < config.inputScaleFactors.size()) {
config.inputScaleFactorsPerInput[input.first] = config.inputScaleFactors[id];
(*inputs_ptr_)[input.first].scale_factor = config.inputScaleFactorsPerInput[input.first];
}
id++;
}
}
// update outputs
{
OutputsDataMap outputs = network.getOutputsInfo();
@@ -799,6 +788,10 @@ void GNAPlugin::LoadNetwork(const CNNNetwork& _network) {
// Set input and output information from orginal network
UpdateInputsAndOutputsInfoFromNetwork(network);
// DEPRECATED: To be removed after fully switching to POT optimized models.
// Set Scale Factors for inputs according to configuration.
ov::intel_gna::helpers::ApplyInputScaleFactors(*inputs_ptr_, config);
if (fake_quantized) {
UpdateInputScaleFromNetwork(network);
}
@@ -1636,7 +1629,7 @@ InferenceEngine::IExecutableNetworkInternal::Ptr GNAPlugin::ImportNetwork(std::i
SetNetworkInputs();
SetNetworkOutputs();
ov::intel_gna::helpers::ApplyInputScaleFactors(config, header, *inputs_ptr_);
ov::intel_gna::helpers::ApplyInputScaleFactors(*inputs_ptr_, config, header);
auto getOrientation = [](Gna2Operation& gnaOperation) {
return gnaOperation.Type == Gna2OperationTypeConvolution ? kDnnNonInterleavedOrientation

View File

@@ -34,10 +34,14 @@ static void ApplyScaleFactorsLegacy(const std::vector<float>& input_scale_factor
IE_THROW() << "Configuration input scale factors count is bigger than inputs count";
}
for (size_t id = 0; id < input_scale_factors.size(); ++id) {
::log::warning() << "Using input scale factor: " << input_scale_factors[id]
for (size_t id = 0; id < inputs.size(); ++id) {
log::warning() << "Using input scale factor: " << input_scale_factors[id]
<< ", defined in configuration for input id: " << id << std::endl;
inputs.Get().at(id).scale_factor = input_scale_factors[id];
if (input_scale_factors.size() > id) {
inputs.Get().at(id).scale_factor = input_scale_factors[id];
} else {
log::warning() << "Using default input scale factor: " << kScaleFactorDefault << " for input id: " << id << std::endl;
}
}
}
@@ -64,7 +68,11 @@ static void ApplyScaleFactorsPerInput(const std::map<std::string, float>& per_in
}
for (auto&& sf : per_input_scale_factors) {
auto input_it = inputs.find(sf.first);
// to support the both legacy and 2.0 API we need to check all possible names in the configuration
auto input_it = std::find_if(inputs.Get().begin(), inputs.Get().end(), [&](const InputDesc& input_desc) {
return sf.first == input_desc.name || input_desc.tensor_names.count(sf.first);
});
if (input_it == inputs.end()) {
IE_THROW() << "Given scale factor for invalid input: " << sf.first;
}
@@ -85,16 +93,11 @@ static bool CheckIfCanApplyCustomScaleFactor(const header_latest::ModelHeader& h
return true;
}
void ApplyInputScaleFactors(const Config& config,
const header_latest::ModelHeader& header,
GnaInputs& inputs) {
// If scale factors are defined in configuration we still need to use them instead of imported values,
// for example to change the scale factors for the old models.
const bool custom_scale_factor_per_input =
IsCustomInputScaleFactorPerInputAvailable(config.inputScaleFactorsPerInput);
const bool custom_scale_factor_legacy = IsCustomInputScaleFactorAvailableLegacy(config.inputScaleFactors);
if (!custom_scale_factor_per_input && !custom_scale_factor_legacy) {
void ApplyInputScaleFactors(GnaInputs& inputs, const Config& config, const header_latest::ModelHeader& header) {
// we have to check that SF exist in the configuration and values are not default
const bool custom_scale_factors = IsCustomInputScaleFactorPerInputAvailable(config.inputScaleFactorsPerInput);
const bool custom_scale_factors_legacy = IsCustomInputScaleFactorAvailableLegacy(config.inputScaleFactors);
if (!custom_scale_factors && !custom_scale_factors_legacy) {
return;
}
@@ -102,14 +105,23 @@ void ApplyInputScaleFactors(const Config& config,
return;
}
// Due the fact inputScaleFactors is set by defuault construcor of Config
// we need to check is_intput_scale_factor_per_input_given as first.
if (custom_scale_factor_per_input) {
if (custom_scale_factors) {
ApplyScaleFactorsPerInput(config.inputScaleFactorsPerInput, inputs);
return;
} else if (custom_scale_factors_legacy) {
ApplyScaleFactorsLegacy(config.inputScaleFactors, inputs);
}
ApplyScaleFactorsLegacy(config.inputScaleFactors, inputs);
return;
}
void ApplyInputScaleFactors(GnaInputs& inputs, const Config& config) {
if (IsCustomInputScaleFactorPerInputAvailable(config.inputScaleFactorsPerInput)) {
ApplyScaleFactorsPerInput(config.inputScaleFactorsPerInput, inputs);
} else if (IsCustomInputScaleFactorAvailableLegacy(config.inputScaleFactors)) {
ApplyScaleFactorsLegacy(config.inputScaleFactors, inputs);
}
return;
}
} // namespace helpers

View File

@@ -19,9 +19,8 @@ class Config;
*/
namespace helpers {
void ApplyInputScaleFactors(const Config& config,
const header_latest::ModelHeader& header,
GnaInputs& inputs);
void ApplyInputScaleFactors(GnaInputs& inputs, const Config& config, const header_latest::ModelHeader& header);
void ApplyInputScaleFactors(GnaInputs& inputs, const Config& config);
} // namespace helpers
} // namespace intel_gna

View File

@@ -25,11 +25,6 @@ std::vector<ov::test::ElementType> netPrecisions = {
ov::element::f16,
};
std::vector<ngraph::helpers::InputLayerType> secondaryInputTypes = {
ngraph::helpers::InputLayerType::CONSTANT,
ngraph::helpers::InputLayerType::PARAMETER,
};
std::vector<CommonTestUtils::OpType> opTypes = {
CommonTestUtils::OpType::SCALAR,
CommonTestUtils::OpType::VECTOR,
@@ -41,7 +36,18 @@ std::vector<ngraph::helpers::EltwiseTypes> eltwiseOpTypes = {
ngraph::helpers::EltwiseTypes::ADD
};
std::vector<ov::AnyMap> additional_config = {
std::vector<ov::AnyMap> additional_config_inputs_1 = {
{
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
{"GNA_SCALE_FACTOR_0", "1638.4"}
},
{
{"GNA_DEVICE_MODE", "GNA_SW_FP32"},
{"GNA_SCALE_FACTOR_0", "1638.4"}
}
};
std::vector<ov::AnyMap> additional_config_inputs_2 = {
{
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
{"GNA_SCALE_FACTOR_0", "1638.4"},
@@ -54,16 +60,30 @@ std::vector<ov::AnyMap> additional_config = {
}
};
const auto multiply_params = ::testing::Combine(
const auto multiply_params_1 = ::testing::Combine(
::testing::ValuesIn(ov::test::static_shapes_to_test_representation(inShapes)),
::testing::ValuesIn(eltwiseOpTypes),
::testing::ValuesIn(secondaryInputTypes),
::testing::Values(ngraph::helpers::InputLayerType::CONSTANT),
::testing::ValuesIn(opTypes),
::testing::ValuesIn(netPrecisions),
::testing::Values(ov::element::undefined),
::testing::Values(ov::element::undefined),
::testing::Values(CommonTestUtils::DEVICE_GNA),
::testing::ValuesIn(additional_config));
::testing::ValuesIn(additional_config_inputs_1));
const auto multiply_params_2 = ::testing::Combine(
::testing::ValuesIn(ov::test::static_shapes_to_test_representation(inShapes)),
::testing::ValuesIn(eltwiseOpTypes),
::testing::Values(ngraph::helpers::InputLayerType::PARAMETER),
::testing::ValuesIn(opTypes),
::testing::ValuesIn(netPrecisions),
::testing::Values(ov::element::undefined),
::testing::Values(ov::element::undefined),
::testing::Values(CommonTestUtils::DEVICE_GNA),
::testing::ValuesIn(additional_config_inputs_2));
INSTANTIATE_TEST_SUITE_P(smoke_EltwiseLayerConstTest, EltwiseLayerTest, multiply_params_1, EltwiseLayerTest::getTestCaseName);
INSTANTIATE_TEST_SUITE_P(smoke_EltwiseLayerParamTest, EltwiseLayerTest, multiply_params_2, EltwiseLayerTest::getTestCaseName);
INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs, EltwiseLayerTest, multiply_params, EltwiseLayerTest::getTestCaseName);
} // namespace

View File

@@ -41,10 +41,6 @@ namespace {
};
std::map<std::string, std::string> additional_config = {
{"GNA_SCALE_FACTOR_1", "1"},
{"GNA_SCALE_FACTOR_2", "1"},
{"GNA_SCALE_FACTOR_3", "1"},
{"GNA_SCALE_FACTOR_4", "1"},
{std::string(GNA_CONFIG_KEY(COMPACT_MODE)), "NO"}
};

View File

@@ -84,8 +84,8 @@ public:
bool _legacy_scale_factor;
};
TEST_P(GNAApplyInputScaleFactorsTest, test_scale_factor_set_properly) {
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(_config, _header, _gna_inputs));
TEST_P(GNAApplyInputScaleFactorsTest, test_import_scale_factor_set_properly) {
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(_gna_inputs, _config, _header));
for (const auto& input : _test_inputs) {
auto& input_ref = _gna_inputs[input.first];
if (_applicable) {
@@ -96,6 +96,14 @@ TEST_P(GNAApplyInputScaleFactorsTest, test_scale_factor_set_properly) {
}
}
TEST_P(GNAApplyInputScaleFactorsTest, test_load_scale_factor_set_properly) {
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(_gna_inputs, _config));
for (const auto& input : _test_inputs) {
auto& input_ref = _gna_inputs[input.first];
EXPECT_FLOAT_EQ(input_ref.scale_factor, input.second);
}
}
TEST_P(GNAApplyInputScaleFactorsTest, inputs_count_not_match_to_scale_factors) {
if (_legacy_scale_factor) {
_config.inputScaleFactors.push_back(10.0);
@@ -104,10 +112,10 @@ TEST_P(GNAApplyInputScaleFactorsTest, inputs_count_not_match_to_scale_factors) {
}
if (_applicable) {
EXPECT_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(_config, _header, _gna_inputs), std::exception);
EXPECT_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(_gna_inputs, _config, _header), std::exception);
} else {
// check if no exception and data are not changed
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(_config, _header, _gna_inputs));
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(_gna_inputs, _config, _header));
for (const auto& input : _test_inputs) {
auto& input_ref = _gna_inputs[input.first];
EXPECT_FLOAT_EQ(input_ref.scale_factor, kScaleFactorDefault);
@@ -139,7 +147,7 @@ TEST(smoke_GNAApplyInputScaleFactorsTest, test_default_scale_factor) {
config.inputScaleFactors.clear();
config.inputScaleFactorsPerInput.clear();
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(config, header, inputs));
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(inputs, config, header));
EXPECT_FLOAT_EQ(input_ref.scale_factor, kScaleFactorDefault);
}
@@ -161,5 +169,30 @@ TEST(smoke_GNAApplyInputScaleFactorsTest, test_wrong_scale_factor_config_input_n
config.inputScaleFactorsPerInput[name_1] = 2.0;
config.inputScaleFactorsPerInput[name_2 + "__"] = 2.0;
EXPECT_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(config, header, gna_inputs), std::exception);
EXPECT_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(gna_inputs, config, header), std::exception);
}
// Tests if input scale factor name matches to the tensor name
TEST_P(GNAApplyInputScaleFactorsTest, test_scale_factor_config_input_tensor_name) {
Config config;
GnaInputs gna_inputs;
const std::string name_1{"input_1"};
const std::string tensor_name_1{"input_1:0"};
const std::string name_2{"input_2"};
const std::string tensor_name_2{"input_2:0"};
gna_inputs[name_1].tensor_names = {tensor_name_1};
gna_inputs[name_2].tensor_names = {tensor_name_2};
config.inputScaleFactorsPerInput.clear();
config.inputScaleFactorsPerInput[tensor_name_1] = 2.0;
config.inputScaleFactorsPerInput[name_2] = 2.0;
EXPECT_NO_THROW(ov::intel_gna::helpers::ApplyInputScaleFactors(gna_inputs, config));
for (const auto& input : gna_inputs.Get()) {
EXPECT_FLOAT_EQ(input.scale_factor, 2.0);
}
}

View File

@@ -6,51 +6,65 @@
static const ti_test_params ti_test_cases[] = {{"GNA", 8, InferenceEngine::Precision(InferenceEngine::Precision::FP32)}};
static const std::map<std::string, std::string> config_fp32 =
{{"GNA_DEVICE_MODE", "GNA_SW_FP32"}, {"GNA_COMPACT_MODE", "NO"}};
static const std::map<std::string, std::string> config_I16 = {
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
{"GNA_COMPACT_MODE", "NO"},
{"GNA_PRECISION", "I16"},
{"GNA_SCALE_FACTOR_0", "1024"},
{"GNA_SCALE_FACTOR_1", "1024"},
{"GNA_SCALE_FACTOR_2", "1024"}
static std::map<std::string, std::string> config_fp32 = {
{"GNA_DEVICE_MODE", "GNA_SW_FP32"},
{"GNA_COMPACT_MODE", "NO"}
};
static const std::map<std::string, std::string> config_I8 = {
static std::map<std::string, std::string> config_I16 = {
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
{"GNA_COMPACT_MODE", "NO"},
{"GNA_PRECISION", "I16"},
{"GNA_SCALE_FACTOR_0", "1024"},
{"GNA_SCALE_FACTOR_1", "1024"},
{"GNA_SCALE_FACTOR_2", "1024"}
{"GNA_PRECISION", "I16"}
};
static std::map<std::string, std::string> config_I8 = {
{"GNA_DEVICE_MODE", "GNA_SW_EXACT"},
{"GNA_COMPACT_MODE", "NO"},
{"GNA_PRECISION", "I16"}
};
static const std::map<std::string, std::string> config_input_1 = {
{"GNA_SCALE_FACTOR_0", "1024"}
};
static const std::map<std::string, std::string> config_input_2 = {
{"GNA_SCALE_FACTOR_0", "1024"},
{"GNA_SCALE_FACTOR_1", "1024"}
};
TEST_P(TITestBase, GNA_sw_fp32_ti_test) {
RunTITest(config_fp32);
std::map<std::string, std::string> test_config(config_fp32);
test_config.insert(config_input_2.begin(), config_input_2.end());
RunTITest(test_config);
}
TEST_P(TITestBase, GNA_I16_ti_test) {
RunTITest(config_I16);
std::map<std::string, std::string> test_config(config_I16);
test_config.insert(config_input_2.begin(), config_input_2.end());
RunTITest(test_config);
}
TEST_P(TITestBase, GNA_I8_ti_test) {
RunTITest(config_I8);
}
std::map<std::string, std::string> test_config(config_I8);
test_config.insert(config_input_2.begin(), config_input_2.end());
RunTITest(test_config);}
RUN_CASE_P_WITH_SUFFIX(GNA, _smoke, TITestBase, ti_test_cases);
TEST_P(TITest2Base, GNA_sw_fp32_ti_test) {
RunTITest(config_fp32);
}
std::map<std::string, std::string> test_config(config_fp32);
test_config.insert(config_input_1.begin(), config_input_1.end());
RunTITest(test_config);}
TEST_P(TITest2Base, GNA_I16_ti_test) {
RunTITest(config_I16);
}
std::map<std::string, std::string> test_config(config_I16);
test_config.insert(config_input_1.begin(), config_input_1.end());
RunTITest(test_config);}
TEST_P(TITest2Base, GNA_I8_ti_test) {
RunTITest(config_I8);
}
std::map<std::string, std::string> test_config(config_I8);
test_config.insert(config_input_1.begin(), config_input_1.end());
RunTITest(test_config);}
RUN_CASE_P_WITH_SUFFIX(GNA, _smoke, TITest2Base, ti_test_cases);