Skip to content

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

[NPUW]Support dynamic LoRA. (#31433)

intelgaoxiong00c7f43c50eb
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
index 6bae3fdd1e9..61455f23d46 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
@@ -111,6 +111,7 @@ DEFINE_OPT(NPUW_LLM_MIN_RESPONSE_LEN, uint32_t, 128, npuw::llm::min_response_len
DEFINE_OPT(NPUW_LLM_OPTIMIZE_V_TENSORS, bool, true, npuw::llm::optimize_v_tensors, RunTime);
DEFINE_OPT(NPUW_LLM_PREFILL_CHUNK_SIZE, uint64_t, 256, npuw::llm::prefill_chunk_size, RunTime);
DEFINE_OPT(NPUW_LLM_SHARED_HEAD, bool, true, npuw::llm::shared_lm_head, CompileTime);
+DEFINE_OPT(NPUW_LLM_MAX_LORA_RANK, uint32_t, 32, npuw::llm::max_lora_rank, RunTime);
namespace npuw {
namespace llm {
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
index 2863c86a900..d68259b6949 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
@@ -431,6 +431,14 @@ static constexpr ov::Property<uint32_t> max_prompt_len{"NPUW_LLM_MAX_PROMPT_LEN"
*/
static constexpr ov::Property<uint32_t> min_response_len{"NPUW_LLM_MIN_RESPONSE_LEN"};
+/**
+ * @brief
+ * Type: uint32_t.
+ * Desirable max LoRA rank.
+ * Default value: 32.
+ */
+static constexpr ov::Property<uint32_t> max_lora_rank{"NPUW_LLM_MAX_LORA_RANK"};
+
/**
* @brief
* FIXME: Should be removed.
diff --git a/src/plugins/intel_npu/src/al/src/config/npuw.cpp b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
index 5e5350008ae..11e266fca25 100644
--- a/src/plugins/intel_npu/src/al/src/config/npuw.cpp
+++ b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
@@ -62,6 +62,7 @@ void intel_npu::registerNPUWLLMOptions(OptionsDesc& desc) {
desc.add<NPUW_LLM_SEQ_LEN_DIM>();
desc.add<NPUW_LLM_MAX_PROMPT_LEN>();
desc.add<NPUW_LLM_MIN_RESPONSE_LEN>();
+ desc.add<NPUW_LLM_MAX_LORA_RANK>();
desc.add<NPUW_LLM_OPTIMIZE_V_TENSORS>();
desc.add<NPUW_LLM_PREFILL_CHUNK_SIZE>();
desc.add<NPUW_LLM_PREFILL_HINT>();
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
index fa2882c06e8..dd9f791f46b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
@@ -7,6 +7,7 @@
#include "logging.hpp"
#include "openvino/op/group_query_attention.hpp"
#include "openvino/op/ops.hpp"
+#include "openvino/op/util/node_util.hpp"
#include "openvino/openvino.hpp"
#include "openvino/opsets/opset13.hpp"
#include "openvino/pass/graph_rewrite.hpp"
@@ -740,7 +741,8 @@ std::shared_ptr<ov::Model> cut_lm_head(std::shared_ptr<ov::Model>& model) {
void reshape_to_static(std::shared_ptr<ov::Model> model,
const uint32_t input_size,
const uint32_t kvcache_size,
- const KVAxesPosition& kv_axes_position) {
+ const KVAxesPosition& kv_axes_position,
+ const uint32_t lora_rank) {
std::map<std::string, ov::PartialShape> new_shapes;
for (const auto& input : model->inputs()) {
const auto& input_name = input.get_any_name();
@@ -762,6 +764,12 @@ void reshape_to_static(std::shared_ptr<ov::Model> model,
NPUW_ASSERT(partial_shape_size == 3u || partial_shape_size == 2u);
new_shape =
partial_shape_size == 3u ? ov::PartialShape({3, 1, input_size}) : ov::PartialShape({1, input_size});
+ } else if (ov::npuw::matchLoRAMatMulAString(input_name)) {
+ new_shape = ov::PartialShape({lora_rank, input.get_partial_shape()[1]});
+ } else if (ov::npuw::matchLoRAMatMulAlphaString(input_name)) {
+ new_shape = ov::PartialShape({input.get_partial_shape()[0], lora_rank});
+ } else if (ov::npuw::matchLoRAMatMulBString(input_name)) {
+ new_shape = ov::PartialShape({input.get_partial_shape()[0], lora_rank});
} else {
const auto& partial_shape = input.get_partial_shape();
new_shape = partial_shape;
@@ -1006,6 +1014,48 @@ std::map<std::string, std::string> any_copy(const ov::AnyMap& params) {
}
} // namespace
+void ov::npuw::LLMCompiledModel::convert_stateful_lora_to_stateless(std::shared_ptr<ov::Model>& model) {
+ typedef std::shared_ptr<ov::op::util::AssignBase> PAssign;
+ typedef std::shared_ptr<ov::op::util::ReadValueBase> PReadValue;
+ std::vector<PReadValue> readValues;
+ std::vector<PAssign> assigns;
+ auto sinks = model->get_sinks();
+ for (size_t i = 0; i < sinks.size(); ++i) {
+ if (auto assign = ov::as_type_ptr<ov::op::util::AssignBase>(sinks[i])) {
+ auto variable_name = assign->get_variable_id();
+ if (!ov::npuw::matchLoRAMatMulAString(variable_name) && !ov::npuw::matchLoRAMatMulBString(variable_name) &&
+ !ov::npuw::matchLoRAMatMulAlphaString(variable_name)) {
+ continue;
+ }
+
+ auto read_value = ov::as_type_ptr<ov::op::util::ReadValueBase>(assign->get_input_node_shared_ptr(0));
+ OPENVINO_ASSERT(read_value, "Can't find ReadValue");
+ readValues.push_back(read_value);
+ assigns.push_back(assign);
+ }
+ }
+
+ ov::ParameterVector new_parameters;
+ new_parameters.reserve(readValues.size());
+ for (size_t i = 0; i < readValues.size(); ++i) {
+ auto read_value = readValues[i];
+ auto variable_name = read_value->get_variable_id();
+ const auto element_type = read_value->get_output_element_type(0);
+ const auto shape = read_value->get_output_partial_shape(0);
+
+ auto parameter = std::make_shared<ov::op::v0::Parameter>(element_type, shape);
+ ov::op::util::set_name(*parameter, variable_name);
+ replace_node(read_value, parameter);
+
+ auto assign = assigns[i];
+ model->remove_sink(assign);
+ model->remove_variable(model->get_variable_by_id(variable_name));
+ new_parameters.push_back(parameter);
+ }
+
+ model->add_parameters(new_parameters);
+}
+
ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& model,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties)
@@ -1043,6 +1093,7 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
auto kvcache_model = model->clone();
LOG_DEBUG("Transform kvcache model from stateful to stateless.");
ov::pass::StatefulToStateless().run_on_model(kvcache_model);
+ convert_stateful_lora_to_stateless(kvcache_model);
LOG_DEBUG(" ...also convert BF16 to FP16");
// Note: we need to identify original bf16 constants for potential weightless deserialization later
// And only then do bf16 to f16 transformation
@@ -1105,16 +1156,22 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
m_kvcache_desc = KVCacheDesc{max_prompt_len, max_prompt_len + min_response_len, 0u, seq_len_dim};
LOG_DEBUG("Make prefill model with static shapes");
+ m_max_lora_rank = m_cfg.get<::intel_npu::NPUW_LLM_MAX_LORA_RANK>();
if (m_use_chunk_prefill) {
reshape_to_static(prefill_model,
static_cast<uint32_t>(m_prefill_chunk_size),
m_kvcache_desc.max_prompt_size,
- axes);
+ axes,
+ m_max_lora_rank);
} else {
- reshape_to_static(prefill_model, m_kvcache_desc.max_prompt_size, m_kvcache_desc.max_prompt_size, axes);
+ reshape_to_static(prefill_model,
+ m_kvcache_desc.max_prompt_size,
+ m_kvcache_desc.max_prompt_size,
+ axes,
+ m_max_lora_rank);
}
LOG_DEBUG("Make kvcache model with static shapes");
- reshape_to_static(kvcache_model, 1u, m_kvcache_desc.total_size, axes);
+ reshape_to_static(kvcache_model, 1u, m_kvcache_desc.total_size, axes, m_max_lora_rank);
if (lm_head_model) {
LOG_DEBUG("Shared LM head: slice the prefill output");
// KVCache model is already reshaped to [1, 1, embed size], so only apply slice to
@@ -1299,6 +1356,7 @@ void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw:
write(model_stream, m_kvcache_desc.v_tensors_transposed);
write(model_stream, m_prefill_chunk_size);
write(model_stream, m_use_chunk_prefill);
+ write(model_stream, m_max_lora_rank);
// Write config
write(model_stream, m_cfg);
@@ -1506,6 +1564,7 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserial
read(model_stream, compiled->m_kvcache_desc.v_tensors_transposed);
read(model_stream, compiled->m_prefill_chunk_size);
read(model_stream, compiled->m_use_chunk_prefill);
+ read(model_stream, compiled->m_max_lora_rank);
// Deserialize config
read(model_stream, compiled->m_cfg);
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
index 70f709f01b0..fbeedcbe809 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
@@ -74,6 +74,10 @@ private:
std::shared_ptr<ov::npuw::CompiledModel> m_prefill_compiled;
// This model is optional, so can be null.
std::shared_ptr<ov::npuw::CompiledModel> m_lm_head_compiled;
+
+ // Support LoRA
+ void convert_stateful_lora_to_stateless(std::shared_ptr<ov::Model>& model);
+ uint32_t m_max_lora_rank = 32;
};
} // namespace npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp
index c0be3cbefda..b629a175290 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp
@@ -140,6 +140,35 @@ std::optional<ov::Output<const ov::Node>> find_port_by_name(const std::vector<ov
return std::make_optional(*it);
}
+void copy_columns_by_row_chunks_2d(ov::SoPtr<ov::ITensor> src, ov::SoPtr<ov::ITensor>& dst) {
+ const auto& src_shape = src->get_shape();
+
+ OPENVINO_ASSERT(src_shape.size() == 2u);
+ OPENVINO_ASSERT(src_shape == dst->get_shape());
+ OPENVINO_ASSERT(src->get_byte_size() == dst->get_byte_size());
+
+ const auto& src_strides = src->get_strides();
+ const auto& dst_strides = dst->get_strides();
+ const auto elem_size = src->get_byte_size() / src->get_size();
+
+ const auto H = src_shape[0];
+ const auto W = src_shape[1];
+
+ const auto IS_H = src_strides[0];
+ const auto OS_H = dst_strides[0];
+
+ const size_t chunk_byte_size = W * elem_size;
+
+ const auto* src_p = static_cast<uint8_t*>(src->data());
+ auto* dst_p = static_cast<uint8_t*>(dst->data());
+
+ for (size_t i = 0; i < H; ++i) {
+ const size_t src_offset = i * IS_H;
+ const size_t dst_offset = i * OS_H;
+ std::copy_n(src_p + src_offset, chunk_byte_size, dst_p + dst_offset);
+ }
+}
+
void pad_position_ids(const ov::SoPtr<ov::ITensor>& padded_position_ids, const ov::SoPtr<ov::ITensor>& position_ids) {
// NB: Regular LLM uses 2D position_ids [BATCH, SEQ_LEN], Qwen2.5 VL/Omni uses 3D position_ids [3, BATCH, SEQ_LEN]
// The first dimension (3) represents the three components of position encoding: time, height, and width
@@ -178,12 +207,76 @@ void pad_position_ids(const ov::SoPtr<ov::ITensor>& padded_position_ids, const o
}
}
+std::string shape_to_String(const ov::Shape& shape) {
+ std::ostringstream oss;
+ oss << "[";
+ for (size_t i = 0; i < shape.size(); ++i) {
+ oss << shape[i];
+ if (i < shape.size() - 1) {
+ oss << ", ";
+ }
+ }
+ oss << "]";
+ return oss.str();
+}
+
+void check_tensor_shape_compatibility(const ov::Shape& state_tensor_shape,
+ const ov::Shape& infer_tensor_shape,
+ size_t full_rank_dim,
+ size_t low_rank_dim,
+ uint32_t max_low_rank_dim_size) {
+ if (state_tensor_shape[full_rank_dim] != infer_tensor_shape[full_rank_dim]) {
+ OPENVINO_THROW("LoRA adapter tensor shape: " + shape_to_String(state_tensor_shape) +
+ " is not compatible with inference tensor shape: " + shape_to_String(infer_tensor_shape) +
+ ". Please check if adapter is compatible with the base model.");
+ }
+
+ uint32_t state_tensor_low_rank_size = static_cast<uint32_t>(state_tensor_shape[low_rank_dim]);
+ if (state_tensor_low_rank_size > max_low_rank_dim_size) {
+ OPENVINO_THROW("LoRA tensor low-rank size: " + std::to_string(state_tensor_low_rank_size) +
+ " is larger than the maximum LoRA low-rank size " + std::to_string(max_low_rank_dim_size) +
+ +". Please adjust NPUW_LLM_MAX_LORA_RANK configuration.");
+ }
+}
+
+std::pair<uint32_t, uint32_t> get_lora_dims_by_name(const std::string& state_name) {
+ uint32_t low_rank_dim, full_rank_dim;
+ if (ov::npuw::matchLoRAMatMulAString(state_name)) {
+ // Shape of A is [r, d]
+ low_rank_dim = 0;
+ full_rank_dim = 1;
+ } else if (ov::npuw::matchLoRAMatMulBString(state_name)) {
+ // Shape of B is [d, r]
+ low_rank_dim = 1;
+ full_rank_dim = 0;
+ } else if (ov::npuw::matchLoRAMatMulAlphaString(state_name)) {
+ // Shape of alpha is [1, r]
+ low_rank_dim = 1;
+ full_rank_dim = 0;
+ } else {
+ OPENVINO_THROW("Unknown LoRA state name: " + state_name);
+ }
+
+ return std::make_pair(low_rank_dim, full_rank_dim);
+}
+
constexpr uint32_t INPUT_IDS_SEQ_LEN_DIM = 1;
constexpr std::size_t kStartOutputKVCacheLayers = 1;
} // anonymous namespace
+void ov::npuw::LLMInferRequest::init_lora_states() {
+ for (const auto& input_port : m_prefill_request->get_compiled_model()->inputs()) {
+ auto input_name = input_port.get_any_name();
+ if (ov::npuw::matchLoRAMatMulAString(input_name) || ov::npuw::matchLoRAMatMulBString(input_name) ||
+ ov::npuw::matchLoRAMatMulAlphaString(input_name)) {
+ auto input_tensor = m_prefill_request->get_tensor(input_port);
+ m_variableStates.push_back(std::make_shared<VariableState>(input_name, input_tensor));
+ }
+ }
+}
+
ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled_model)
: ov::ISyncInferRequest(compiled_model),
m_npuw_llm_compiled_model(compiled_model) {
@@ -220,6 +313,8 @@ ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCo
m_kvcache_out_ports.emplace(output_port.get_any_name(), output_port);
}
+ init_lora_states();
+
const bool use_chunk_prefill = m_npuw_llm_compiled_model->m_use_chunk_prefill;
if (use_chunk_prefill) {
clear_chunk_prefill_kv_cache();
@@ -288,11 +383,85 @@ void ov::npuw::LLMInferRequest::init_tensor(const ov::Output<const ov::Node>& po
}
}
+void ov::npuw::LLMInferRequest::apply_lora() {
+ uint32_t max_low_rank_dim_size = m_npuw_llm_compiled_model->m_max_lora_rank;
+
+ for (auto state : m_variableStates) {
+ auto state_name = state->get_name();
+ auto state_tensor = state->get_state();
+
+ auto variableState = dynamic_cast<VariableState*>(state.operator->());
+ if (!variableState) {
+ OPENVINO_THROW("Failed to cast ov::IVariableState to VariableState.");
+ }
+
+ bool stateUpdated = variableState->is_state_updated();
+ if (!stateUpdated) {
+ continue;
+ }
+
+ if (state_tensor->get_size() == 0) {
+ // Generate without LoRA:
+ // the size of applied LoRA tensor from GenAI is 0
+
+ // Initialize a new tensor for inference
+ // Note: Clearing data in inference requests may lead to a segmentation fault on Linux systems
+ auto prefill_lora_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(state_name));
+ auto new_tensor_for_infer = ov::get_tensor_impl(
+ ov::Tensor(prefill_lora_in_tensor->get_element_type(), prefill_lora_in_tensor->get_shape()));
+ fill_tensor<float>(new_tensor_for_infer, 0.0f);
+
+ // Set new tensor for inference
+ m_prefill_request->set_tensor(m_prefill_in_ports.at(state_name), new_tensor_for_infer);
+ m_kvcache_request->set_tensor(m_kvcache_in_ports.at(state_name), new_tensor_for_infer);
+ } else {
+ // Generate with LoRA
+ auto infer_tensor_shape = m_prefill_request->get_tensor(m_prefill_in_ports.at(state_name))->get_shape();
+ auto state_tensor_shape = state_tensor->get_shape();
+ auto [low_rank_dim, full_rank_dim] = get_lora_dims_by_name(state_name);
+
+ check_tensor_shape_compatibility(state_tensor_shape,
+ infer_tensor_shape,
+ full_rank_dim,
+ low_rank_dim,
+ max_low_rank_dim_size);
+
+ uint32_t state_tensor_rank = static_cast<uint32_t>(state_tensor_shape[low_rank_dim]);
+ uint32_t target_lora_rank = static_cast<uint32_t>(infer_tensor_shape[low_rank_dim]);
+ if (state_tensor_rank == target_lora_rank) {
+ // Use state tensor directly in case rank is compatible with LoRA input tensor
+ m_prefill_request->set_tensor(m_prefill_in_ports.at(state_name), state_tensor);
+ m_kvcache_request->set_tensor(m_kvcache_in_ports.at(state_name), state_tensor);
+ } else {
+ // Fill LoRA into a new tensor
+ auto prefill_lora_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(state_name));
+ auto new_tensor_for_infer = ov::get_tensor_impl(
+ ov::Tensor(prefill_lora_in_tensor->get_element_type(), prefill_lora_in_tensor->get_shape()));
+
+ fill_tensor<float>(new_tensor_for_infer, 0.0f);
+ auto new_tensor_slice = make_tensor_slice(new_tensor_for_infer, low_rank_dim, 0u, state_tensor_rank);
+ if (low_rank_dim == 1) {
+ copy_columns_by_row_chunks_2d(state_tensor, new_tensor_slice);
+ } else {
+ state_tensor->copy_to(new_tensor_slice._ptr);
+ }
+
+ // Set new tensor for inference
+ m_prefill_request->set_tensor(m_prefill_in_ports.at(state_name), new_tensor_for_infer);
+ m_kvcache_request->set_tensor(m_kvcache_in_ports.at(state_name), new_tensor_for_infer);
+ }
+ }
+ variableState->clear_state_updated();
+ }
+}
+
void ov::npuw::LLMInferRequest::prepare_for_new_conversation() {
fill_tensor_bytes(m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name)), 0u);
fill_tensor<int64_t>(m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::attention_mask)), 0);
fill_tensor<int64_t>(m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::position_ids)), 0);
m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens = 0u;
+
+ apply_lora();
}
void ov::npuw::LLMInferRequest::copy_kvcache() {
@@ -660,3 +829,7 @@ ov::SoPtr<ov::ITensor> ov::npuw::LLMInferRequest::get_tensor(const ov::Output<co
}
return ov::ISyncInferRequest::get_tensor(port);
}
+
+std::vector<ov::SoPtr<ov::IVariableState>> ov::npuw::LLMInferRequest::query_state() const {
+ return m_variableStates;
+}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp
index 5b9fcb7b572..7197ef4f19f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp
@@ -7,6 +7,7 @@
#include <memory>
#include "llm_compiled_model.hpp"
+#include "llm_lora_states.hpp"
#include "openvino/core/descriptor/output.hpp"
#include "openvino/runtime/isync_infer_request.hpp"
@@ -36,13 +37,13 @@ public:
std::vector<ov::ProfilingInfo> get_profiling_info() const override {
return {};
}
- std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override {
- return {};
- }
+ std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override;
private:
void prepare_for_new_conversation();
+ void apply_lora();
+
void clear_chunk_prefill_kv_cache();
void init_tensor(const ov::Output<const ov::Node>& port);
@@ -85,6 +86,10 @@ private:
std::string m_input_ids_name;
bool m_generate_initialized = false;
+
+ // Support LoRA
+ std::vector<ov::SoPtr<ov::IVariableState>> m_variableStates;
+ void init_lora_states();
};
} // namespace npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_lora_states.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_lora_states.hpp
new file mode 100644
index 00000000000..95ad68abf94
--- /dev/null
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_lora_states.hpp
@@ -0,0 +1,71 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <regex>
+
+#include "openvino/runtime/itensor.hpp"
+#include "openvino/runtime/ivariable_state.hpp"
+
+namespace ov {
+namespace npuw {
+
+class VariableState final : public ov::IVariableState {
+public:
+ VariableState(const std::string& name, const ov::SoPtr<ov::ITensor>& tensor) : ov::IVariableState(name) {
+ m_state = tensor;
+ clear_state_updated();
+ }
+
+ void set_state(const ov::SoPtr<ov::ITensor>& newState) override {
+ m_state = newState;
+ m_state_updapted = true;
+ }
+
+ void reset() override {
+ OPENVINO_THROW("VariableState::reset() is not implemented");
+ }
+
+ ~VariableState() override = default;
+
+ bool is_state_updated() const {
+ return m_state_updapted;
+ }
+
+ void clear_state_updated() {
+ m_state_updapted = false;
+ }
+
+private:
+ bool m_state_updapted;
+};
+
+struct LoRANames {
+ static constexpr const char* MatMul_A = "MatMul\\.A";
+ static constexpr const char* MatMul_B = "MatMul\\.B";
+ static constexpr const char* MatMul_alpha = "MatMul\\.alpha";
+};
+
+static bool matchStringWithLoRAPattern(const std::string& input, const std::string& pattern_suffix) {
+ std::string pattern = "^lora_state.*" + pattern_suffix + "$";
+ std::regex regex_pattern(pattern);
+
+ return std::regex_match(input, regex_pattern);
+}
+
+static bool matchLoRAMatMulAString(const std::string& input) {
+ return matchStringWithLoRAPattern(input, LoRANames::MatMul_A);
+}
+
+static bool matchLoRAMatMulBString(const std::string& input) {
+ return matchStringWithLoRAPattern(input, LoRANames::MatMul_B);
+}
+
+static bool matchLoRAMatMulAlphaString(const std::string& input) {
+ return matchStringWithLoRAPattern(input, LoRANames::MatMul_alpha);
+}
+
+} // namespace npuw
+} // namespace ov