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
NPUW: Introduce a fail-safe submodel + tests (#34888)
Dmitry Matveev149f64f0f372
diff --git a/src/plugins/intel_npu/src/plugin/npuw/v1/elements/failsafe.cpp b/src/plugins/intel_npu/src/plugin/npuw/v1/elements/failsafe.cppnew file mode 100644index 00000000000..b8790fcfc44--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/v1/elements/failsafe.cpp@@ -0,0 +1,380 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "failsafe.hpp"++#include <cstring>+#include <exception>+#include <utility>++#include "openvino/runtime/make_tensor.hpp"++namespace {++[[noreturn]] void throw_failsafe_error(const char* stage, const std::exception_ptr& failure) {+ try {+ if (failure) {+ std::rethrow_exception(failure);+ }+ } catch (const std::exception& ex) {+ OPENVINO_THROW("Failsafe ", stage, " fallback exhausted: ", ex.what());+ } catch (...) {+ OPENVINO_THROW("Failsafe ", stage, " fallback exhausted");+ }++ OPENVINO_THROW("Failsafe ", stage, " fallback exhausted");+}++ov::SoPtr<ov::ITensor> allocate_tensor_like(const ov::SoPtr<ov::ITensor>& tensor) {+ return ov::get_tensor_impl(ov::Tensor(tensor->get_element_type(), tensor->get_shape()));+}++// The wrapper keeps public output tensors stable across failover. For plain+// host tensors the generic ITensor::copy_to path is not consistently available+// in lightweight test doubles, so use memcpy for the common contiguous case and+// fall back to plugin-provided copy_to for non-contiguous / remote tensors.+void copy_tensor_data(const ov::SoPtr<ov::ITensor>& src, const ov::SoPtr<ov::ITensor>& dst) {+ OPENVINO_ASSERT(src->get_byte_size() == dst->get_byte_size(), "Failsafe tensor copy size mismatch");+ if (src->is_continuous() && dst->is_continuous()) {+ std::memcpy(dst->data(), src->data(), src->get_byte_size());+ return;+ }+ src->copy_to(dst._ptr);+}++} // namespace++std::shared_ptr<ov::ICompiledModel> ov::npuw::failsafe::CompiledModel::create(+ const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ std::vector<std::string> devices,+ Factory factory) {+ OPENVINO_ASSERT(!devices.empty(), "Failsafe compiled model requires at least one device");+ OPENVINO_ASSERT(static_cast<bool>(factory), "Failsafe compiled model requires a factory");++ if (devices.size() == 1u) {+ auto compiled_model = factory(devices.front());+ OPENVINO_ASSERT(compiled_model != nullptr,+ "Failsafe factory returned null compiled model for device ",+ devices.front());+ return compiled_model;+ }++ auto compiled_model = std::make_shared<CompiledModel>(model, plugin, std::move(devices), std::move(factory));+ std::lock_guard<std::mutex> lock(compiled_model->m_mutex);+ compiled_model->ensure_active_compiled_model_locked();+ return compiled_model;+}++ov::npuw::failsafe::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ std::vector<std::string> devices,+ Factory factory)+ : ov::ICompiledModel(model, plugin),+ m_devices(std::move(devices)),+ m_factory(std::move(factory)) {+ OPENVINO_ASSERT(!m_devices.empty(), "Failsafe compiled model requires at least one device");+ OPENVINO_ASSERT(static_cast<bool>(m_factory), "Failsafe compiled model requires a factory");+}++ov::npuw::failsafe::CompiledModel::ActiveState ov::npuw::failsafe::CompiledModel::ensure_active_compiled_model_locked()+ const {+ if (m_active_state.has_value()) {+ return m_active_state.value();+ }++ std::exception_ptr last_failure;+ for (std::size_t idx = 0; idx < m_devices.size(); ++idx) {+ try {+ auto compiled_model = m_factory(m_devices[idx]);+ OPENVINO_ASSERT(compiled_model != nullptr,+ "Failsafe factory returned null compiled model for device ",+ m_devices[idx]);+ m_active_state = ActiveState{idx, 0u, std::move(compiled_model)};+ return m_active_state.value();+ } catch (...) {+ last_failure = std::current_exception();+ }+ }++ throw_failsafe_error("compile", last_failure);+}++ov::npuw::failsafe::CompiledModel::ActiveState ov::npuw::failsafe::CompiledModel::failover_from_locked(+ std::size_t generation,+ const char* stage,+ std::exception_ptr failure) const {+ auto current = ensure_active_compiled_model_locked();+ if (current.generation != generation) {+ return current;+ }++ for (std::size_t idx = current.device_index + 1; idx < m_devices.size(); ++idx) {+ try {+ auto compiled_model = m_factory(m_devices[idx]);+ OPENVINO_ASSERT(compiled_model != nullptr,+ "Failsafe factory returned null compiled model for device ",+ m_devices[idx]);+ m_active_state = ActiveState{idx, current.generation + 1u, std::move(compiled_model)};+ return m_active_state.value();+ } catch (...) {+ failure = std::current_exception();+ }+ }++ throw_failsafe_error(stage, failure);+}++std::shared_ptr<ov::IAsyncInferRequest> ov::npuw::failsafe::CompiledModel::create_request(+ std::size_t& generation) const {+ std::lock_guard<std::mutex> lock(m_mutex);++ while (true) {+ const auto current = ensure_active_compiled_model_locked();+ try {+ auto request = current.compiled_model->create_infer_request();+ OPENVINO_ASSERT(request != nullptr,+ "Failsafe compiled model returned null infer request for device ",+ m_devices[current.device_index]);+ generation = current.generation;+ return request;+ } catch (...) {+ failover_from_locked(current.generation, "infer-request creation", std::current_exception());+ }+ }+}++bool ov::npuw::failsafe::CompiledModel::is_generation_current(std::size_t generation) const {+ std::lock_guard<std::mutex> lock(m_mutex);+ return ensure_active_compiled_model_locked().generation == generation;+}++std::size_t ov::npuw::failsafe::CompiledModel::active_device_index() const {+ std::lock_guard<std::mutex> lock(m_mutex);+ return ensure_active_compiled_model_locked().device_index;+}++std::string ov::npuw::failsafe::CompiledModel::active_device_name() const {+ std::lock_guard<std::mutex> lock(m_mutex);+ const auto current = ensure_active_compiled_model_locked();+ return m_devices[current.device_index];+}++void ov::npuw::failsafe::CompiledModel::export_model(std::ostream& model) const {+ std::lock_guard<std::mutex> lock(m_mutex);+ ensure_active_compiled_model_locked().compiled_model->export_model(model);+}++std::shared_ptr<const ov::Model> ov::npuw::failsafe::CompiledModel::get_runtime_model() const {+ std::lock_guard<std::mutex> lock(m_mutex);+ return ensure_active_compiled_model_locked().compiled_model->get_runtime_model();+}++void ov::npuw::failsafe::CompiledModel::set_property(const ov::AnyMap& properties) {+ std::lock_guard<std::mutex> lock(m_mutex);+ ensure_active_compiled_model_locked().compiled_model->set_property(properties);+}++ov::Any ov::npuw::failsafe::CompiledModel::get_property(const std::string& name) const {+ std::lock_guard<std::mutex> lock(m_mutex);+ return ensure_active_compiled_model_locked().compiled_model->get_property(name);+}++std::shared_ptr<ov::ISyncInferRequest> ov::npuw::failsafe::CompiledModel::create_sync_infer_request() const {+ auto self = std::static_pointer_cast<const CompiledModel>(shared_from_this());+ auto request = std::make_shared<InferRequest>(std::move(self));+ request->ensure_inner_request_locked();+ return request;+}++std::shared_ptr<ov::IAsyncInferRequest> ov::npuw::failsafe::CompiledModel::create_infer_request() const {+ return std::make_shared<ov::IAsyncInferRequest>(create_sync_infer_request(),+ get_task_executor(),+ get_callback_executor());+}++ov::npuw::failsafe::InferRequest::InferRequest(std::shared_ptr<const CompiledModel> compiled_model)+ : ov::ISyncInferRequest(compiled_model),+ m_failsafe_compiled_model(std::move(compiled_model)) {}++void ov::npuw::failsafe::InferRequest::ensure_inner_request_locked() const {+ if (m_request != nullptr && m_failsafe_compiled_model->is_generation_current(m_generation)) {+ return;+ }++ m_request = m_failsafe_compiled_model->create_request(m_generation);+ rebind_public_input_tensors_locked();+}++ov::npuw::failsafe::InferRequest::PortKey ov::npuw::failsafe::InferRequest::port_key_locked(+ const ov::Output<const ov::Node>& port) const {+ auto found = find_port(port);+ OPENVINO_ASSERT(found.found(), "Cannot find failsafe port ", port);+ return PortKey{port.get_node(), port.get_index(), found.is_output()};+}++bool ov::npuw::failsafe::InferRequest::is_output_port_locked(const ov::Output<const ov::Node>& port) const {+ return port_key_locked(port).is_output;+}++ov::npuw::failsafe::InferRequest::PortTensors& ov::npuw::failsafe::InferRequest::get_or_create_port_tensors_locked(+ const ov::Output<const ov::Node>& port) const {+ auto [it, _] = m_public_tensors.try_emplace(port_key_locked(port), PortTensors{port, {}});+ auto& stored = it->second;+ if (!stored.tensors.empty()) {+ return stored;+ }++ auto tensors = m_request->get_tensors(port);+ if (!tensors.empty()) {+ stored.tensors.reserve(tensors.size());+ for (const auto& tensor : tensors) {+ stored.tensors.push_back(allocate_tensor_like(tensor));+ }+ } else {+ stored.tensors.push_back(allocate_tensor_like(m_request->get_tensor(port)));+ }++ if (is_output_port_locked(port)) {+ auto current_tensors = m_request->get_tensors(port);+ if (!current_tensors.empty()) {+ OPENVINO_ASSERT(current_tensors.size() == stored.tensors.size(),+ "Failsafe output tensor count mismatch for port ",+ port);+ for (std::size_t idx = 0; idx < current_tensors.size(); ++idx) {+ copy_tensor_data(current_tensors[idx], stored.tensors[idx]);+ }+ } else {+ copy_tensor_data(m_request->get_tensor(port), stored.tensors.front());+ }+ } else {+ bind_public_input_tensors_locked(stored.port, stored.tensors);+ }++ return stored;+}++void ov::npuw::failsafe::InferRequest::bind_public_input_tensors_locked(+ const ov::Output<const ov::Node>& port,+ const std::vector<ov::SoPtr<ov::ITensor>>& tensors) const {+ if (tensors.empty()) {+ return;+ }++ // Output tensors stay wrapper-owned/public. If the caller overrides an+ // output buffer via set_tensor()/set_tensors(), infer() copies the current+ // inner-request outputs into that buffer after execution and after failover.+ if (is_output_port_locked(port)) {+ return;+ }++ if (tensors.size() == 1u) {+ m_request->set_tensor(port, tensors.front());+ } else {+ m_request->set_tensors(port, tensors);+ }+}++void ov::npuw::failsafe::InferRequest::sync_public_output_tensors_locked() const {+ for (const auto& [_, stored] : m_public_tensors) {+ if (!is_output_port_locked(stored.port) || stored.tensors.empty()) {+ continue;+ }++ auto current_tensors = m_request->get_tensors(stored.port);+ if (!current_tensors.empty()) {+ OPENVINO_ASSERT(current_tensors.size() == stored.tensors.size(),+ "Failsafe output tensor count mismatch for port ",+ stored.port);+ for (std::size_t idx = 0; idx < current_tensors.size(); ++idx) {+ if (current_tensors[idx]._ptr != stored.tensors[idx]._ptr) {+ copy_tensor_data(current_tensors[idx], stored.tensors[idx]);+ }+ }+ continue;+ }++ const auto current = m_request->get_tensor(stored.port);+ if (current._ptr != stored.tensors.front()._ptr) {+ copy_tensor_data(current, stored.tensors.front());+ }+ }+}++void ov::npuw::failsafe::InferRequest::rebind_public_input_tensors_locked() const {+ for (const auto& [_, stored] : m_public_tensors) {+ if (stored.tensors.empty()) {+ continue;+ }+ bind_public_input_tensors_locked(stored.port, stored.tensors);+ }+}++void ov::npuw::failsafe::InferRequest::infer() {+ std::lock_guard<std::mutex> lock(m_mutex);++ while (true) {+ ensure_inner_request_locked();+ try {+ m_request->infer();+ sync_public_output_tensors_locked();+ return;+ } catch (...) {+ {+ std::lock_guard<std::mutex> model_lock(m_failsafe_compiled_model->m_mutex);+ m_failsafe_compiled_model->failover_from_locked(m_generation, "infer", std::current_exception());+ }+ m_request.reset();+ m_generation = std::numeric_limits<std::size_t>::max();+ }+ }+}++ov::SoPtr<ov::ITensor> ov::npuw::failsafe::InferRequest::get_tensor(const ov::Output<const ov::Node>& port) const {+ std::lock_guard<std::mutex> lock(m_mutex);+ ensure_inner_request_locked();+ return get_or_create_port_tensors_locked(port).tensors.front();+}++void ov::npuw::failsafe::InferRequest::set_tensor(const ov::Output<const ov::Node>& port,+ const ov::SoPtr<ov::ITensor>& tensor) {+ std::lock_guard<std::mutex> lock(m_mutex);+ const auto key = port_key_locked(port);+ m_public_tensors[key] = PortTensors{port, {tensor}};+ ensure_inner_request_locked();+ bind_public_input_tensors_locked(port, {tensor});+}++std::vector<ov::SoPtr<ov::ITensor>> ov::npuw::failsafe::InferRequest::get_tensors(+ const ov::Output<const ov::Node>& port) const {+ std::lock_guard<std::mutex> lock(m_mutex);+ ensure_inner_request_locked();+ return get_or_create_port_tensors_locked(port).tensors;+}++void ov::npuw::failsafe::InferRequest::set_tensors(const ov::Output<const ov::Node>& port,+ const std::vector<ov::SoPtr<ov::ITensor>>& tensors) {+ std::lock_guard<std::mutex> lock(m_mutex);+ const auto key = port_key_locked(port);+ m_public_tensors[key] = PortTensors{port, tensors};+ ensure_inner_request_locked();+ bind_public_input_tensors_locked(port, tensors);+}++void ov::npuw::failsafe::InferRequest::check_tensors() const {+ std::lock_guard<std::mutex> lock(m_mutex);+ ensure_inner_request_locked();+}++std::vector<ov::SoPtr<ov::IVariableState>> ov::npuw::failsafe::InferRequest::query_state() const {+ std::lock_guard<std::mutex> lock(m_mutex);+ ensure_inner_request_locked();+ return m_request->query_state();+}++std::vector<ov::ProfilingInfo> ov::npuw::failsafe::InferRequest::get_profiling_info() const {+ std::lock_guard<std::mutex> lock(m_mutex);+ ensure_inner_request_locked();+ return m_request->get_profiling_info();+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/v1/elements/failsafe.hpp b/src/plugins/intel_npu/src/plugin/npuw/v1/elements/failsafe.hppnew file mode 100644index 00000000000..e4dcd5d0f29--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/v1/elements/failsafe.hpp@@ -0,0 +1,128 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <functional>+#include <limits>+#include <map>+#include <memory>+#include <mutex>+#include <optional>+#include <string>+#include <tuple>+#include <vector>++#include "openvino/runtime/iasync_infer_request.hpp"+#include "openvino/runtime/icompiled_model.hpp"+#include "openvino/runtime/isync_infer_request.hpp"++namespace ov::npuw::failsafe {++class InferRequest;++class CompiledModel final : public ov::ICompiledModel {+public:+ using Factory = std::function<std::shared_ptr<ov::ICompiledModel>(const std::string& device)>;++ static std::shared_ptr<ov::ICompiledModel> create(const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ std::vector<std::string> devices,+ Factory factory);++ CompiledModel(const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ std::vector<std::string> devices,+ Factory factory);++ void export_model(std::ostream& model) const override;+ std::shared_ptr<const ov::Model> get_runtime_model() const override;++ void set_property(const ov::AnyMap& properties) override;+ ov::Any get_property(const std::string& name) const override;++ std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override;+ std::shared_ptr<ov::IAsyncInferRequest> create_infer_request() const override;++ std::size_t active_device_index() const;+ std::string active_device_name() const;++private:+ friend class InferRequest;++ struct ActiveState {+ std::size_t device_index = 0;+ std::size_t generation = 0;+ std::shared_ptr<ov::ICompiledModel> compiled_model;+ };++ ActiveState ensure_active_compiled_model_locked() const;+ ActiveState failover_from_locked(std::size_t generation, const char* stage, std::exception_ptr failure) const;+ std::shared_ptr<ov::IAsyncInferRequest> create_request(std::size_t& generation) const;+ bool is_generation_current(std::size_t generation) const;++ std::vector<std::string> m_devices;+ Factory m_factory;+ mutable std::mutex m_mutex;+ mutable std::optional<ActiveState> m_active_state;+};++class InferRequest final : public ov::ISyncInferRequest {+public:+ explicit InferRequest(std::shared_ptr<const CompiledModel> compiled_model);++ void infer() override;++ ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override;+ void set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) override;++ std::vector<ov::SoPtr<ov::ITensor>> get_tensors(const ov::Output<const ov::Node>& port) const override;+ void set_tensors(const ov::Output<const ov::Node>& port,+ const std::vector<ov::SoPtr<ov::ITensor>>& tensors) override;++ void check_tensors() const override;++ std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override;+ std::vector<ov::ProfilingInfo> get_profiling_info() const override;++private:+ friend class CompiledModel;++ // The wrapper exposes stable public tensors even if the inner infer request+ // gets recreated on failover. Keying by (node, port index, input/output+ // side) keeps wrapper-owned tensors distinct from the inner request's+ // transient tensor handles and also avoids collisions in trivial graphs+ // where input/output descriptors may alias.+ struct PortKey {+ const ov::Node* node = nullptr;+ std::size_t index = 0;+ bool is_output = false;++ bool operator<(const PortKey& other) const {+ return std::tie(node, index, is_output) < std::tie(other.node, other.index, other.is_output);+ }+ };++ struct PortTensors {+ ov::Output<const ov::Node> port;+ std::vector<ov::SoPtr<ov::ITensor>> tensors;+ };++ void ensure_inner_request_locked() const;+ PortKey port_key_locked(const ov::Output<const ov::Node>& port) const;+ bool is_output_port_locked(const ov::Output<const ov::Node>& port) const;+ PortTensors& get_or_create_port_tensors_locked(const ov::Output<const ov::Node>& port) const;+ void bind_public_input_tensors_locked(const ov::Output<const ov::Node>& port,+ const std::vector<ov::SoPtr<ov::ITensor>>& tensors) const;+ void sync_public_output_tensors_locked() const;+ void rebind_public_input_tensors_locked() const;++ std::shared_ptr<const CompiledModel> m_failsafe_compiled_model;+ mutable std::mutex m_mutex;+ mutable std::shared_ptr<ov::IAsyncInferRequest> m_request;+ mutable std::size_t m_generation = std::numeric_limits<std::size_t>::max();+ mutable std::map<PortKey, PortTensors> m_public_tensors;+};++} // namespace ov::npuw::failsafediff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txtindex ac85f492c13..89860abe77b 100644--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt@@ -60,6 +60,7 @@ ov_add_test_target(${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/util.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/util_xarch.cpp+ ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/v1/elements/failsafe.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/attention.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/pyramid_attention.cppdiff --git a/src/plugins/intel_npu/tests/unit/npuw/failsafe.cpp b/src/plugins/intel_npu/tests/unit/npuw/failsafe.cppnew file mode 100644index 00000000000..d0b0f7db250--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/failsafe.cpp@@ -0,0 +1,469 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <algorithm>+#include <map>+#include <memory>+#include <set>+#include <stdexcept>+#include <string>+#include <utility>+#include <vector>++#include "v1/elements/failsafe.hpp"+#include "openvino/opsets/opset10.hpp"+#include "openvino/runtime/make_tensor.hpp"++namespace {++constexpr const char* kCandidateProperty = "TEST_CANDIDATE_NAME";++std::shared_ptr<ov::Model> make_test_model() {+ auto input = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{1});+ input->set_friendly_name("input");+ auto zero = ov::opset10::Constant::create(ov::element::f32, ov::Shape{1}, {0.f});+ auto add = std::make_shared<ov::opset10::Add>(input, zero);+ add->set_friendly_name("output_add");+ auto result = std::make_shared<ov::opset10::Result>(add);+ result->set_friendly_name("output");+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input}, "FailsafeTestModel");+}++class NullPlugin final : public ov::IPlugin {+public:+ std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,+ const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,+ const ov::AnyMap&,+ const ov::SoPtr<ov::IRemoteContext>&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(std::istream&, const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(std::istream&,+ const ov::SoPtr<ov::IRemoteContext>&,+ const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor&, const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor&,+ const ov::SoPtr<ov::IRemoteContext>&,+ const ov::AnyMap&) const override {+ return {};+ }+ ov::SupportedOpsMap query_model(const std::shared_ptr<const ov::Model>&, const ov::AnyMap&) const override {+ return {};+ }+ void set_property(const ov::AnyMap&) override {}+ ov::Any get_property(const std::string&, const ov::AnyMap&) const override {+ return {};+ }+ ov::SoPtr<ov::IRemoteContext> create_context(const ov::AnyMap&) const override {+ return {};+ }+ ov::SoPtr<ov::IRemoteContext> get_default_context(const ov::AnyMap&) const override {+ return {};+ }+};++struct CandidateState {+ std::string name;+ std::vector<std::string>* events = nullptr;+ int create_request_failures = 0;+ int infer_failures = 0;+ float output_bias = 0.f;+ float last_input_value = 0.f;+ float last_output_value = 0.f;+ std::vector<const void*> bound_input_ptrs;+};++class TestCompiledModel;++class TestInferRequest final : public ov::ISyncInferRequest {+public:+ TestInferRequest(std::shared_ptr<const TestCompiledModel> compiled_model, std::shared_ptr<CandidateState> state);++ void infer() override;+ ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override;+ void set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) override;+ std::vector<ov::SoPtr<ov::ITensor>> get_tensors(const ov::Output<const ov::Node>& port) const override;+ void set_tensors(const ov::Output<const ov::Node>& port,+ const std::vector<ov::SoPtr<ov::ITensor>>& tensors) override;+ void check_tensors() const override {}+ std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override {+ return {};+ }+ std::vector<ov::ProfilingInfo> get_profiling_info() const override {+ return {};+ }++private:+ static bool is_input_port(const ov::Output<const ov::Node>& port) {+ return port.get_node() != nullptr && port.get_node()->get_friendly_name() == "input";+ }++ std::shared_ptr<CandidateState> m_state;+};++class TestCompiledModel final : public ov::ICompiledModel {+public:+ TestCompiledModel(const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ std::shared_ptr<CandidateState> state)+ : ov::ICompiledModel(model, plugin),+ m_model(model),+ m_state(std::move(state)) {}++ void export_model(std::ostream&) const override {}+ std::shared_ptr<const ov::Model> get_runtime_model() const override {+ return m_model;+ }+ void set_property(const ov::AnyMap&) override {}+ ov::Any get_property(const std::string& name) const override {+ if (name == kCandidateProperty) {+ return m_state->name;+ }+ OPENVINO_THROW("Unsupported property: ", name);+ }++ std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override {+ if (m_state->events != nullptr) {+ m_state->events->push_back("create-request:" + m_state->name);+ }+ if (m_state->create_request_failures-- > 0) {+ OPENVINO_THROW("create request failed for ", m_state->name);+ }++ auto self = std::static_pointer_cast<const TestCompiledModel>(shared_from_this());+ return std::make_shared<TestInferRequest>(std::move(self), m_state);+ }++ std::shared_ptr<ov::IAsyncInferRequest> create_infer_request() const override {+ return std::make_shared<ov::IAsyncInferRequest>(create_sync_infer_request(),+ get_task_executor(),+ get_callback_executor());+ }++private:+ std::shared_ptr<ov::Model> m_model;+ std::shared_ptr<CandidateState> m_state;+};++TestInferRequest::TestInferRequest(std::shared_ptr<const TestCompiledModel> compiled_model, std::shared_ptr<CandidateState> state)+ : ov::ISyncInferRequest(std::move(compiled_model)),+ m_state(std::move(state)) {+ const auto& inputs = get_compiled_model()->inputs();+ for (const auto& input : inputs) {+ ov::ISyncInferRequest::set_tensor(input,+ ov::get_tensor_impl(ov::Tensor(input.get_element_type(), input.get_shape())));+ }+ const auto& outputs = get_compiled_model()->outputs();+ for (const auto& output : outputs) {+ ov::ISyncInferRequest::set_tensor(output,+ ov::get_tensor_impl(ov::Tensor(output.get_element_type(), output.get_shape())));+ }+}++void TestInferRequest::infer() {+ if (m_state->events != nullptr) {+ m_state->events->push_back("infer:" + m_state->name);+ }+ if (m_state->infer_failures-- > 0) {+ OPENVINO_THROW("infer failed for ", m_state->name);+ }++ const auto& input_port = get_compiled_model()->inputs().front();+ const auto& output_port = get_compiled_model()->outputs().front();+ const auto& input = ov::ISyncInferRequest::get_tensor(input_port);+ const auto& output = ov::ISyncInferRequest::get_tensor(output_port);+ m_state->last_input_value = input->data<const float>()[0];+ m_state->last_output_value = m_state->last_input_value + m_state->output_bias;+ output->data<float>()[0] = m_state->last_output_value;+}++ov::SoPtr<ov::ITensor> TestInferRequest::get_tensor(const ov::Output<const ov::Node>& port) const {+ return ov::ISyncInferRequest::get_tensor(port);+}++void TestInferRequest::set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) {+ if (is_input_port(port)) {+ m_state->bound_input_ptrs.push_back(tensor->data());+ }+ ov::ISyncInferRequest::set_tensor(port, tensor);+}++std::vector<ov::SoPtr<ov::ITensor>> TestInferRequest::get_tensors(const ov::Output<const ov::Node>& port) const {+ return ov::ISyncInferRequest::get_tensors(port);+}++void TestInferRequest::set_tensors(const ov::Output<const ov::Node>& port,+ const std::vector<ov::SoPtr<ov::ITensor>>& tensors) {+ if (is_input_port(port)) {+ for (const auto& tensor : tensors) {+ m_state->bound_input_ptrs.push_back(tensor->data());+ }+ }+ ov::ISyncInferRequest::set_tensors(port, tensors);+}++ov::npuw::failsafe::CompiledModel::Factory make_factory(+ const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ std::map<std::string, std::shared_ptr<CandidateState>> states,+ std::set<std::string> compile_failures = {}) {+ return [model, plugin, states = std::move(states), compile_failures = std::move(compile_failures)](+ const std::string& device) -> std::shared_ptr<ov::ICompiledModel> {+ const auto it = states.find(device);+ OPENVINO_ASSERT(it != states.end(), "Missing test state for device ", device);++ const auto& state = it->second;+ if (state->events != nullptr) {+ state->events->push_back("compile:" + state->name);+ }+ if (compile_failures.count(device) > 0) {+ OPENVINO_THROW("compile failed for ", state->name);+ }+ return std::make_shared<TestCompiledModel>(model, plugin, state);+ };+}++std::string error_message(const ov::Exception& ex) {+ return ex.what() == nullptr ? std::string{} : std::string(ex.what());+}++std::shared_ptr<ov::npuw::failsafe::CompiledModel> make_wrapped_compiled_model(+ const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ std::vector<std::string> devices,+ ov::npuw::failsafe::CompiledModel::Factory factory) {+ auto compiled = std::make_shared<ov::npuw::failsafe::CompiledModel>(model, plugin, std::move(devices), std::move(factory));+ (void)compiled->active_device_name();+ return compiled;+}++} // namespace++TEST(FailsafeCompiledModelTest, CompileFailureFallsBackToLaterCandidate) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();+ std::vector<std::string> events;+ auto first = std::make_shared<CandidateState>(CandidateState{"NPU", &events});+ auto second = std::make_shared<CandidateState>(CandidateState{"CPU", &events});++ auto compiled = std::dynamic_pointer_cast<ov::npuw::failsafe::CompiledModel>(ov::npuw::failsafe::CompiledModel::create(+ model,+ plugin,+ {"NPU", "CPU"},+ make_factory(model, plugin, {{"NPU", first}, {"CPU", second}}, {"NPU"})));++ ASSERT_NE(compiled, nullptr);+ EXPECT_EQ(compiled->active_device_name(), "CPU");+ EXPECT_EQ(compiled->get_property(kCandidateProperty).as<std::string>(), "CPU");+ EXPECT_EQ(events, (std::vector<std::string>{"compile:NPU", "compile:CPU"}));+}++TEST(FailsafeCompiledModelTest, CreateInferRequestFailureFallsBackToLaterCandidate) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();+ std::vector<std::string> events;+ auto first = std::make_shared<CandidateState>(CandidateState{"NPU", &events, 1});+ auto second = std::make_shared<CandidateState>(CandidateState{"CPU", &events});++ auto compiled = std::dynamic_pointer_cast<ov::npuw::failsafe::CompiledModel>(ov::npuw::failsafe::CompiledModel::create(+ model,+ plugin,+ {"NPU", "CPU"},+ make_factory(model, plugin, {{"NPU", first}, {"CPU", second}})));++ ASSERT_NE(compiled, nullptr);+ auto request = compiled->create_sync_infer_request();++ ASSERT_NE(request, nullptr);+ EXPECT_EQ(compiled->active_device_name(), "CPU");+ EXPECT_EQ(events,+ (std::vector<std::string>{"compile:NPU", "create-request:NPU", "compile:CPU", "create-request:CPU"}));+}++TEST(FailsafeCompiledModelTest, InferenceFailureFallsBackAndRebindsTensors) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();+ std::vector<std::string> events;+ auto first = std::make_shared<CandidateState>(CandidateState{"NPU", &events, 0, 1, 10.f});+ auto second = std::make_shared<CandidateState>(CandidateState{"CPU", &events, 0, 0, 20.f});++ auto compiled = std::dynamic_pointer_cast<ov::npuw::failsafe::CompiledModel>(ov::npuw::failsafe::CompiledModel::create(+ model,+ plugin,+ {"NPU", "CPU"},+ make_factory(model, plugin, {{"NPU", first}, {"CPU", second}})));++ ASSERT_NE(compiled, nullptr);+ auto request = compiled->create_sync_infer_request();+ auto input = ov::get_tensor_impl(ov::Tensor(ov::element::f32, ov::Shape{1}));+ input->data<float>()[0] = 3.f;+ request->set_tensor(model->inputs().front(), input);++ ASSERT_NO_THROW(request->infer());+ EXPECT_EQ(compiled->active_device_name(), "CPU");+ EXPECT_EQ(first->bound_input_ptrs, (std::vector<const void*>{input->data()}));+ EXPECT_EQ(second->bound_input_ptrs, (std::vector<const void*>{input->data()}));+ EXPECT_FLOAT_EQ(second->last_input_value, 3.f);+ EXPECT_FLOAT_EQ(second->last_output_value, 23.f);+ EXPECT_FLOAT_EQ(request->get_tensor(model->outputs().front())->data<const float>()[0], 23.f);+ EXPECT_EQ(events,+ (std::vector<std::string>{"compile:NPU",+ "create-request:NPU",+ "infer:NPU",+ "compile:CPU",+ "create-request:CPU",+ "infer:CPU"}));+}++TEST(FailsafeCompiledModelTest, UserProvidedOutputBufferReceivesFailoverResult) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();+ std::vector<std::string> events;+ auto first = std::make_shared<CandidateState>(CandidateState{"NPU", &events, 0, 1, 10.f});+ auto second = std::make_shared<CandidateState>(CandidateState{"CPU", &events, 0, 0, 20.f});++ auto compiled = std::dynamic_pointer_cast<ov::npuw::failsafe::CompiledModel>(ov::npuw::failsafe::CompiledModel::create(+ model,+ plugin,+ {"NPU", "CPU"},+ make_factory(model, plugin, {{"NPU", first}, {"CPU", second}})));++ ASSERT_NE(compiled, nullptr);+ auto request = compiled->create_sync_infer_request();+ auto input = ov::get_tensor_impl(ov::Tensor(ov::element::f32, ov::Shape{1}));+ auto output = ov::get_tensor_impl(ov::Tensor(ov::element::f32, ov::Shape{1}));+ input->data<float>()[0] = 3.f;+ output->data<float>()[0] = -1.f;+ request->set_tensor(model->inputs().front(), input);+ request->set_tensor(model->outputs().front(), output);++ ASSERT_NO_THROW(request->infer());+ EXPECT_EQ(compiled->active_device_name(), "CPU");+ EXPECT_EQ(request->get_tensor(model->outputs().front())._ptr, output._ptr);+ EXPECT_FLOAT_EQ(output->data<const float>()[0], 23.f);+ EXPECT_EQ(events,+ (std::vector<std::string>{"compile:NPU",+ "create-request:NPU",+ "infer:NPU",+ "compile:CPU",+ "create-request:CPU",+ "infer:CPU"}));+}++TEST(FailsafeCompiledModelTest, ChainedInferenceSurvivesUpstreamAndDownstreamFailover) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();++ std::vector<std::string> first_events;+ auto first_npu = std::make_shared<CandidateState>(CandidateState{"NPU", &first_events, 0, 1, 10.f});+ auto first_cpu = std::make_shared<CandidateState>(CandidateState{"CPU", &first_events, 0, 0, 20.f});+ auto first_compiled = std::dynamic_pointer_cast<ov::npuw::failsafe::CompiledModel>(ov::npuw::failsafe::CompiledModel::create(+ model,+ plugin,+ {"NPU", "CPU"},+ make_factory(model, plugin, {{"NPU", first_npu}, {"CPU", first_cpu}})));++ std::vector<std::string> second_events;+ auto second_npu = std::make_shared<CandidateState>(CandidateState{"NPU", &second_events, 0, 1, 1.f});+ auto second_cpu = std::make_shared<CandidateState>(CandidateState{"CPU", &second_events, 0, 0, 2.f});+ auto second_compiled = std::dynamic_pointer_cast<ov::npuw::failsafe::CompiledModel>(ov::npuw::failsafe::CompiledModel::create(+ model,+ plugin,+ {"NPU", "CPU"},+ make_factory(model, plugin, {{"NPU", second_npu}, {"CPU", second_cpu}})));++ ASSERT_NE(first_compiled, nullptr);+ ASSERT_NE(second_compiled, nullptr);+ auto first_request = first_compiled->create_sync_infer_request();+ auto second_request = second_compiled->create_sync_infer_request();++ auto source = ov::get_tensor_impl(ov::Tensor(ov::element::f32, ov::Shape{1}));+ source->data<float>()[0] = 3.f;+ first_request->set_tensor(model->inputs().front(), source);++ auto chained_tensor = first_request->get_tensor(model->outputs().front());+ second_request->set_tensor(model->inputs().front(), chained_tensor);++ ASSERT_NO_THROW(first_request->infer());+ EXPECT_EQ(first_compiled->active_device_name(), "CPU");+ EXPECT_FLOAT_EQ(first_cpu->last_input_value, 3.f);+ EXPECT_FLOAT_EQ(first_cpu->last_output_value, 23.f);+ EXPECT_EQ(first_request->get_tensor(model->outputs().front())._ptr, chained_tensor._ptr);+ EXPECT_FLOAT_EQ(chained_tensor->data<const float>()[0], 23.f);++ ASSERT_NO_THROW(second_request->infer());+ EXPECT_EQ(second_compiled->active_device_name(), "CPU");+ EXPECT_EQ(second_npu->bound_input_ptrs, (std::vector<const void*>{chained_tensor->data()}));+ EXPECT_EQ(second_cpu->bound_input_ptrs, (std::vector<const void*>{chained_tensor->data()}));+ EXPECT_FLOAT_EQ(second_cpu->last_input_value, 23.f);+ EXPECT_FLOAT_EQ(second_cpu->last_output_value, 25.f);+ EXPECT_FLOAT_EQ(second_request->get_tensor(model->outputs().front())->data<const float>()[0], 25.f);+}++TEST(FailsafeCompiledModelTest, CompileFailureWithoutFallbackPropagatesError) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();+ auto first = std::make_shared<CandidateState>(CandidateState{"NPU"});++ try {+ (void)make_wrapped_compiled_model(+ model,+ plugin,+ {"NPU"},+ make_factory(model, plugin, {{"NPU", first}}, {"NPU"}));+ FAIL() << "Expected compile failure";+ } catch (const ov::Exception& ex) {+ EXPECT_NE(error_message(ex).find("Failsafe compile fallback exhausted"), std::string::npos);+ }+}++TEST(FailsafeCompiledModelTest, CreateInferRequestFailureWithoutFallbackPropagatesError) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();+ auto first = std::make_shared<CandidateState>(CandidateState{"NPU", nullptr, 1});++ auto compiled = make_wrapped_compiled_model(+ model,+ plugin,+ {"NPU"},+ make_factory(model, plugin, {{"NPU", first}}));++ try {+ (void)compiled->create_sync_infer_request();+ FAIL() << "Expected infer-request creation failure";+ } catch (const ov::Exception& ex) {+ EXPECT_NE(error_message(ex).find("Failsafe infer-request creation fallback exhausted"), std::string::npos);+ }+}++TEST(FailsafeCompiledModelTest, InferFailureWithoutFallbackPropagatesError) {+ auto model = make_test_model();+ auto plugin = std::make_shared<NullPlugin>();+ auto first = std::make_shared<CandidateState>(CandidateState{"NPU", nullptr, 0, 1});++ auto compiled = make_wrapped_compiled_model(+ model,+ plugin,+ {"NPU"},+ make_factory(model, plugin, {{"NPU", first}}));+ auto request = compiled->create_sync_infer_request();++ try {+ request->infer();+ FAIL() << "Expected inference failure";+ } catch (const ov::Exception& ex) {+ EXPECT_NE(error_message(ex).find("Failsafe infer fallback exhausted"), std::string::npos);+ }+}