Skip to content

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

22.3 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <algorithm>
#include <cmath>
#include <cstring>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "compiled_model.hpp"
#include "executor.hpp"
#include "llm_compiled_model.hpp"
#include "model_builder.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/fake_convert.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/result.hpp"
#include "openvino/op/scaled_dot_product_attention.hpp"
#include "openvino/pass/stateful_to_stateless.hpp"
#include "openvino/runtime/iasync_infer_request.hpp"
#include "openvino/runtime/iplugin.hpp"
#include "openvino/runtime/isync_infer_request.hpp"
#include "serialization.hpp"
#include "weights_bank.hpp"

namespace ov::test::npuw {

template <typename Config = LLMConfig>
inline Config make_test_model_config() {
    Config cfg;
    cfg.num_layers = 2;
    cfg.hidden_size = 64;
    cfg.num_heads = 4;
    cfg.head_dim = 16;
    cfg.num_kv_heads = 4;
    cfg.vocab_size = 256;
    return cfg;
}

inline std::shared_ptr<ov::Model> build_llm_test_model(const LLMConfig& cfg) {
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

inline std::shared_ptr<ov::Model> build_llm_test_model() {
    return build_llm_test_model(make_test_model_config());
}

/// Phi-style LongRoPE LLM: the RoPE inverse frequencies are picked from
/// max(position_ids) + 1 <= context_limit, so extract_longrope_context_limit() finds the
/// limit and RopeCache rewrites the chain into the host-fed npuw_lr_cos / npuw_lr_sin
/// model inputs. Compile it with NPUW_LLM_CACHE_ROPE=YES to get that rewrite.
inline std::shared_ptr<ov::Model> build_longrope_llm_test_model(int64_t context_limit) {
    auto cfg = make_test_model_config();
    for (size_t i = 0; i < cfg.head_dim / 2; ++i) {
        cfg.longrope.inv_freq_short.push_back(1.0f / std::pow(3.0f, static_cast<float>(i)));
        cfg.longrope.inv_freq_long.push_back(0.6f / std::pow(3.4f, static_cast<float>(i)));
    }
    cfg.longrope.context_limit = context_limit;
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

/// Build an LLM test model with no position_ids input, mirroring models with NoPE 
/// such as granite-4.0-h-micro. An identity RoPE leaves setup_position_ids with
/// no RoPE Range pattern, so the builder creates no position_ids Parameter and
/// AddPositionIdsParam stays a no-op during compilation.
inline std::shared_ptr<ov::Model> build_llm_test_model_without_position_ids() {
    auto cfg = make_test_model_config();
    cfg.rope = [](const ov::Output<ov::Node>& x, const std::string&) {
        return x;
    };
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

/// Build an LLM test model configured so that Attention::from() can unambiguously
/// identify the past-KV dimension in the isolated attention function body.
///
/// The default test config has num_kv_heads == kPastKvLen == 4 which makes the
/// KV-cache shape [1, 4, 4, 16] ambiguous — find_context_dim sees two dims equal
/// to past_len and returns false.  By setting num_kv_heads=2 and using a past
/// length of 8, the KV shape becomes [1, 2, 8, 16] where only dim 2 equals
/// past_len=8, so Attention::from() succeeds and f._attention gets set.
inline std::shared_ptr<ov::Model> build_dynamic_attention_llm_model() {
    ov::test::npuw::LLMConfig cfg;
    cfg.num_layers = 2;
    cfg.hidden_size = 64;
    cfg.num_heads = 4;
    cfg.head_dim = 16;
    cfg.num_kv_heads = 2;  // must differ from kPastKvLen so find_context_dim is unambiguous
    cfg.vocab_size = 256;
    cfg.force_gqa_broadcast = true;  // produces 5-input SDPA needed by the SDPA isolation pattern

    ModelBuilder mb;
    auto model = mb.build_llm(cfg);
    ov::pass::StatefulToStateless().run_on_model(model);
    model = model->clone();

    constexpr std::size_t kSeq = 4;
    constexpr std::size_t kPast = 8;  // != num_kv_heads (2), so KV shape [1,2,8,16] is unambiguous

    std::map<std::string, ov::PartialShape> new_shapes;
    for (const auto& input : model->inputs()) {
        const auto& name = input.get_any_name();
        const auto& pshape = input.get_partial_shape();
        if (name.find("input_ids") != std::string::npos || name.find("token_type_ids") != std::string::npos) {
            new_shapes[name] = ov::PartialShape{1, kSeq};
        } else if (name.find("attention_mask") != std::string::npos) {
            new_shapes[name] = ov::PartialShape{1, kSeq + kPast};
        } else if (name.find("position_ids") != std::string::npos) {
            new_shapes[name] = ov::PartialShape{1, kSeq};
        } else {
            // KV cache params: fix batch=1, past_len=kPast, leave head/head_dim from pshape
            auto static_shape = pshape;
            static_shape[0] = 1;
            static_shape[2] = kPast;
            new_shapes[name] = static_shape;
        }
    }
    model->reshape(new_shapes);
    model->validate_nodes_and_infer_types();
    return model;
}

inline LLMConfig make_test_model_config_gqa() {
    auto cfg = make_test_model_config();
    cfg.num_kv_heads = 2;  // num_heads=4 / num_kv_heads=2 -> n_rep=2
    return cfg;
}

inline std::shared_ptr<ov::Model> build_llm_gqa_test_model() {
    ModelBuilder mb;
    return mb.build_llm(make_test_model_config_gqa());
}

/// Minimal Qwen3-style reranker: a GQA causal decoder with RMSNorm and per-head
/// Q/K normalization, stateful KV cache and an LM head (logits output). Matches the
/// I/O signature of Qwen3-Reranker (input_ids/attention_mask/position_ids + beam_idx),
/// which is what the batched scoring element fans out over.
inline LLMConfig make_test_model_config_reranker() {
    auto cfg = make_test_model_config_gqa();
    cfg.norm = RMSNorm(cfg.hidden_size, cfg.precision);
    cfg.qk_norm = RMSNorm(cfg.head_dim, cfg.precision);
    return cfg;
}

inline std::shared_ptr<ov::Model> build_reranker_test_model() {
    ModelBuilder mb;
    return mb.build_llm(make_test_model_config_reranker());
}

inline std::shared_ptr<ov::Model> build_llm_test_model_with_kv_fake_convert(const ov::element::Type fake_convert_type) {
    auto model = build_llm_test_model();
    auto scale = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f});

    for (const auto& op : model->get_ordered_ops()) {
        auto sdpa = ov::as_type_ptr<ov::op::v13::ScaledDotProductAttention>(op);
        if (!sdpa) {
            continue;
        }

        auto inject_fake_convert = [&](size_t input_idx, const std::string& suffix) {
            auto fake_convert_1 =
                std::make_shared<ov::op::v13::FakeConvert>(sdpa->input_value(input_idx), scale, fake_convert_type);
            auto fake_convert_2 = std::make_shared<ov::op::v13::FakeConvert>(fake_convert_1, scale, fake_convert_type);
            fake_convert_1->set_friendly_name(sdpa->get_friendly_name() + "/" + suffix + "_1");
            fake_convert_2->set_friendly_name(sdpa->get_friendly_name() + "/" + suffix + "_2");
            sdpa->input(input_idx).replace_source_output(fake_convert_2);
        };

        inject_fake_convert(1, "key_fake_convert");
        inject_fake_convert(2, "value_fake_convert");
    }

    model->validate_nodes_and_infer_types();
    return model;
}

/// Hybrid LLM: alternating linear-attention / full-attention layers.
/// 4 layers → layers 0,2 linear; layers 1,3 full attention.
/// Attention layers are Qwen3.5-style: output-gated, partial RoPE.
inline std::shared_ptr<ov::Model> build_hybrid_llm_test_model() {
    auto cfg = make_test_model_config();
    cfg.num_layers = 4;
    cfg.attn_output_gate = true;
    cfg.rotary_dim = cfg.head_dim / 4;
    cfg.is_linear_layer = make_mamba_schedule(1);
    auto mixer = std::make_shared<GatedDeltaNetMixer>();
    mixer->hidden_size = cfg.hidden_size;
    mixer->precision = cfg.precision;
    mixer->weight_fn = cfg.weight;
    mixer->num_heads = cfg.num_heads;
    mixer->key_head_dim = cfg.head_dim;
    mixer->value_head_dim = cfg.head_dim;
    cfg.linear_mixer = mixer;
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

/// LFM2-style hybrid: gated short-conv mixer layers interleaved with full attention.
/// 4 layers → layers 0,2 short-conv; layers 1,3 full attention.
inline std::shared_ptr<ov::Model> build_lfm2_llm_test_model() {
    auto cfg = make_test_model_config();
    cfg.num_layers = 4;
    cfg.is_linear_layer = make_mamba_schedule(1);
    auto mixer = std::make_shared<ShortConvMixer>();
    mixer->hidden_size = cfg.hidden_size;
    mixer->precision = cfg.precision;
    mixer->weight_fn = cfg.weight;
    mixer->conv_dim = cfg.hidden_size;
    cfg.linear_mixer = mixer;
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

inline std::shared_ptr<ov::Model> build_whisper_decoder_test_model() {
    ModelBuilder mb;
    return mb.build_whisper_decoder(make_test_model_config<WhisperConfig>());
}

inline std::shared_ptr<ov::Model> build_embedding_test_model() {
    ModelBuilder mb;
    return mb.build_embedding_encoder(make_test_model_config<BertConfig>());
}

inline std::shared_ptr<ov::Model> build_embedding_decoder_test_model() {
    ModelBuilder mb;
    auto cfg = make_test_model_config<LLMConfig>();
    cfg.use_kv_cache = false;
    cfg.internal_position_ids = true;
    cfg.lm_head_weight = {};
    return mb.build_llm(cfg);
}

inline std::shared_ptr<ov::Model> build_moe_llm_test_model() {
    ModelBuilder mb;
    auto cfg = make_test_model_config();
    cfg.num_experts = 8;
    cfg.num_experts_per_tok = 2;
    return mb.build_llm(cfg);
}

/// Qwen3-style MoE: separate gate/up expert MatMuls (SwiGLU) and a Softmax->TopK router
/// with ReduceSum->Divide renormalization, matching NPUW's Qwen3Expert + Qwen3Router
/// patterns (real Qwen3-30B-A3B). Contrast build_moe_llm_test_model, which emits the
/// GPT-OSS topology (fused gate_up, TopK->Softmax router). Stateful (KV cache) like the
/// real model; consumers that need static shapes run StatefulToStateless + reshape, the
/// same way production prepares an LLM.
inline std::shared_ptr<ov::Model> build_qwen3_moe_llm_test_model() {
    ModelBuilder mb;
    auto cfg = make_test_model_config();
    cfg.num_experts = 8;
    cfg.num_experts_per_tok = 2;
    cfg.moe_factory = make_qwen3_moe_ffn;
    return mb.build_llm(cfg);
}

/// Gemma4 26B A4B MoE model: inputs_embeds + token_type_ids + Gemma4-style MoE FFN
/// (separate gate/up MatMuls with GeLU activation, Softmax->TopK router with
/// ReduceSum->Divide renormalization plus per-expert learned scale Gather and an extra
/// Slice before scatter, matching NPUW's Gemma4Expert + Gemma4Router patterns) + dangling
/// per_layer_inputs (proj_dim=0) matching the real Gemma4-26B-A4B input structure.
/// per_layer_inputs is a graph parameter with shape [1,-1,0,0]: it exists by name so the
/// compilation pipeline can find it, but carries no data (proj_dim=0).
inline std::shared_ptr<ov::Model> build_gemma4_moe_llm_test_model() {
    auto cfg = make_test_model_config();
    cfg.use_inputs_embeds = true;
    cfg.use_token_type_ids = true;
    cfg.num_experts = 8;
    cfg.num_experts_per_tok = 2;
    cfg.moe_factory = make_gemma4_moe_ffn;
    ModelBuilder mb;
    auto model = mb.build_llm(cfg);

    // Append a dangling per_layer_inputs with zero proj_dim (MoE unused-PLE pattern).
    // add_parameters() registers the node in the existing model graph without requiring
    // a corresponding Result, matching how reshape_to_static handles unused PLE inputs.
    auto ple = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, -1, 0, 0});
    ple->output(0).set_names({"per_layer_inputs"});
    model->add_parameters({ple});
    return model;
}

/// Real stateful LLM (like build_llm_test_model) plus a *consumed* per_layer_inputs
/// parameter (non-zero, dynamic proj_dim), used to probe LLMCompiledModel's
/// is_per_layer_inputs_model auto-enable path (Gemma-4 E2B/E4B cross-group KV sharing).
/// A minimal standalone graph without beam_idx/KV-cache state fails
/// StatefulToStateless (which LLMCompiledModel always runs), so this builds on the same
/// base topology as the other test models and appends the probe input via a dedicated
/// Add + Result, matching how build_gemma4_moe_llm_test_model appends its dangling PLE.
inline std::shared_ptr<ov::Model> build_per_layer_inputs_probe_model() {
    ModelBuilder mb;
    auto model = mb.build_llm(make_test_model_config());

    auto per_layer_inputs = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, -1, -1, -1});
    per_layer_inputs->output(0).set_names({"per_layer_inputs"});
    auto sibling = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1, 1, 1, 1}, {0.0f});
    auto add = std::make_shared<ov::op::v1::Add>(per_layer_inputs, sibling);
    add->output(0).set_names({"per_layer_inputs_probe_output"});
    auto result = std::make_shared<ov::op::v0::Result>(add);
    result->set_friendly_name("per_layer_inputs_probe_output");

    model->add_parameters({per_layer_inputs});
    model->add_results({result});
    return model;
}

inline std::shared_ptr<ov::Model> build_sliding_window_test_model(size_t window_size = 512,
                                                                  size_t sliding_to_full_ratio = 0,
                                                                  const SlidingMaskFn& sliding_mask_fn = {},
                                                                  size_t num_layers = 2) {
    auto cfg = make_test_model_config();
    cfg.num_layers = num_layers;
    cfg.sliding_window_size = window_size;
    cfg.sliding_to_full_ratio = sliding_to_full_ratio;
    cfg.sliding_mask_fn = sliding_mask_fn;
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

inline std::shared_ptr<ov::Model> build_lora_adapter_test_model() {
    ModelBuilder mb;
    return mb.build_lora_adapter(make_test_model_config<LoRAConfig>());
}

inline std::shared_ptr<ov::Model> build_lora_llm_test_model() {
    auto cfg = make_test_model_config();
    cfg.lora_rank = 8;
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

inline std::shared_ptr<ov::Model> build_token_type_ids_test_model(size_t window_size = 512,
                                                                  size_t sliding_to_full_ratio = 1,
                                                                  const SlidingMaskFn& sliding_mask_fn = {}) {
    auto cfg = make_test_model_config();
    cfg.sliding_window_size = window_size;
    cfg.sliding_to_full_ratio = sliding_to_full_ratio;
    cfg.sliding_mask_fn = sliding_mask_fn;
    cfg.use_inputs_embeds = true;
    cfg.use_token_type_ids = true;
    ModelBuilder mb;
    return mb.build_llm(cfg);
}

class NullPlugin : 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 {};
    }
};

class MockSubCompiledModel;

// Minimal working sub-request used by MockSubCompiledModel. Allocates a backing
// tensor for every input/output port so a real LLMInferRequest can be built on
// top of models compiled through RecordingFactory and exercise runtime paths
// (e.g. copy_kvcache) without a real device.
class MockSubInferRequest final : public ov::ISyncInferRequest {
public:
    explicit MockSubInferRequest(std::shared_ptr<const MockSubCompiledModel> compiled_model);

    void infer() override;
    ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override {
        return ov::ISyncInferRequest::get_tensor(port);
    }
    void set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) override {
        ov::ISyncInferRequest::set_tensor(port, tensor);
    }
    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 {};
    }
};

class MockSubCompiledModel : public ov::npuw::ICompiledModel_v0 {
public:
    MockSubCompiledModel(const std::shared_ptr<ov::Model>& model,
                         const std::shared_ptr<const ov::IPlugin>& plugin,
                         const ov::AnyMap&)
        : ov::npuw::ICompiledModel_v0(model, plugin),
          m_model(model) {}

    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&) const override {
        return {};
    }
    std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override {
        auto self = std::static_pointer_cast<const MockSubCompiledModel>(shared_from_this());
        return std::make_shared<MockSubInferRequest>(std::move(self));
    }
    std::shared_ptr<ov::npuw::IBaseInferRequest> create_base_infer_request() const override {
        return {};
    }
    std::shared_ptr<ov::IAsyncInferRequest> wrap_async_infer_request(
        std::shared_ptr<ov::npuw::IBaseInferRequest>) const override {
        return std::make_shared<ov::IAsyncInferRequest>(create_sync_infer_request(),
                                                        ::intel_npu::make_executor("mock_sub_task", 1),
                                                        ::intel_npu::make_executor("mock_sub_callback", 1));
    }
    std::string submodel_device(std::size_t) const override {
        return "CPU";
    }
    std::size_t num_submodels() const override {
        return 0;
    }
    std::shared_ptr<ov::npuw::weights::Bank> get_weights_bank() const override {
        return {};
    }
    void set_weights_bank(std::shared_ptr<ov::npuw::weights::Bank>) override {}
    void finalize_weights_bank() override {}
    void reconstruct_closure() override {}
    void serialize(std::ostream&, const ov::npuw::s11n::CompiledContext&) const override {}

private:
    std::shared_ptr<ov::Model> m_model;
};

inline MockSubInferRequest::MockSubInferRequest(std::shared_ptr<const MockSubCompiledModel> compiled_model)
    : ov::ISyncInferRequest(std::move(compiled_model)) {
    for (const auto& input : get_compiled_model()->inputs()) {
        ov::ISyncInferRequest::set_tensor(input,
                                          ov::get_tensor_impl(ov::Tensor(input.get_element_type(), input.get_shape())));
    }
    for (const auto& output : get_compiled_model()->outputs()) {
        ov::ISyncInferRequest::set_tensor(
            output,
            ov::get_tensor_impl(ov::Tensor(output.get_element_type(), output.get_shape())));
    }
}

inline void MockSubInferRequest::infer() {
    for (const auto& output : get_compiled_model()->outputs()) {
        auto tensor = ov::ISyncInferRequest::get_tensor(output);
        std::memset(tensor->data(), 0, tensor->get_byte_size());
    }
}

struct CompileCall {
    std::string friendly_name;
    ov::AnyMap props;
    std::shared_ptr<ov::Model> model;
};

class RecordingFactory {
public:
    ov::npuw::LLMCompiledModel::CompiledModelFactory make_factory() {
        return [this](const std::shared_ptr<ov::Model>& model,
                      const std::shared_ptr<const ov::IPlugin>& plugin,
                      const ov::AnyMap& props) -> std::shared_ptr<ov::npuw::ICompiledModel_v0> {
            m_calls.push_back({model->get_friendly_name(), props, model});
            return std::make_shared<MockSubCompiledModel>(model, plugin, props);
        };
    }

    const std::vector<CompileCall>& calls() const {
        return m_calls;
    }

    std::size_t count_suffix(std::string_view suffix) const {
        return std::count_if(m_calls.begin(), m_calls.end(), [suffix](const CompileCall& call) {
            return call.friendly_name.size() >= suffix.size() &&
                   call.friendly_name.compare(call.friendly_name.size() - suffix.size(), suffix.size(), suffix) == 0;
        });
    }

    std::size_t count_contains(std::string_view fragment) const {
        return std::count_if(m_calls.begin(), m_calls.end(), [fragment](const CompileCall& call) {
            return call.friendly_name.find(fragment) != std::string::npos;
        });
    }

    const CompileCall* find_suffix(std::string_view suffix) const {
        const auto it = std::find_if(m_calls.begin(), m_calls.end(), [suffix](const CompileCall& call) {
            return call.friendly_name.size() >= suffix.size() &&
                   call.friendly_name.compare(call.friendly_name.size() - suffix.size(), suffix.size(), suffix) == 0;
        });
        return it == m_calls.end() ? nullptr : &(*it);
    }

    const CompileCall* find_contains(std::string_view fragment) const {
        const auto it = std::find_if(m_calls.begin(), m_calls.end(), [fragment](const CompileCall& call) {
            return call.friendly_name.find(fragment) != std::string::npos;
        });
        return it == m_calls.end() ? nullptr : &(*it);
    }

private:
    std::vector<CompileCall> m_calls;
};

}  // namespace ov::test::npuw