Skip to content

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

NPUW: Introduce basic profiling means (#32268)

Dmitry Matveevbac064b79a39
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
index 2a9d7cd0ebf..8e44914693a 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
@@ -23,6 +23,13 @@ ov::npuw::IBaseInferRequest::IBaseInferRequest(const std::shared_ptr<ov::npuw::C
if (m_npuw_model->m_acc_check) {
m_ref_subrequests.resize(m_num_submodels);
}
+
+ // Initialize profiling
+ m_profile.report_on_die = ov::npuw::profiling_enabled();
+ m_profile.area = m_npuw_model->m_name + "/performance";
+
+ m_footprint.report_on_die = ov::npuw::profiling_enabled();
+ m_footprint.area = m_npuw_model->m_name + "/memory";
}
ov::npuw::IBaseInferRequest::RqPtrs ov::npuw::IBaseInferRequest::create_infer_requests(std::size_t id,
@@ -239,6 +246,12 @@ std::vector<ov::ProfilingInfo> ov::npuw::IBaseInferRequest::get_profiling_info()
return info;
}
+std::string ov::npuw::IBaseInferRequest::profile_tag(std::size_t idx) const {
+ // So far accumulate over devices involved
+ const auto& proto_comp_model_desc = m_npuw_model->m_compiled_submodels[real(idx)];
+ return *proto_comp_model_desc.device_it;
+}
+
void ov::npuw::IBaseInferRequest::infer() {
m_now_idx.reset();
prepare_for_infer();
@@ -250,7 +263,9 @@ void ov::npuw::IBaseInferRequest::infer() {
}
subscribe_subrequest(idx, [](std::exception_ptr) {});
bool failover = false;
- run_subrequest_for_success(idx, failover);
+ m_profile[profile_tag(idx)].record([&]() {
+ run_subrequest_for_success(idx, failover);
+ });
failover_happened |= failover;
complete_subrequest(idx);
if (m_npuw_model->m_acc_check) {
@@ -277,7 +292,9 @@ std::size_t ov::npuw::IBaseInferRequest::total_subrequests() const {
ov::npuw::TensorPtr ov::npuw::IBaseInferRequest::allocMem(const ov::element::Type type,
const ov::Shape& shape,
const std::string& device) {
- return ov::npuw::util::allocMem(type, shape, device, m_npuw_model->get_plugin());
+ auto ptr = ov::npuw::util::allocMem(type, shape, device, m_npuw_model->get_plugin());
+ m_footprint[device] += ptr->get_byte_size();
+ return ptr;
}
ov::npuw::TensorPtr ov::npuw::IBaseInferRequest::allocOut(const ov::Output<const ov::Node>& node,
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
index f279ee74d64..bcd3a81e2e6 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
@@ -158,7 +158,14 @@ protected:
void dump_output_tensors(std::size_t idx);
// Quick-and-dirty profiling
- ov::npuw::perf::metric<float, ov::npuw::perf::MSec> m_ms_unpack;
+ using MS = ov::npuw::perf::metric<ov::npuw::perf::MSec>;
+ using B = ov::npuw::perf::counter<ov::npuw::perf::Bytes>;
+
+ MS m_ms_unpack;
+ ov::npuw::perf::Profile<MS> m_profile;
+ ov::npuw::perf::Profile<B> m_footprint;
+
+ std::string profile_tag(std::size_t idx) const;
// Various name/dump formatting methods
// TODO: These methods should probably go to CompiledModel
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
index 32bfeb4a32c..9e9633523e7 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -226,7 +226,11 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
// Identify based on compiler version, user config and pattern
ctx.use_host_gather_quant = should_use_quantized_host_gather(model, npuw_props);
- auto partitioning = getPartitioning(model, m_cfg, ctx);
+ ov::npuw::Partitioning partitioning;
+ m_profile["partitioning"].record([&]() {
+ partitioning = getPartitioning(model, m_cfg, ctx);
+ });
+
m_total_stat.gflops = partitioning.total_gflops;
m_total_stat.ops = partitioning.total_ops;
const std::vector<ov::npuw::Subgraph>& orderedSubgraphs = partitioning.subgraphs;
@@ -530,6 +534,12 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
LOG_DEBUG("CompiledModel is being deserialized, skipping the full constructor flow...");
}
+void ov::npuw::CompiledModel::init_profiling() {
+ // to be called from contructors
+ m_profile.report_on_die = ov::npuw::profiling_enabled();
+ m_profile.area = m_name + "/compilation";
+}
+
bool ov::npuw::CompiledModel::should_use_quantized_host_gather(const std::shared_ptr<ov::Model>& model,
const ov::AnyMap& properties) const {
LOG_INFO("Identifying best HOST_GATHER config value...");
@@ -1240,7 +1250,9 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {
}
// Evaluate and allocate all LazyTensors inside the bank
- m_weights_bank->evaluate_and_allocate();
+ m_profile["weights bank"].record([&]() {
+ m_weights_bank->evaluate_and_allocate();
+ });
// Set evaluated and allocated ov::Tensors to closures
for (size_t idx = 0; idx < m_compiled_submodels.size(); ++idx) {
@@ -1452,7 +1464,10 @@ bool ov::npuw::CompiledModel::compile_for_device(std::size_t id, const std::stri
}
try {
- m_compiled_submodels[id].compiled_model = compile_submodel(m_compiled_submodels[id].model, device_to_try);
+ // WARNING: These requests can be issues in parallel, so timer should be thread-safe
+ m_profile["compile/" + device_to_try].record([&]() {
+ m_compiled_submodels[id].compiled_model = compile_submodel(m_compiled_submodels[id].model, device_to_try);
+ });
} catch (const std::exception& ex) {
LOG_ERROR("Subgraph [" << id << "] Failed to compile: " << std::endl << ex.what());
dump_on_fail(id, device_to_try, ex.what());
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
index 09f9722dfaf..425281be488 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -15,6 +15,7 @@
#include "openvino/runtime/so_ptr.hpp"
#include "openvino/util/mmap_object.hpp"
#include "partitioning/partitioning.hpp"
+#include "perf.hpp"
#include "serialization.hpp"
#include "spatial.hpp"
#include "weights_bank.hpp"
@@ -146,6 +147,14 @@ private:
std::size_t ops{};
};
+ // Shouldn't this be counter instead? There's nothing much to
+ // average across compilation processes per model (it's a single
+ // process).
+ using MS = ov::npuw::perf::metric<ov::npuw::perf::MSec>;
+ ov::npuw::perf::Profile<MS> m_profile;
+
+ void init_profiling();
+
struct CompiledModelDesc {
DevList::const_iterator device_it;
std::set<std::string> devices_to_avoid;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/logging.cpp b/src/plugins/intel_npu/src/plugin/npuw/logging.cpp
index 3677184dec7..8285a05fcb8 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/logging.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/logging.cpp
@@ -49,6 +49,40 @@ ov::npuw::LogLevel ov::npuw::get_log_level() {
return log_level;
}
+bool ov::npuw::debug_groups() {
+ static bool do_debug_groups = false;
+#ifdef NPU_PLUGIN_DEVELOPER_BUILD
+ static std::once_flag flag;
+
+ std::call_once(flag, []() {
+ const auto* debug_opt = get_env({"OPENVINO_NPUW_DEBUG_GROUPS"});
+ if (!debug_opt) {
+ return;
+ }
+ const std::string debug_str(debug_opt);
+ do_debug_groups = (debug_str == "YES" || debug_str == "ON" || debug_str == "1");
+ });
+#endif
+ return do_debug_groups;
+}
+
+bool ov::npuw::profiling_enabled() {
+ static bool do_profiling = false;
+#ifdef NPU_PLUGIN_DEVELOPER_BUILD
+ static std::once_flag flag;
+
+ std::call_once(flag, []() {
+ const auto* prof_opt = get_env({"OPENVINO_NPUW_PROF"});
+ if (!prof_opt) {
+ return;
+ }
+ const std::string prof_str(prof_opt);
+ do_profiling = (prof_str == "YES" || prof_str == "ON" || prof_str == "1");
+ });
+#endif
+ return do_profiling;
+}
+
thread_local int ov::npuw::__logging_indent__::this_indent = 0;
ov::npuw::__logging_indent__::__logging_indent__() {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/logging.hpp b/src/plugins/intel_npu/src/plugin/npuw/logging.hpp
index c235876463e..7186a46db9f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/logging.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/logging.hpp
@@ -16,9 +16,12 @@ namespace ov {
namespace npuw {
enum class LogLevel { None = 0, Error = 1, Warning = 2, Info = 3, Verbose = 4, Debug = 5 };
-
LogLevel get_log_level();
+bool debug_groups();
+
+bool profiling_enabled();
+
class __logging_indent__ {
static thread_local int this_indent;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp
index 1966d786942..b2d75a925b4 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp
@@ -247,6 +247,25 @@ void Group::fuse(const Group::GPtr& gptr_prod) {
// This group absorbs the consumer
void Group::fuseWith(const Group::GPtr& gptr_cons) {
+ if (ov::npuw::debug_groups()) {
+ LOG_DEBUG("Fusing...");
+ LOG_BLOCK();
+ {
+ LOG_DEBUG("Merger:");
+ LOG_BLOCK();
+ for (auto&& layer : m_content) {
+ LOG_DEBUG(layer);
+ }
+ }
+ {
+ LOG_DEBUG("Mergee:");
+ LOG_BLOCK();
+ for (auto&& layer : gptr_cons->m_content) {
+ LOG_DEBUG(layer);
+ }
+ }
+ }
+
auto locked_snapshot = m_snapshot.lock();
auto node_to_gr = locked_snapshot->getNodeToGroupMap();
for (const auto& layer : gptr_cons->m_content) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/perf.cpp b/src/plugins/intel_npu/src/plugin/npuw/perf.cpp
index 6271e1cc14e..4583cfeb19c 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/perf.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/perf.cpp
@@ -6,7 +6,7 @@
#include <chrono>
-float ov::npuw::perf::ms_to_run(std::function<void()>&& body) {
+float ov::npuw::perf::ms_to_run(const std::function<void()>& body) {
namespace khr = std::chrono;
const auto s = khr::steady_clock::now();
body();
diff --git a/src/plugins/intel_npu/src/plugin/npuw/perf.hpp b/src/plugins/intel_npu/src/plugin/npuw/perf.hpp
index 3a188fa2dec..793dbb20d07 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/perf.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/perf.hpp
@@ -11,49 +11,184 @@
#include <string>
#include <vector>
+#include "logging.hpp"
+
namespace ov {
namespace npuw {
namespace perf {
-float ms_to_run(std::function<void()>&& body);
+float ms_to_run(const std::function<void()>& body);
struct MSec {
constexpr static const char* name = "ms";
+
+ using type_t = float;
+
+ static type_t sample(const std::function<void()>& body) {
+ return ms_to_run(body);
+ }
+};
+
+struct Bytes {
+ constexpr static const char* name = "MB";
+ constexpr static float scaler = 1024.0 * 1024;
+
+ using type_t = uint64_t;
};
-template <typename T, typename U>
+template <typename U>
class metric {
+ using T = typename U::type_t;
+
+private:
std::vector<T> records;
T vmin = std::numeric_limits<T>::max();
T vmax = std::numeric_limits<T>::min();
- std::string unit;
+ T total = 0;
+ std::string name;
+ bool enabled = false;
public:
+ metric() = default;
+ metric(metric&& m) : records(std::move(m.records)), name(std::move(m.name)), enabled(m.enabled) {}
+
+ explicit metric(const std::string& named, bool active = false) : name(named), enabled(active) {}
+
+ void enable() {
+ enabled = true;
+ }
+
void operator+=(T&& t) {
+ if (!enabled) {
+ return;
+ }
vmin = std::min(vmin, t);
vmax = std::max(vmax, t);
records.push_back(std::move(t));
+ total += t;
}
+
float avg() const {
+ NPUW_ASSERT(enabled);
float acc = 0.f;
for (auto&& t : records) {
acc += static_cast<float>(t) / records.size();
}
return acc;
}
+
T med() const {
+ NPUW_ASSERT(enabled);
std::vector<T> cpy(records);
std::nth_element(cpy.begin(), cpy.begin() + cpy.size() / 2, cpy.end());
return cpy[cpy.size() / 2];
}
- friend std::ostream& operator<<(std::ostream& os, const metric<T, U>& m) {
+
+ template <typename F>
+ void record(F&& f) {
+ if (!enabled) {
+ f();
+ } else {
+ *this += U::sample(f);
+ }
+ }
+
+ friend std::ostream& operator<<(std::ostream& os, const metric<U>& m) {
const char* units = U::name;
- os << "[ min = " << m.vmin << units << ", avg = " << m.avg() << units << ", med = " << m.med() << units
- << ", max = " << m.vmax << units << " in " << m.records.size() << " records ]";
+ os << std::left << std::setw(20) << (m.name.empty() ? std::string("<unnamed timer>") : m.name);
+ if (m.enabled) {
+ if (m.records.empty()) {
+ os << "[ no data ]";
+ } else {
+ os << "[ avg = " << m.avg() << " " << units << ", med = " << m.med() << " " << units << " in " << m.vmin
+ << ".." << m.vmax << " " << units << " range over " << m.records.size() << " records"
+ << ", total = " << m.total << units << " ]";
+ }
+ } else {
+ os << "[ disabled ]";
+ }
return os;
}
};
+template <typename U>
+class counter {
+public:
+ using T = typename U::type_t;
+
+private:
+ T total = 0;
+ std::size_t calls = 0;
+ std::string name;
+ bool enabled = false;
+
+public:
+ counter() = default;
+ counter(counter&& c) : total(std::move(c.total)), calls(c.calls), name(std::move(c.name)), enabled(c.enabled) {}
+
+ explicit counter(const std::string& named, bool active = false) : name(named), enabled(active) {}
+
+ void enable() {
+ enabled = true;
+ }
+
+ void operator+=(T&& t) {
+ if (enabled) {
+ total += t;
+ calls++;
+ }
+ }
+
+ friend std::ostream& operator<<(std::ostream& os, const counter<U>& c) {
+ const char* units = U::name;
+ os << std::left << std::setw(20) << (c.name.empty() ? std::string("<unnamed counter>") : c.name);
+ if (c.enabled) {
+ os << "[ " << static_cast<decltype(U::scaler)>(c.total) / U::scaler << " " << units << " total in "
+ << c.calls << " records ]";
+ } else {
+ os << "[ disabled ]";
+ }
+ return os;
+ }
+};
+
+template <class Metric>
+struct Profile {
+ std::map<std::string, Metric> metrics;
+ std::string area;
+ bool report_on_die = false;
+
+ Metric& operator[](const std::string& tag) {
+ auto iter = metrics.find(tag);
+ if (iter == metrics.end()) {
+ // Use report_on_die as a "enabled" marker here
+ auto [new_iter, unused] = metrics.insert({tag, Metric(tag, report_on_die)});
+ return new_iter->second;
+ }
+ return iter->second;
+ }
+
+ void report() const {
+ if (metrics.empty()) {
+ return;
+ }
+ if (!area.empty()) {
+ std::cout << area << ":" << std::endl;
+ } else {
+ std::cout << std::hex << this << ":" << std::endl;
+ }
+ for (auto&& m : metrics) {
+ std::cout << " " << m.second << std::endl;
+ }
+ }
+
+ ~Profile() {
+ if (report_on_die) {
+ report();
+ }
+ }
+};
+
} // namespace perf
} // namespace npuw
} // namespace ov