Skip to content

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

[NPUW] Support gemma4 26B A4B MoE with Chunk Prefill (#37009)

Guoliang Shiaeb724fd7e7e
diff --git a/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.cpp b/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.cpp
index d9460559803..bd2a2c91eab 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.cpp
@@ -216,6 +216,16 @@ void ov::npuw::util::copy_per_layer_inputs_chunk_to_right(const ov::SoPtr<ov::IT
const ov::SoPtr<ov::ITensor>& dst,
uint32_t src_offset_tokens,
uint32_t chunk_tokens) {
+ // Gemma4 26B A4B has dangling per_layer_inputs with zero-sized tensors.
+ if (src->get_byte_size() == 0u || dst->get_byte_size() == 0u) {
+ OPENVINO_ASSERT(src->get_byte_size() == 0u && dst->get_byte_size() == 0u,
+ "per_layer_inputs zero-byte mismatch between src and dst. src_bytes=",
+ src->get_byte_size(),
+ ", dst_bytes=",
+ dst->get_byte_size());
+ return;
+ }
+
const auto src_seq_len = src->get_shape().at(1);
const auto dst_seq_len = dst->get_shape().at(1);
OPENVINO_ASSERT(chunk_tokens > 0u, "chunk_tokens must be > 0");
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 6ec861aba6b..7a3f938b18a 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
@@ -768,10 +768,6 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
// If chunk size covers the entire prompt, just follow the static behavior.
// Otherwise, use chunking and align the prompt size to the chunk size.
if (m_use_chunk_prefill) {
- OPENVINO_ASSERT(
- !ov::npuw::util::has_input(model, "token_type_ids") || !ov::npuw::util::has_input(model, "inputs_embeds"),
- "Chunking is not implemented for Gemma model family yet. "
- "Please set NPUW_LLM_PREFILL_HINT to 'STATIC'");
if (m_prefill_chunk_size >= max_prompt_len) {
m_use_chunk_prefill = false;
} else {
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 41e6ef36090..19c9fb95768 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
@@ -862,6 +862,7 @@ void ov::npuw::LLMInferRequest::clear_chunk_prefill_kv_cache() {
void ov::npuw::LLMInferRequest::infer_chunked_prefill(ov::SoPtr<ov::ITensor> input_ids,
ov::SoPtr<ov::ITensor> attention_mask,
ov::SoPtr<ov::ITensor> position_ids,
+ ov::SoPtr<ov::ITensor> token_type_ids,
ov::SoPtr<ov::ITensor> per_layer_inputs,
ov::SoPtr<ov::ITensor> visual_pos_masks,
ov::SoPtr<ov::ITensor> deepstack_visual_embeds) {
@@ -890,6 +891,15 @@ void ov::npuw::LLMInferRequest::infer_chunked_prefill(ov::SoPtr<ov::ITensor> inp
auto attn_mask_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::attention_mask));
auto pos_ids_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::position_ids));
+ const auto token_type_ids_it = m_prefill_in_ports.find(layer_names::token_type_ids);
+ const bool has_token_type_ids = token_type_ids_it != m_prefill_in_ports.end();
+ ov::SoPtr<ov::ITensor> token_type_ids_in_tensor;
+ if (has_token_type_ids) {
+ OPENVINO_ASSERT(token_type_ids,
+ "token_type_ids input is provided, but the prefill model does not have a token_type_ids port.");
+ token_type_ids_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::token_type_ids));
+ }
+
auto& kvcache_desc = m_npuw_llm_compiled_model->m_kvcache_desc;
uint64_t remaining_prompts = input_prompt_len;
@@ -1001,7 +1011,7 @@ void ov::npuw::LLMInferRequest::infer_chunked_prefill(ov::SoPtr<ov::ITensor> inp
// dynamic attention selector needs history size to determin the past KV shape and attention mask shape
m_prefill_base_request->update_history_size(kvcache_desc.num_stored_tokens);
- // Gemma4: copy the current chunk of per_layer_inputs right-aligned on seq_len dim.
+ // Gemma4 E2B/E4B: copy the current chunk of per_layer_inputs right-aligned on seq_len dim.
// Source shape: [1, input_prompt_len, num_layers, proj_dim]
// Dest shape: [1, chunk_prompt_len, num_layers, proj_dim] (static)
if (per_layer_inputs) {
@@ -1012,6 +1022,21 @@ void ov::npuw::LLMInferRequest::infer_chunked_prefill(ov::SoPtr<ov::ITensor> inp
static_cast<uint32_t>(current_prompts_len));
}
+ // Gemma4-26B-A4B MoE: token_type_ids is [BATCH, SEQ_LEN].
+ // clear the tail window only for last chunk, then right-align current tokens.
+ if (has_token_type_ids) {
+ const size_t total_len = token_type_ids_in_tensor->get_size();
+ if (current_prompts_len < chunk_prompt_len) {
+ // Skip clear for full chunks since copy_n overwrites the whole window.
+ std::fill_n(token_type_ids_in_tensor->data<int64_t>() + total_len - chunk_prompt_len,
+ chunk_prompt_len,
+ int64_t{0});
+ }
+ std::copy_n(token_type_ids->data<int64_t>() + kvcache_desc.num_stored_tokens,
+ current_prompts_len,
+ token_type_ids_in_tensor->data<int64_t>() + total_len - current_prompts_len);
+ }
+
// Prepare KV blocks or bind memory for this chunk via strategy.
m_kvcache_strategy->on_prefill_chunk_begin(static_cast<uint32_t>(current_prompts_len));
});
@@ -1166,12 +1191,10 @@ void ov::npuw::LLMInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_ids,
const bool use_chunk_prefill = m_npuw_llm_compiled_model->m_use_chunk_prefill;
m_llm_profile["1/prefill:3.infer"].record([&]() {
if (use_chunk_prefill) {
- OPENVINO_ASSERT(!token_type_ids,
- "Chunking is not implemented for Gemma model family yet. "
- "Please set NPUW_LLM_PREFILL_HINT to 'STATIC'");
infer_chunked_prefill(input_ids,
attention_mask,
position_ids,
+ token_type_ids,
per_layer_inputs,
visual_pos_masks,
deepstack_visual_embeds);
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 c0dd38ef862..b1c49a3a6cf 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
@@ -74,6 +74,7 @@ protected:
void infer_chunked_prefill(ov::SoPtr<ov::ITensor> input_ids,
ov::SoPtr<ov::ITensor> attention_mask,
ov::SoPtr<ov::ITensor> position_ids,
+ ov::SoPtr<ov::ITensor> token_type_ids,
ov::SoPtr<ov::ITensor> per_layer_inputs,
ov::SoPtr<ov::ITensor> visual_pos_masks,
ov::SoPtr<ov::ITensor> deepstack_visual_embeds);
diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
index 012bf9f4845..2ea20aeb9bc 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
@@ -856,4 +856,17 @@ TEST_F(LLMCompiledModelFactoryOptionsTest, TextEmbedOptionCompilesEmbeddingDecod
EXPECT_NE(recorder.find_suffix("_prefill"), nullptr);
}
+TEST_F(LLMCompiledModelFactoryOptionsTest, Gemma4MoEModelWithInputsEmbedsAndTokenTypeIdsCompilesWithChunkPrefill) {
+ RecordingFactory recorder;
+ auto model = ov::test::npuw::build_gemma4_moe_llm_test_model();
+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+
+ ASSERT_NO_THROW(
+ compiled = create_compiled_model(model,
+ {{"NPUW_LLM_PREFILL_HINT", "DYNAMIC"}, {"NPUW_LLM_PREFILL_CHUNK_SIZE", "128"}},
+ recorder));
+ ASSERT_NE(compiled, nullptr);
+ EXPECT_NE(recorder.find_suffix("_prefill"), nullptr);
+}
+
} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp b/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp
index 387b0b83701..a3d47d05d6e 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp
@@ -207,17 +207,30 @@ inline std::shared_ptr<ov::Model> build_qwen3_moe_llm_test_model() {
return mb.build_llm(cfg);
}
-/// Gemma4-style MoE: separate gate/up expert MatMuls (GeLU activation) and a
-/// 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 (real Gemma4-26B-A4B).
+/// 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() {
- ModelBuilder mb;
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;
- return mb.build_llm(cfg);
+ 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;
}
inline std::shared_ptr<ov::Model> build_sliding_window_test_model(size_t window_size = 512,