Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[SIT] Add post proc options for l2norm (#36900)

Vigh Sebastian71a64cffaf01
diff --git a/src/plugins/intel_npu/tools/single-image-test/README.md b/src/plugins/intel_npu/tools/single-image-test/README.md
index ba19a795de3..cedeadf14e1 100644
--- a/src/plugins/intel_npu/tools/single-image-test/README.md
+++ b/src/plugins/intel_npu/tools/single-image-test/README.md
@@ -107,6 +107,18 @@ single-image-test.exe: Usage: Release\single-image-test.exe[<options>]
default: ""
-is_tiny_yolo (Is it Tiny Yolo or not (true or false)?) type: bool
default: false
+ -l2norm_dequant_scale (Optional. Dequantization scale factor applied to
+ the inference output tensor(s) before the 'l2norm' comparison is
+ performed: dequantized = (quantized - zero_point) * scale. Can be a
+ single global value applied to all outputs, or per-output:
+ 'output1:0.00392;output2:0.01'. If not set, dequantization is skipped
+ and outputs are compared as-is. The reference tensor is never
+ modified.) type: string default: ""
+ -l2norm_dequant_zp (Optional. Dequantization zero point used together
+ with --l2norm_dequant_scale for 'l2norm' mode. Can be a single global
+ value or per-output: 'output1:128;output2:0'. Defaults to 0 for any
+ output without an explicit value. Has no effect unless
+ --l2norm_dequant_scale is set.) type: string default: ""
-l2norm_threshold (Threshold for 'l2norm' mode. Can be a single value or
per-layer: 'layer1:1.0;layer2:2.0') type: string default: "1.000000"
-log_level (IE logger level (optional)) type: string default: ""
diff --git a/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.cpp b/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.cpp
index 1f56a019403..a82a5aaf14d 100644
--- a/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.cpp
+++ b/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.cpp
@@ -106,6 +106,15 @@ DEFINE_string(nrmse_prefill_seq_len_size, "",
"is compared.");
DEFINE_string(l2norm_threshold, std::to_string(metric_defaults::l2norm_threshold),
"Threshold for 'l2norm' mode. Can be a single value or per-layer: 'layer1:1.0;layer2:2.0'");
+DEFINE_string(l2norm_dequant_scale, "",
+ "Optional. Dequantization scale factor applied to the inference output tensor(s) before the "
+ "'l2norm' comparison is performed: dequantized = (quantized - zero_point) * scale. Can be a single "
+ "global value applied to all outputs, or per-output: 'output1:0.00392;output2:0.01'. If not set, "
+ "dequantization is skipped and outputs are compared as-is. The reference tensor is never modified.");
+DEFINE_string(l2norm_dequant_zp, "",
+ "Optional. Dequantization zero point used together with --l2norm_dequant_scale for 'l2norm' mode. "
+ "Can be a single global value or per-output: 'output1:128;output2:0'. Defaults to 0 for any output "
+ "without an explicit value. Has no effect unless --l2norm_dequant_scale is set.");
DEFINE_string(overlap_threshold, std::to_string(metric_defaults::overlap_threshold),
"IoU threshold for 'map' mode (detection matching). " \
"Can be a single value or per-layer: 'layer1:0.5;layer2:0.6'");
@@ -206,6 +215,11 @@ void utils::parseCommandLine(int argc, char* argv[]) {
}
} else if (strEq(FLAGS_mode, "l2norm")) {
std::cout << " Threshold: " << FLAGS_l2norm_threshold << std::endl;
+ if (!FLAGS_l2norm_dequant_scale.empty()) {
+ std::cout << " Dequant scale: " << FLAGS_l2norm_dequant_scale << std::endl;
+ std::cout << " Dequant zero point: "
+ << (FLAGS_l2norm_dequant_zp.empty() ? "0" : FLAGS_l2norm_dequant_zp) << std::endl;
+ }
}
}
std::cout << " Log level: " << FLAGS_log_level << std::endl;
@@ -298,3 +312,27 @@ double utils::getValueForLayer(const PerLayerValueMap& valueMap, const std::stri
// Should never be reached for properly initialised maps.
return 0.0;
}
+
+/**
+ * @brief Checks whether a per-layer value map represents a single global value that applies to every
+ * layer (as opposed to an explicit per-layer specification with a wildcard fallback default).
+ * @param valueMap Map produced by parsePerLayerValues
+ * @return true if the map only contains the wildcard entry
+ */
+bool utils::isGlobalValue(const PerLayerValueMap& valueMap) {
+ return valueMap.size() == 1 && valueMap.count("*") != 0;
+}
+
+/**
+ * @brief Get the explicit value set for a specific layer, ignoring the wildcard fallback.
+ * @param valueMap Map of layer name to value (as produced by parsePerLayerValues)
+ * @param layerName Name of the layer
+ * @return The explicitly configured value for the layer, or std::nullopt if the layer has no explicit entry
+ */
+std::optional<double> utils::getExplicitValueForLayer(const PerLayerValueMap& valueMap, const std::string& layerName) {
+ auto it = valueMap.find(layerName);
+ if (it != valueMap.end()) {
+ return it->second;
+ }
+ return std::nullopt;
+}
diff --git a/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.hpp b/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.hpp
index 4be061f3fa5..43a6e6ee1f1 100644
--- a/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.hpp
+++ b/src/plugins/intel_npu/tools/single-image-test/argument_parse_helpers.hpp
@@ -6,6 +6,7 @@
#include <limits>
#include <map>
+#include <optional>
#include <string>
#include <vector>
@@ -19,6 +20,7 @@ namespace metric_defaults {
constexpr double rrmse_loss_threshold = std::numeric_limits<double>::max();
constexpr double nrmse_loss_threshold = 0.02;
constexpr double l2norm_threshold = 1.0;
+ constexpr double l2norm_dequant_zp = 0.0;
constexpr double overlap_threshold = 0.50;
constexpr double map_threshold = 0.50;
constexpr double confidence_threshold = 1e-4;
@@ -87,6 +89,8 @@ DECLARE_string(nrmse_loss_threshold);
DECLARE_string(nrmse_prefill_seq_len_axis);
DECLARE_string(nrmse_prefill_seq_len_size);
DECLARE_string(l2norm_threshold);
+DECLARE_string(l2norm_dequant_scale);
+DECLARE_string(l2norm_dequant_zp);
DECLARE_string(overlap_threshold);
DECLARE_string(map_threshold);
DECLARE_string(confidence_threshold);
@@ -117,4 +121,20 @@ using PerLayerValueMap = std::map<std::string, double>;
PerLayerValueMap parsePerLayerValues(const std::string& str, double defaultValue);
double getValueForLayer(const PerLayerValueMap& valueMap, const std::string& layerName);
+/**
+ * @brief Checks whether a per-layer value map represents a single global value that applies to every
+ * layer (as opposed to an explicit per-layer specification with a wildcard fallback default).
+ * @param valueMap Map produced by parsePerLayerValues
+ * @return true if the map only contains the wildcard entry
+ */
+bool isGlobalValue(const PerLayerValueMap& valueMap);
+
+/**
+ * @brief Get the explicit value set for a specific layer, ignoring the wildcard fallback.
+ * @param valueMap Map of layer name to value (as produced by parsePerLayerValues)
+ * @param layerName Name of the layer
+ * @return The explicitly configured value for the layer, or std::nullopt if the layer has no explicit entry
+ */
+std::optional<double> getExplicitValueForLayer(const PerLayerValueMap& valueMap, const std::string& layerName);
+
} // namespace utils
diff --git a/src/plugins/intel_npu/tools/single-image-test/main.cpp b/src/plugins/intel_npu/tools/single-image-test/main.cpp
index 66992927b2f..4b146a8049e 100644
--- a/src/plugins/intel_npu/tools/single-image-test/main.cpp
+++ b/src/plugins/intel_npu/tools/single-image-test/main.cpp
@@ -1903,7 +1903,8 @@ bool testNRMSE(const TensorMap& outputs, const TensorMap& references, const Layo
// Direction of metric’s growth is lower-better. If the inputs are identical, the L2NORM is zero.
//
-bool computeL2Norm(const ov::Tensor& output, const ov::Tensor& reference, double threshold) {
+bool computeL2Norm(const ov::Tensor& output, const ov::Tensor& reference, double threshold,
+ const std::optional<std::pair<double, double>>& dequant) {
if (output.get_size() != reference.get_size()) {
std::cout << "Output and reference tensors have different sizes" << std::endl;
return false;
@@ -1913,9 +1914,22 @@ bool computeL2Norm(const ov::Tensor& output, const ov::Tensor& reference, double
const ov::Tensor referenceFP32 = npu::utils::toFP32(reference);
const auto size = outputFP32.get_size();
- const auto* outputData = outputFP32.data<const float>();
const auto* referenceData = referenceFP32.data<const float>();
+ // Optionally dequantize the inference output before comparing against the (untouched) reference:
+ // dequantized = (quantized - zero_point) * scale. The reference tensor is never modified.
+ std::vector<float> dequantizedOutput;
+ const float* outputData = outputFP32.data<const float>();
+ if (dequant.has_value()) {
+ const double scale = dequant->first;
+ const double zeroPoint = dequant->second;
+ dequantizedOutput.resize(size);
+ for (size_t i = 0; i < size; ++i) {
+ dequantizedOutput[i] = static_cast<float>((static_cast<double>(outputData[i]) - zeroPoint) * scale);
+ }
+ outputData = dequantizedOutput.data();
+ }
+
double sumSquares = 0.0;
for (size_t i = 0; i < size; ++i) {
const double diff = static_cast<double>(outputData[i]) - static_cast<double>(referenceData[i]);
@@ -1939,6 +1953,15 @@ bool testL2Norm(const TensorMap& outputs, const TensorMap& references, const Lay
// Parse per-layer thresholds
auto thresholdMap = utils::parsePerLayerValues(FLAGS_l2norm_threshold, metric_defaults::l2norm_threshold);
+ // Parse optional per-layer dequantization scale/zero-point, used to convert quantized integer
+ // outputs to floating point before the L2Norm comparison. Dequantization is skipped entirely when
+ // --l2norm_dequant_scale is not set.
+ const bool dequantEnabled = !FLAGS_l2norm_dequant_scale.empty();
+ auto scaleMap = utils::parsePerLayerValues(FLAGS_l2norm_dequant_scale, 1.0);
+ auto zpMap = utils::parsePerLayerValues(FLAGS_l2norm_dequant_zp, metric_defaults::l2norm_dequant_zp);
+ const bool scaleIsGlobal = utils::isGlobalValue(scaleMap);
+ const bool zpIsGlobal = utils::isGlobalValue(zpMap);
+
bool allPassed = true;
for (auto& [tensorName, output] : outputs) {
auto referencesIterator = references.find(tensorName);
@@ -1946,8 +1969,21 @@ bool testL2Norm(const TensorMap& outputs, const TensorMap& references, const Lay
double layerThreshold = utils::getValueForLayer(thresholdMap, tensorName);
- BlobTestMethod blobComparator = [layerThreshold](ov::Tensor outputTensor, ov::Tensor referenceTensor) {
- return computeL2Norm(outputTensor, referenceTensor, layerThreshold);
+ std::optional<std::pair<double, double>> dequant;
+ if (dequantEnabled) {
+ std::optional<double> scale =
+ scaleIsGlobal ? std::optional<double>(utils::getValueForLayer(scaleMap, tensorName))
+ : utils::getExplicitValueForLayer(scaleMap, tensorName);
+ if (scale.has_value()) {
+ const double zeroPoint = zpIsGlobal ? utils::getValueForLayer(zpMap, tensorName)
+ : utils::getExplicitValueForLayer(zpMap, tensorName)
+ .value_or(metric_defaults::l2norm_dequant_zp);
+ dequant = std::make_pair(*scale, zeroPoint);
+ }
+ }
+
+ BlobTestMethod blobComparator = [layerThreshold, dequant](ov::Tensor outputTensor, ov::Tensor referenceTensor) {
+ return computeL2Norm(outputTensor, referenceTensor, layerThreshold, dequant);
};
if (!test_blobs_in_batch(tensorName,