Skip to content

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

[NPUW] Use padding inside kokoro model (#34491)

Sergey Shumihin3261b47ea2ff
diff --git a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.cpp
index b2f8b157d73..488541cdb22 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.cpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2025 Intel Corporation
+// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
@@ -14,6 +14,7 @@
#include <string>
#include <vector>
+#include "kokoro_utils.hpp"
#include "npuw/infer_request_utils.hpp"
#include "npuw/logging.hpp"
#include "npuw/util.hpp"
@@ -126,9 +127,21 @@ ov::npuw::KokoroInferRequest::KokoroInferRequest(const std::shared_ptr<ov::npuw:
const auto a_inputs = m_model_a_request->get_compiled_model()->inputs();
std::vector<std::string> missing_ports;
for (const auto& a_in : a_inputs) {
- auto original_port = ov::npuw::util::find_port_by_names(original_inputs, a_in.get_names());
+ const auto& names = a_in.get_names();
+
+ // text_mask is generated internally — not exposed to the user
+ if (names.count("text_mask")) {
+ m_a_text_mask = a_in;
+ continue;
+ }
+
+ auto original_port = ov::npuw::util::find_port_by_names(original_inputs, names);
if (original_port.has_value()) {
m_model_a_in_map.push_back({a_in, original_port.value()});
+ // Remember the original input_ids port so we can derive text_mask
+ if (names.count("input_ids")) {
+ m_orig_input_ids = original_port.value();
+ }
} else {
missing_ports.push_back(safe_any_name(a_in));
}
@@ -211,7 +224,10 @@ void ov::npuw::KokoroInferRequest::infer() {
const auto original_inputs = m_kokoro_compiled_model->inputs();
const auto original_outputs = m_kokoro_compiled_model->outputs();
- // 1) Infer Model A
+ // 1) Auto-fill text_mask from input_ids and infer Model A
+ if (m_a_text_mask.get_node()) {
+ fill_text_mask();
+ }
m_model_a_request->infer();
const auto pred_dur_tensor = m_model_a_request->get_tensor(m_a_pred_dur);
@@ -219,25 +235,33 @@ void ov::npuw::KokoroInferRequest::infer() {
const auto asr_left_tensor = m_model_a_request->get_tensor(m_a_asr_left);
OPENVINO_ASSERT(pred_dur_tensor && en_left_tensor && asr_left_tensor);
- auto orig_pred_dur = ov::npuw::util::find_port_by_name(original_outputs, "pred_dur");
- set_tensor(orig_pred_dur.value(), pred_dur_tensor);
-
// 2) Build repeat-interleave indices
+ // Only use the first m_real_seq_len entries (real tokens before padding).
+ const std::size_t full_len = pred_dur_tensor->get_size();
+ const std::size_t l_max = (m_real_seq_len > 0 && m_real_seq_len <= full_len) ? m_real_seq_len : full_len;
std::vector<int64_t> pred;
- pred.resize(pred_dur_tensor->get_size());
+ pred.resize(l_max);
const auto et = pred_dur_tensor->get_element_type();
if (et == ov::element::i64) {
const auto* p = pred_dur_tensor->data<const int64_t>();
- std::copy_n(p, pred.size(), pred.data());
+ std::copy_n(p, l_max, pred.data());
} else if (et == ov::element::i32) {
const auto* p = pred_dur_tensor->data<const int32_t>();
- std::copy_n(p, pred.size(), pred.data());
+ std::copy_n(p, l_max, pred.data());
} else {
OPENVINO_THROW("Unexpected element type from pred_dur data, expected i64 or i32, got: ", et.get_type_name());
}
- const std::size_t l_max = pred.size();
+
+ // Zero out padding positions in pred_dur so they don't contribute to audio.
+ if (et == ov::element::i64) {
+ ov::npuw::kokoro::zero_padding_durations(pred_dur_tensor->data<int64_t>(), full_len, l_max);
+ } else {
+ ov::npuw::kokoro::zero_padding_durations(pred_dur_tensor->data<int32_t>(), full_len, l_max);
+ }
+ auto orig_pred_dur = ov::npuw::util::find_port_by_name(original_outputs, "pred_dur");
+ set_tensor(orig_pred_dur.value(), pred_dur_tensor);
std::size_t total_frames = 0;
for (auto token_frames : pred) {
@@ -420,6 +444,41 @@ ov::SoPtr<ov::ITensor> ov::npuw::KokoroInferRequest::get_tensor(const ov::Output
return ov::ISyncInferRequest::get_tensor(port);
}
+void ov::npuw::KokoroInferRequest::fill_text_mask() {
+ // Compute text_mask from input_ids: text_mask[i] = True for positions beyond the
+ // real sequence (padding). The kokoro input format is [0, tok1, ..., tokN, 0, pad, ...].
+ // The first 0 is BOS, the second 0 is EOS, everything after is padding.
+ //
+ // text_mask semantics (matching PyTorch):
+ // True = padded/invalid position, False = valid position
+
+ OPENVINO_ASSERT(m_orig_input_ids.get_node(), "input_ids port not initialized for text_mask");
+
+ auto input_ids_tensor = get_tensor(m_orig_input_ids);
+ OPENVINO_ASSERT(input_ids_tensor);
+
+ const auto& ids_shape = input_ids_tensor->get_shape();
+ OPENVINO_ASSERT(ids_shape.size() == 2u && ids_shape[0] == 1u);
+ const std::size_t seq_len = ids_shape[1];
+
+ const auto* ids = input_ids_tensor->data<const int64_t>();
+ std::size_t real_len = ov::npuw::kokoro::find_real_sequence_length(ids, seq_len);
+
+ // Create / get the text_mask tensor [1, seq_len] of BOOL
+ auto mask_tensor = m_model_a_request->get_tensor(m_a_text_mask);
+ if (!mask_tensor || mask_tensor->get_shape() != ov::Shape{1, seq_len}) {
+ mask_tensor = ov::make_tensor(ov::element::boolean, ov::Shape{1, seq_len});
+ m_model_a_request->set_tensor(m_a_text_mask, mask_tensor);
+ }
+
+ ov::npuw::kokoro::fill_text_mask_from_lengths(mask_tensor->data<bool>(), seq_len, real_len);
+
+ m_real_seq_len = real_len;
+
+ LOG_DEBUG("text_mask filled — real_len=" << real_len << ", seq_len=" << seq_len
+ << ", padded=" << (seq_len - real_len));
+}
+
std::vector<ov::SoPtr<ov::IVariableState>> ov::npuw::KokoroInferRequest::query_state() const {
// FIXME Not implemented
// OPENVINO_NOT_IMPLEMENTED;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.hpp
index d119b33d71f..7227aa1581f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_infer_request.hpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2025 Intel Corporation
+// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
@@ -47,8 +47,17 @@ protected:
ov::Output<const ov::Node> m_b_en_block;
ov::Output<const ov::Node> m_b_asr_block;
+ // Model A attention mask input (auto-filled from input_ids)
+ ov::Output<const ov::Node> m_a_text_mask;
+ // Original input_ids port (used to compute text_mask at runtime)
+ ov::Output<const ov::Node> m_orig_input_ids;
+ // Real (unpadded) sequence length — set by fill_text_mask(), used to
+ // truncate pred_dur so Model B only processes valid tokens.
+ std::size_t m_real_seq_len = 0;
+
private:
void init_tensor(const ov::Output<const ov::Node>& port);
+ void fill_text_mask();
};
} // namespace npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_split.cpp b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_split.cpp
index 25f2602f9ec..8995a88c9c5 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_split.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_split.cpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2025 Intel Corporation
+// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
@@ -19,6 +19,51 @@
namespace ov::npuw {
+namespace {
+// Replace the text_mask generation subgraph (Range -> ... -> Greater) with a Parameter input.
+// When the model is reshaped to static input, the mask generation derives input_lengths from
+// ShapeOf(input_ids), which becomes a constant equal to the static size. This means the mask
+// is always all-False ("attend everywhere") and padding tokens are never masked out.
+// By exposing text_mask as an explicit Parameter we allow the host to provide the correct mask
+// at runtime, so the BERT encoder (and downstream TextEncoder/DurationEncoder) properly ignore
+// padded positions.
+void replace_text_mask_with_parameter(std::shared_ptr<ov::Model>& model) {
+ // Find the Greater node that produces text_mask. In the kokoro IR its output tensor
+ // carries the name "text_mask".
+ std::shared_ptr<ov::Node> text_mask_node;
+ for (const auto& op : model->get_ops()) {
+ for (size_t i = 0; i < op->get_output_size(); ++i) {
+ const auto& names = op->output(i).get_names();
+ if (names.count("text_mask")) {
+ text_mask_node = op;
+ break;
+ }
+ }
+ if (text_mask_node)
+ break;
+ }
+
+ if (!text_mask_node) {
+ LOG_WARN("text_mask node not found — skipping attention mask replacement");
+ return;
+ }
+
+ LOG_DEBUG("replacing text_mask generation with Parameter input");
+
+ // text_mask shape is [1, seq_len] with element type BOOL
+ auto text_mask_param = std::make_shared<ov::op::v0::Parameter>(text_mask_node->get_output_element_type(0),
+ text_mask_node->get_output_partial_shape(0));
+ text_mask_param->set_friendly_name("text_mask");
+ text_mask_param->output(0).get_tensor().set_names({"text_mask"});
+
+ // Redirect all consumers of the old text_mask output to the new Parameter
+ text_mask_node->output(0).replace(text_mask_param->output(0));
+
+ model->add_parameters({text_mask_param});
+ model->validate_nodes_and_infer_types();
+}
+} // namespace
+
// Main logic for kokoro model is splitting it into two parts,
// replacing repeat_interleave with custom processing which would be handled on host side during
// infer request execution. Part 1 will be named "model_a", part 2 - "model_b".
@@ -110,6 +155,10 @@ std::shared_ptr<ov::Model> KokoroSplit::create_model_a(const std::shared_ptr<ov:
model_a->output(1).get_tensor().set_names({"en_left"});
model_a->output(2).get_tensor().set_names({"asr_left"});
+ // Replace text_mask generation with an explicit Parameter input so that
+ // the host can provide the correct padding mask for static-shape models.
+ replace_text_mask_with_parameter(model_a);
+
return model_a;
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_utils.cpp b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_utils.cpp
new file mode 100644
index 00000000000..90bd46d5771
--- /dev/null
+++ b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_utils.cpp
@@ -0,0 +1,44 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "kokoro_utils.hpp"
+
+#include <algorithm>
+
+namespace ov {
+namespace npuw {
+namespace kokoro {
+
+std::size_t find_real_sequence_length(const int64_t* input_ids, std::size_t seq_len) {
+ // The Kokoro input format is [BOS=0, tok1, ..., tokN, EOS=0, PAD, ...].
+ // Scan from position 1 to find the first zero (EOS).
+ for (std::size_t i = 1; i < seq_len; ++i) {
+ if (input_ids[i] == 0) {
+ return i + 1; // include the EOS token
+ }
+ }
+ return seq_len; // no padding detected
+}
+
+void fill_text_mask_from_lengths(bool* mask_data, std::size_t seq_len, std::size_t real_len) {
+ for (std::size_t i = 0; i < seq_len; ++i) {
+ mask_data[i] = (i >= real_len);
+ }
+}
+
+void zero_padding_durations(int64_t* pred_dur, std::size_t full_len, std::size_t real_len) {
+ if (real_len < full_len) {
+ std::fill(pred_dur + real_len, pred_dur + full_len, int64_t{0});
+ }
+}
+
+void zero_padding_durations(int32_t* pred_dur, std::size_t full_len, std::size_t real_len) {
+ if (real_len < full_len) {
+ std::fill(pred_dur + real_len, pred_dur + full_len, int32_t{0});
+ }
+}
+
+} // namespace kokoro
+} // namespace npuw
+} // namespace ov
diff --git a/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_utils.hpp b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_utils.hpp
new file mode 100644
index 00000000000..7ed3a67ee53
--- /dev/null
+++ b/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_utils.hpp
@@ -0,0 +1,53 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cstddef>
+#include <cstdint>
+
+namespace ov {
+namespace npuw {
+namespace kokoro {
+
+/// Find the real (unpadded) sequence length in a Kokoro input_ids tensor.
+///
+/// The Kokoro input format is [BOS=0, tok1, ..., tokN, EOS=0, PAD, PAD, ...].
+/// This function scans from position 1 to find the first zero (EOS), and returns
+/// the real length including the EOS token itself.
+///
+/// @param input_ids Pointer to int64 input_ids data, shape [1, seq_len].
+/// @param seq_len Total length of the input_ids sequence.
+/// @return Number of valid tokens (including BOS and EOS).
+/// Returns seq_len if no EOS is found after position 0.
+std::size_t find_real_sequence_length(const int64_t* input_ids, std::size_t seq_len);
+
+/// Fill a boolean text_mask from the real sequence length.
+///
+/// text_mask[i] = false for valid positions [0, real_len),
+/// text_mask[i] = true for padding positions [real_len, seq_len).
+///
+/// This matches the PyTorch semantics where True = masked/padded.
+///
+/// @param mask_data Output boolean array of size seq_len.
+/// @param seq_len Total sequence length.
+/// @param real_len Number of valid (non-padding) tokens.
+void fill_text_mask_from_lengths(bool* mask_data, std::size_t seq_len, std::size_t real_len);
+
+/// Zero out padding positions in a pred_dur tensor (int64).
+///
+/// Positions [real_len, full_len) are set to 0 so padded tokens contribute
+/// zero frames to the audio output.
+///
+/// @param pred_dur Pointer to the pred_dur data.
+/// @param full_len Total size of the pred_dur tensor.
+/// @param real_len Number of valid tokens. Positions beyond this are zeroed.
+void zero_padding_durations(int64_t* pred_dur, std::size_t full_len, std::size_t real_len);
+
+/// @overload for int32 pred_dur tensors.
+void zero_padding_durations(int32_t* pred_dur, std::size_t full_len, std::size_t real_len);
+
+} // namespace kokoro
+} // namespace npuw
+} // namespace ov
diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
index 0a184f1952a..c78d928d1c3 100644
--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt
+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
@@ -47,6 +47,7 @@ ov_add_test_target(
${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/moe_transformations/moe_unroll_patterns.cpp
${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/moe_transformations/device_routed_moe_transform.cpp
${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/moe_transformations/gather_to_2d_gather.cpp
+ ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/pipelines/kokoro/kokoro_utils.cpp
${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/tests/functional/behavior/npuw/test_engine/models/model_builder.cpp
LINK_LIBRARIES
${MANDATORY_UNIT_TESTS_LIBS}
diff --git a/src/plugins/intel_npu/tests/unit/npuw/kokoro_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/kokoro_test.cpp
new file mode 100644
index 00000000000..225bf84865c
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/kokoro_test.cpp
@@ -0,0 +1,114 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <cstdint>
+#include <vector>
+
+#include "pipelines/kokoro/kokoro_utils.hpp"
+
+using ov::npuw::kokoro::fill_text_mask_from_lengths;
+using ov::npuw::kokoro::find_real_sequence_length;
+using ov::npuw::kokoro::zero_padding_durations;
+
+// ============================================================================
+// fill_text_mask end-to-end: input_ids → real_len → mask
+// This mirrors the logic in KokoroInferRequest::fill_text_mask().
+// ============================================================================
+
+// Typical Kokoro input: [BOS=0, phoneme1..N, EOS=0, PAD...]
+TEST(KokoroPadding, FillTextMask_TypicalPaddedInput) {
+ std::vector<int64_t> ids{0, 10, 20, 30, 0, 0, 0, 0}; // 3 phonemes + padding
+ const std::size_t seq_len = ids.size();
+
+ const auto real_len = find_real_sequence_length(ids.data(), seq_len);
+ EXPECT_EQ(real_len, 5u); // BOS + 3 phonemes + EOS
+
+ bool mask[8] = {};
+ fill_text_mask_from_lengths(mask, seq_len, real_len);
+
+ // Valid positions (BOS, tok, tok, tok, EOS) → false
+ for (std::size_t i = 0; i < 5; ++i) {
+ EXPECT_FALSE(mask[i]) << "Position " << i << " is valid, should be false";
+ }
+ // Padding positions → true
+ for (std::size_t i = 5; i < seq_len; ++i) {
+ EXPECT_TRUE(mask[i]) << "Position " << i << " is padding, should be true";
+ }
+}
+
+// No padding — input fills the entire static buffer
+TEST(KokoroPadding, FillTextMask_NoPadding) {
+ std::vector<int64_t> ids{0, 1, 2, 3, 4, 5}; // all non-zero after BOS
+ const std::size_t seq_len = ids.size();
+
+ const auto real_len = find_real_sequence_length(ids.data(), seq_len);
+ EXPECT_EQ(real_len, seq_len); // no EOS found → entire buffer is valid
+
+ bool mask[6] = {};
+ fill_text_mask_from_lengths(mask, seq_len, real_len);
+
+ for (std::size_t i = 0; i < seq_len; ++i) {
+ EXPECT_FALSE(mask[i]);
+ }
+}
+
+// Empty phoneme sequence: [BOS=0, EOS=0, PAD, PAD, ...]
+TEST(KokoroPadding, FillTextMask_EmptyPhonemes) {
+ std::vector<int64_t> ids{0, 0, 0, 0};
+ const auto real_len = find_real_sequence_length(ids.data(), ids.size());
+ EXPECT_EQ(real_len, 2u); // BOS + EOS only
+
+ bool mask[4] = {};
+ fill_text_mask_from_lengths(mask, ids.size(), real_len);
+
+ EXPECT_FALSE(mask[0]); // BOS
+ EXPECT_FALSE(mask[1]); // EOS
+ EXPECT_TRUE(mask[2]); // PAD
+ EXPECT_TRUE(mask[3]); // PAD
+}
+
+// Non-zero padding tokens after EOS: [BOS=0, tok, EOS=0, 20, 20, 20]
+// User might accidentally fill padding with a real token id (as it was before with " ")
+TEST(KokoroPadding, FillTextMask_NonZeroPaddingAfterEos) {
+ std::vector<int64_t> ids{0, 42, 0, 20, 20, 20};
+ const std::size_t seq_len = ids.size();
+
+ const auto real_len = find_real_sequence_length(ids.data(), seq_len);
+ EXPECT_EQ(real_len, 3u); // BOS + 1 phoneme + EOS
+
+ bool mask[6] = {};
+ fill_text_mask_from_lengths(mask, seq_len, real_len);
+
+ EXPECT_FALSE(mask[0]); // BOS
+ EXPECT_FALSE(mask[1]); // tok
+ EXPECT_FALSE(mask[2]); // EOS
+ EXPECT_TRUE(mask[3]); // non-zero padding — still masked
+ EXPECT_TRUE(mask[4]);
+ EXPECT_TRUE(mask[5]);
+}
+
+// ============================================================================
+// zero_padding_durations: pred_dur truncation
+// ============================================================================
+
+TEST(KokoroPadding, ZeroPadDurations_PaddingZeroed) {
+ std::vector<int64_t> ids{0, 10, 20, 0, 0, 0};
+ const auto real_len = find_real_sequence_length(ids.data(), ids.size());
+
+ // Simulate pred_dur output — every position got a non-zero duration
+ std::vector<int64_t> dur{3, 5, 2, 4, 8, 1};
+ zero_padding_durations(dur.data(), dur.size(), real_len);
+
+ // Valid positions preserved
+ EXPECT_EQ(dur[0], 3);
+ EXPECT_EQ(dur[1], 5);
+ EXPECT_EQ(dur[2], 2);
+ EXPECT_EQ(dur[3], 4);
+ // Padding zeroed
+ EXPECT_EQ(dur[4], 0);
+ EXPECT_EQ(dur[5], 0);
+}
+