OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[SIT] Add per input dynamic inputs (#38979)
Vigh Sebastiane18405f20aee
diff --git a/src/plugins/intel_npu/tools/common/include/tools_helpers.hpp b/src/plugins/intel_npu/tools/common/include/tools_helpers.hppindex cc638d5f503..583d35f6d49 100644--- a/src/plugins/intel_npu/tools/common/include/tools_helpers.hpp+++ b/src/plugins/intel_npu/tools/common/include/tools_helpers.hpp@@ -18,7 +18,9 @@ struct InputInfo {};using InputsInfo = std::map<std::string, InputInfo>;-std::string parameterNameToTensorName(std::string& name, std::vector<ov::Output<ov::Node>>& inputs_info) {+// NodeType is either ov::Node (ov::Model::inputs()) or const ov::Node (ov::CompiledModel::inputs()).+template <typename NodeType>+std::string parameterNameToTensorName(const std::string& name, const std::vector<ov::Output<NodeType>>& inputs_info) {// Looking for a tensor name matchfor (const auto& port : inputs_info) {if (port.get_names().count(name) > 0) {@@ -35,8 +37,9 @@ std::string parameterNameToTensorName(std::string& name, std::vector<ov::Output<"\" is not found neither in tensor names nor in nodes names.");}-std::map<std::string, std::vector<std::string>> parseInputParameters(std::string& parameter_string,- std::vector<ov::Output<ov::Node>>& input_info) {+template <typename NodeType>+std::map<std::string, std::vector<std::string>> parseInputParameters(const std::string& parameter_string,+ const std::vector<ov::Output<NodeType>>& input_info) {// Parse parameter string like "input0[value0],input1[value1]" or "[value]" (applied to all// inputs)std::map<std::string, std::vector<std::string>> return_value;@@ -52,7 +55,7 @@ std::map<std::string, std::vector<std::string>> parseInputParameters(std::stringif (!input_name.empty()) {return_value[parameterNameToTensorName(input_name, input_info)].push_back(std::move(input_value));} else {- for (auto& item : input_info) {+ for (const auto& item : input_info) {return_value[item.get_any_name()].push_back(input_value);}}diff --git a/src/plugins/intel_npu/tools/single-image-test/README.md b/src/plugins/intel_npu/tools/single-image-test/README.mdindex acc03bc26b5..12d52b221b0 100644--- a/src/plugins/intel_npu/tools/single-image-test/README.md+++ b/src/plugins/intel_npu/tools/single-image-test/README.md@@ -89,8 +89,9 @@ single-image-test.exe: Usage: Release\single-image-test.exe[<options>]-cosim_threshold (Threshold for 'cosim' mode. Can be a single value orper-layer: 'layer1:0.95;layer2:0.90') type: string default: "0.900000"-data_shape (Required for models with dynamic shapes. Set shape for input- blobs. Only one shape can be set. In case of one input size:- "[1,3,224,224]") type: string default: ""+ blobs. Only one shape can be set per input. In case of one input size:+ "[1,3,224,224]", applied to every dynamic input. For several inputs:+ "input1[1,3,224,224],input2[1,4]") type: string default: ""-dataset (The dataset used to train the model. Useful for instances such assemantic segmentation to visualize the accuracy per-class) type: stringdefault: "NONE"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.cppindex 6daa008868c..1e72822de1a 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@@ -49,8 +49,9 @@ DEFINE_string(shape, ""," For dynamic dimensions use symbol `?` or '-1'. Ex. [?,3,?,?]."" For bounded dimensions specify range 'min..max'. Ex. [1..10,3,?,?].");DEFINE_string(data_shape, "",- "Required for models with dynamic shapes. Set shape for input blobs. Only one shape can be set."- "In case of one input size: \"[1,3,224,224]\"");+ "Required for models with dynamic shapes. Set shape for input blobs. Only one shape can be set per input."+ " In case of one input size: \"[1,3,224,224]\", applied to every dynamic input."+ " For several inputs: \"input1[1,3,224,224],input2[1,4]\".");DEFINE_string(skip_output_layers, "", "Skip output layers from the network."" Accept ';' separated list of output layers");DEFINE_bool(clamp_u8_outputs, false, "Apply clamping when converting FP to U8");diff --git a/src/plugins/intel_npu/tools/single-image-test/main.cpp b/src/plugins/intel_npu/tools/single-image-test/main.cppindex dc093953903..e3fbf57500f 100644--- a/src/plugins/intel_npu/tools/single-image-test/main.cpp+++ b/src/plugins/intel_npu/tools/single-image-test/main.cpp@@ -18,6 +18,8 @@#include <opencv2/imgcodecs.hpp>#include <opencv2/imgproc.hpp>+#include <algorithm>+#include <cctype>#include <cmath>#include <filesystem>#include <fstream>@@ -2414,20 +2416,71 @@ bool testMeanIoU(const TensorMap& outputs, const TensorMap& references, const LablobComparator);}-static ov::Shape parseDataShape(const std::string& dataShapeStr) {- std::vector<uint64_t> dataShape;- std::stringstream ss(dataShapeStr);+// Parses a comma separated list of non-negative dimensions, e.g. "1,3,224,224".+static ov::Shape parseDimensions(const std::string& dimensionsStr) {+ ov::Shape dataShape;+ std::stringstream ss(dimensionsStr);+ std::string dimStr;- char ch; // To discard non-numeric characters- int64_t dim;- while (ss >> ch) {- if (std::isdigit(ch)) {- ss.putback(ch);- ss >> dim;- dataShape.push_back(dim);+ while (std::getline(ss, dimStr, ',')) {+ const bool isNumber = !dimStr.empty() && std::all_of(dimStr.begin(), dimStr.end(), [](unsigned char ch) {+ return std::isdigit(ch) != 0;+ });+ OPENVINO_ASSERT(isNumber, "Dimension \"", dimStr, "\" of the shape \"", dimensionsStr,+ "\" is not a non-negative integer");+ try {+ dataShape.push_back(std::stoull(dimStr));+ } catch (const std::out_of_range&) {+ OPENVINO_THROW("Dimension \"", dimStr, "\" of the shape \"", dimensionsStr, "\" is out of range");}}- return ov::Shape(dataShape);+ OPENVINO_ASSERT(!dataShape.empty(), "Can't parse \"", dimensionsStr, "\" as a shape");+ return dataShape;+}++// Data shapes by tensor name, as returned by ov::Output::get_any_name().+using DataShapes = std::map<std::string, ov::Shape>;++// Parses --data_shape. Accepted formats:+// - "[1,3,224,224]" or "1,3,224,224": a single shape used for every input;+// - "input1[1,3,224,224],input2[1,4]": one shape per input.+static DataShapes parseDataShapes(std::string dataShapeStr,+ const std::vector<ov::Output<const ov::Node>>& inputsInfo) {+ dataShapeStr.erase(std::remove_if(dataShapeStr.begin(), dataShapeStr.end(),+ [](unsigned char ch) {+ return std::isspace(ch) != 0;+ }),+ dataShapeStr.end());++ DataShapes dataShapes;+ if (dataShapeStr.empty()) {+ return dataShapes;+ }++ // The shared parser expects bracketed values, while --data_shape also accepts a bare "1,3,224,224".+ const std::string bracketed =+ dataShapeStr.find('[') == std::string::npos ? "[" + dataShapeStr + "]" : dataShapeStr;++ std::map<std::string, std::vector<std::string>> shapesPerInput;+ try {+ shapesPerInput = parseInputParameters(bracketed, inputsInfo);+ } catch (const std::exception& ex) {+ OPENVINO_THROW("Can't parse --data_shape \"", dataShapeStr, "\": ", ex.what());+ }++ for (const auto& entry : shapesPerInput) {+ OPENVINO_ASSERT(entry.second.size() == 1, "--data_shape contains more than one shape for input \"",+ entry.first, "\"");+ dataShapes.emplace(entry.first, parseDimensions(entry.second.front()));+ }+ return dataShapes;+}++static ov::Shape getDataShape(const DataShapes& dataShapes, const ov::Output<const ov::Node>& inputInfo) {+ const auto it = dataShapes.find(inputInfo.get_any_name());+ OPENVINO_ASSERT(it != dataShapes.end(), "--data_shape has no shape for the dynamic input \"",+ inputInfo.get_any_name(), "\"");+ return it->second;}std::string getRefBlobFilePath(const std::string& blobNamePrefix, const std::vector<std::string>& refFiles,@@ -2762,6 +2815,8 @@ static int runSingleImageTest() {const std::string blobNamePrefix =FLAGS_blob_name_prefix.empty() ? netFileName : cleanName(FLAGS_blob_name_prefix);+ const DataShapes dataShapes = parseDataShapes(FLAGS_data_shape, compiledModel.inputs());+for (size_t numberOfTestCase = 0; numberOfTestCase < inputFilesPerCase.size(); ++numberOfTestCase) {const auto inputsInfo = compiledModel.inputs();const auto outputsInfo = compiledModel.outputs();@@ -2783,7 +2838,9 @@ static int runSingleImageTest() {// Load the input datafor (const auto& inputInfo : inputsInfo) {const auto& shape = inputInfo.get_partial_shape();- const auto dataShape = shape.is_static() ? shape.get_shape() : parseDataShape(FLAGS_data_shape);+ const auto dataShape = shape.is_static() ? shape.get_shape() : getDataShape(dataShapes, inputInfo);+ OPENVINO_ASSERT(shape.compatible(ov::PartialShape(dataShape)), "Data shape ", dataShape,+ " of the input \"", inputInfo.get_any_name(), "\" is not compatible with ", shape);const ov::element::Type& precision = inputInfo.get_element_type();// Determine the input layout