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] Match host gather patterns through the index clamp (#38053)
Wei Wangb54ca21efb03
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cppindex 955bb4cc1be..2b4ed7ca38b 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp@@ -1431,6 +1431,46 @@ DQUnpackDictGatherGQi::DQUnpackDictGatherGQi(Context::Ref ctx) {register_matcher(std::make_shared<opp::Matcher>(qcvtm, "DQDictGatherGQu"), std::move(callback));}+namespace {++// The ids feeding a vocab Gather may come guarded by a Minimum(N-1, Maximum(-N, ids))+// clamp instead of straight from the Parameter. The HostGather* passes below all feed+// their host-side gather from that Parameter, so the clamp is folded away and+// util::gather() saturates into the same range in its place. The pattern therefore has+// to look through the clamp, and the callback has to confirm it really is that clamp -+// folding away a guard with any other bound would change results.+struct IdClamp {+ std::shared_ptr<ov::Node> maxids, minids;+};++IdClamp id_clamp(const std::shared_ptr<ov::Node>& ids) {+ auto maxids = opp::optional<ov::op::v1::Maximum>({ids, opp::any_input()});+ auto minids = opp::optional<ov::op::v1::Minimum>({maxids, opp::any_input()});+ return {maxids, minids};+}++bool id_clamp_ok(const ov::pass::pattern::PatternValueMap& m,+ const IdClamp& clamp,+ const ov::Output<ov::Node>& gather) {+ const auto vocab_size = static_cast<int64_t>(gather.get_node_shared_ptr()->get_input_shape(0)[0]);+ auto bound_is = [&](const std::shared_ptr<ov::Node>& pattern_node, int64_t expected) {+ const auto iter = m.find(pattern_node);+ if (iter == m.end()) {+ return true;+ }+ for (auto&& in : iter->second.get_node_shared_ptr()->input_values()) {+ const auto bound = ov::as_type_ptr<ov::op::v0::Constant>(in.get_node_shared_ptr());+ if (bound && ov::shape_size(bound->get_shape()) == 1 && bound->cast_vector<int64_t>()[0] == expected) {+ return true;+ }+ }+ return false;+ };+ return bound_is(clamp.maxids, -vocab_size) && bound_is(clamp.minids, vocab_size - 1);+}++} // anonymous namespace+// This is a companion to DQLiftGatherAsymCW step. This pass runs if// the respective block (mainly, a head) was turned a function// (e.g. with FUNCALL_FOR_ALL) As in this case the HostGatherQuant@@ -1441,13 +1481,14 @@ template <typename WType>HostGatherQuantAsymm<WType>::HostGatherQuantAsymm(Context::Ref ctx, bool verify_only) {auto pids = opp::wrap_type<ov::op::v0::Parameter>();auto cvtids = opp::optional<ov::op::v0::Convert>({pids->output(0)});+ auto ids = id_clamp(cvtids);auto qweight = opp::wrap_type<WType>();auto qzerop = opp::wrap_type<WType>();auto qcoeff = opp::wrap_type<WType>();- auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight, cvtids, opp::any_input()});- auto qgthrz = opp::wrap_type<ov::op::v8::Gather>({qzerop, cvtids, opp::any_input()});- auto qgthrs = opp::wrap_type<ov::op::v8::Gather>({qcoeff, cvtids, opp::any_input()});+ auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight, ids.minids, opp::any_input()});+ auto qgthrz = opp::wrap_type<ov::op::v8::Gather>({qzerop, ids.minids, opp::any_input()});+ auto qgthrs = opp::wrap_type<ov::op::v8::Gather>({qcoeff, ids.minids, opp::any_input()});auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qgthrw});auto qcvtz = opp::wrap_type<ov::op::v0::Convert>({qgthrz});@@ -1472,6 +1513,10 @@ HostGatherQuantAsymm<WType>::HostGatherQuantAsymm(Context::Ref ctx, bool verify_return false;}+ if (!id_clamp_ok(node_to_output, ids, node_to_output.at(qgthrw))) {+ return false;+ }+// shape=3 == CW model, 1 x N x Hs// shape=4 == GQ model, 1 x G x(N/G) x Hs// were Hs = hidden size, G is # of groups, N is the prompt size.@@ -1543,11 +1588,12 @@ template <typename WType>HostGatherQuantSymm<WType>::HostGatherQuantSymm(Context::Ref ctx, bool verify_only) {auto pids = opp::wrap_type<ov::op::v0::Parameter>();auto cvtids = opp::optional<ov::op::v0::Convert>({pids->output(0)});+ auto ids = id_clamp(cvtids);auto qweight = opp::wrap_type<WType>();auto qcoeff = opp::wrap_type<WType>();- auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight, cvtids, opp::any_input()});- auto qgthrs = opp::wrap_type<ov::op::v8::Gather>({qcoeff, cvtids, opp::any_input()});+ auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight, ids.minids, opp::any_input()});+ auto qgthrs = opp::wrap_type<ov::op::v8::Gather>({qcoeff, ids.minids, opp::any_input()});auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qgthrw});auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qgthrs});@@ -1564,6 +1610,10 @@ HostGatherQuantSymm<WType>::HostGatherQuantSymm(Context::Ref ctx, bool verify_onreturn false;}+ if (!id_clamp_ok(node_to_output, ids, node_to_output.at(qgthrw))) {+ return false;+ }+// shape=3 == CW model, 1 x N x Hs// shape=4 == GQ model, 1 x G x(N/G) x Hs// were Hs = hidden size, G is # of groups, N is the prompt size.@@ -1627,10 +1677,11 @@ template class HostGatherQuantSymm<ov::op::v0::Constant>;HostGather::HostGather(Context::Ref ctx) {auto pids = opp::wrap_type<ov::op::v0::Parameter>();auto cvtids = opp::optional<ov::op::v0::Convert>({pids->output(0)});+ auto ids = id_clamp(cvtids);auto qweight = opp::wrap_type<ov::op::v0::Parameter>();auto qweight_cvt = opp::optional<ov::op::v0::Convert>({qweight->output(0)});- auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight_cvt, cvtids, opp::any_input()});+ auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight_cvt, ids.minids, opp::any_input()});auto callback = [=](ov::pass::pattern::Matcher& m) {auto& node_to_output = m.get_pattern_value_map();@@ -1640,6 +1691,10 @@ HostGather::HostGather(Context::Ref ctx) {const auto& matched_out_gather = node_to_output.at(qgthrw);+ if (!id_clamp_ok(node_to_output, ids, matched_out_gather)) {+ return false;+ }+auto sole_reader = [](ov::Output<ov::Node> out) {const auto readers = out.get_target_inputs();NPUW_ASSERT(readers.size() >= 1);@@ -1684,6 +1739,7 @@ HostGather::HostGather(Context::Ref ctx) {HostGatherDQ::HostGatherDQ(Context::Ref ctx) {auto pids = opp::wrap_type<ov::op::v0::Parameter>();auto cvtids = opp::optional<ov::op::v0::Convert>({pids->output(0)});+ auto ids = id_clamp(cvtids);auto qweight = opp::wrap_type<ov::op::v0::Parameter>();auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});@@ -1691,8 +1747,8 @@ HostGatherDQ::HostGatherDQ(Context::Ref ctx) {auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();auto qcvtc = opp::optional<ov::op::v0::Convert>({qcoeff->output(0)});- auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qcvtw, cvtids, opp::any_input()});- auto qgthrc = opp::wrap_type<ov::op::v8::Gather>({qcvtc, cvtids, opp::any_input()});+ auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qcvtw, ids.minids, opp::any_input()});+ auto qgthrc = opp::wrap_type<ov::op::v8::Gather>({qcvtc, ids.minids, opp::any_input()});auto qmul = opp::wrap_type<ov::op::v1::Multiply>({qgthrw, qgthrc});auto callback = [=](ov::pass::pattern::Matcher& m) {@@ -1708,6 +1764,10 @@ HostGatherDQ::HostGatherDQ(Context::Ref ctx) {return false;}+ if (!id_clamp_ok(node_to_output, ids, node_to_output.at(qgthrw))) {+ return false;+ }+// shape=3 == CW model, 1 x N x Hs// shape=4 == GQ model, 1 x G x(N/G) x Hs// were Hs = hidden size, G is # of groups, N is the prompt size.@@ -1739,6 +1799,7 @@ HostGatherDQ::HostGatherDQ(Context::Ref ctx) {HostGatherCB4::HostGatherCB4(Context::Ref ctx) {auto pids = opp::wrap_type<ov::op::v0::Parameter>();auto cvtids = opp::wrap_type<ov::op::v0::Convert>({pids});+ auto ids = id_clamp(cvtids);auto qweight = opp::wrap_type<ov::op::v0::Parameter>();auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});@@ -1751,13 +1812,17 @@ HostGatherCB4::HostGatherCB4(Context::Ref ctx) {auto qmul = opp::wrap_type<ov::op::v1::Multiply>({qweightg, qcoeff});auto qcvtmul = opp::wrap_type<ov::op::v0::Convert>({qmul});- auto qgthr = opp::wrap_type<ov::op::v8::Gather>({qcvtmul, cvtids, opp::any_input()});+ auto qgthr = opp::wrap_type<ov::op::v8::Gather>({qcvtmul, ids.minids, opp::any_input()});auto callback = [=](ov::pass::pattern::Matcher& m) {auto& node_to_output = m.get_pattern_value_map();const auto& matched_out_mul = node_to_output.at(qmul);auto out_shape = matched_out_mul.get_shape();+ if (!id_clamp_ok(node_to_output, ids, node_to_output.at(qgthr))) {+ return false;+ }+if (out_shape.size() == 2 && out_shape.back() >= 2048 &&node_to_output.at(qweight).get_element_type() == ov::element::u4) {auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cppindex 8b5fe32ceb4..19a224f08bb 100644--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp@@ -4,6 +4,7 @@#include "util.hpp"+#include <algorithm>#include <intel_npu/config/config.hpp>#include <iomanip>#include <openvino/core/parallel.hpp>@@ -447,8 +448,18 @@ void ov::npuw::util::gather(const ov::SoPtr<ov::ITensor>& src,const uint8_t* pSrc = static_cast<uint8_t*>(src->data());uint8_t* pDst = static_cast<uint8_t*>(dst->data());+ const auto num_rows = static_cast<int64_t>(src_shape[0]);+ NPUW_ASSERT(num_rows > 0);+for (std::size_t r = 0; r < idx_shape[1]; r++) {- auto srcRowIdx = pIdx[r];+ // HostGather (see partitioning/patterns/opt.cpp) folds away both the Gather and+ // the Minimum(N-1, Maximum(-N, ids)) clamp guarding it, so reproduce both here:+ // saturate first, then apply Gather's negative-index semantics. The copy below+ // is raw pointer arithmetic, so no index may escape [0, N).+ auto srcRowIdx = std::clamp(pIdx[r], -num_rows, num_rows - 1);+ if (srcRowIdx < 0) {+ srcRowIdx += num_rows;+ }auto pSrcRow = pSrc + src_shape[1] * srcRowIdx * src_type.size();std::copy_n(pSrcRow, src_shape[1] * src_type.size(), pDst);pDst += dst_shape[2] * dst_type.size();diff --git a/src/plugins/intel_npu/tests/unit/npuw/host_gather_id_clamp_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/host_gather_id_clamp_test.cppnew file mode 100644index 00000000000..8513be2d2ce--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/host_gather_id_clamp_test.cpp@@ -0,0 +1,393 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <algorithm>+#include <memory>+#include <vector>++#include "openvino/op/ops.hpp"+#include "openvino/pass/graph_rewrite.hpp"+#include "openvino/runtime/make_tensor.hpp"+#include "partitioning/patterns/opt.hpp"+#include "util.hpp"++// The HostGather* passes replace a vocab Gather with a host-side gather fed directly+// from the ids Parameter. Some models guard the ids with a Minimum(N-1, Maximum(-N, ids))+// clamp in between, which the passes now look through (and fold away, relying on+// util::gather() saturating identically). These tests cover all three shapes of the+// input graph for every affected pass:+// * no clamp at all - the pre-change form, must keep matching;+// * the exact [-N, N-1] clamp - the newly supported form, must match;+// * a clamp with any other bound - must NOT match, folding it would change results.++namespace {++using namespace ov;++constexpr size_t kVocab = 128;+constexpr size_t kHidden = 2048; // HostGather* passes require the embedding size >= 2048+constexpr size_t kTokens = 4;++enum class Clamp {+ None, // ids -> Gather+ Exact, // ids -> Maximum(-N) -> Minimum(N-1) -> Gather+ MaxOnly, // ids -> Maximum(-N) -> Gather+ WrongBound, // ids -> Maximum(-N+1) -> Minimum(N-2) -> Gather+};++Output<Node> apply_clamp(const Output<Node>& ids, Clamp clamp) {+ if (clamp == Clamp::None) {+ return ids;+ }+ const bool exact = (clamp != Clamp::WrongBound);+ const int64_t lo = exact ? -static_cast<int64_t>(kVocab) : -static_cast<int64_t>(kVocab) + 1;+ const int64_t hi = exact ? static_cast<int64_t>(kVocab) - 1 : static_cast<int64_t>(kVocab) - 2;++ auto max = std::make_shared<op::v1::Maximum>(ids, op::v0::Constant::create(element::i64, Shape{}, {lo}));+ if (clamp == Clamp::MaxOnly) {+ return max;+ }+ return std::make_shared<op::v1::Minimum>(max, op::v0::Constant::create(element::i64, Shape{}, {hi}));+}++std::shared_ptr<op::v0::Constant> axis0() {+ return op::v0::Constant::create(element::i32, Shape{}, {0});+}++bool has_gather(const std::shared_ptr<ov::Model>& model) {+ for (auto&& node : model->get_ordered_ops()) {+ if (ov::as_type_ptr<op::v8::Gather>(node)) {+ return true;+ }+ }+ return false;+}++// Param(ids) -> [clamp] ->+// Param(W:f16)[V,H] -----> Gather -> Convert(f32) -> Result+std::shared_ptr<ov::Model> make_host_gather_model(Clamp clamp) {+ auto ids = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, kTokens});+ auto w = std::make_shared<op::v0::Parameter>(element::f16, Shape{kVocab, kHidden});+ auto g = std::make_shared<op::v8::Gather>(w, apply_clamp(ids, clamp), axis0());+ auto out = std::make_shared<op::v0::Convert>(g, element::f32);+ return std::make_shared<ov::Model>(ResultVector{std::make_shared<op::v0::Result>(out)}, ParameterVector{ids, w});+}++// Param(ids) -> [clamp] -------------> (indices of both Gathers)+// Param(W:i4)[V,H] -> Convert(f16) -> Gather(W) -.+// Param(S:f16)[V,1] -----------------> Gather(S) -> Multiply -> Result+std::shared_ptr<ov::Model> make_host_gather_dq_model(Clamp clamp) {+ auto ids = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, kTokens});+ auto clamped = apply_clamp(ids, clamp);++ auto w = std::make_shared<op::v0::Parameter>(element::i4, Shape{kVocab, kHidden});+ auto cvtw = std::make_shared<op::v0::Convert>(w, element::f16);+ auto s = std::make_shared<op::v0::Parameter>(element::f16, Shape{kVocab, 1});++ auto gw = std::make_shared<op::v8::Gather>(cvtw, clamped, axis0());+ auto gs = std::make_shared<op::v8::Gather>(s, clamped, axis0());+ auto mul = std::make_shared<op::v1::Multiply>(gw, gs);+ return std::make_shared<ov::Model>(ResultVector{std::make_shared<op::v0::Result>(mul)}, ParameterVector{ids, w, s});+}++// Param(ids) -> [clamp] ---> (indices of all three Gathers)+// Param(W:u8) ------------> Gather(W) -> Convert(f16) -.+// Param(Z:u8) ------------> Gather(Z) -> Convert(f16) -> Subtract -.+// Param(S:f16) ------------> Gather(S) -----------------------------> Multiply -> Convert(f32) -> Result+std::shared_ptr<ov::Model> make_host_gather_quant_asymm_model(Clamp clamp) {+ auto ids = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, kTokens});+ auto clamped = apply_clamp(ids, clamp);++ auto w = std::make_shared<op::v0::Parameter>(element::u8, Shape{kVocab, kHidden});+ auto z = std::make_shared<op::v0::Parameter>(element::u8, Shape{kVocab, 1});+ auto s = std::make_shared<op::v0::Parameter>(element::f16, Shape{kVocab, 1});++ auto gw = std::make_shared<op::v8::Gather>(w, clamped, axis0());+ auto gz = std::make_shared<op::v8::Gather>(z, clamped, axis0());+ auto gs = std::make_shared<op::v8::Gather>(s, clamped, axis0());++ auto cvtw = std::make_shared<op::v0::Convert>(gw, element::f16);+ auto cvtz = std::make_shared<op::v0::Convert>(gz, element::f16);+ auto sub = std::make_shared<op::v1::Subtract>(cvtw, cvtz);+ auto mul = std::make_shared<op::v1::Multiply>(sub, gs);+ auto out = std::make_shared<op::v0::Convert>(mul, element::f32);++ return std::make_shared<ov::Model>(ResultVector{std::make_shared<op::v0::Result>(out)},+ ParameterVector{ids, w, z, s});+}++// Param(ids) -> [clamp] --> (indices of both Gathers)+// Param(W:i4) -----------> Gather(W) -> Convert(f16) -.+// Param(S:f16) -----------> Gather(S) -----------------> Multiply -> Reshape -> Convert(f32) -> Result+// W is also read by a second (tied-embedding) consumer: the pass requires the weight+// to have more than one reader.+std::shared_ptr<ov::Model> make_host_gather_quant_symm_model(Clamp clamp) {+ auto ids = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, kTokens});+ auto clamped = apply_clamp(ids, clamp);++ auto w = std::make_shared<op::v0::Parameter>(element::i4, Shape{kVocab, kHidden});+ auto s = std::make_shared<op::v0::Parameter>(element::f16, Shape{kVocab, 1});++ auto gw = std::make_shared<op::v8::Gather>(w, clamped, axis0());+ auto gs = std::make_shared<op::v8::Gather>(s, clamped, axis0());+ auto cvtw = std::make_shared<op::v0::Convert>(gw, element::f16);+ auto mul = std::make_shared<op::v1::Multiply>(cvtw, gs);++ auto rshp_c =+ op::v0::Constant::create(element::i32,+ Shape{3},+ std::vector<int32_t>{1, static_cast<int32_t>(kTokens), static_cast<int32_t>(kHidden)});+ auto rshp = std::make_shared<op::v1::Reshape>(mul, rshp_c, false);+ auto out = std::make_shared<op::v0::Convert>(rshp, element::f32);++ auto tied = std::make_shared<op::v0::Convert>(w, element::f16);++ return std::make_shared<ov::Model>(+ ResultVector{std::make_shared<op::v0::Result>(out), std::make_shared<op::v0::Result>(tied)},+ ParameterVector{ids, w, s});+}++// Const(T:f32)[16] -> Convert(f16) ----.+// Param(W:u4)[V,H] -> Convert(i32) ----> Gather(codebook) -.+// Param(S:f16)[V,1] -----------------------------------------> Multiply -> Convert(f32) -.+// Param(ids) -> Convert(i64) -> [clamp] --------------------------------> Gather(vocab) -> Result+std::shared_ptr<ov::Model> make_host_gather_cb4_model(Clamp clamp) {+ auto ids = std::make_shared<op::v0::Parameter>(element::i32, Shape{1, kTokens});+ auto cvtids = std::make_shared<op::v0::Convert>(ids, element::i64);+ auto clamped = apply_clamp(cvtids, clamp);++ auto w = std::make_shared<op::v0::Parameter>(element::u4, Shape{kVocab, kHidden});+ auto cvtw = std::make_shared<op::v0::Convert>(w, element::i32);++ auto table = op::v0::Constant::create(element::f32, Shape{16}, std::vector<float>(16, 1.0f));+ auto cvtt = std::make_shared<op::v0::Convert>(table, element::f16);+ auto wg = std::make_shared<op::v8::Gather>(cvtt, cvtw, axis0());++ auto s = std::make_shared<op::v0::Parameter>(element::f16, Shape{kVocab, 1});+ auto mul = std::make_shared<op::v1::Multiply>(wg, s);+ auto cvtmul = std::make_shared<op::v0::Convert>(mul, element::f32);++ auto g = std::make_shared<op::v8::Gather>(cvtmul, clamped, axis0());+ return std::make_shared<ov::Model>(ResultVector{std::make_shared<op::v0::Result>(g)}, ParameterVector{ids, w, s});+}++template <typename Pass, typename... Args>+void run_pass(const std::shared_ptr<ov::Model>& model, Args&&... args) {+ ov::pass::GraphRewrite rewr;+ rewr.add_matcher<Pass>(std::forward<Args>(args)...);+ rewr.run_on_model(model);+}++} // namespace++using namespace ov;+using namespace ov::npuw::patterns::opt;++// ─────────────────────────────────────────────────────────────────────────────+// HostGather+// ─────────────────────────────────────────────────────────────────────────────+TEST(HostGatherIdClamp, HostGather_NoClamp_Matches) {+ auto model = make_host_gather_model(Clamp::None);++ Context ctx;+ run_pass<HostGather>(model, std::ref(ctx));++ EXPECT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_FALSE(has_gather(model));+}++TEST(HostGatherIdClamp, HostGather_ExactClamp_Matches) {+ auto model = make_host_gather_model(Clamp::Exact);++ Context ctx;+ run_pass<HostGather>(model, std::ref(ctx));++ EXPECT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_FALSE(has_gather(model));+}++TEST(HostGatherIdClamp, HostGather_MaximumOnlyClamp_Matches) {+ auto model = make_host_gather_model(Clamp::MaxOnly);++ Context ctx;+ run_pass<HostGather>(model, std::ref(ctx));++ EXPECT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_FALSE(has_gather(model));+}++TEST(HostGatherIdClamp, HostGather_WrongBoundClamp_DoesNotMatch) {+ auto model = make_host_gather_model(Clamp::WrongBound);++ Context ctx;+ run_pass<HostGather>(model, std::ref(ctx));++ EXPECT_FALSE(ctx.params_to_gather.has_value());+ EXPECT_TRUE(has_gather(model));+}++// ─────────────────────────────────────────────────────────────────────────────+// HostGatherDQ+// ─────────────────────────────────────────────────────────────────────────────+TEST(HostGatherIdClamp, HostGatherDQ_NoClamp_Matches) {+ auto model = make_host_gather_dq_model(Clamp::None);++ Context ctx;+ run_pass<HostGatherDQ>(model, std::ref(ctx));++ EXPECT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_EQ(ctx.params_to_unpack.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherDQ_ExactClamp_Matches) {+ auto model = make_host_gather_dq_model(Clamp::Exact);++ Context ctx;+ run_pass<HostGatherDQ>(model, std::ref(ctx));++ EXPECT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_EQ(ctx.params_to_unpack.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherDQ_WrongBoundClamp_DoesNotMatch) {+ auto model = make_host_gather_dq_model(Clamp::WrongBound);++ Context ctx;+ run_pass<HostGatherDQ>(model, std::ref(ctx));++ EXPECT_FALSE(ctx.params_to_gather.has_value());+ EXPECT_TRUE(ctx.params_to_unpack.empty());+}++// ─────────────────────────────────────────────────────────────────────────────+// HostGatherQuantAsymm+// ─────────────────────────────────────────────────────────────────────────────+TEST(HostGatherIdClamp, HostGatherQuantAsymm_NoClamp_Matches) {+ auto model = make_host_gather_quant_asymm_model(Clamp::None);++ Context ctx;+ run_pass<HostGatherQuantAsymm<op::v0::Parameter>>(model, std::ref(ctx), false);++ ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());+ EXPECT_EQ(ctx.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherQuantAsymm_ExactClamp_Matches) {+ auto model = make_host_gather_quant_asymm_model(Clamp::Exact);++ Context ctx;+ run_pass<HostGatherQuantAsymm<op::v0::Parameter>>(model, std::ref(ctx), false);++ ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());+ EXPECT_EQ(ctx.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherQuantAsymm_WrongBoundClamp_DoesNotMatch) {+ auto model = make_host_gather_quant_asymm_model(Clamp::WrongBound);++ Context ctx;+ run_pass<HostGatherQuantAsymm<op::v0::Parameter>>(model, std::ref(ctx), false);++ EXPECT_FALSE(ctx.params_to_quant_gather_unpack.has_value());+}++// ─────────────────────────────────────────────────────────────────────────────+// HostGatherQuantSymm+// ─────────────────────────────────────────────────────────────────────────────+TEST(HostGatherIdClamp, HostGatherQuantSymm_NoClamp_Matches) {+ auto model = make_host_gather_quant_symm_model(Clamp::None);++ Context ctx;+ run_pass<HostGatherQuantSymm<op::v0::Parameter>>(model, std::ref(ctx), false);++ ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());+ EXPECT_EQ(ctx.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherQuantSymm_ExactClamp_Matches) {+ auto model = make_host_gather_quant_symm_model(Clamp::Exact);++ Context ctx;+ run_pass<HostGatherQuantSymm<op::v0::Parameter>>(model, std::ref(ctx), false);++ ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());+ EXPECT_EQ(ctx.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherQuantSymm_WrongBoundClamp_DoesNotMatch) {+ auto model = make_host_gather_quant_symm_model(Clamp::WrongBound);++ Context ctx;+ run_pass<HostGatherQuantSymm<op::v0::Parameter>>(model, std::ref(ctx), false);++ EXPECT_FALSE(ctx.params_to_quant_gather_unpack.has_value());+}++// ─────────────────────────────────────────────────────────────────────────────+// HostGatherCB4+// ─────────────────────────────────────────────────────────────────────────────+TEST(HostGatherIdClamp, HostGatherCB4_NoClamp_Matches) {+ auto model = make_host_gather_cb4_model(Clamp::None);++ Context ctx;+ run_pass<HostGatherCB4>(model, std::ref(ctx));++ EXPECT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_EQ(ctx.params_to_nf4_gather.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherCB4_ExactClamp_Matches) {+ auto model = make_host_gather_cb4_model(Clamp::Exact);++ Context ctx;+ run_pass<HostGatherCB4>(model, std::ref(ctx));++ EXPECT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_EQ(ctx.params_to_nf4_gather.size(), 1u);+}++TEST(HostGatherIdClamp, HostGatherCB4_WrongBoundClamp_DoesNotMatch) {+ auto model = make_host_gather_cb4_model(Clamp::WrongBound);++ Context ctx;+ run_pass<HostGatherCB4>(model, std::ref(ctx));++ EXPECT_FALSE(ctx.params_to_gather.has_value());+ EXPECT_TRUE(ctx.params_to_nf4_gather.empty());+}++// ─────────────────────────────────────────────────────────────────────────────+// util::gather - the host-side replacement must reproduce both the folded-away+// clamp (saturation) and Gather's negative-index semantics.+// ─────────────────────────────────────────────────────────────────────────────+TEST(HostGatherIdClamp, UtilGatherSaturatesOutOfRangeIds) {+ constexpr size_t kRows = 4;+ constexpr size_t kCols = 3;++ auto src = ov::Tensor(element::f32, Shape{kRows, kCols});+ auto* psrc = src.data<float>();+ for (size_t r = 0; r < kRows; r++) {+ for (size_t c = 0; c < kCols; c++) {+ psrc[r * kCols + c] = static_cast<float>(r);+ }+ }++ const std::vector<int64_t> ids = {0, 3, -1, -4, 1000, -1000};+ auto idx = ov::Tensor(element::i64, Shape{1, ids.size()});+ std::copy(ids.begin(), ids.end(), idx.data<int64_t>());++ auto dst = ov::Tensor(element::f32, Shape{1, ids.size(), kCols});+ ov::npuw::util::gather(ov::get_tensor_impl(src), ov::get_tensor_impl(idx), ov::get_tensor_impl(dst));++ // 1000 saturates to 3, -1000 saturates to -4 which then wraps to row 0.+ const std::vector<float> expected_rows = {0.f, 3.f, 3.f, 0.f, 3.f, 0.f};+ const auto* pdst = dst.data<float>();+ for (size_t r = 0; r < expected_rows.size(); r++) {+ for (size_t c = 0; c < kCols; c++) {+ EXPECT_EQ(pdst[r * kCols + c], expected_rows[r]) << "row " << r << " col " << c;+ }+ }+}