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
[GPU] updated sdpa_micro to use raw keys and values for current input (#33379)
Eddy Kim5aa59a208a55
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cppindex 648793acdf8..f1a1268d03e 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp@@ -870,10 +870,10 @@ void SDPAMicroGenerator::init_sdpa_configuration(const kernel_impl_params& impl_}KernelData SDPAMicroGenerator::get_kernel_data(const kernel_impl_params& params) const {- std::vector<micro::Package> gemms(2); // KQ and VS+ std::vector<micro::Package> gemms(4); // KQ, VS, KcQ and VcSsdpa_configuration sdpa_config;init_sdpa_configuration(params, sdpa_config);- init_microkernels(params, sdpa_config, gemms[kq_id], gemms[vs_id], m_is_prefill, m_is_gqa_single_token);+ init_microkernels(params, sdpa_config, gemms[kq_id], gemms[vs_id], gemms[kcq_id], gemms[vcs_id], m_is_prefill, m_is_gqa_single_token);const auto& device_info = params.get_device_info();auto jit = get_jit_constants(params, gemms[kq_id], gemms[vs_id]);@@ -907,12 +907,30 @@ KernelData SDPAMicroGenerator::get_kernel_data(const kernel_impl_params& params)shim_options.decorator = "vs";kd.code->jit += generateShim(gemms[vs_id], micro::HostLanguage::OpenCL_C, shim_options);+ bool need_256_grf = false;if (gemms[kq_id].grfMin > 128 || gemms[vs_id].grfMin > 128) {+ need_256_grf = true;kd.code->options += " -cl-intel-256-GRF-per-thread";}- for (auto& p : gemms) {- kd.micro_kernels.push_back(std::make_shared<micro::MicroKernelPackage>(p));+ kd.micro_kernels.push_back(std::make_shared<micro::MicroKernelPackage>(gemms[kq_id]));+ kd.micro_kernels.push_back(std::make_shared<micro::MicroKernelPackage>(gemms[vs_id]));++ if (!m_is_prefill && !m_is_gqa_single_token) {+ shim_options.microkernelID++;+ shim_options.decorator = "kcq";+ kd.code->jit += generateShim(gemms[kcq_id], micro::HostLanguage::OpenCL_C, shim_options);++ shim_options.microkernelID++;+ shim_options.decorator = "vcs";+ kd.code->jit += generateShim(gemms[vcs_id], micro::HostLanguage::OpenCL_C, shim_options);++ if (!need_256_grf && (gemms[kcq_id].grfMin > 128 || gemms[vcs_id].grfMin > 128)) {+ kd.code->options += " -cl-intel-256-GRF-per-thread";+ }++ kd.micro_kernels.push_back(std::make_shared<micro::MicroKernelPackage>(gemms[kcq_id]));+ kd.micro_kernels.push_back(std::make_shared<micro::MicroKernelPackage>(gemms[vcs_id]));}return kd;@@ -1253,6 +1271,8 @@ Arguments SDPAMicroGenerator::get_arguments_desc(const kernel_impl_params& paramargs.push_back({ArgumentDescriptor::Types::INPUT, 3}); // Key cacheargs.push_back({ArgumentDescriptor::Types::INPUT, 0}); // Qargs.push_back({ArgumentDescriptor::Types::INPUT, 4}); // Value cache+ args.push_back({ArgumentDescriptor::Types::INPUT, 1}); // Key+ args.push_back({ArgumentDescriptor::Types::INPUT, 2}); // Value}args.push_back({ArgumentDescriptor::Types::OUTPUT, 0}); // A@@ -1383,6 +1403,8 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,const sdpa_configuration& configuration,micro::Package& gemm_kq,micro::Package& gemm_vs,+ micro::Package& gemm_kcq,+ micro::Package& gemm_vcs,bool is_prefill,bool is_gqa_single_token) {// TODO: Remove once micro API is thread safe@@ -1579,6 +1601,33 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,throw;}+ if (!is_prefill && !is_gqa_single_token) {+ /* Update for optional GEMM: Kc*Q */+ opts_kq.scaleA = false;+ opts_kq.offsetA = false;++ auto problem_kcq = problem;+ problem_kcq.Ta_ext = convert_type(Q.data_type);+ problem_kcq.A.layout = micro::MatrixLayout::T;+ problem_kcq.B.layout = micro::MatrixLayout::Pr;+ problem_kcq.C.layout = micro::MatrixLayout::T;+ problem_kcq.A.setAlignment(micro::alignment_for_ld(k_head_size * problem.Ta));+ problem_kcq.B.setAlignment(64); // Q is packed in VNNI format in SLM+ problem_kcq.B.crosspack = 2;+ problem_kcq.B.tileR = static_cast<uint16_t>(d_max);+ problem_kcq.B.tileC = static_cast<uint16_t>(get_subgroup_size(device_info.arch));++ GPU_DEBUG_TRACE_DETAIL << "kcq: sizes = {" << sizes.m << ", " << sizes.n << ", " << sizes.k << ", " << sizes.batch << "}\n";++ /* Ask microkernel provider for microkernel */+ try {+ gemm_kcq = micro::select_gemm_microkernel(opts_kq, hw_info, sizes, problem_kcq, reqs_kq);+ } catch (const std::runtime_error& ex) {+ GPU_DEBUG_TRACE_DETAIL << "Can't create KcQ sdpa_micro kernel: " << ex.what() << "\n";+ throw;+ }+ }+/* Set up microkernel options */micro::GEMMProtocol::Options opts_vs;opts_vs.localB = true;@@ -1668,6 +1717,35 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,GPU_DEBUG_TRACE_DETAIL << "Can't create VS sdpa_micro kernel: " << ex.what() << "\n";throw;}++ if (!is_prefill && !is_gqa_single_token) {+ /* Update for optional GEMM: Vc*S */+ opts_vs.scaleA = false;+ opts_vs.offsetA = false;++ auto problem_vcs = problem;+ problem_vcs.Ta_ext = convert_type(Q.data_type);+ problem_vcs.A.layout = micro::MatrixLayout::N;++ problem_vcs.B.layout = micro::MatrixLayout::Pr;+ problem_vcs.C.layout = micro::MatrixLayout::N;+ problem_vcs.A.setAlignment(micro::alignment_for_ld(v_head_size * problem.Ta));+ problem_vcs.B.setAlignment(64); // S is packed in SLM+ problem_vcs.B.crosspack = 16;+ sizes.m = n_values.is_dynamic() ? -1 : n_values.get_length();+ sizes.n = gemm_kq.getSetting("wg_tile_n");+ sizes.k = gemm_kq.getSetting("wg_tile_m");++ GPU_DEBUG_TRACE_DETAIL << "vcs: sizes = {" << sizes.m << ", " << sizes.n << ", " << sizes.k << ", " << sizes.batch << "}\n";++ /* Ask microkernel provider for microkernel */+ try {+ gemm_vcs = micro::select_gemm_microkernel(opts_vs, hw_info, sizes, problem_vcs, reqs_vs, adjust_vs);+ } catch (const std::runtime_error& ex) {+ GPU_DEBUG_TRACE_DETAIL << "Can't create VcS sdpa_micro kernel: " << ex.what() << "\n";+ throw;+ }+ }}} // namespace ov::intel_gpu::ocl#endifdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.hppindex dfea7bbff13..de5cb622405 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.hpp@@ -53,6 +53,8 @@ private:const sdpa_configuration& sdpa_config,micro::Package& gemm_kq,micro::Package& gemm_vs,+ micro::Package& gemm_kcq,+ micro::Package& gemm_vcs,bool is_prefill,bool is_gqa_single_token);static void init_sdpa_configuration(const kernel_impl_params& params, sdpa_configuration& config);@@ -63,6 +65,8 @@ private:static constexpr size_t kq_id = 0;static constexpr size_t vs_id = 1;+ static constexpr size_t kcq_id = 2; // kc - key from current input+ static constexpr size_t vcs_id = 3; // vc - value from current inputstatic constexpr size_t prefill_id = 0;static constexpr size_t generate_id = 1;diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.clindex ba0d67e80d9..e75e53741a6 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.cl@@ -142,10 +142,14 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARGconst global KEY_DATA_T *K,const global QRY_DATA_T *Q,const global VAL_DATA_T *V,+#if IS_PAGED_ATTENTION && !IS_PREFILL+ const global QRY_DATA_T *Kc,+ const global QRY_DATA_T *Vc,+#endifglobal half *A,#if IS_PAGED_ATTENTIONconst __global INPUT3_TYPE* subsequence_begins,- #if IS_PREFILL == 0+ #if !IS_PREFILLconst __global INPUT3_TYPE* past_lens,const __global INPUT3_TYPE* block_indices,const __global INPUT3_TYPE* block_indices_begins,@@ -219,6 +223,8 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG#elseuint ldk = ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;uint ldv = HEAD_SIZE;+ uint ldkc = HEAD_SIZE * KV_HEADS_NUM + INPUT1_PAD_BEFORE_FEATURE_NUM + INPUT1_PAD_AFTER_FEATURE_NUM;+ uint ldvc = HEAD_SIZE * KV_HEADS_NUM + INPUT2_PAD_BEFORE_FEATURE_NUM + INPUT2_PAD_AFTER_FEATURE_NUM;#if IS_KV_COMPRESSED_PA#if IS_KEY_BY_CHANNELuint ldkq = ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE / 2;@@ -295,6 +301,10 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARGK += b0_kv * ADJUSTED_K_HEAD_SIZE * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;V += b0_kv * ADJUSTED_V_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;+ Kc += subsequence_begin * ldkc+ + b0_kv * HEAD_SIZE + INPUT1_PAD_BEFORE_FEATURE_NUM;+ Vc += subsequence_begin * ldvc+ + b0_kv * HEAD_SIZE + INPUT2_PAD_BEFORE_FEATURE_NUM;#endif#elseK += (KEY_OFF(b1, b0_kv, 0, 0) + INPUT1_OFFSET) / KEY_ELEMENTS_PER_BYTE;@@ -495,26 +505,49 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG#endif/* Calculate S = (K^T) * Q */-#if IS_PAGED_ATTENTION && IS_PREFILL == 0- int k_block_num = k0 / PAGED_ATTENTION_BLOCK_SIZE + sg_i_kq;- global KEY_DATA_T *K0 = K + KV_HEADS_NUM * ADJUSTED_K_HEAD_SIZE * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE * block_indices[base_block_index + k_block_num]- - PAGED_ATTENTION_BLOCK_SIZE * sg_i_kq;- #if IS_KV_COMPRESSED_PA- #if IS_KEY_BY_CHANNEL- global KEY_DATA_T *K0_scales = K0 + PAGED_ATTENTION_BLOCK_SIZE * (sg_i_kq + 1);- global KEY_DATA_T *K0_zp = K0_scales + 2;- #else- global KEY_DATA_T *K0_scales = K0 + PAGED_ATTENTION_BLOCK_SIZE * (HEAD_SIZE - sg_i_kq);- global KEY_DATA_T *K0_zp = K0_scales + 2 * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;+#if IS_PAGED_ATTENTION && !IS_PREFILL+ #if !IS_GQA_SINGLE_TOKEN+ s_tile_type S_tile;+ tile_fill(S_tile, 0.0f);++ for (;k0 < past_lens[gws_mapping];) {+ #endif+ int k_block_num = k0 / PAGED_ATTENTION_BLOCK_SIZE + sg_i_kq;+ global KEY_DATA_T *K0 = K + KV_HEADS_NUM * ADJUSTED_K_HEAD_SIZE * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE * block_indices[base_block_index + k_block_num]+ - PAGED_ATTENTION_BLOCK_SIZE * sg_i_kq;+ #if IS_KV_COMPRESSED_PA+ #if IS_KEY_BY_CHANNEL+ global KEY_DATA_T *K0_scales = K0 + PAGED_ATTENTION_BLOCK_SIZE * (sg_i_kq + 1);+ global KEY_DATA_T *K0_zp = K0_scales + 2;+ #else+ global KEY_DATA_T *K0_scales = K0 + PAGED_ATTENTION_BLOCK_SIZE * (HEAD_SIZE - sg_i_kq);+ global KEY_DATA_T *K0_zp = K0_scales + 2 * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;+ #endif#endif- #endif- s_tile_type S_tile- = ugemm_kq(K0, ldk, Q_slm, D_MAX, k_chunk, ugemm_kq_wg_tile_n, d, 0,- 0, 0, sg_i_kq, sg_j_kq, (local char *)ugemm_slm- #if IS_KV_COMPRESSED_PA- , (global half *)K0_scales, (global half *)K0_zp, ldkq+ #if IS_GQA_SINGLE_TOKEN+ s_tile_type S_tile =+ #else+ s_tile_type S_tile1 =#endif+ ugemm_kq(K0, ldk, Q_slm, D_MAX, k_chunk, ugemm_kq_wg_tile_n, d, 0,+ 0, 0, sg_i_kq, sg_j_kq, (local char *)ugemm_slm+ #if IS_KV_COMPRESSED_PA+ , (global half *)K0_scales, (global half *)K0_zp, ldkq+ #endif+ );+ #if !IS_GQA_SINGLE_TOKEN+ tile_binary(S_tile, S_tile1, binary_add);+ break;+ }++ for (; k0 >= past_lens[gws_mapping];) {+ s_tile_type S_tile1 = ugemm_kcq(Kc, ldkc, Q_slm, D_MAX, (k - past_lens[gws_mapping]), ugemm_kq_wg_tile_n, d, (k0 - past_lens[gws_mapping]),+ 0, 0, sg_i_kq, sg_j_kq, (local char *)ugemm_slm);+ tile_binary(S_tile, S_tile1, binary_add);+ break;+ }+ #endif#elses_tile_type S_tile= ugemm_kq(K, ldk, Q_slm, D_MAX, k, ugemm_kq_wg_tile_n, d, k0,@@ -531,8 +564,8 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG,ldkq#endif-#endif);+#endif#if KEY_SCALES == QUANTIZE_COMMON#define k_scale_op(x) ((x)*k_scale)@@ -572,7 +605,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG#endifint col_offset = wg_j0 + sg_j0_kq;- #if IS_PAGED_ATTENTION && IS_PREFILL == 0+ #if IS_PAGED_ATTENTION && !IS_PREFILL#if IS_GQA_SINGLE_TOKENcol_offset += k - 1 - get_sub_group_local_id();#else@@ -818,8 +851,14 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARGintel_work_group_barrier_arrive(CLK_LOCAL_MEM_FENCE);/* Accumulate A += V * S */-#if IS_PAGED_ATTENTION && IS_PREFILL == 0- for (int kb0 = 0; kb0 < k_chunk; kb0 += PAGED_ATTENTION_BLOCK_SIZE) {+#if IS_PAGED_ATTENTION && !IS_PREFILL+ int kb0 = 0;+ for (; kb0 < k_chunk; kb0 += PAGED_ATTENTION_BLOCK_SIZE) {+ #if !IS_GQA_SINGLE_TOKEN+ if ((k0 + kb0) >= past_lens[gws_mapping]) {+ break;+ }+ #endifuint s_block_num = kb0 / PAGED_ATTENTION_BLOCK_SIZE;local half *Sb0 = S_slm + s_block_num * ugemm_kq_sg_tile_m * ugemm_kq_sg_tile_n;uint v_block_num = (k0 + kb0) / PAGED_ATTENTION_BLOCK_SIZE;@@ -842,6 +881,22 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARGtile_binary(A_tile, A_tile1, binary_add);}+ #if !IS_GQA_SINGLE_TOKEN+ for (; kb0 < k_chunk; kb0 += k_chunk) {+ global QRY_DATA_T *Vb0 = Vc + ldvc * (k0 + kb0 - past_lens[gws_mapping]);+ uint s_block_num = kb0 / PAGED_ATTENTION_BLOCK_SIZE;+ local half *Sb0 = S_slm + s_block_num * ugemm_kq_sg_tile_m * ugemm_kq_sg_tile_n;+ int kb_chunk = k_chunk - kb0;++ a_tile_type A_tile1 = ugemm_vcs(+ Vb0, ldvc, Sb0, ugemm_kq_wg_tile_m,+ d, ugemm_kq_wg_tile_n, kb_chunk,+ 0, 0, 0,+ sg_i_vs, sg_j_vs, (local char *)ugemm_slm);++ tile_binary(A_tile, A_tile1, binary_add);+ }+ #endif#elsea_tile_type A_tile1 = ugemm_vs(V, ldv, S_slm, ugemm_kq_wg_tile_m, d, ugemm_kq_wg_tile_n,@@ -869,7 +924,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG#if VAL_ZERO_POINTS == QUANTIZE_2DV_zp += ldvq * ugemm_kq_wg_tile_m / VAL_ZP_ELEMENTS_PER_BYTE;#endif-#if IS_PAGED_ATTENTION && IS_PREFILL == 0+#if IS_PAGED_ATTENTION && !IS_PREFILL// already done#elsetile_binary(A_tile, A_tile1, binary_add);diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cppindex 94ff27f6eb1..584a7de0978 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp@@ -1755,7 +1755,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_paged_attention, paged_attention_test, ::testing:paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 2, 48, 64, 16, {100.0}, 128, false, DISABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, ENABLE_FA_V2 }, // 1st token + 1st tokenpaged_attention_test_params{ {{1, 10}}, 2, 2, 64, 64, 16, {100.0}, 4, false, DISABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 2nd tokenpaged_attention_test_params{ {{5, 10}}, 2, 2, 64, 64, 16, {100.0}, 2, false, DISABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 2nd token- paged_attention_test_params{ {{8, 8}}, 16, 16, 256, 256, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // mixed: prefix caching+ paged_attention_test_params{ {{248, 16}}, 2, 2, 128, 128, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // mixed: prefix cachingpaged_attention_test_params{ {{34, 0}}, 2, 2, 32, 32, 16, {100.0}, 2, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // 1st tokenpaged_attention_test_params{ {{1, 1008}}, 32, 32, 128, 128, 16, {100.0}, 6, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // 2nd tokenpaged_attention_test_params{ {{6, 20}}, 2, 2, 128, 128, 16, {100.0}, 8, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // mixed: prefix caching