Skip to content

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

48.1 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "transformations/paged_attention/convert_pagedattn_inputs.hpp"

#include <gtest/gtest.h>

#include "common_test_utils/ov_test_utils.hpp"
#include "openvino/core/rt_info.hpp"
#include "openvino/op/paged_attention.hpp"
#include "openvino/op/paged_causal_conv1d.hpp"
#include "openvino/op/paged_gated_delta_net.hpp"
#include "openvino/op/paged_selective_ssm.hpp"
#include "openvino/op/paged_sparse_attention.hpp"
#include "openvino/pass/constant_folding.hpp"
#include "openvino/pass/manager.hpp"
#include "openvino/runtime/properties.hpp"
#include "transformations/convert_precision.hpp"
#include "transformations/rt_info/keep_const_precision.hpp"
#include "transformations/utils/gen_pattern.hpp"

using namespace ov::test;
using namespace ov;
using namespace ov::gen_pattern;
using ConvertPagedAttnInputsParams = std::tuple<std::vector<ov::element::Type>,  // cache_precision
                                                std::vector<size_t>,             // cache_group_size
                                                std::vector<size_t>,             // block_size
                                                ov::element::Type,               // infer_precsion
                                                bool,                            // quant_key_by_channel
                                                bool,                            // accuracy_mode
                                                bool                             // is_ir_kv_cache_f16
                                                >;

namespace v0 = ov::op::v0;
namespace {
class ConvertPagedAttnInputsTest : public TransformationTestsF,
                                   public testing::WithParamInterface<ConvertPagedAttnInputsParams> {
public:
    static std::string getTestCaseName(const testing::TestParamInfo<ConvertPagedAttnInputsParams>& obj) {
        const auto& [cachePrecision,
                     cacheGroupSize,
                     blcokSize,
                     inferPrec,
                     quantKeybychannel,
                     isAccuracyMode,
                     isIRKVCacheF16] = obj.param;
        std::ostringstream result;
        result << "KeyPrc=" << cachePrecision[0] << "_";
        result << "ValuePrc=" << cachePrecision[1] << "_";
        result << "KeyBlockSize=" << blcokSize[0] << "_";
        result << "ValueBlockSize=" << blcokSize[1] << "_";
        result << "InferPrec=" << inferPrec << "_";
        result << "KeyGS=" << cacheGroupSize[0] << "_";
        result << "ValueGS=" << cacheGroupSize[1] << "_";
        result << "KeyByChannel=" << quantKeybychannel << "_";
        result << "isAccuracyMode=" << isAccuracyMode << "_";
        result << "isIRKVCacheF16=" << isIRKVCacheF16;
        return result.str();
    }

public:
    size_t keyHeadSize;
    size_t valueHeadSize;
    size_t numKeyHeads;
    size_t numValueHeads;
    ov::element::Type inferPrec;
    ov::element::Type keyCachePrecision;
    ov::element::Type valueCachePrecision;
    std::vector<size_t> blockSize;
    size_t keyCacheGroupSize;
    size_t valueCacheGroupSize;
    bool quantKeybychannel;
    bool isAccuracyMode;
    bool isIRKVCacheF16;
};

TEST_P(ConvertPagedAttnInputsTest, checkPrecisionAndShape) {
    const auto& [cachePrecision,
                 cacheGroupSize,
                 _blockSize,
                 _inferPrec,
                 _quantKeybychannel,
                 _isAccuracyMode,
                 _isIRKVCacheF16] = this->GetParam();
    blockSize = _blockSize;
    inferPrec = _inferPrec;
    quantKeybychannel = _quantKeybychannel;
    isAccuracyMode = _isAccuracyMode;
    isIRKVCacheF16 = _isIRKVCacheF16;
    keyCachePrecision = cachePrecision[0];
    valueCachePrecision = cachePrecision[1];
    keyCacheGroupSize = cacheGroupSize[0];
    valueCacheGroupSize = cacheGroupSize[1];
    numKeyHeads = 2;
    keyHeadSize = 32;

    numValueHeads = 2;
    valueHeadSize = 64;
    {
        auto Q = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{-1, 4 * 32});
        auto K = std::make_shared<v0::Parameter>(
            ov::element::f32,
            PartialShape{-1, static_cast<ov::Dimension::value_type>(numKeyHeads * keyHeadSize)});
        auto V = std::make_shared<v0::Parameter>(
            ov::element::f32,
            PartialShape{-1, static_cast<ov::Dimension::value_type>(numValueHeads * valueHeadSize)});
        auto max_context_len = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{});
        auto block_indices_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto subsequence_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto past_lens = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto key_cache_0 = std::make_shared<v0::Parameter>(ov::element::dynamic, PartialShape::dynamic(4));
        auto value_cache_0 = std::make_shared<v0::Parameter>(ov::element::dynamic, PartialShape::dynamic(4));
        auto scale = std::make_shared<v0::Constant>(element::f32, Shape{}, 0.5f);
        auto sliding_window = std::make_shared<v0::Constant>(element::i32, Shape{}, 0);
        auto alibi_slopes = std::make_shared<v0::Constant>(element::f32, Shape{0});
        auto score_aggregation_window = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto rotated_block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto rotation_deltas = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto rotation_trig_lut = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
        auto xattention_threshold = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
        auto xattention_block_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
        auto xattention_stride = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
        auto sinks = std::make_shared<v0::Constant>(element::f32, Shape{0, 0, 0, 0});
        auto adaptive_rkv_start_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
        auto adaptive_rkv_evictable_sizes = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto adaptive_rkv_diversity_block_set_indices =
            std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto adaptive_rkv_diversity_block_set_indices_begins =
            std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto token_type_ids = std::make_shared<op::v0::Parameter>(ov::element::i32, ov::Shape{0});

        auto qq_bias = std::make_shared<v0::Parameter>(ov::element::u8, PartialShape{DYN});
        auto qq_bias_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto pa =
            std::make_shared<op::PagedAttentionExtension>(OutputVector{Q,
                                                                       K,
                                                                       V,
                                                                       key_cache_0,
                                                                       value_cache_0,
                                                                       past_lens,
                                                                       subsequence_begins,
                                                                       block_indices,
                                                                       block_indices_begins,
                                                                       scale,
                                                                       sliding_window,
                                                                       alibi_slopes,
                                                                       max_context_len,
                                                                       score_aggregation_window,
                                                                       rotated_block_indices,
                                                                       rotation_deltas,
                                                                       rotation_trig_lut,
                                                                       xattention_threshold,
                                                                       xattention_block_size,
                                                                       xattention_stride,
                                                                       sinks,
                                                                       adaptive_rkv_start_size,
                                                                       adaptive_rkv_evictable_sizes,
                                                                       adaptive_rkv_diversity_block_set_indices,
                                                                       adaptive_rkv_diversity_block_set_indices_begins,
                                                                       token_type_ids,
                                                                       qq_bias,
                                                                       qq_bias_begins});
        pa->get_rt_info()["num_k_heads"] = numKeyHeads;
        pa->get_rt_info()["k_head_size"] = keyHeadSize;
        pa->get_rt_info()["num_v_heads"] = numValueHeads;
        pa->get_rt_info()["v_head_size"] = valueHeadSize;

        model = std::make_shared<ov::Model>(ov::OutputVector{pa},
                                            ov::ParameterVector{Q,
                                                                K,
                                                                V,
                                                                key_cache_0,
                                                                value_cache_0,
                                                                past_lens,
                                                                subsequence_begins,
                                                                block_indices,
                                                                block_indices_begins,
                                                                max_context_len,
                                                                score_aggregation_window,
                                                                rotated_block_indices,
                                                                rotation_deltas,
                                                                rotation_trig_lut,
                                                                xattention_threshold,
                                                                xattention_block_size,
                                                                xattention_stride,
                                                                adaptive_rkv_start_size,
                                                                adaptive_rkv_evictable_sizes,
                                                                adaptive_rkv_diversity_block_set_indices,
                                                                adaptive_rkv_diversity_block_set_indices_begins,
                                                                token_type_ids,
                                                                qq_bias,
                                                                qq_bias_begins});

        if (isIRKVCacheF16) {
            model->set_rt_info("f16", "runtime_options", ov::hint::kv_cache_precision.name());
        }
    }

    {
        auto getCachePrec = [&](ov::element::Type tensorPrec) {
            tensorPrec =
                tensorPrec == ov::element::f16 && inferPrec == ov::element::bf16 ? ov::element::bf16 : tensorPrec;
            if (isAccuracyMode)
                tensorPrec = ov::element::f32;
            return tensorPrec;
        };
        auto getCacheShape = [&](ov::element::Type cachePrec,
                                 size_t headNums,
                                 size_t headSize,
                                 size_t groupSize,
                                 size_t blockSize,
                                 bool quantBychannel) {
            auto targeShape = PartialShape::dynamic(4);
            targeShape[1] = headNums;
            groupSize = groupSize ? groupSize : headSize;
            const size_t paramSize = 2 * sizeof(float) * 8 / cachePrec.bitwidth();
            if (!cachePrec.is_integral()) {
                targeShape[2] = blockSize;
                targeShape[3] = headSize;
            } else if (quantBychannel) {
                targeShape[2] = paramSize + blockSize;
                targeShape[3] = headSize;
            } else {
                size_t groupNum = headSize / groupSize;
                targeShape[2] = blockSize;
                targeShape[3] = headSize + paramSize * groupNum;
            }
            return targeShape;
        };

        keyCachePrecision = getCachePrec(keyCachePrecision);
        valueCachePrecision = getCachePrec(valueCachePrecision);
        auto Q = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{-1, 4 * 32});
        auto K = std::make_shared<v0::Parameter>(
            ov::element::f32,
            PartialShape{-1, static_cast<ov::Dimension::value_type>(numKeyHeads * keyHeadSize)});
        auto V = std::make_shared<v0::Parameter>(
            ov::element::f32,
            PartialShape{-1, static_cast<ov::Dimension::value_type>(numValueHeads * valueHeadSize)});
        auto max_context_len = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{});
        auto block_indices_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto subsequence_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto past_lens = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto key_cache_0 = std::make_shared<v0::Parameter>(keyCachePrecision,
                                                           getCacheShape(keyCachePrecision,
                                                                         numKeyHeads,
                                                                         keyHeadSize,
                                                                         keyCacheGroupSize,
                                                                         blockSize[0],
                                                                         quantKeybychannel));
        auto value_cache_0 = std::make_shared<v0::Parameter>(
            valueCachePrecision,
            getCacheShape(valueCachePrecision, numKeyHeads, valueHeadSize, valueCacheGroupSize, blockSize[1], false));
        auto scale = std::make_shared<v0::Constant>(element::f32, Shape{}, 0.5f);
        auto sliding_window = std::make_shared<v0::Constant>(element::i32, Shape{}, 0);
        auto alibi_slopes = std::make_shared<v0::Constant>(element::f32, Shape{0});
        auto score_aggregation_window = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto rotated_block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto rotation_deltas = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto rotation_trig_lut = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
        auto xattention_threshold = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
        auto xattention_block_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
        auto xattention_stride = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
        auto sinks = std::make_shared<v0::Constant>(element::f32, Shape{0, 0, 0, 0});
        auto adaptive_rkv_start_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
        auto adaptive_rkv_evictable_sizes = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto adaptive_rkv_diversity_block_set_indices =
            std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto adaptive_rkv_diversity_block_set_indices_begins =
            std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto token_type_ids = std::make_shared<v0::Parameter>(ov::element::i32, ov::Shape{0});

        auto qq_bias = std::make_shared<v0::Parameter>(ov::element::u8, PartialShape{DYN});
        auto qq_bias_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
        auto pa =
            std::make_shared<op::PagedAttentionExtension>(OutputVector{Q,
                                                                       K,
                                                                       V,
                                                                       key_cache_0,
                                                                       value_cache_0,
                                                                       past_lens,
                                                                       subsequence_begins,
                                                                       block_indices,
                                                                       block_indices_begins,
                                                                       scale,
                                                                       sliding_window,
                                                                       alibi_slopes,
                                                                       max_context_len,
                                                                       score_aggregation_window,
                                                                       rotated_block_indices,
                                                                       rotation_deltas,
                                                                       rotation_trig_lut,
                                                                       xattention_threshold,
                                                                       xattention_block_size,
                                                                       xattention_stride,
                                                                       sinks,
                                                                       adaptive_rkv_start_size,
                                                                       adaptive_rkv_evictable_sizes,
                                                                       adaptive_rkv_diversity_block_set_indices,
                                                                       adaptive_rkv_diversity_block_set_indices_begins,
                                                                       token_type_ids,
                                                                       qq_bias,
                                                                       qq_bias_begins});
        pa->get_rt_info()["num_k_heads"] = numKeyHeads;
        pa->get_rt_info()["k_head_size"] = keyHeadSize;
        pa->get_rt_info()["num_v_heads"] = numValueHeads;
        pa->get_rt_info()["v_head_size"] = valueHeadSize;

        model_ref = std::make_shared<ov::Model>(ov::OutputVector{pa},
                                                ov::ParameterVector{Q,
                                                                    K,
                                                                    V,
                                                                    key_cache_0,
                                                                    value_cache_0,
                                                                    past_lens,
                                                                    subsequence_begins,
                                                                    block_indices,
                                                                    block_indices_begins,
                                                                    max_context_len,
                                                                    score_aggregation_window,
                                                                    rotated_block_indices,
                                                                    rotation_deltas,
                                                                    rotation_trig_lut,
                                                                    xattention_threshold,
                                                                    xattention_block_size,
                                                                    xattention_stride,
                                                                    adaptive_rkv_start_size,
                                                                    adaptive_rkv_evictable_sizes,
                                                                    adaptive_rkv_diversity_block_set_indices,
                                                                    adaptive_rkv_diversity_block_set_indices_begins,
                                                                    token_type_ids,
                                                                    qq_bias,
                                                                    qq_bias_begins});
    }
    ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
    cacheConfig.keyCacheBlockSize = blockSize[0];
    cacheConfig.valueCacheBlockSize = blockSize[1];
    if (isAccuracyMode) {
        cacheConfig.inferencePrecision = ov::element::f32;
        cacheConfig.keyCachePrecision = ov::element::f32;
        cacheConfig.valueCachePrecision = ov::element::f32;
    } else {
        cacheConfig.keyCachePrecision = keyCachePrecision;
        cacheConfig.valueCachePrecision = valueCachePrecision;
        cacheConfig.keyCacheGroupSize = keyCacheGroupSize;
        cacheConfig.valueCacheGroupSize = valueCacheGroupSize;
    }
    auto update_paged_attention_shape_func = [](const ov::element::Type& precision,
                                                const bool bychannel,
                                                const size_t group_num,
                                                int64_t& head_size,
                                                int64_t& block_size) {
        if (precision == ov::element::u8) {
            if (bychannel) {
                block_size += 2 * sizeof(float);
            } else {
                head_size += sizeof(float) * 2 * group_num;
            }
        } else if (precision == ov::element::u4) {
            head_size += sizeof(float) * 2 * group_num * 2;
        }
    };

    manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig, update_paged_attention_shape_func);
    comparator.disable(FunctionsComparator::ACCURACY);
    comparator.disable(FunctionsComparator::RUNTIME_KEYS);
    disable_result_friendly_names_check();
    disable_rt_info_check();
}

std::vector<std::vector<ov::element::Type>> get_cache_prec() {
    return {
        {ov::element::f32, ov::element::f32},
        {ov::element::f16, ov::element::f16},
        {ov::element::u8, ov::element::u8},
        {ov::element::u8, ov::element::u4},
        {ov::element::u4, ov::element::u4},
    };
}

// group size
const std::vector<std::vector<size_t>> cache_gs = {{32, 16}, {0, 0}};

// block size
const std::vector<std::vector<size_t>> cache_bs = {{32, 16}};

INSTANTIATE_TEST_SUITE_P(smoke_ConvertPagedAttnInputsTest,
                         ConvertPagedAttnInputsTest,
                         ::testing::Combine(::testing::ValuesIn(get_cache_prec()),
                                            ::testing::ValuesIn(cache_gs),
                                            ::testing::ValuesIn(cache_bs),
                                            ::testing::Values(ov::element::f32, ov::element::bf16),
                                            ::testing::Values(false),
                                            ::testing::Values(true, false),
                                            ::testing::Values(true, false)),
                         ConvertPagedAttnInputsTest::getTestCaseName);

class ConvertPagedAttnInputsStateTableTest : public testing::Test {};

// The tests bellow replicate the behavior of ConvertPagedAttnInputs pass in a GPU pipeline.
// Testing it in isolation does not really make sense because in a real pipeline the model is
// different and it's important to replicate the same conditions that a model is going through.
// TODO: consider moving ConvertPagedAttnInputs above in the pipeline so we avoid taking into account
// the ConvertPrecision pass that much.
TEST_F(ConvertPagedAttnInputsStateTableTest, ConvertPagedCausalConv1DInputsPrecision) {
    auto input_embeds = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 256});
    auto conv_state_table = std::make_shared<v0::Parameter>(element::dynamic, PartialShape{-1, 256, 4});
    conv_state_table->set_friendly_name("conv_state_table.0");
    ov::enable_keep_const_precision(conv_state_table);
    auto conv_weight = std::make_shared<v0::Parameter>(element::f32, PartialShape{256, 1, 4});
    auto conv_bias = std::make_shared<v0::Parameter>(element::f32, PartialShape{256});
    auto subsequence_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto past_lens = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto cache_interval = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});

    auto paged_conv = std::make_shared<op::internal::PagedCausalConv1D>(input_embeds,
                                                                        conv_state_table,
                                                                        conv_weight,
                                                                        conv_bias,
                                                                        subsequence_begins,
                                                                        block_indices,
                                                                        block_indices_begins,
                                                                        past_lens,
                                                                        cache_interval);
    auto local_model = std::make_shared<Model>(OutputVector{paged_conv},
                                               ParameterVector{input_embeds,
                                                               conv_state_table,
                                                               conv_weight,
                                                               conv_bias,
                                                               subsequence_begins,
                                                               block_indices,
                                                               block_indices_begins,
                                                               past_lens,
                                                               cache_interval});

    // Replicate GPU pipeline: clear keep_const_precision on all nodes
    for (auto& node : local_model->get_ops()) {
        ov::disable_keep_const_precision(node);
    }

    ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
    cacheConfig.inferencePrecision = ov::element::f16;

    ov::pass::Manager local_manager;

    precisions_map fp_convert_precision_map = {{ov::element::f64, ov::element::f32},
                                               {ov::element::f32, ov::element::f16}};
    type_to_fuse_map empty_fuse_map = {};
    local_manager.register_pass<ov::pass::ConvertPrecision>(fp_convert_precision_map,
                                                            empty_fuse_map,
                                                            /*keep_precision_sensitive_in_fp32*/ true,
                                                            /*convert_input_output_precision*/ false,
                                                            /*store_original_precision_as_rt_attribute*/ true);

    auto update_paged_attention_shape_func =
        [](const ov::element::Type&, const bool, const size_t, int64_t&, int64_t&) {};
    local_manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig, update_paged_attention_shape_func);
    local_manager.run_passes(local_model);

    // Verify no Convert node between Parameter and PagedCausalConv1D
    EXPECT_TRUE(ov::is_type<v0::Parameter>(paged_conv->get_input_node_shared_ptr(1)));
    EXPECT_EQ(conv_state_table->get_element_type(), ov::element::f16);
}

TEST_F(ConvertPagedAttnInputsStateTableTest, ConvertPagedGatedDeltaNetPrecision) {
    auto query = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2, 64});
    auto key = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2, 64});
    auto value = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2, 64});
    auto gated_delta_state_table = std::make_shared<v0::Parameter>(element::dynamic, PartialShape{-1, 2, 64, 64});
    gated_delta_state_table->set_friendly_name("gated_delta_state_table.0");
    enable_keep_const_precision(gated_delta_state_table);
    auto gate = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2});
    auto beta = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2});
    auto subsequence_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto past_lens = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto cache_interval = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});

    auto paged_gdn = std::make_shared<op::internal::PagedGatedDeltaNet>(query,
                                                                        key,
                                                                        value,
                                                                        gated_delta_state_table,
                                                                        gate,
                                                                        beta,
                                                                        subsequence_begins,
                                                                        block_indices,
                                                                        block_indices_begins,
                                                                        past_lens,
                                                                        cache_interval);
    auto local_model = std::make_shared<Model>(OutputVector{paged_gdn},
                                               ParameterVector{query,
                                                               key,
                                                               value,
                                                               gated_delta_state_table,
                                                               gate,
                                                               beta,
                                                               subsequence_begins,
                                                               block_indices,
                                                               block_indices_begins,
                                                               past_lens,
                                                               cache_interval});

    // Replicate GPU pipeline: clear keep_const_precision on all nodes
    for (auto& node : local_model->get_ops()) {
        ov::disable_keep_const_precision(node);
    }

    ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
    cacheConfig.inferencePrecision = ov::element::f16;

    ov::pass::Manager local_manager;

    precisions_map fp_convert_precision_map = {{ov::element::f64, ov::element::f32},
                                               {ov::element::f32, ov::element::f16}};
    type_to_fuse_map empty_fuse_map = {};
    local_manager.register_pass<ov::pass::ConvertPrecision>(fp_convert_precision_map,
                                                            empty_fuse_map,
                                                            /*keep_precision_sensitive_in_fp32*/ true,
                                                            /*convert_input_output_precision*/ false,
                                                            /*store_original_precision_as_rt_attribute*/ true);

    auto update_paged_attention_shape_func =
        [](const ov::element::Type&, const bool, const size_t, int64_t&, int64_t&) {};
    local_manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig, update_paged_attention_shape_func);
    local_manager.run_passes(local_model);

    // Verify no Convert node between Parameter and PagedGatedDeltaNet
    EXPECT_TRUE(ov::is_type<v0::Parameter>(paged_gdn->get_input_node_shared_ptr(3)));
    EXPECT_EQ(gated_delta_state_table->get_element_type(), ov::element::f16);
}

TEST_F(ConvertPagedAttnInputsStateTableTest, ConvertPagedSelectiveSSMPrecision) {
    auto A = std::make_shared<v0::Parameter>(element::f32, PartialShape{4});
    auto dt = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 4});
    auto B = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2, 16});
    auto x = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 4, 8});
    auto C = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2, 16});
    auto selective_ssm_state_table = std::make_shared<v0::Parameter>(element::dynamic, PartialShape{-1, 4, 8, 16});
    selective_ssm_state_table->set_friendly_name("selective_ssm_state_table.0");
    enable_keep_const_precision(selective_ssm_state_table);
    auto subsequence_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto num_processed_tokens = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto cache_interval = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});

    auto paged_ssm = std::make_shared<op::internal::PagedSelectiveSSM>(A,
                                                                       dt,
                                                                       B,
                                                                       x,
                                                                       C,
                                                                       selective_ssm_state_table,
                                                                       subsequence_begins,
                                                                       block_indices,
                                                                       block_indices_begins,
                                                                       num_processed_tokens,
                                                                       cache_interval);
    auto local_model = std::make_shared<Model>(OutputVector{paged_ssm},
                                               ParameterVector{A,
                                                               dt,
                                                               B,
                                                               x,
                                                               C,
                                                               selective_ssm_state_table,
                                                               subsequence_begins,
                                                               block_indices,
                                                               block_indices_begins,
                                                               num_processed_tokens,
                                                               cache_interval});

    // Replicate GPU pipeline: clear keep_const_precision on all nodes
    for (auto& node : local_model->get_ops()) {
        ov::disable_keep_const_precision(node);
    }

    ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
    cacheConfig.inferencePrecision = ov::element::f16;

    ov::pass::Manager local_manager;

    precisions_map fp_convert_precision_map = {{ov::element::f64, ov::element::f32},
                                               {ov::element::f32, ov::element::f16}};
    type_to_fuse_map empty_fuse_map = {};
    local_manager.register_pass<ov::pass::ConvertPrecision>(fp_convert_precision_map,
                                                            empty_fuse_map,
                                                            /*keep_precision_sensitive_in_fp32*/ true,
                                                            /*convert_input_output_precision*/ false,
                                                            /*store_original_precision_as_rt_attribute*/ true);

    auto update_paged_attention_shape_func =
        [](const ov::element::Type&, const bool, const size_t, int64_t&, int64_t&) {};
    local_manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig, update_paged_attention_shape_func);
    local_manager.run_passes(local_model);

    for (size_t input = 0; input < 5; ++input) {
        EXPECT_EQ(paged_ssm->get_input_element_type(input), element::f16);
    }
    // Verify no Convert node between Parameter and PagedSelectiveSSM
    EXPECT_TRUE(ov::is_type<v0::Parameter>(paged_ssm->get_input_node_shared_ptr(5)));
    EXPECT_EQ(selective_ssm_state_table->get_element_type(), ov::element::f16);
    EXPECT_EQ(paged_ssm->get_output_element_type(0), element::f16);
}

TEST_F(ConvertPagedAttnInputsStateTableTest, ConvertPagedSelectiveSSMIndependentStatePrecision) {
    auto A = std::make_shared<v0::Parameter>(element::f32, PartialShape{4});
    auto dt = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 4});
    auto B = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2, 16});
    auto x = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 4, 8});
    auto C = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, 2, 16});
    auto selective_ssm_state_table = std::make_shared<v0::Parameter>(element::dynamic, PartialShape{-1, 4, 8, 16});
    selective_ssm_state_table->set_friendly_name("selective_ssm_state_table.0");
    for (const auto& parameter : {A, dt, B, x, C, selective_ssm_state_table}) {
        enable_keep_const_precision(parameter);
    }
    auto subsequence_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto num_processed_tokens = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto cache_interval = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});

    auto paged_ssm = std::make_shared<op::internal::PagedSelectiveSSM>(A,
                                                                       dt,
                                                                       B,
                                                                       x,
                                                                       C,
                                                                       selective_ssm_state_table,
                                                                       subsequence_begins,
                                                                       block_indices,
                                                                       block_indices_begins,
                                                                       num_processed_tokens,
                                                                       cache_interval);
    auto local_model = std::make_shared<Model>(OutputVector{paged_ssm},
                                               ParameterVector{A,
                                                               dt,
                                                               B,
                                                               x,
                                                               C,
                                                               selective_ssm_state_table,
                                                               subsequence_begins,
                                                               block_indices,
                                                               block_indices_begins,
                                                               num_processed_tokens,
                                                               cache_interval});

    ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
    cacheConfig.inferencePrecision = ov::element::f16;

    ov::pass::Manager local_manager;

    precisions_map fp_convert_precision_map = {{ov::element::f64, ov::element::f32},
                                               {ov::element::f32, ov::element::f16}};
    type_to_fuse_map empty_fuse_map = {};
    local_manager.register_pass<ov::pass::ConvertPrecision>(fp_convert_precision_map,
                                                            empty_fuse_map,
                                                            /*keep_precision_sensitive_in_fp32*/ true,
                                                            /*convert_input_output_precision*/ false,
                                                            /*store_original_precision_as_rt_attribute*/ true);

    auto update_paged_attention_shape_func =
        [](const ov::element::Type&, const bool, const size_t, int64_t&, int64_t&) {};
    local_manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig, update_paged_attention_shape_func);
    local_manager.run_passes(local_model);

    for (size_t input = 0; input < 5; ++input) {
        EXPECT_EQ(paged_ssm->get_input_element_type(input), element::f32);
    }
    EXPECT_TRUE(ov::is_type<v0::Parameter>(paged_ssm->get_input_node_shared_ptr(5)));
    EXPECT_EQ(selective_ssm_state_table->get_element_type(), ov::element::f16);
    EXPECT_EQ(paged_ssm->get_output_element_type(0), element::f32);
}

// PagedSparseAttention caches get the same precision and padded shapes as PagedAttentionExtension, and the op
// stays valid with the padded shapes.
TEST(ConvertPagedAttnInputsSparseTest, PagedSparseAttentionQuantizedCaches) {
    constexpr size_t num_heads = 4;
    constexpr size_t num_kv_heads = 2;
    constexpr size_t key_head_size = 32;
    constexpr size_t value_head_size = 16;
    constexpr size_t key_block_size = 32;
    constexpr size_t value_block_size = 32;

    auto query = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, num_heads * key_head_size});
    auto key = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, num_kv_heads * key_head_size});
    auto value = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, num_kv_heads * value_head_size});
    auto key_cache = std::make_shared<v0::Parameter>(element::dynamic, PartialShape::dynamic(4));
    auto value_cache = std::make_shared<v0::Parameter>(element::dynamic, PartialShape::dynamic(4));
    auto past_lens = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto subsequence_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto block_indices_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
    auto selected_block_indices = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1, 1, 8});
    auto selected_block_count = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1, 1});
    auto scale = std::make_shared<v0::Constant>(element::f32, Shape{}, 0.5f);
    auto sinks = std::make_shared<v0::Constant>(element::f32, Shape{0});
    auto psa = std::make_shared<op::internal::PagedSparseAttention>(OutputVector{query,
                                                                                 key,
                                                                                 value,
                                                                                 key_cache,
                                                                                 value_cache,
                                                                                 past_lens,
                                                                                 subsequence_begins,
                                                                                 block_indices,
                                                                                 block_indices_begins,
                                                                                 selected_block_indices,
                                                                                 selected_block_count,
                                                                                 scale,
                                                                                 sinks},
                                                                    4);
    psa->get_rt_info()["num_k_heads"] = num_kv_heads;
    psa->get_rt_info()["k_head_size"] = key_head_size;
    psa->get_rt_info()["num_v_heads"] = num_kv_heads;
    psa->get_rt_info()["v_head_size"] = value_head_size;
    auto model = std::make_shared<ov::Model>(OutputVector{psa},
                                             ParameterVector{query,
                                                             key,
                                                             value,
                                                             key_cache,
                                                             value_cache,
                                                             past_lens,
                                                             subsequence_begins,
                                                             block_indices,
                                                             block_indices_begins,
                                                             selected_block_indices,
                                                             selected_block_count});

    ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
    cacheConfig.inferencePrecision = element::f32;
    cacheConfig.keyCachePrecision = element::u8;
    cacheConfig.valueCachePrecision = element::u8;
    cacheConfig.keyCacheBlockSize = key_block_size;
    cacheConfig.valueCacheBlockSize = value_block_size;
    // u8 by-token: scale and zero point are appended to the head size.
    auto update_shape = [](const ov::element::Type& precision, const bool, const size_t, int64_t& head_size, int64_t&) {
        if (precision == ov::element::u8) {
            head_size += 2 * sizeof(float);
        }
    };

    ov::pass::Manager manager;
    manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig, update_shape);
    manager.run_passes(model);

    EXPECT_EQ(key_cache->get_element_type(), element::u8);
    EXPECT_EQ(value_cache->get_element_type(), element::u8);
    EXPECT_EQ(key_cache->get_partial_shape(),
              (PartialShape{-1, num_kv_heads, key_block_size, key_head_size + 2 * sizeof(float)}));
    EXPECT_EQ(value_cache->get_partial_shape(),
              (PartialShape{-1, num_kv_heads, value_block_size, value_head_size + 2 * sizeof(float)}));

    // The padded caches must still validate against the unpadded key/value widths.
    EXPECT_NO_THROW(model->validate_nodes_and_infer_types());
    EXPECT_EQ(psa->get_output_partial_shape(0), (PartialShape{-1, num_heads * value_head_size}));
}

}  // namespace