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
[TRANSFORMATIONS] Add compilation to SDPAToPA tests (#29922)
Andrii Staikovf92ff0708a4e
diff --git a/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cpp b/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cppindex 8c281cf4271..661ce748260 100644--- a/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cpp@@ -110,34 +110,37 @@ ov::pass::ConvertPagedAttnInputs::ConvertPagedAttnInputs(const KVCacheConfig& coauto value_cache_precision = format_cache_precision(m_config.valueCachePrecision, m_config.inferencePrecision);key_cache->set_element_type(key_cache_precision);value_cache->set_element_type(value_cache_precision);- if (!pa_op->get_rt_info().count("num_k_heads") || !pa_op->get_rt_info().count("k_head_size") ||- !pa_op->get_rt_info().count("num_v_heads") || !pa_op->get_rt_info().count("num_v_heads")) {+ bool status = false;+ if (pa_op->get_rt_info().count("num_k_heads") && pa_op->get_rt_info().count("k_head_size") &&+ pa_op->get_rt_info().count("num_v_heads") && pa_op->get_rt_info().count("num_v_heads")) {+ const auto key_cache_shape = init_cache_shape(pa_op->get_rt_info()["num_k_heads"].as<size_t>(),+ pa_op->get_rt_info()["k_head_size"].as<size_t>(),+ m_config.keyCacheBlockSize,+ key_cache_precision,+ m_config.keyCacheGroupSize,+ m_config.keyCacheQuantBychannel,+ m_config.keyCacheDimOrder);+ const auto value_cache_shape = init_cache_shape(pa_op->get_rt_info()["num_v_heads"].as<size_t>(),+ pa_op->get_rt_info()["v_head_size"].as<size_t>(),+ m_config.valueCacheBlockSize,+ value_cache_precision,+ m_config.valueCacheGroupSize,+ m_config.valueCacheQuantBychannel,+ m_config.valueCacheDimOrder);++ key_cache->set_partial_shape(key_cache_shape);+ value_cache->set_partial_shape(value_cache_shape);+ status = true;+ } else {OPENVINO_DEBUG("PagedAttn ",pa_op->get_friendly_name()," doesn't have rtinfo for num_k_heads/k_head_size/num_v_heads/num_v_heads");- return false;+ status = false;}- const auto key_cache_shape = init_cache_shape(pa_op->get_rt_info()["num_k_heads"].as<size_t>(),- pa_op->get_rt_info()["k_head_size"].as<size_t>(),- m_config.keyCacheBlockSize,- key_cache_precision,- m_config.keyCacheGroupSize,- m_config.keyCacheQuantBychannel,- m_config.keyCacheDimOrder);- const auto value_cache_shape = init_cache_shape(pa_op->get_rt_info()["num_v_heads"].as<size_t>(),- pa_op->get_rt_info()["v_head_size"].as<size_t>(),- m_config.valueCacheBlockSize,- value_cache_precision,- m_config.valueCacheGroupSize,- m_config.valueCacheQuantBychannel,- m_config.valueCacheDimOrder);-- key_cache->set_partial_shape(key_cache_shape);- value_cache->set_partial_shape(value_cache_shape);key_cache->validate_and_infer_types();value_cache->validate_and_infer_types();- return true;+ return status;};auto m = std::make_shared<ov::pass::pattern::Matcher>(result, matcher_name);diff --git a/tests/model_hub_tests/transformation_tests/test_pa_transformation.py b/tests/model_hub_tests/transformation_tests/test_pa_transformation.pyindex 46a029bdca9..2951713fe94 100644--- a/tests/model_hub_tests/transformation_tests/test_pa_transformation.py+++ b/tests/model_hub_tests/transformation_tests/test_pa_transformation.py@@ -14,17 +14,20 @@ import pytestimport osimport re-def compare_diffs(ov_model: ov.Model,- model_id: str,- use_block_indices_inputs: bool,- use_score_outputs: bool,- allow_cache_rotation: bool):+def apply_transformation_and_compare_diffs(ov_model: ov.Model,+ model_id: str,+ use_block_indices_inputs: bool,+ use_score_outputs: bool,+ allow_cache_rotation: bool,+ ie_device: str):+before_map = {}for op in ov_model.get_ordered_ops():if op.get_type_name() in nodes_to_compare:before_map[op.get_type_name()] = before_map.get(op.get_type_name(), 0) + 1paged_attention_transformation(ov_model, use_block_indices_inputs, use_score_outputs, allow_cache_rotation)+ ov.Core().compile_model(ov_model, ie_device)after_map = {}for op in ov_model.get_ordered_ops():@@ -101,11 +104,12 @@ def run_pa(tmp_path,cls: Union[Type[OVModelForCausalLM], Type[OVModelForVisualCausalLM]],use_block_indices_inputs,use_score_outputs,- allow_cache_rotation):+ allow_cache_rotation,+ ie_device):model = cls.from_pretrained(model_id, export=True, trust_remote_code=True)ov_model = model.model if cls is OVModelForCausalLM else model.lm_model- compare_diffs(ov_model, model_id, use_block_indices_inputs, use_score_outputs, allow_cache_rotation)+ apply_transformation_and_compare_diffs(ov_model, model_id, use_block_indices_inputs, use_score_outputs, allow_cache_rotation, ie_device)@pytest.mark.precommit@pytest.mark.parametrize("model_name, model_link, mark, reason", utils.get_models_list(os.path.join(os.path.dirname(__file__), "models", "hf-tiny-random-models-precommit")))@@ -116,7 +120,7 @@ def test_pa_precommit(tmp_path, model_name, model_link, mark, reason, ie_device)pytest.skip(reason)elif mark == 'xfail':pytest.xfail(reason)- run_pa(tmp_path, model_name, model_link, OVModelForCausalLM, False, False, False)+ run_pa(tmp_path, model_name, model_link, OVModelForCausalLM, False, False, False, ie_device)@pytest.mark.precommit@pytest.mark.parametrize("model_name, model_link, mark, reason", utils.get_models_list(os.path.join(os.path.dirname(__file__), "models", "hf-tiny-random-models-precommit")))@@ -127,7 +131,7 @@ def test_pa_precommit_use_cache_eviction(tmp_path, model_name, model_link, mark,pytest.skip(reason)elif mark == 'xfail':pytest.xfail(reason)- run_pa(tmp_path, model_name, model_link, OVModelForCausalLM, True, True, True)+ run_pa(tmp_path, model_name, model_link, OVModelForCausalLM, True, True, True, ie_device)@pytest.mark.precommit@pytest.mark.parametrize("model_name, model_link, mark, reason", utils.get_models_list(os.path.join(os.path.dirname(__file__), "models", "hf-tiny-random-vl-models-precommit")))@@ -138,7 +142,7 @@ def test_pa_vlm(tmp_path, model_name, model_link, mark, reason, ie_device):pytest.skip(reason)elif mark == 'xfail':pytest.xfail(reason)- run_pa(tmp_path, model_name, model_link, OVModelForVisualCausalLM, False, False, False)+ run_pa(tmp_path, model_name, model_link, OVModelForVisualCausalLM, False, False, False, ie_device)@pytest.mark.precommit@pytest.mark.parametrize("model_name, model_link, mark, reason", utils.get_models_list(os.path.join(os.path.dirname(__file__), "models", "hf-tiny-random-vl-models-precommit")))@@ -149,4 +153,4 @@ def test_pa_vlm_use_cache_eviction(tmp_path, model_name, model_link, mark, reasopytest.skip(reason)elif mark == 'xfail':pytest.xfail(reason)- run_pa(tmp_path, model_name, model_link, OVModelForVisualCausalLM, True, True, True)+ run_pa(tmp_path, model_name, model_link, OVModelForVisualCausalLM, True, True, True, ie_device)\ No newline at end of file