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] Fix dynamic quantization large group case (#36147)
Chenhu Wang791fa9486263
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.clindex f7f184c4b81..2b5c7133053 100644--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.cl+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.cl@@ -137,6 +137,7 @@ KERNEL(dynamic_quantize_gpu_opt)(const uint local_id = get_local_id(1);__local half local_mem_max[BLOCK_NUM];__local half local_mem_min[BLOCK_NUM];+ FOR_PRECOMPUTED_REDUCTION(__local int local_mem_reduction[BLOCK_NUM]);MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_SIZE) val;MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_SIZE) abs_val;@@ -168,20 +169,29 @@ KERNEL(dynamic_quantize_gpu_opt)(min_value = sub_group_reduce_min(grp_min);#endif- const uint block_offset_idx = local_id * QUANTIZE_GROUP_SIZE / block_size;+ const uint blocks_per_group = QUANTIZE_GROUP_SIZE / block_size;+ const uint group_id = local_id / blocks_per_group;+ const uint block_in_group = local_id % blocks_per_group;+ const uint group_base_idx = group_id * blocks_per_group;+if (sglid == 0) {- local_mem_max[block_offset_idx + blockid] = max_value;+ local_mem_max[local_id] = max_value;#if ASYMMETRIC_QUANTIZATION- local_mem_min[block_offset_idx + blockid] = min_value;+ local_mem_min[local_id] = min_value;#endif}barrier(CLK_LOCAL_MEM_FENCE);- for (int j = 0; j < QUANTIZE_GROUP_SIZE / block_size; j++) {- max_value = fmax(max_value, local_mem_max[block_offset_idx + j]);+ // Aggregate max/min across all blocks in this quantization group+ max_value = local_mem_max[group_base_idx];#if ASYMMETRIC_QUANTIZATION- min_value = fmin(min_value, local_mem_min[block_offset_idx + j]);+ min_value = local_mem_min[group_base_idx];+#endif+ unroll_for (int j = 1; j < QUANTIZE_GROUP_SIZE / SIMD / VEC_SIZE; j++) {+ max_value = fmax(max_value, local_mem_max[group_base_idx + j]);+#if ASYMMETRIC_QUANTIZATION+ min_value = fmin(min_value, local_mem_min[group_base_idx + j]);#endif}@@ -202,12 +212,31 @@ KERNEL(dynamic_quantize_gpu_opt)(#if GENERATE_PRECOMPUTED_REDUCTION// TODO: Optimize this part+ // Calculate local reduction for this work-itemint precomputed_reduction = 0;MAKE_VECTOR_TYPE(OUTPUT2_TYPE, VEC_SIZE) val_int = CAT(CONVERT_INT_N, _rte)(val);unroll_for (int j = 0; j < VEC_SIZE; j++) {precomputed_reduction += val_int[j];}+ // Reduce within subgroupprecomputed_reduction = sub_group_reduce_add(precomputed_reduction);++ // Store to local memory for cross-block aggregation+ if (sglid == 0) {+ local_mem_reduction[local_id] = precomputed_reduction;+ }++ barrier(CLK_LOCAL_MEM_FENCE);++ // Aggregate reduction across all blocks in this quantization group+ // Only the first work-item in each group does this+ if (sglid == 0 && block_in_group == 0) {+ int total_reduction = 0;+ unroll_for (int j = 0; j < QUANTIZE_GROUP_SIZE / SIMD / VEC_SIZE; j++) {+ total_reduction += local_mem_reduction[group_base_idx + j];+ }+ precomputed_reduction = total_reduction;+ }#endifif (sglid == 0 && blockid == 0) {diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cppindex 2460ea8db75..e7b8a1af907 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cpp@@ -204,6 +204,20 @@ bool DynamicQuantizeKernelOpt::Validate(const Params& params) const {if (((bf.second) % (simd * 2)) != 0)DO_NOT_USE_THIS_KERNEL(params.layerID);+ // For MODE_LARGE_GS, ensure the quantization group fits within a single work_group+ // There is no cross work_group synchronization.+ if (get_dynamic_quantize_mode(dq_params) == DynQuanMode::LARGE_GS) {+ auto vec_size = get_match_vector_size(dq_params);+ size_t block_size = simd * vec_size;+ size_t blocks_per_group = dq_params.group_sizes.back() / block_size;++ // BLOCK_NUM is limited to 32 in GetJitConstants, so we can only handle+ // quantization groups that require <= 32 blocks+ if (blocks_per_group > 32) {+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ }+ }+if (dq_params.inputs[0].GetPaddedVal() != 0 || dq_params.outputs[0].GetPaddedVal() != 0)DO_NOT_USE_THIS_KERNEL(params.layerID);diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cppindex 0d01d99f2c5..a4fbe299f84 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cpp@@ -432,3 +432,21 @@ TEST_F(dynamic_quantization_gpu_tests, simple_quantizing_kv_cache_inner_most_dimthis->test_dynamic_quantization(false, {-1, 8, -1, 128}, {1, 8, 52, 128}, QuantizationType::Asymmetric, UINT64_MAX,data_types::i8, data_types::f16, OutputStorageType::InterleavedScalesZP, "dynamic_quantize_gpu_kv_cache", SetInnerMostDimValuesZero::Yes);}++TEST_F(dynamic_quantization_gpu_tests, dynamic_quantize_opt_group_size_256) {+ this->test_dynamic_quantization(false, {1, 1, 8192}, {1, 1, 8192}, QuantizationType::Symmetric, 256,+ data_types::i8, data_types::dynamic, OutputStorageType::Planar,+ "dynamic_quantize_gpu_opt");+}++TEST_F(dynamic_quantization_gpu_tests, dynamic_quantize_opt_group_size_256_precompute_sum) {+ this->test_dynamic_quantization(false, {1, 1, 8192}, {1, 1, 8192}, QuantizationType::Symmetric, 256,+ data_types::i8, data_types::i8, OutputStorageType::Planar,+ "dynamic_quantize_gpu_opt", SetInnerMostDimValuesZero::No, PrecomputeSum::Enabled);+}++TEST_F(dynamic_quantization_gpu_tests, dynamic_quantize_group_size_8192_with_precompute_sum) {+ this->test_dynamic_quantization(false, {1, 1, 16384}, {1, 1, 16384}, QuantizationType::Symmetric, 8192,+ data_types::i8, data_types::i8, OutputStorageType::Planar,+ "", SetInnerMostDimValuesZero::No, PrecomputeSum::Enabled);+}