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] Set key_by_channel mode as default key quantization mode (#31602)
Taylor Yeonbok Leefedb9c57bc6c
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inlindex 366dba722aa..661797e8b5a 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl@@ -35,7 +35,7 @@ OV_CONFIG_RELEASE_OPTION(ov::hint, activations_scale_factor, -1.0f, "Scalar floaOV_CONFIG_RELEASE_OPTION(ov::internal, enable_lp_transformations, false, "Enable/Disable Low precision transformations set")OV_CONFIG_RELEASE_OPTION(ov::intel_gpu, config_file, "", "Path to custom layers config file")OV_CONFIG_RELEASE_OPTION(ov::hint, model, nullptr, "Shared pointer to the ov::Model")-OV_CONFIG_RELEASE_OPTION(ov::internal, key_cache_quant_mode, ov::internal::CacheQuantMode::BY_TOKEN, "AUTO or BY_CHANNEL or BY_TOKEN")+OV_CONFIG_RELEASE_OPTION(ov::internal, key_cache_quant_mode, ov::internal::CacheQuantMode::BY_CHANNEL, "AUTO or BY_CHANNEL or BY_TOKEN")OV_CONFIG_RELEASE_OPTION(ov::internal, value_cache_quant_mode, ov::internal::CacheQuantMode::BY_TOKEN, "AUTO or BY_CHANNEL or BY_TOKEN")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, shape_predictor_settings, {10, 16 * 1024, 2, 1.1f}, "Preallocation settings")diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_update_ref.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_update_ref.clindex f56e24294ae..c7aeac168fa 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_update_ref.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_update_ref.cl@@ -62,9 +62,12 @@ inline void FUNC(quantize_and_save_by_channel_block_with_requantize)(__global coconst uint out_data_pitch,const uint token_pos_in_block,const uint new_tokens_num,- const uint sglid) {- for (int h_sub = 0; h_sub < NUM_HEAD_SIZE_GROUPS; h_sub++) {- const int hidden_idx = h_sub * SUBGROUP_SIZE + sglid;+ const uint sglid,+ const uint is_prefill_stage) {+ int head_size_offset = SUBGROUP_SIZE * get_group_id(2);+ int num_head_size_groups = is_prefill_stage ? NUM_HEAD_SIZE_GROUPS : NUM_HEAD_SIZE_GROUPS / NUM_K_HEAD_SIZE_PARTITIONS;+ for (int h_sub = 0; h_sub < num_head_size_groups; h_sub++) {+ const int hidden_idx = head_size_offset + h_sub * SUBGROUP_SIZE + sglid;const uint out_offset_per_wi = out_data_offset + hidden_idx * out_data_pitch;// Read original scale and zpINPUT0_TYPE* comp_ptr = (INPUT0_TYPE*) (&out_data[out_offset_per_wi + COMP_K_OFFSET]);@@ -79,14 +82,14 @@ inline void FUNC(quantize_and_save_by_channel_block_with_requantize)(__global co#define VLOAD CAT(vload, READ_SIZE)IN_DATA_VEC cache_data_vec_decompressed;for (int j = 0; j < new_tokens_num; ++j) {- INPUT0_TYPE new_token = BLOCK_READN(INPUT0_TYPE, 1, in_data, in_data_offset + j * K_HEAD_SIZE * KV_HEADS_NUM + h_sub * SUBGROUP_SIZE);- // Read a hidden dim of the previously quantized cache => decompress- // TODO : current block size is 16 (same as PA block size),- // but when the block size becomes different, this part should be updated as well+ INPUT0_TYPE new_token = BLOCK_READN(INPUT0_TYPE, 1, in_data, in_data_offset + j * K_HEAD_SIZE * KV_HEADS_NUM + hidden_idx);cache_data_vec_decompressed[token_pos_in_block + j] = new_token;INPUT0_TYPE max_value = fmax(max_value, new_token);INPUT0_TYPE min_value = fmin(min_value, new_token);}+ // Read a hidden dim of the previously quantized cache => decompress+ // TODO : current block size is 16 (same as PA block size),+ // but when the block size becomes different, this part should be updated as wellOUT_DATA_VEC prev_cache_data_vec = VLOAD(0, out_data + out_offset_per_wi);#undef READ_SIZE#undef VLOAD@@ -202,7 +205,7 @@ KERNEL(pa_kv_cache_update)(// 2nd+ tokenconst uint seq_idx = (uint)get_global_id(0);const uint head_idx = (uint)get_global_id(1);- const uint sglid = (uint)get_global_id(2);+ const uint sglid = (uint)get_local_id(2);const uint past_seq_len = past_lens[seq_idx];const uint current_token_pos_in_block = past_seq_len % PAGED_ATTENTION_BLOCK_SIZE;@@ -255,6 +258,7 @@ KERNEL(pa_kv_cache_update)(}#else // IS_KV_COMPRESSED#ifdef IS_KEY_BY_CHANNEL+ // key by channel{FUNC_CALL(quantize_and_save_by_channel_block_with_requantize)(key_data,key_in_offset,@@ -264,9 +268,18 @@ KERNEL(pa_kv_cache_update)(k_hidden_stride,current_token_pos_in_block,1,- sglid);+ sglid,+ 0);+ }+ // value per token+ if (get_group_id(2) == 0) {+ const uint comp_v_offset = block_v_base_offset + V_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;+ INPUT0_TYPE input_data[V_HEAD_SIZE / SUBGROUP_SIZE];+ FUNC_CALL(quantize_and_save_per_token)(value_data, value_in_offset, value_cache_data, value_out_offset, 1, comp_v_offset,+ current_token_pos_in_block, sglid, V_HEAD_SIZE / SUBGROUP_SIZE, &input_data[0]);}- #else // Key per token+ #else+ // IS_KEY_BY_CHANNEL false{const uint comp_k_offset = block_k_base_offset + K_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;// key processing@@ -274,14 +287,13 @@ KERNEL(pa_kv_cache_update)(FUNC_CALL(quantize_and_save_per_token)(key_data, key_in_offset, key_cache_data, key_out_offset, PAGED_ATTENTION_BLOCK_SIZE, comp_k_offset,current_token_pos_in_block, sglid, K_HEAD_SIZE / SUBGROUP_SIZE, &input_data[0]);}- #endif- // value per token{const uint comp_v_offset = block_v_base_offset + V_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;INPUT0_TYPE input_data[V_HEAD_SIZE / SUBGROUP_SIZE];FUNC_CALL(quantize_and_save_per_token)(value_data, value_in_offset, value_cache_data, value_out_offset, 1, comp_v_offset,current_token_pos_in_block, sglid, V_HEAD_SIZE / SUBGROUP_SIZE, &input_data[0]);}+ #endif#endif // IS_KV_COMPRESSED} else {@@ -289,7 +301,6 @@ KERNEL(pa_kv_cache_update)(const uint block_idx = get_global_id(0);const uint head_idx = get_global_id(1);const uint sglid = get_global_id(2);-const uint subsequence_idx = gws_seq_indexes_correspondence[block_idx];const uint subsequence_begin_idx = subsequence_begins[subsequence_idx];@@ -340,7 +351,8 @@ KERNEL(pa_kv_cache_update)(k_hidden_stride,token_start_pos_key,tokens_num,- sglid);+ sglid,+ 1);} else {FUNC_CALL(quantize_and_save_by_channel_prefill)(key_data,key_in_offset,@@ -507,7 +519,8 @@ KERNEL(pa_kv_cache_update)(k_hidden_stride,token_start_pos_key,tokens_num,- sglid);+ sglid,+ 1);} else {FUNC_CALL(quantize_and_save_by_channel_prefill)(key_data,key_in_offset,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 4012de2e2b3..ffe4de92cc7 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@@ -86,6 +86,17 @@ static size_t get_heads_per_wi(const size_t kv_group_size) {return 1;}+static size_t get_num_k_head_size_partitions(bool is_key_by_channel, int64_t k_head_size) {+ size_t head_size_partition = 1;+ if (is_key_by_channel) {+ if (k_head_size % subgroup_size == 0) {+ head_size_partition = std::max(static_cast<size_t>(1), static_cast<size_t>(k_head_size / subgroup_size));+ head_size_partition = std::min(static_cast<size_t>(16), head_size_partition);+ }+ }+ return head_size_partition;+}+inline size_t get_generate_stage_block_size(size_t head_size) {auto preferred_block_size = {4, 2, 1};for (const auto& block_size : preferred_block_size) {@@ -257,7 +268,6 @@ public:jit.make("PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size);jit.make("SUBGROUP_SIZE", subgroup_size);jit.make("SLIDING_WINDOW_SIZE", desc->sliding_window);- jit.make("HEADS_PER_WI", 1);bool is_kv_compressed = get_kv_compressed(params);jit.make("IS_KV_COMPRESSED", is_kv_compressed);@@ -441,6 +451,7 @@ public:auto heads_per_wi = get_heads_per_wi(kv_group_size);// GQA+ jit.remove("HEADS_PER_WI");jit.make("HEADS_PER_WI", heads_per_wi);jit.make("ITERATIONS_PER_KV_HEADS_GROUP", ceil_div(kv_group_size, heads_per_wi));jit.make("HEADS_LEFTOVERS_NUM", kv_group_size % heads_per_wi);@@ -475,6 +486,7 @@ public:[[nodiscard]] JitConstants get_jit_constants(const kernel_impl_params& params) const override {auto jit = PagedAttentionGeneratorBase::get_jit_constants(params);jit.make("SDPA_STAGE_1", 1);+ jit.make("HEADS_PER_WI", 1);const auto& in_offsets_map = params.in_port_to_shape_info_offset;const auto& out_offsets_map = params.out_port_to_shape_info_offset;@@ -531,6 +543,7 @@ public:auto jit = PagedAttentionGeneratorBase::get_jit_constants(params);jit.make("SDPA_STAGE_0", 1);jit.make("MULTI_TOKENS_PROCESSING", 1);+ jit.make("HEADS_PER_WI", 1);const auto desc = params.typed_desc<paged_attention>();const auto has_alibi = params.get_input_layout(PagedAttentionInputIdx::ALIBI).count() > 0;@@ -621,6 +634,7 @@ public:auto jit = PagedAttentionGeneratorBase::get_jit_constants(params);jit.make("SDPA_STAGE_1", 1);jit.make("MULTI_TOKENS_PROCESSING", 1);+ jit.make("HEADS_PER_WI", 1);const auto& in_offsets_map = params.in_port_to_shape_info_offset;const auto& out_offsets_map = params.out_port_to_shape_info_offset;@@ -677,6 +691,7 @@ public:[[nodiscard]] JitConstants get_jit_constants(const kernel_impl_params& params) const override {auto jit = PagedAttentionGeneratorBase::get_jit_constants(params);jit.make("SDPA_STAGE_2", 1);+ jit.make("HEADS_PER_WI", 1);const auto& in_offsets_map = params.in_port_to_shape_info_offset;const auto& out_offsets_map = params.out_port_to_shape_info_offset;@@ -784,6 +799,7 @@ protected:jit.make("IS_KEY_BY_CHANNEL", 1);jit.make("ADJUSTED_K_HEAD_SIZE", desc->k_head_size);jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size + scales_zp_size);+ jit.make("NUM_K_HEAD_SIZE_PARTITIONS", get_num_k_head_size_partitions(desc->is_key_by_channel, desc->k_head_size));} else {jit.make("ADJUSTED_K_HEAD_SIZE", desc->k_head_size + scales_zp_size);jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size);@@ -846,8 +862,8 @@ protected:} else {const auto& key_input = params.input_layouts[0];const auto sequences_number = key_input.get_partial_shape()[0].get_length();-- wgs.global = {static_cast<size_t>(sequences_number), heads_number, subgroup_size};+ size_t head_size_partition = get_num_k_head_size_partitions(desc->is_key_by_channel, desc->k_head_size);+ wgs.global = {static_cast<size_t>(sequences_number), heads_number, subgroup_size * head_size_partition};wgs.local = {1, 1, subgroup_size};}diff --git a/src/plugins/intel_gpu/src/runtime/execution_config.cpp b/src/plugins/intel_gpu/src/runtime/execution_config.cppindex d5f86c4e1af..03fc7da88a8 100644--- a/src/plugins/intel_gpu/src/runtime/execution_config.cpp+++ b/src/plugins/intel_gpu/src/runtime/execution_config.cpp@@ -237,7 +237,7 @@ void ExecutionConfig::apply_model_specific_options(const IRemoteContext* context}if (!is_set_by_user(ov::internal::key_cache_quant_mode) || get_key_cache_quant_mode() == ov::internal::CacheQuantMode::AUTO) {- m_key_cache_quant_mode = ov::internal::CacheQuantMode::BY_TOKEN;+ m_key_cache_quant_mode = ov::internal::CacheQuantMode::BY_CHANNEL;}if (!is_set_by_user(ov::internal::value_cache_quant_mode) || get_value_cache_quant_mode() == ov::internal::CacheQuantMode::AUTO) {diff --git a/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp b/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cppindex 7dcd90baa24..2d75afddd1b 100644--- a/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp@@ -191,6 +191,7 @@ TEST(update_shape_test, max_context_len_shapeof_subgraph) {pa_prim.has_alibi = false;pa_prim.num_outputs = 1;pa_prim.has_rotated_blocks = false;+ pa_prim.is_key_by_channel = true;topology topology;topology.add(input_layout("input_data", input_data_layout));diff --git a/src/plugins/intel_gpu/tests/unit/passes/mark_shape_of_subgraphs_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/mark_shape_of_subgraphs_test.cppindex e04cb8545cd..8494faa1af7 100644--- a/src/plugins/intel_gpu/tests/unit/passes/mark_shape_of_subgraphs_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/passes/mark_shape_of_subgraphs_test.cpp@@ -496,6 +496,7 @@ TEST(mark_shape_of_subgraphs, paged_attention_max_context_len_input) {pa_prim.has_alibi = false;pa_prim.num_outputs = 1;pa_prim.has_rotated_blocks = false;+ pa_prim.is_key_by_channel = true;topology topology;topology.add(input_layout("query", query_layout));@@ -531,6 +532,7 @@ TEST(mark_shape_of_subgraphs, paged_attention_max_context_len_input) {ExecutionConfig config = get_test_default_config(engine);config.set_property(ov::intel_gpu::allow_new_shape_infer(true));config.set_property(ov::intel_gpu::optimize_data(true));+ config.set_property(ov::internal::key_cache_quant_mode(ov::internal::CacheQuantMode::BY_CHANNEL));network network(engine, topology, config);auto prog = network.get_program();