Skip to content

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

[NPUW] Extended PYRAMID attention to Phi-3.5 (#33185)

Eugene Smirnov5a2383cae6b2
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
index 908b6267f49..66240e8736c 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
@@ -47,8 +47,8 @@ void dump_partitioning(const ov::npuw::Ensemble& ens, const std::string& to) {
if (!group.avoid_list.empty()) {
gr.append_attribute("avoid") = group.avoid_list.data();
}
- if (!group.tag.empty()) {
- gr.append_attribute("tag") = group.tag.data();
+ if (!group.gettag().empty()) {
+ gr.append_attribute("tag") = group.gettag().data();
}
// Note: Ensemble also add "id" attribute but it's not used by the plugin
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp
index b2af258f427..30fb8635783 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/group.cpp
@@ -121,7 +121,7 @@ ov::npuw::Group Group::toGroup() const {
}
}
- g.tag = m_isol_tag;
+ g.settag(m_isol_tag);
return g;
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
index 491f1bd477e..cd316234312 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -150,7 +150,7 @@ ov::npuw::Ensemble load_groups(const std::shared_ptr<ov::Model>& model, const st
this_group.gflops = get_float_attr(group, "gflops");
this_group.repeated_id = get_str_attr(group, "repeated", "");
this_group.avoid_list = get_str_attr(group, "avoid", "");
- this_group.tag = get_str_attr(group, "tag", "");
+ this_group.settag(get_str_attr(group, "tag", ""));
FOREACH_CHILD(input, group, "input") {
this_group.input_layers.push_back(get_str_attr(input, "name"));
}
@@ -420,7 +420,7 @@ void Partitioner::identifySubgraphs() {
P.total_ops += group.sg._ops;
group.sg._avoid_list = group.avoid_list;
- group.sg._tag = group.tag;
+ group.sg.settag(group.gettag());
// Note inputs and outputs are included in the above set, so if
// we are here, those nodes should be present in the model.
@@ -1620,7 +1620,7 @@ void Partitioner::createFunction(FunctionPipeline& func_ggg) {
ov::npuw::Function function;
function._model = func_ggg.mdls.front();
function._param_offset = body_sg._parameters.size();
- function._tag = body_sg._tag;
+ function.settag(body_sg.gettag());
std::size_t new_param_idx = function._param_offset;
for (auto&& node_ptr : function._model->get_ordered_ops()) {
@@ -1686,7 +1686,7 @@ void Partitioner::createFunction(FunctionPipeline& func_ggg) {
}
void Partitioner::identifySpatialRange(ov::npuw::Function& f) {
- NPUW_ASSERT(f._tag == "compute");
+ NPUW_ASSERT(f.gettag() == "compute");
// NB: The current logic must be changed. Here we assume we only
// apply this change to "compute" subgraphs which we identify
@@ -1844,7 +1844,7 @@ void Partitioner::spatial(const std::string& func_name) {
// Identify the spatial dimension for this function
// Works only for Compute case.
// FIXME: Replace this string identification with smt better
- if (!cfg.get<::intel_npu::NPUW_SPATIAL>() || f._tag != "compute") {
+ if (!cfg.get<::intel_npu::NPUW_SPATIAL>() || f.gettag() != "compute") {
LOG_VERB("No spatial optimizations will be done to " << func_name << " in model " << model->get_friendly_name()
<< "...");
return;
@@ -1886,7 +1886,7 @@ void Partitioner::attention(const std::string& func_name) {
ov::npuw::Function& f = P.functions.at(func_name);
// Support only attention at the time
- if (f._tag != "attn") {
+ if (f.gettag() != "attn") {
LOG_VERB("No dynamic handling be done to " << func_name << " in model " << model->get_friendly_name()
<< "...");
return;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
index 33879450a80..0729ad429b3 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
@@ -34,7 +34,6 @@ struct Subgraph {
bool _optimized_out = false;
std::string _avoid_list;
- std::string _tag;
// Function calls only (note: all the above fields are not used)
//
@@ -73,6 +72,17 @@ struct Subgraph {
QuantUnpackGather _quant_unpack_gather;
using Ref = std::reference_wrapper<Subgraph>;
+
+ void settag(const std::string& t) {
+ LOG_DEBUG("Subgraph set-tag=" << t);
+ _tag = t;
+ }
+ std::string gettag() const {
+ return _tag;
+ }
+
+private:
+ std::string _tag;
};
struct Function {
@@ -80,8 +90,6 @@ struct Function {
std::size_t _param_offset;
std::size_t _num_params_total;
- std::string _tag; // derived from the partitioning
-
// Mapping: from a prototype {Layer/input_idx} to {param_idx}
// NOTE: it seems it is required only for `matchRepeatedSubgraphs()'
std::map<std::pair<std::string, std::size_t>, std::size_t> _param_mapping;
@@ -92,9 +100,19 @@ struct Function {
// Multiple attention graphs with different shapes
std::optional<ov::npuw::function::PyramidAttention> _pyramid_attention;
// FIXME: They should exclude each other (introduce a hierarchy, finally?)
-
// FIXME: shouldn't be here. Needed to not unpack some lazy closures in DCOFF
std::set<std::size_t> _idx_lazy_unpack;
+
+ void settag(const std::string& t) {
+ LOG_DEBUG("Function set-tag=" << t);
+ _tag = t;
+ }
+ std::string gettag() const {
+ return _tag;
+ }
+
+private:
+ std::string _tag; // derived from the partitioning
};
struct Group {
@@ -106,7 +124,6 @@ struct Group {
float gflops;
std::string avoid_list;
- std::string tag;
// Set to true if the Group was forcibly turned to functon. Such
// function has just a single associated funcall and are subjects
@@ -114,6 +131,17 @@ struct Group {
bool forced_to_fcall = false;
ov::npuw::Subgraph sg;
+
+ void settag(const std::string& t) {
+ LOG_DEBUG("group set-tag=" << t);
+ _tag = t;
+ }
+ std::string gettag() const {
+ return _tag;
+ }
+
+private:
+ std::string _tag;
};
struct RepeatedBlock {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.cpp
index 4cd84bbd2c3..8e5a0a64e2d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.cpp
@@ -84,17 +84,17 @@ SDPA::SDPA(const std::shared_ptr<ov::npuw::online::Snapshot>& snapshot, const st
\ /
Concat
|
- Unsqueeze
+ opt:Unsqueeze
|
- Broadcast Convert
+ opt:Broadcast Convert
| \ /
- Reshape Concat
+ opt:Reshape Concat
\ / |
- MatMul Unsqueeze
+ MatMul opt:Unsqueeze
\ / |
- Add Broadcast
+ Add opt:Broadcast
| |
- Softmax Reshape
+ Softmax opt:Reshape
\ /
MatMul
|
@@ -108,15 +108,19 @@ SDPADecomposed::SDPADecomposed(const std::shared_ptr<ov::npuw::online::Snapshot>
const std::string& isol_tag) {
auto convert1 = opp::wrap_type<ov::op::v0::Convert>({opp::any_input()});
auto concat1 = opp::wrap_type<ov::op::v0::Concat>({convert1, opp::any_input()});
- auto unsqueeze1 = opp::wrap_type<ov::op::v0::Unsqueeze>({concat1, opp::any_input()});
- auto broadcast1 = opp::wrap_type<ov::op::v3::Broadcast>({unsqueeze1, opp::any_input()});
- auto reshape1 = opp::wrap_type<ov::op::v1::Reshape>({broadcast1, opp::any_input()});
+
+ // GQA optional nodes
+ auto unsqueeze1 = opp::optional<ov::op::v0::Unsqueeze>({concat1, opp::any_input()});
+ auto broadcast1 = opp::optional<ov::op::v3::Broadcast>({unsqueeze1, opp::any_input()});
+ auto reshape1 = opp::optional<ov::op::v1::Reshape>({broadcast1, opp::any_input()});
auto convert2 = opp::wrap_type<ov::op::v0::Convert>({opp::any_input()});
auto concat2 = opp::wrap_type<ov::op::v0::Concat>({convert2, opp::any_input()});
- auto unsqueeze2 = opp::wrap_type<ov::op::v0::Unsqueeze>({concat2, opp::any_input()});
- auto broadcast2 = opp::wrap_type<ov::op::v3::Broadcast>({unsqueeze2, opp::any_input()});
- auto reshape2 = opp::wrap_type<ov::op::v1::Reshape>({broadcast2, opp::any_input()});
+
+ // GQA optional nodes
+ auto unsqueeze2 = opp::optional<ov::op::v0::Unsqueeze>({concat2, opp::any_input()});
+ auto broadcast2 = opp::optional<ov::op::v3::Broadcast>({unsqueeze2, opp::any_input()});
+ auto reshape2 = opp::optional<ov::op::v1::Reshape>({broadcast2, opp::any_input()});
auto matmul1 = opp::wrap_type<ov::op::v0::MatMul>({opp::any_input(), reshape1});
auto add = opp::wrap_type<ov::op::v1::Add>({matmul1, opp::any_input()});
@@ -136,8 +140,11 @@ SDPADecomposed::SDPADecomposed(const std::shared_ptr<ov::npuw::online::Snapshot>
// Helper lambda to extract and isolate matched nodes
auto isolate_matched = [&](const auto& pattern) {
- auto matched_node = node_to_output.at(pattern).get_node_shared_ptr();
- node_to_gptr->at(matched_node)->isolate(isol_tag);
+ auto optional_node = node_to_output.find(pattern);
+ if (optional_node != node_to_output.end()) {
+ auto matched_node = optional_node->second.get_node_shared_ptr();
+ node_to_gptr->at(matched_node)->isolate(isol_tag);
+ }
};
// Isolate all matched nodes in the pattern