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] Fix Whisper to work with transformers 4.53.3 (#32468)
Anastasiya(Asya) Proninaad68a10ea8bb
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cppindex 2f367d6ebb9..960a3e1e961 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cpp@@ -328,7 +328,8 @@ public:auto unsqueeze1 = opp::wrap_type<ov::op::v0::Unsqueeze>({range, opp::any_input()});auto unsqueeze2 = opp::wrap_type<ov::op::v0::Unsqueeze>({unsqueeze1, opp::any_input()});auto unsqueeze3 = opp::wrap_type<ov::op::v0::Unsqueeze>({unsqueeze2, opp::any_input()});- auto lessequal = opp::wrap_type<ov::op::v1::LessEqual>({unsqueeze3, opp::any_input()});+ auto opt_convert = opp::optional<ov::op::v0::Convert>({unsqueeze3->output(0)});+ auto lessequal = opp::wrap_type<ov::op::v1::LessEqual>({opt_convert, opp::any_input()});register_matcher(std::make_shared<opp::Matcher>(lessequal, this->get_type_info().name),@@ -393,21 +394,27 @@ public:attention_mask->get_output_tensor(0).set_names({"attention_mask"});model->add_parameters({attention_mask});- auto slice = self_attn_nodes[0]->input(kAttnMaskPort).get_source_output().get_node();+ auto cst_ninf = std::make_shared<ov::op::v0::Constant>(ov::element::f32,+ ov::Shape{1},+ std::vector<float>{-std::numeric_limits<float>::max()});+ auto cst_1 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<float>{1});+ auto cst_0 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<float>{0});++ auto slice = self_attn_nodes[0]->input(kAttnMaskPort).get_source_output().get_node_shared_ptr();+ std::shared_ptr<ov::Node> slice_f32;+ if (slice->get_element_type() == ov::element::boolean) {+ slice_f32 = std::make_shared<ov::op::v1::Select>(slice->output(0), cst_0->output(0), cst_ninf->output(0));+ } else {+ slice_f32 = slice;+ }auto cvt = std::make_shared<ov::op::v0::Convert>(attention_mask->output(0), ov::element::f32);- auto add = std::make_shared<ov::op::v1::Add>(slice->output(0), cvt->output(0));+ auto add = std::make_shared<ov::op::v1::Add>(slice_f32->output(0), cvt->output(0));auto trps = std::make_shared<ov::op::v1::Transpose>(cvt->output(0),ov::op::v0::Constant::create(ov::element::i32, ov::Shape{2}, std::vector<int>{1, 0}));auto mtpl = std::make_shared<ov::op::v1::Multiply>(trps->output(0), add->output(0));- auto cst_ninf = std::make_shared<ov::op::v0::Constant>(ov::element::f32,- ov::Shape{1},- std::vector<float>{-std::numeric_limits<float>::max()});- auto cst_1 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<float>{1});- auto cst_0 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<float>{0});-auto equal = std::make_shared<ov::op::v1::Equal>(mtpl->output(0), cst_1->output(0));auto select = std::make_shared<ov::op::v1::Select>(equal->output(0), cst_0->output(0), cst_ninf->output(0));@@ -450,6 +457,39 @@ public:}}};++class CachePositionInput : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("npuw::LLMCompiledModel::CachePositionInput");++ CachePositionInput(std::shared_ptr<ov::Model> model) {+ auto gather = opp::wrap_type<ov::op::v8::Gather>({opp::any_input(), opp::any_input(), opp::any_input()});+ auto add = opp::wrap_type<ov::op::v1::Add>({gather, opp::any_input()});+ auto range = opp::wrap_type<ov::op::v4::Range>({gather, add, opp::any_input()});+ auto unsqueeze = opp::wrap_type<ov::op::v0::Unsqueeze>({range, opp::any_input()});+ auto tile = opp::wrap_type<ov::op::v0::Tile>({unsqueeze, opp::any_input()});++ register_matcher(+ std::make_shared<opp::Matcher>(tile, this->get_type_info().name),+ [model, unsqueeze](opp::Matcher& m) {+ auto& node_to_output = m.get_pattern_value_map();+ auto unsqueeze_node = node_to_output.at(unsqueeze).get_node_shared_ptr();+ auto matched_unsqueeze = std::static_pointer_cast<ov::op::v0::Unsqueeze>(unsqueeze_node);++ auto cache_position = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::Shape{1});+ cache_position->get_output_tensor(0).set_names({"cache_position"});+ cache_position->set_friendly_name("cache_position");+ model->add_parameters({cache_position});+ // If cache_position input is missed in the model, it means that position is calculated+ // by the model itself using fp32 range constructed from the shapes of inputs.+ // So operations below this range expect fp32 input.+ auto cache_position_f32 = std::make_shared<ov::op::v0::Convert>(cache_position, ov::element::f32);++ matched_unsqueeze->input(0).replace_source_output(cache_position_f32->output(0));+ return false;+ });+ }+};} // namespace#ifdef __GNUC__@@ -705,6 +745,22 @@ void add_attention_mask_input(std::shared_ptr<ov::Model> model,ov::pass::Validate().run_on_model(model);}++void add_cache_position_input(std::shared_ptr<ov::Model> model) {+ ov::pass::GraphRewrite rewr;+ rewr.add_matcher<CachePositionInput>(model);+ rewr.run_on_model(model);++ ov::pass::Validate().run_on_model(model);+}++bool input_exists(const std::shared_ptr<ov::Model>& model, const std::string& name) {+ auto inputs = model->inputs();+ auto it = std::find_if(inputs.begin(), inputs.end(), [&](const auto& port) {+ return port.get_names().count(name) != 0;+ });+ return it != inputs.end();+}} // namespacestd::shared_ptr<ov::Model> ov::npuw::util::prepare_whisper_prefill_model(std::shared_ptr<ov::Model>& model,@@ -714,8 +770,10 @@ std::shared_ptr<ov::Model> ov::npuw::util::prepare_whisper_prefill_model(std::sh// remove_input_kv_tensors(model); -> Done for LLM also// 3) Expose all states that requires initialization on the first run as outputsexpose_runtime_states_as_outputs(model);- // 4) Remove cache_position input- remove_cache_position(model);+ // 4) Remove cache_position input if it exists+ if (input_exists(model, "cache_position")) {+ remove_cache_position(model);+ }// 5) Normalize output names - should be done in stateful_to_stateless_transformationnormalize_output_key_value_names(model);@@ -731,6 +789,10 @@ std::shared_ptr<ov::Model> ov::npuw::util::prepare_whisper_kvcache_model(std::shnormalize_output_key_value_names(model);expose_runtime_states_as_inputs(model);+ if (!input_exists(model, "cache_position")) {+ add_cache_position_input(model);+ }+add_attention_mask_input(model);model->reshape({{"input_ids", ov::PartialShape({-1, 1})}});