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] Enable BY_CHANNEL de-quantize/re-quantize support in KV_CACHE_R… (#32783)
Prithviraj-R1687ab91e617
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_rotate_ref.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_rotate_ref.clindex 0d7c1523454..5995fcc9955 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_rotate_ref.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_rotate_ref.cl@@ -67,74 +67,131 @@ KERNEL(pa_kv_cache_rotate)(barrier(CLK_LOCAL_MEM_FENCE);const uint token_coefficient_idx = per_token_rotation ? sglid : 0;- const uint block_base_offset = rotated_block_indices[block_idx] * KV_HEADS_NUM * ADJUSTED_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE +- head_idx * ADJUSTED_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;+ const uint block_base_offset = rotated_block_indices[block_idx] * KV_HEADS_NUM * ADJUSTED_HEAD_SIZE * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE ++ head_idx * ADJUSTED_HEAD_SIZE * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;const uint token_offset = block_base_offset + sglid;#if IS_KV_COMPRESSED- const uint comp_offset = block_base_offset + HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;- UNCOMPRESSED_TYPE* comp_ptr = key_cache + comp_offset;- UNCOMPRESSED_TYPE comp_scale = comp_ptr[0 + sglid];- UNCOMPRESSED_TYPE comp_zp = comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + sglid];-- UNCOMPRESSED_TYPE max_value = UNCOMPRESSED_VAL_MIN;- UNCOMPRESSED_TYPE min_value = UNCOMPRESSED_VAL_MAX;+ #if IS_KEY_BY_CHANNEL+ UNCOMPRESSED_TYPE min_value[HEAD_SIZE];+ UNCOMPRESSED_TYPE max_value[HEAD_SIZE];+ for (uint i = 0; i < HEAD_SIZE; i++) {+ min_value[i] = UNCOMPRESSED_VAL_MAX;+ max_value[i] = UNCOMPRESSED_VAL_MIN;+ }+ #else+ UNCOMPRESSED_TYPE min_value = UNCOMPRESSED_VAL_MAX;+ UNCOMPRESSED_TYPE max_value = UNCOMPRESSED_VAL_MIN;+ const uint comp_offset = block_base_offset + HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;+ UNCOMPRESSED_TYPE* comp_ptr = key_cache + comp_offset;+ UNCOMPRESSED_TYPE comp_scale = comp_ptr[0 + sglid];+ UNCOMPRESSED_TYPE comp_zp = comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + sglid];+ #endif// Reuse SLM to store dequantized rotated values__local UNCOMPRESSED_TYPE* rotated_data = (__local UNCOMPRESSED_TYPE*)(&rotation_coefficients[0][0]);#endif- // Apply cache rotation- for (uint i = 0; i < HEAD_SIZE / 2; i++) {+// Apply cache rotation+for (uint i = 0; i < HEAD_SIZE / 2; i++) {+ #if IS_KV_COMPRESSED && IS_KEY_BY_CHANNEL+ const uint cache_offset_first = token_offset + i * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;+ const uint cache_offset_second = token_offset + i * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + (HEAD_SIZE / 2) * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;++ const uint comp_offset_first = cache_offset_first - sglid + PAGED_ATTENTION_BLOCK_SIZE;+ const uint comp_offset_second = cache_offset_second - sglid + PAGED_ATTENTION_BLOCK_SIZE;++ UNCOMPRESSED_TYPE* comp_ptr_first = key_cache + comp_offset_first;+ UNCOMPRESSED_TYPE comp_scale_first = comp_ptr_first[0];+ UNCOMPRESSED_TYPE comp_zp_first = comp_ptr_first[1];++ UNCOMPRESSED_TYPE* comp_ptr_second = key_cache + comp_offset_second;+ UNCOMPRESSED_TYPE comp_scale_second = comp_ptr_second[0];+ UNCOMPRESSED_TYPE comp_zp_second = comp_ptr_second[1];+ #elseconst uint cache_offset = token_offset + i * PAGED_ATTENTION_BLOCK_SIZE;--#if IS_KV_COMPRESSED- UNCOMPRESSED_TYPE cache_value_first = TO_UNCOMPRESSED_TYPE(key_cache[cache_offset] - comp_zp) * comp_scale;- UNCOMPRESSED_TYPE cache_value_second = TO_UNCOMPRESSED_TYPE(key_cache[cache_offset + (HEAD_SIZE / 2) * PAGED_ATTENTION_BLOCK_SIZE] - comp_zp) * comp_scale;-#else- UNCOMPRESSED_TYPE cache_value_first = key_cache[cache_offset];- UNCOMPRESSED_TYPE cache_value_second = key_cache[cache_offset + (HEAD_SIZE / 2) * PAGED_ATTENTION_BLOCK_SIZE];-#endif-- INPUT2_TYPE rotation_value_cos = rotation_coefficients[i][token_coefficient_idx];- INPUT2_TYPE rotation_value_sin = rotation_coefficients[i + (HEAD_SIZE / 2)][token_coefficient_idx];-- UNCOMPRESSED_TYPE new_cache_value_first = cache_value_first * rotation_value_cos - cache_value_second * rotation_value_sin;- UNCOMPRESSED_TYPE new_cache_value_second = cache_value_first * rotation_value_sin + cache_value_second * rotation_value_cos;--#if IS_KV_COMPRESSED- max_value = fmax(fmax(max_value, new_cache_value_first), new_cache_value_second);- min_value = fmin(fmin(min_value, new_cache_value_first), new_cache_value_second);-+ #endif++ #if IS_KV_COMPRESSED+ #if IS_KEY_BY_CHANNEL+ UNCOMPRESSED_TYPE cache_value_first = TO_UNCOMPRESSED_TYPE(key_cache[cache_offset_first] - comp_zp_first) * comp_scale_first;+ UNCOMPRESSED_TYPE cache_value_second = TO_UNCOMPRESSED_TYPE(key_cache[cache_offset_second] - comp_zp_second) * comp_scale_second;+ #else+ UNCOMPRESSED_TYPE cache_value_first = TO_UNCOMPRESSED_TYPE(key_cache[cache_offset] - comp_zp) * comp_scale;+ UNCOMPRESSED_TYPE cache_value_second = TO_UNCOMPRESSED_TYPE(key_cache[cache_offset + (HEAD_SIZE / 2) * PAGED_ATTENTION_BLOCK_SIZE] - comp_zp) * comp_scale;+ #endif+ #else+ UNCOMPRESSED_TYPE cache_value_first = key_cache[cache_offset];+ UNCOMPRESSED_TYPE cache_value_second = key_cache[cache_offset + (HEAD_SIZE / 2) * PAGED_ATTENTION_BLOCK_SIZE];+ #endif++ INPUT2_TYPE rotation_value_cos = rotation_coefficients[i][token_coefficient_idx];+ INPUT2_TYPE rotation_value_sin = rotation_coefficients[i + (HEAD_SIZE / 2)][token_coefficient_idx];++ UNCOMPRESSED_TYPE new_cache_value_first = cache_value_first * rotation_value_cos - cache_value_second * rotation_value_sin;+ UNCOMPRESSED_TYPE new_cache_value_second = cache_value_first * rotation_value_sin + cache_value_second * rotation_value_cos;++ #if IS_KV_COMPRESSED+ #if !IS_KEY_BY_CHANNEL+ max_value = fmax(fmax(max_value, new_cache_value_first), new_cache_value_second);+ min_value = fmin(fmin(min_value, new_cache_value_first), new_cache_value_second);+ #endif+rotated_data[(i + 0) * PAGED_ATTENTION_BLOCK_SIZE + sglid] = new_cache_value_first;rotated_data[(i + (HEAD_SIZE / 2)) * PAGED_ATTENTION_BLOCK_SIZE + sglid] = new_cache_value_second;-#else+ #elsekey_cache[cache_offset] = new_cache_value_first;key_cache[cache_offset + (HEAD_SIZE / 2) * PAGED_ATTENTION_BLOCK_SIZE] = new_cache_value_second;-#endif- }+ #endif+}#if IS_KV_COMPRESSED- // Re-quantize cache data- ACCUMULATOR_TYPE grp_max = 0.001;- ACCUMULATOR_TYPE diff_value = max_value == min_value ? (grp_max) : (max_value - min_value);- ACCUMULATOR_TYPE scale_tmp = (ACCUMULATOR_TYPE)((CHAR_MAX - CHAR_MIN) / diff_value);- ACCUMULATOR_TYPE zp_tmp = (ACCUMULATOR_TYPE)(-min_value * scale_tmp) + CHAR_MIN;- UNCOMPRESSED_TYPE scale = (UNCOMPRESSED_TYPE)(scale_tmp);- UNCOMPRESSED_TYPE zp = (UNCOMPRESSED_TYPE)(zp_tmp);-+ UNCOMPRESSED_TYPE scale;+ UNCOMPRESSED_TYPE zp;+ ACCUMULATOR_TYPE diff_value;+ ACCUMULATOR_TYPE scale_tmp;+ ACCUMULATOR_TYPE zp_tmp;// Note: absence of this explicit unrolling directive leads to automatic// unrolling and causes registers spill. Set unrolling to a reasonable value manually__attribute__((opencl_unroll_hint(8)))for (uint i = 0; i < HEAD_SIZE; i++) {- OUTPUT_TYPE quantized_res = convert_char_rte(rotated_data[i * PAGED_ATTENTION_BLOCK_SIZE + sglid] * scale + zp);-- const uint cache_offset = token_offset + i * PAGED_ATTENTION_BLOCK_SIZE;+ // Re-quantize cache data+ ACCUMULATOR_TYPE grp_max = 0.001;+ UNCOMPRESSED_TYPE rotated_res = rotated_data[i * PAGED_ATTENTION_BLOCK_SIZE + sglid];+ #if IS_KEY_BY_CHANNEL+ max_value[i] = fmax(max_value[i], rotated_res);+ min_value[i] = fmin(min_value[i], rotated_res);++ min_value[i] = sub_group_reduce_min(min_value[i]);+ max_value[i] = sub_group_reduce_max(max_value[i]);++ diff_value = max_value[i] == min_value[i] ? (grp_max) : (max_value[i] - min_value[i]);+ scale_tmp = (ACCUMULATOR_TYPE)((CHAR_MAX - CHAR_MIN) / diff_value);+ zp_tmp = (ACCUMULATOR_TYPE)(-min_value[i] * scale_tmp) + CHAR_MIN;+ #else+ diff_value = max_value == min_value ? (grp_max) : (max_value - min_value);+ scale_tmp = (ACCUMULATOR_TYPE)((CHAR_MAX - CHAR_MIN) / diff_value);+ zp_tmp = (ACCUMULATOR_TYPE)(-min_value * scale_tmp) + CHAR_MIN;+ #endif+ scale = (UNCOMPRESSED_TYPE)(scale_tmp);+ zp = (UNCOMPRESSED_TYPE)(zp_tmp);++ OUTPUT_TYPE quantized_res = convert_char_rte(rotated_res * scale + zp);+ const uint cache_offset = token_offset + i * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;+ #if IS_KEY_BY_CHANNEL+ if(sglid == 0)+ {+ UNCOMPRESSED_TYPE* comp_ptr = key_cache + cache_offset + PAGED_ATTENTION_BLOCK_SIZE;+ comp_ptr[0] = 1.0 / scale;+ comp_ptr[1] = zp;+ }+ #endifkey_cache[cache_offset] = quantized_res;}-- comp_ptr[0 + sglid] = 1.0 / scale;- comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + sglid] = zp;+ #if !IS_KEY_BY_CHANNEL+ comp_ptr[0 + sglid] = 1.0 / scale;+ comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + sglid] = zp;+ #endif#endif}diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cppindex e9ce67de6dc..b07cc4ffdf3 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp@@ -904,25 +904,32 @@ protected:constexpr size_t key_cache_id = PagedAttentionInputIdx::KEY_CACHE;jit.add(make_layout_jit_constants("OUTPUT", params.input_layouts[key_cache_id], in_offsets_map.at(key_cache_id)));-jit.make("HEAD_SIZE", desc->k_head_size);jit.make("HEADS_NUM", desc->heads_num);jit.make("KV_HEADS_NUM", desc->kv_heads_num);jit.make("PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size);jit.make("SUBGROUP_SIZE", subgroup_size);-const bool is_kv_compressed = get_kv_compressed(params);jit.make("IS_KV_COMPRESSED", is_kv_compressed ? 1 : 0);const auto original_cache_dt = params.get_input_layout(PagedAttentionInputIdx::KEY).data_type;jit.add(make_type_jit_constants("UNCOMPRESSED", original_cache_dt));+ const auto is_key_by_channel = desc->is_key_by_channel;+ jit.make("IS_KEY_BY_CHANNEL", (is_kv_compressed && is_key_by_channel) ? 1 : 0);if (is_kv_compressed) {auto scales_zp_size = get_element_size(original_cache_dt) * 2; // scale + zp;jit.make("SCALE_ZP_SIZE_PER_TOKEN", scales_zp_size);- jit.make("ADJUSTED_HEAD_SIZE", desc->k_head_size + scales_zp_size);+ if (is_key_by_channel) {+ jit.make("ADJUSTED_HEAD_SIZE", desc->k_head_size);+ jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size + scales_zp_size);+ } else {+ jit.make("ADJUSTED_HEAD_SIZE", desc->k_head_size + scales_zp_size);+ jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size);+ }} else {jit.make("ADJUSTED_HEAD_SIZE", desc->k_head_size);+ jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size);}return jit;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 536b844417f..2c4219ed17e 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@@ -1634,6 +1634,25 @@ INSTANTIATE_TEST_SUITE_P(smoke_paged_attention, paged_attention_test, ::testing:paged_attention_test_params{ {{1008, 692}}, 32, 32, 128, 128, 16, {100.0}, 128, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // mixed: prefix cachingpaged_attention_test_params{ {{1008, 792}}, 32, 32, 128, 128, 16, {100.0}, 256, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // mixed: prefix cachingpaged_attention_test_params{ {{1, 34}, {2, 20}, {10, 34}}, 2, 2, 64, 64, 16, {100.0}, 10, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // mixed: 2nd token + 1st token + part of 1st token+ /* Per-Block rotation cases with Per-Channel quantization*/+ paged_attention_test_params{ {{16, 32}}, 2, 2, 32, 32, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate+ paged_attention_test_params{ {{8, 34}}, 2, 2, 64, 64, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate+ paged_attention_test_params{ {{256, 56}}, 2, 2, 64, 32, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate+ paged_attention_test_params{ {{48, 1024}}, 8, 8, 128, 128, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate+ paged_attention_test_params{ {{2, 34}, {2, 515}}, 2, 2, 64, 64, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate+ paged_attention_test_params{ {{4, 34}, {4, 515}}, 2, 2, 64, 32, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate+ paged_attention_test_params{ {{6, 34}, {25, 86}, {10, 64}}, 2, 2, 64, 64, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate+ paged_attention_test_params{ {{8, 34}, {25, 86}, {10, 64}}, 2, 2, 64, 32, 16, {100.0}, 0, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token, per block rotate++ /* Per-Token rotation cases with Per-Channel quantization*/+ paged_attention_test_params{ {{8, 38}}, 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 }, // 2nd token, per token rotate+ paged_attention_test_params{ {{34, 34}}, 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 }, // 2nd token, per token rotate+ paged_attention_test_params{ {{2, 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 token, per token rotate+ paged_attention_test_params{ {{6, 40}}, 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 }, // 2nd token, per token rotate+ paged_attention_test_params{ {{254, 60}}, 32, 8, 128, 128, 16, {100.0}, 10, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // 2nd token, per token rotate+ paged_attention_test_params{ {{84, 256}}, 32, 32, 128, 128, 16, {100.0}, 16, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // 2nd token, per token rotate+ paged_attention_test_params{ {{40, 96}, {30, 50}}, 2, 2, 64, 64, 16, {100.0}, 8, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // 2nd token, per token rotate+ paged_attention_test_params{ {{128, 130}, {256, 60}}, 2, 2, 48, 64, 16, {100.0}, 128, false, ENABLE_CACHE_COMPRESSION,ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION, ENABLE_FA_V2 }, // 2nd token, per token rotate}));INSTANTIATE_TEST_SUITE_P(smoke_cm_xattention, xattention_test, ::testing::ValuesIn(std::vector<paged_attention_test_params>{