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
[`NPU`] General NPU optimizations for `set_tensor` API (#33934)
Mircea-Aurelian Dan8f1e6829dc8e
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hppindex 7fb3454c54b..04df1e9ea63 100644--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hpp+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hpp@@ -173,7 +173,6 @@ struct OptionPrinter final {ss << std::fixed << std::setprecision(2) << val;} else if constexpr (std::is_enum_v<std::decay_t<T>>) {ss << stringifyEnum(val);- return ss.str();} else {ss << val;}@@ -437,6 +436,7 @@ public:private:std::unordered_map<std::string, details::OptionConcept> _impl;std::unordered_map<std::string, std::string> _deprecated;+ Logger _log{Logger::global().clone("OptionsDesc")};};template <class Opt>@@ -460,7 +460,7 @@ void OptionsDesc::add(std::optional<std::function<bool(std::string_view)>> custoclass Config {public:using ConfigMap = std::map<std::string, std::string>;- using ImplMap = std::unordered_map<std::string, std::shared_ptr<details::OptionValue>>;+ using ImplMap = std::unordered_map<std::string_view, std::shared_ptr<details::OptionValue>>;explicit Config(const std::shared_ptr<const OptionsDesc>& desc);@@ -489,6 +489,9 @@ public:protected:std::shared_ptr<const OptionsDesc> _desc;ImplMap _impl;++private:+ Logger _log{Logger::global().clone("Config")};};template <class Opt>@@ -500,14 +503,13 @@ template <class Opt>typename Opt::ValueType Config::get() const {using ValueType = typename Opt::ValueType;- auto log = Logger::global().clone("Config");- log.trace("Get value for the option '%s'", Opt::key().data());+ _log.trace("Get value for the option '%s'", Opt::key().data());- const auto it = _impl.find(Opt::key().data());+ const auto it = _impl.find(Opt::key());if (it == _impl.end()) {const std::optional<ValueType> optional = Opt::defaultValue();- log.trace("The option '%s' was not set by user, try default value", Opt::key().data());+ _log.trace("The option '%s' was not set by user, try default value", Opt::key().data());OPENVINO_ASSERT(optional.has_value(),"Option '",diff --git a/src/plugins/intel_npu/src/al/src/config/config.cpp b/src/plugins/intel_npu/src/al/src/config/config.cppindex 97edac8b46e..58ca92f7df9 100644--- a/src/plugins/intel_npu/src/al/src/config/config.cpp+++ b/src/plugins/intel_npu/src/al/src/config/config.cpp@@ -150,13 +150,11 @@ details::OptionValue::~OptionValue() = default;//details::OptionConcept OptionsDesc::get(std::string_view key, OptionMode mode) const {- auto log = Logger::global().clone("OptionsDesc");-std::string searchKey{key};const auto itDeprecated = _deprecated.find(std::string(key));if (itDeprecated != _deprecated.end()) {searchKey = itDeprecated->second;- log.warning("Deprecated option '%s' was used, '%s' should be used instead", key.data(), searchKey.c_str());+ _log.warning("Deprecated option '%s' was used, '%s' should be used instead", key.data(), searchKey.c_str());}const auto itMain = _impl.find(searchKey);@@ -169,10 +167,10 @@ details::OptionConcept OptionsDesc::get(std::string_view key, OptionMode mode) cif (mode == OptionMode::RunTime) {if (desc.mode() == OptionMode::CompileTime) {- log.warning("%s option '%s' was used in %s mode",- stringifyEnum(desc.mode()).data(),- key.data(),- stringifyEnum(mode).data());+ _log.warning("%s option '%s' was used in %s mode",+ stringifyEnum(desc.mode()).data(),+ key.data(),+ stringifyEnum(mode).data());}}@@ -202,7 +200,7 @@ std::vector<std::string> OptionsDesc::getSupported(bool includePrivate) const {for (const auto& p : _impl) {if (p.second.isPublic() || includePrivate) {- res.push_back(p.first);+ res.push_back(p.first.data());}}@@ -215,7 +213,7 @@ std::vector<ov::PropertyName> OptionsDesc::getSupportedOptions(bool includePrivafor (const auto& p : _impl) {if (p.second.isPublic() || includePrivate) {- res.push_back({p.first, p.second.mutability()});+ res.push_back({p.first.data(), p.second.mutability()});}}@@ -250,15 +248,13 @@ Config::Config(const std::shared_ptr<const OptionsDesc>& desc) : _desc(desc) {}void Config::parseEnvVars() {- auto log = Logger::global().clone("Config");-_desc->walk([&](const details::OptionConcept& opt) {if (!opt.envVar().empty()) {if (const auto envVar = std::getenv(opt.envVar().data())) {- log.trace("Update option '%s' to value '%s' parsed from environment variable '%s'",- opt.key().data(),- envVar,- opt.envVar().data());+ _log.trace("Update option '%s' to value '%s' parsed from environment variable '%s'",+ opt.key().data(),+ envVar,+ opt.envVar().data());_impl[opt.key().data()] = opt.validateAndParse(envVar);}@@ -271,10 +267,8 @@ bool Config::has(std::string key) const {}void Config::update(const ConfigMap& options, OptionMode mode) {- auto log = Logger::global().clone("Config");-for (const auto& p : options) {- log.trace("Update option '%s' to value '%s'", p.first.c_str(), p.second.c_str());+ _log.trace("Update option '%s' to value '%s'", p.first.c_str(), p.second.c_str());const auto opt = _desc->get(p.first, mode);_impl[opt.key().data()] = opt.validateAndParse(p.second);diff --git a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cppindex 3a6f4b03b71..bf4450566e1 100644--- a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp+++ b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp@@ -68,8 +68,6 @@ void* get_tensor_data_ptr(const std::shared_ptr<ov::ITensor>& tensor) {} else {return tensor->data();}-- return nullptr;}} // namespace@@ -282,7 +280,7 @@ void ZeroInferRequest::set_tensor(const ov::Output<const ov::Node>& port, constreturn;}- auto ioShape = _compiledModel->inputs()[foundPort.idx].get_partial_shape();+ const auto& ioShape = _compiledModel->inputs()[foundPort.idx].get_partial_shape();auto batchSizeCandidate =determine_dynamic_batch_size(_metadata.inputs.at(foundPort.idx), ioShape, tensor._ptr, std::nullopt);@@ -324,7 +322,7 @@ void ZeroInferRequest::set_tensor(const ov::Output<const ov::Node>& port, constreturn;}- auto ioShape = _compiledModel->outputs()[foundPort.idx].get_partial_shape();+ const auto& ioShape = _compiledModel->outputs()[foundPort.idx].get_partial_shape();auto batchSizeCandidate =determine_dynamic_batch_size(_metadata.outputs.at(foundPort.idx), ioShape, tensor._ptr, std::nullopt);@@ -416,7 +414,7 @@ void ZeroInferRequest::set_tensors(const ov::Output<const ov::Node>& port,_logger.debug("ZeroInferRequest::set_tensors: %zu", tensors.size());- auto ioShape = _compiledModel->inputs()[foundPort.idx].get_partial_shape();+ const auto& ioShape = _compiledModel->inputs()[foundPort.idx].get_partial_shape();auto batchSizeCandidate =determine_dynamic_batch_size(_metadata.inputs.at(foundPort.idx), ioShape, nullptr, tensors.size());@@ -689,7 +687,7 @@ void ZeroInferRequest::infer_async() {auto batch_size = _graph->get_batch_size();size_t inputIndex = 0;for (const auto& userTensor : _userInputTensors) {- const IODescriptor inputDescriptor = _metadata.inputs.at(inputIndex);+ const IODescriptor& inputDescriptor = _metadata.inputs.at(inputIndex);OPENVINO_ASSERT(!inputDescriptor.isInitInputWeights,"This path should not be used for running inferences for the \"init\" model");@@ -789,7 +787,7 @@ void ZeroInferRequest::get_result() {size_t outputIndex = 0;for (const auto& userTensor : _userOutputTensors) {- const IODescriptor outputDescriptor = _metadata.outputs.at(outputIndex);+ const IODescriptor& outputDescriptor = _metadata.outputs.at(outputIndex);if (outputDescriptor.isShapeTensor) {OPENVINO_ASSERT(outputDescriptor.relatedDescriptorIndex.has_value(),"The link between the dynamic tensor and its shape tensor is missing, entry name: ",diff --git a/src/plugins/intel_npu/src/common/src/filtered_config.cpp b/src/plugins/intel_npu/src/common/src/filtered_config.cppindex 31851de7db9..3c8873b9014 100644--- a/src/plugins/intel_npu/src/common/src/filtered_config.cpp+++ b/src/plugins/intel_npu/src/common/src/filtered_config.cpp@@ -97,7 +97,6 @@ void FilteredConfig::addOrUpdateInternal(std::string key, std::string value) {}std::string FilteredConfig::getInternal(std::string key) const {- auto log = Logger::global().clone("Config");if (_internal_compiler_configs.count(key) == 0) {OPENVINO_THROW(std::string("Internal compiler option " + key + " does not exist! "));}@@ -120,7 +119,7 @@ std::string FilteredConfig::toStringForCompiler() const {const auto& key = it->first;// Only include available configs which options have OptionMode::Compile or OptionMode::Both- if (isAvailable(key)) {+ if (isAvailable(key.data())) {if (_desc->has(key)) {if (_desc->get(key).mode() != OptionMode::RunTime) {resultStream << key << "=\"" << it->second->toString() << "\"";diff --git a/src/plugins/intel_npu/src/common/src/sync_infer_request.cpp b/src/plugins/intel_npu/src/common/src/sync_infer_request.cppindex 0294ea53fc9..3fa52ff76fa 100644--- a/src/plugins/intel_npu/src/common/src/sync_infer_request.cpp+++ b/src/plugins/intel_npu/src/common/src/sync_infer_request.cpp@@ -62,8 +62,8 @@ SyncInferRequest::FoundPort SyncInferRequest::find_port(const ov::Output<const o// Find port without caching work slow because we need each time iterate over all ports and compare different// strings So use WA with caching in order to make 2+ calls for the same ports faster.// Calculate hash for the port- size_t port_hash = ov::util::hash_combine(- std::vector<size_t>{std::hash<const ov::Node*>()(port.get_node()), std::hash<size_t>()(port.get_index())});+ size_t port_hash =+ ov::util::hash_combine({std::hash<const ov::Node*>()(port.get_node()), std::hash<size_t>()(port.get_index())});{std::lock_guard<std::mutex> lock(_cacheMutex);if (_cachedPorts.find(port_hash) != _cachedPorts.end()) {@@ -174,7 +174,7 @@ void SyncInferRequest::check_tensor(const ov::Output<const ov::Node>& port,OPENVINO_THROW("The tensor is not initialized!");bool is_input = ov::op::util::is_parameter(port.get_node());- std::string tensor_type = is_input ? "input" : "output";+ const std::string_view tensor_type = is_input ? "input" : "output";if (!support_strides) {OPENVINO_ASSERT(@@ -185,55 +185,60 @@ void SyncInferRequest::check_tensor(const ov::Output<const ov::Node>& port," 2. Enable stride support using the 'enable_strides_for' configuration property if this is supported.");}- if ((port.get_element_type() == ov::element::Type_t::boolean ||- tensor->get_element_type() == ov::element::Type_t::boolean) &&- port.get_element_type() != tensor->get_element_type()) {+ const auto& port_element_type = port.get_element_type();+ const auto& tensor_element_type = tensor->get_element_type();++ if ((port_element_type == ov::element::Type_t::boolean || tensor_element_type == ov::element::Type_t::boolean) &&+ port_element_type != tensor_element_type) {// Exception case for boolean treated as u8 in the NPU driver- OPENVINO_ASSERT(- port.get_element_type() == ov::element::Type_t::u8 || tensor->get_element_type() == ov::element::Type_t::u8,- "The tensor element type is not corresponding with output element type (",- tensor->get_element_type(),- " != ",- port.get_element_type());+ OPENVINO_ASSERT(port_element_type == ov::element::Type_t::u8 || tensor_element_type == ov::element::Type_t::u8,+ "The tensor element type is not corresponding with output element type (",+ tensor_element_type,+ " != ",+ port_element_type);} else {- OPENVINO_ASSERT(port.get_element_type() == tensor->get_element_type(),+ OPENVINO_ASSERT(port_element_type == tensor_element_type,"The tensor element type is not corresponding with output element type (",- tensor->get_element_type(),+ tensor_element_type," != ",- port.get_element_type());+ port_element_type);}- bool is_dynamic = port.get_partial_shape().is_dynamic();+ const auto& port_partial_shape = port.get_partial_shape();+ const auto& tensor_shape = tensor->get_shape();++ bool is_dynamic = port_partial_shape.is_dynamic();if (is_dynamic) {- auto port_length = port.get_partial_shape().rank().get_length();- OPENVINO_ASSERT(ov::PartialShape(tensor->get_shape()).rank().get_length() == port_length,+ auto port_length = port_partial_shape.rank().get_length();+ OPENVINO_ASSERT(ov::PartialShape(tensor_shape).rank().get_length() == port_length,"The tensor shape size is not equal to the model input/output rank: got ",- tensor->get_shape().size(),+ tensor_shape.size()," expecting ",port_length);if (port_length > 0) {+ const auto& port_max_shape = port_partial_shape.get_max_shape();for (auto i = 0; i < port_length; ++i) {- if (tensor->get_shape()[i] > port.get_partial_shape().get_max_shape()[i]) {+ if (tensor_shape[i] > port_max_shape[i]) {OPENVINO_THROW("The tensor shape is not compatible with the model input/output max shape: got ",- tensor->get_shape(),+ tensor_shape," expecting max shape ",- port.get_partial_shape().get_max_shape());+ port_max_shape);}}}}- OPENVINO_ASSERT(is_dynamic || port.get_shape() == tensor->get_shape(),+ OPENVINO_ASSERT(is_dynamic || port_partial_shape == tensor_shape,"The ",tensor_type," tensor size is not equal to the model ",tensor_type," type: got ",- tensor->get_shape(),+ tensor_shape," expecting ",- port.get_shape(),+ port_partial_shape,".");OPENVINO_ASSERT(std::dynamic_pointer_cast<ov::IRemoteTensor>(tensor._ptr) || tensor->data() != nullptr || is_dynamic,diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/logger/logger.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/logger/logger.hppindex 975e52612ca..69b8aa3887e 100644--- a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/logger/logger.hpp+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/logger/logger.hpp@@ -32,16 +32,23 @@ class Logger {public:static Logger& global();- Logger(std::string_view name, ov::log::Level lvl = ov::log::Level::NO);+ Logger(const char* name, ov::log::Level lvl = ov::log::Level::NO);+ Logger(const std::string&, ov::log::Level lvl = ov::log::Level::NO) = delete;+ Logger(const std::string_view& name, ov::log::Level lvl = ov::log::Level::NO) = delete;Logger(const Logger& log) = default;- Logger clone(std::string_view name) const;+ Logger clone(const char* name) const;+ Logger clone(const std::string& name) const = delete;+ Logger clone(const std::string_view& name) const = delete;++ void setName(const std::string& name) = delete;+ void setName(const std::string_view& name) = delete;auto name() const {return _name;}- void setName(std::string_view name) {+ void setName(const char* name) {_name = name;}@@ -100,7 +107,7 @@ private:}private:- std::string _name;+ const char* _name;ov::log::Level _logLevel = ov::log::Level::NO;};diff --git a/src/plugins/intel_npu/src/utils/src/logger/logger.cpp b/src/plugins/intel_npu/src/utils/src/logger/logger.cppindex c56141f7130..0283b62fafa 100644--- a/src/plugins/intel_npu/src/utils/src/logger/logger.cpp+++ b/src/plugins/intel_npu/src/utils/src/logger/logger.cpp@@ -71,9 +71,9 @@ Logger& Logger::global() {return log;}-Logger::Logger(std::string_view name, ov::log::Level lvl) : _name(name), _logLevel(lvl) {}+Logger::Logger(const char* name, ov::log::Level lvl) : _name(name), _logLevel(lvl) {}-Logger Logger::clone(std::string_view name) const {+Logger Logger::clone(const char* name) const {Logger logger(name, level());return logger;}