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] Add fully_connected_bf_tiled_dyn_b kernel for small-batch INT4 FC (#34525)
Jade Cho4589d3357319
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/fully_connected_gpu_bf_tiled_dyn_b.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/fully_connected_gpu_bf_tiled_dyn_b.clnew file mode 100644index 00000000000..906e05f5485--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/fully_connected_gpu_bf_tiled_dyn_b.cl@@ -0,0 +1,288 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//+// Multi-TILE_B dynamic kernel for fully_connected with INT4 compressed weights.+//+// Design:+// - Dynamic shapes (shape-agnostic): batch unknown at compile time+// - Precompiles 8 tile variants (FORCED_TILE_B=1..8)+// - Runtime two-phase dispatch:+// 1) Main body: selects largest TILE_B in [8..4] that evenly divides batch.+// If no exact divisor exists (primes, etc.), uses TILE_B=8.+// 2) Tail: remaining (batch % main_tile) elements dispatched with a smaller+// tile variant. Zero-overhead when batch is exactly divisible.+// - Each tile variant has NO batch-tail bounds checking in hot loops.+// - INT4 compressed weights with per-group scale/zp decompression+// - SIMD=16, supports os_is_yx_osv32_isv2, os_iyx_osv16, os_is_yx_osv64_isv2++#include "include/batch_headers/common.cl"+#include "include/batch_headers/sub_group_block_read.cl"+#include "include/batch_headers/sub_group_block_write.cl"+#include "include/batch_headers/sub_group_shuffle.cl"++// =====================================================================+// Compile-time macros expected from C++ wrapper:+// SIMD - sub-group size (16)+// TILE_OFM - output feature tile {1, 2, 4}+// TILE_IFM - input feature tile {1, 2}+// TILE_K - weight elements per load {1, 2, 4}+// TILE_K_OFM - TILE_K * TILE_OFM+// TILE_K_OFM_PACKED - TILE_K_OFM / 2 (for INT4)+// IFM_SIZE, TILE_OUT_F_NUM, TILE_OUT_F_PITCH+// TILE_IN_B_PITCH, TILE_OUT_B_PITCH+// COMPRESSED_WEIGHTS_INT4, COMPRESSED_WEIGHTS+// DECOMPRESSION_SCALE_TERM, DECOMPRESSION_ZP_TERM, etc.+// W_IDX, TILE_OFM_PER_OSV_SIZE+// DECOMPRESSION_SCALE_POST_OP (optional)+// BATCH_SIZE - runtime value from shape_info+// =====================================================================++// Verify essential JIT parameters+#if SIMD != 16+# error "fully_connected_gpu_bf_tiled_dyn_b.cl - SIMD must be 16"+#endif++#if TILE_OFM != 1 && TILE_OFM != 2 && TILE_OFM != 4+# error "fully_connected_gpu_bf_tiled_dyn_b.cl - TILE_OFM must be one of {1, 2, 4}"+#endif++#if TILE_K != 1 && TILE_K != 2 && TILE_K != 4+# error "fully_connected_gpu_bf_tiled_dyn_b.cl - TILE_K must be one of {1, 2, 4}"+#endif++#if !COMPRESSED_WEIGHTS_INT4+# error "fully_connected_gpu_bf_tiled_dyn_b.cl - only INT4 compressed weights supported"+#endif++#if TILE_K_OFM != (TILE_K * TILE_OFM) || TILE_K_OFM > 8+# error "fully_connected_gpu_bf_tiled_dyn_b.cl - TILE_K_OFM must be TILE_K * TILE_OFM and <= 8"+#endif++#if TILE_K_OFM != TILE_K_OFM_PACKED * 2+# error "fully_connected_gpu_bf_tiled_dyn_b.cl - TILE_K_OFM must be divisible by 2 for INT4"+#endif++// INT4 unpack selection based on weight layout+#if TILE_K == 4 && FILTER_LAYOUT_OS_IS_YX_OSV32_ISV2+ #define UNPACK_INT4 UNPACK_INT4x2_OSV32_ISV2+#else+ #define UNPACK_INT4 UNPACK_INT4x2+#endif++// Vector type macros+#define INPUT_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, TILE_IFM)+#define ACCUMULATOR_VEC_TYPE MAKE_VECTOR_TYPE(ACCUMULATOR_TYPE, TILE_OFM)+#define FILTER_VEC_TYPE MAKE_VECTOR_TYPE(ACCUMULATOR_TYPE, TILE_K_OFM)+#define FILTER_PACKED_VEC_TYPE MAKE_VECTOR_TYPE(FILTER_TYPE, TILE_K_OFM_PACKED)+#define BIAS_VEC_TYPE MAKE_VECTOR_TYPE(BIAS_TYPE, TILE_OFM)+#define OUTPUT_VEC_TYPE MAKE_VECTOR_TYPE(OUTPUT_TYPE, TILE_OFM)+#define ACTIVATION_VEC_TYPE MAKE_VECTOR_TYPE(ACTIVATION_TYPE, TILE_OFM)+#define TO_OUTPUT_VEC_TYPE(x) CAT(convert_, OUTPUT_VEC_TYPE)(x)+#define TO_ACTIVATION_VEC_TYPE(x) CAT(convert_, ACTIVATION_VEC_TYPE)(x)+#define TO_FILTER_VEC_TYPE(x) CAT(convert_, FILTER_VEC_TYPE)(x)+#define TO_ACCUMULATOR_VEC_TYPE(x) CAT(convert_, ACCUMULATOR_VEC_TYPE)(x)++// Block I/O macros+#define INPUT_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT0_TYPE, TILE_IFM, ptr, offset)+#define FILTER_BLOCK_READ(ptr, offset) BLOCK_READN(FILTER_TYPE, TILE_K_OFM_PACKED, ptr, offset)+#define BIAS_BLOCK_READ(ptr, offset) BLOCK_READN(BIAS_TYPE, TILE_OFM, ptr, offset)+#define OUTPUT_BLOCK_WRITE(ptr, offset, val) BLOCK_WRITEN(OUTPUT_TYPE, TILE_OFM, ptr, offset, val)++// Block write alignment check+#define USE_BLOCK_WRITE ((OUTPUT_TYPE_SIZE * TILE_OUT_B_PITCH) % 16 == 0 && (OUTPUT_TYPE_SIZE * OUTPUT_OFFSET) % 16 == 0)++#if !REALIGN_FP16_OFFSET+ #define MAIN_LOOP_ELEMENTS_COUNT IFM_SIZE+#else+ #define MAIN_LOOP_ELEMENTS_COUNT (IFM_SIZE - 1)+#endif++#define INPUT_ELEMENTS_COUNT IFM_SIZE++// =====================================================================+// Generate 8 tile variants (FORCED_TILE_B = 1..8)+// Each variant is a clean function with NO batch-tail bounds checking.+// =====================================================================+#pragma disable_includes_optimization++#define FORCED_TILE_B 1+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#define FORCED_TILE_B 2+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#define FORCED_TILE_B 3+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#define FORCED_TILE_B 4+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#define FORCED_TILE_B 5+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#define FORCED_TILE_B 6+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#define FORCED_TILE_B 7+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#define FORCED_TILE_B 8+#include "include/fully_connected_gpu_bf_tiled_dyn_b_core.cl"+#undef FORCED_TILE_B++#pragma enable_includes_optimization++// =====================================================================+// Argument forwarding macro for dispatching to tile variants+// =====================================================================+#define DYN_B_DISPATCH_ARGS \+ OPTIONAL_SHAPE_INFO_TENSOR \+ input, \+ DECOMPRESSION_SCALE_TERM_ARG \+ DECOMPRESSION_ZP_TERM_ARG \+ output, \+ weights \+ BIAS_TERM_ARG \+ FUSED_OPS_TERM_ARG++// Conditional argument macros+#if DECOMPRESSION_SCALE_TERM+ #define DECOMPRESSION_SCALE_TERM_ARG decompression_scale,+#else+ #define DECOMPRESSION_SCALE_TERM_ARG+#endif++#if DECOMPRESSION_ZP_TERM && !DECOMPRESSION_ZP_SCALAR+ #define DECOMPRESSION_ZP_TERM_ARG decompression_zp,+#else+ #define DECOMPRESSION_ZP_TERM_ARG+#endif++#if BIAS_TERM+ #define BIAS_TERM_ARG , biases+#else+ #define BIAS_TERM_ARG+#endif++#if HAS_FUSED_OPS_DECLS+ #define FUSED_OPS_TERM_ARG , FUSED_OPS_ARGS+#else+ #define FUSED_OPS_TERM_ARG+#endif++// =====================================================================+// Main kernel entry: two-phase runtime TILE_B dispatch+//+// Phase 1 (main): Selects largest TILE_B in [8..4] that evenly divides+// batch_size. If none exists (primes, etc.), uses TILE_B=8.+// Processes floor(batch/main_tb) full tiles.+// Phase 2 (tail): Remaining batch%main_tb elements are dispatched to+// a smaller tile variant. Only runs when batch is not exactly divisible.+//+// All branches are uniform (all work-items in a sub-group take the same+// path) so there is no SIMD divergence.+// =====================================================================+REQD_SUB_GROUP_SIZE(SIMD)+KERNEL(fc_bf_tiled_dyn_b)(+ OPTIONAL_SHAPE_INFO_ARG+ const __global INPUT0_TYPE* input,+#if DECOMPRESSION_SCALE_TERM+ const __global DECOMPRESSION_SCALE_TYPE* decompression_scale,+#endif+#if DECOMPRESSION_ZP_TERM && !DECOMPRESSION_ZP_SCALAR+ const __global DECOMPRESSION_ZP_TYPE* decompression_zp,+#endif+ __global OUTPUT_TYPE* output,+ const __global FILTER_TYPE* weights+#if BIAS_TERM+ , const __global BIAS_TYPE* biases+#endif+#if HAS_FUSED_OPS_DECLS+ , FUSED_OPS_DECLS+#endif+) {+ const uint batch_size = BATCH_SIZE;+ const uint gid = (uint)get_group_id(0);++ // ---- Compute OFM tile from flat gid ----+ const uint ofm_tiles = CEIL_DIV(TILE_OUT_F_NUM, TILE_OFM * SIMD);+ const uint batch_tile = gid / ofm_tiles;+ const uint ofm_tile = gid % ofm_tiles;+ const uint out_f = ofm_tile * (TILE_OFM * SIMD);++ // ---- Select main tile size (must match C++ select_tile_b logic) ----+ uint main_tb;+ if (batch_size <= 8) {+ main_tb = batch_size;+ } else {+ main_tb = 8; // default for primes / no exact divisor in [4..8]+ if (batch_size % 8 == 0) main_tb = 8;+ else if (batch_size % 7 == 0) main_tb = 7;+ else if (batch_size % 6 == 0) main_tb = 6;+ else if (batch_size % 5 == 0) main_tb = 5;+ else if (batch_size % 4 == 0) main_tb = 4;+ }++ const uint main_tiles = batch_size / main_tb;+ const uint tail = batch_size - main_tiles * main_tb;++ // ---- Phase 1: Main body (full tiles) ----+ if (batch_tile < main_tiles) {+ const uint out_b = batch_tile * main_tb;+ switch (main_tb) {+ case 1: FUNC_CALL(fc_dyn_b_tile1)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 2: FUNC_CALL(fc_dyn_b_tile2)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 3: FUNC_CALL(fc_dyn_b_tile3)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 4: FUNC_CALL(fc_dyn_b_tile4)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 5: FUNC_CALL(fc_dyn_b_tile5)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 6: FUNC_CALL(fc_dyn_b_tile6)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 7: FUNC_CALL(fc_dyn_b_tile7)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 8: FUNC_CALL(fc_dyn_b_tile8)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ }+ return;+ }++ // ---- Phase 2: Tail (remaining elements, only when batch % main_tb != 0) ----+ if (batch_tile == main_tiles && tail > 0) {+ const uint out_b = main_tiles * main_tb;+ switch (tail) {+ case 1: FUNC_CALL(fc_dyn_b_tile1)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 2: FUNC_CALL(fc_dyn_b_tile2)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 3: FUNC_CALL(fc_dyn_b_tile3)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 4: FUNC_CALL(fc_dyn_b_tile4)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 5: FUNC_CALL(fc_dyn_b_tile5)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 6: FUNC_CALL(fc_dyn_b_tile6)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ case 7: FUNC_CALL(fc_dyn_b_tile7)(out_b, out_f, DYN_B_DISPATCH_ARGS); break;+ }+ }+ // batch_tile > main_tiles + (tail>0): no work (excess work-items)+}++#undef INPUT_VEC_TYPE+#undef ACCUMULATOR_VEC_TYPE+#undef FILTER_VEC_TYPE+#undef FILTER_PACKED_VEC_TYPE+#undef BIAS_VEC_TYPE+#undef OUTPUT_VEC_TYPE+#undef ACTIVATION_VEC_TYPE+#undef TO_OUTPUT_VEC_TYPE+#undef TO_ACTIVATION_VEC_TYPE+#undef TO_FILTER_VEC_TYPE+#undef TO_ACCUMULATOR_VEC_TYPE++#undef INPUT_BLOCK_READ+#undef FILTER_BLOCK_READ+#undef BIAS_BLOCK_READ+#undef OUTPUT_BLOCK_WRITE++#undef USE_BLOCK_WRITE+#undef MAIN_LOOP_ELEMENTS_COUNT+#undef INPUT_ELEMENTS_COUNT+#undef UNPACK_INT4diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/fully_connected_gpu_bf_tiled_dyn_b_core.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/fully_connected_gpu_bf_tiled_dyn_b_core.clnew file mode 100644index 00000000000..acca5dc0f16--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/fully_connected_gpu_bf_tiled_dyn_b_core.cl@@ -0,0 +1,378 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//+// Core computation function for fully_connected_gpu_bf_tiled_dyn_b.+// Included multiple times with different FORCED_TILE_B values (1..8).+// Each variant processes exactly FORCED_TILE_B batch elements per work-item.+// The caller (entry point) computes out_b and out_f and passes them explicitly,+// enabling main+tail two-phase dispatch for non-exact batch tiling.++#define DYN_B_FUNC_BATCHED CAT(fc_dyn_b_tile, FORCED_TILE_B)+#define DYN_B_FUNC_NAME CAT(_, CAT(CAT(DYN_B_FUNC_BATCHED, _), KERNEL_ID))++inline void (DYN_B_FUNC_NAME)(+ uint out_b,+ uint out_f,+ OPTIONAL_SHAPE_INFO_ARG+ const __global INPUT0_TYPE* input,+#if DECOMPRESSION_SCALE_TERM+ const __global DECOMPRESSION_SCALE_TYPE* decompression_scale,+#endif+#if DECOMPRESSION_ZP_TERM && !DECOMPRESSION_ZP_SCALAR+ const __global DECOMPRESSION_ZP_TYPE* decompression_zp,+#endif+ __global OUTPUT_TYPE* output,+ const __global FILTER_TYPE* weights+#if BIAS_TERM+ , const __global BIAS_TYPE* biases+#endif+#if HAS_FUSED_OPS_DECLS+ , FUSED_OPS_DECLS+#endif+) {+ // ---- Work-item identification ----+ const uint sglid = (uint)get_sub_group_local_id();+ // out_b and out_f are passed from the dispatcher (main or tail phase)++ // ---- Accumulators ----+ ACCUMULATOR_VEC_TYPE acc[FORCED_TILE_B] = { };+ INPUT_VEC_TYPE in_0[FORCED_TILE_B] = { };+ FILTER_VEC_TYPE wei = 0;++ // ---- Input offset ----+#if OUTPUT_3D+ const uint out_b0 = out_b / OUTPUT_FEATURE_NUM;+ const uint out_b1 = out_b % OUTPUT_FEATURE_NUM;+ uint input_offset = out_b0 * INPUT0_BATCH_PITCH + out_b1 * INPUT0_FEATURE_PITCH + INPUT0_OFFSET;+#else+ uint input_offset = out_b * TILE_IN_B_PITCH + INPUT0_OFFSET;+#endif++ // ---- Weight offset (INT4 packed) ----+#if TILE_OFM == 1 && FILTER_LAYOUT_OS_IS_YX_OSV32_ISV2+ const uint osv32_weight_base = ((out_f >> 5) << 5);+ const uint osv_weight_stride = (INPUT_ELEMENTS_COUNT >> 1);+ const uint out_f_offset = ((out_f >> 4) & 0x1) << 4;+ uint weights_offset = osv32_weight_base * osv_weight_stride + out_f_offset;+#elif TILE_OFM == 2 && FILTER_LAYOUT_OS_IS_YX_OSV64_ISV2+ const uint osv64_weight_base = ((out_f >> 6) << 6);+ const uint osv_weight_stride = (INPUT_ELEMENTS_COUNT >> 1);+ const uint out_f_offset = ((out_f >> 5) & 0x1) << 5;+ uint weights_offset = osv64_weight_base * osv_weight_stride + out_f_offset;+#else+ uint weights_offset = out_f * (INPUT_ELEMENTS_COUNT / 2);+#endif++ // ---- Pre-load per-tensor scale/zp (if scale_groups == 1) ----+#if COMPRESSED_WEIGHTS && DECOMPRESSION_SCALE_GROUPS_NUM == 1+ #if DECOMPRESSION_SCALE_LENGTH > 1 && DECOMPRESSION_SCALE_LENGTH % (TILE_OFM * SIMD) == 0+ ACCUMULATOR_VEC_TYPE d_scale = TO_ACCUMULATOR_VEC_TYPE(BLOCK_READN(DECOMPRESSION_SCALE_TYPE, TILE_OFM, decompression_scale, out_f));+ #elif DECOMPRESSION_SCALE_LENGTH > 1 && DECOMPRESSION_SCALE_LENGTH % (TILE_OFM * SIMD) != 0+ ACCUMULATOR_VEC_TYPE d_scale = 0;+ unroll_for(uint of = 0; of < TILE_OFM; ++of) {+ uint offset = out_f + of * SIMD + sglid;+ if (offset < DECOMPRESSION_SCALE_LENGTH)+ ((ACCUMULATOR_TYPE*)(&d_scale))[of] = decompression_scale[offset];+ }+ #else+ ACCUMULATOR_VEC_TYPE d_scale = decompression_scale[0];+ #endif+ ACCUMULATOR_TYPE* d_scales = (ACCUMULATOR_TYPE*)(&d_scale);+#endif++#if COMPRESSED_WEIGHTS && DECOMPRESSION_ZP_TERM && DECOMPRESSION_ZP_GROUPS_NUM == 1 && !DECOMPRESSION_ZP_SCALAR+ #if DECOMPRESSION_ZP_LENGTH > 1 && DECOMPRESSION_ZP_LENGTH % (TILE_OFM * SIMD) == 0+ ACCUMULATOR_VEC_TYPE d_zp = TO_ACCUMULATOR_VEC_TYPE(BLOCK_READN(DECOMPRESSION_ZP_TYPE, TILE_OFM, decompression_zp, out_f));+ #elif DECOMPRESSION_ZP_LENGTH > 1 && DECOMPRESSION_ZP_LENGTH % (TILE_OFM * SIMD) != 0+ ACCUMULATOR_VEC_TYPE d_zp = 0;+ unroll_for(uint of = 0; of < TILE_OFM; ++of) {+ uint offset = out_f + of * SIMD + sglid;+ if (offset < DECOMPRESSION_ZP_LENGTH)+ ((ACCUMULATOR_TYPE*)(&d_zp))[of] = decompression_zp[offset];+ }+ #else+ ACCUMULATOR_VEC_TYPE d_zp = decompression_zp[0];+ #endif+ ACCUMULATOR_TYPE* d_zps = (ACCUMULATOR_TYPE*)(&d_zp);+#endif++ // ---- FP16 offset realignment ----+#if REALIGN_FP16_OFFSET+ {+ INPUT0_TYPE tmp_input = input[input_offset + sglid % FORCED_TILE_B * TILE_IN_B_PITCH];+ ACCUMULATOR_VEC_TYPE tmp_wei = TO_ACCUMULATOR_VEC_TYPE(BLOCK_READN(FILTER_TYPE, TILE_OFM, weights, weights_offset));+ #if COMPRESSED_WEIGHTS+ tmp_wei = (tmp_wei - d_zp) * d_scale;+ #endif+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ acc[bi] = _sub_group_shuffle(tmp_input, bi) * tmp_wei;+ }+ weights_offset += TILE_OFM * SIMD;+ input_offset += 1;+ }+#endif++ // ================================================================+ // Main computation loop+ // ================================================================+ const uint iterations = MAIN_LOOP_ELEMENTS_COUNT / (TILE_IFM * SIMD);+ __attribute__((opencl_unroll_hint(1)))+ for (uint ni = 0; ni < iterations; ++ni) {+ // Load input tile: FORCED_TILE_B elements (no bounds check needed)+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ in_0[bi] = INPUT_BLOCK_READ(input, input_offset);+ input_offset += TILE_IN_B_PITCH;+ }+ input_offset += TILE_IFM * SIMD - TILE_IN_B_PITCH * FORCED_TILE_B;++#if DECOMPRESSION_SCALE_POST_OP+ ACCUMULATOR_VEC_TYPE acc_tmp[FORCED_TILE_B] = { };+#endif++ // Inner K loop: load/unpack INT4 weights, decompress, multiply-accumulate+ unroll_for(uint ki = 0; ki < (TILE_IFM * SIMD) / TILE_K; ++ki) {+ FILTER_PACKED_VEC_TYPE wei_packed = FILTER_BLOCK_READ(weights, weights_offset);+ wei = UNPACK_INT4(ACCUMULATOR_TYPE, *((INT4_PACKED_TYPE*)&wei_packed));++ ACCUMULATOR_TYPE* w = (ACCUMULATOR_TYPE*)(&wei);+ unroll_for(uint kii = 0; kii < TILE_K; ++kii) {+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+ const uint offset_ofm = out_f + fi * SIMD + sglid;++ // Decompression scale+ #if !DECOMPRESSION_SCALE_POST_OP+ #if DECOMPRESSION_SCALE_GROUPS_NUM > 1+ const uint scale_offset = (offset_ofm % DECOMPRESSION_SCALE_BATCH_NUM) * DECOMPRESSION_SCALE_BATCH_PITCH ++ ((kii + ki * TILE_K + ni * TILE_IFM * SIMD) / DECOMPRESSION_SCALE_GROUP_SIZE) * DECOMPRESSION_SCALE_FEATURE_PITCH;+ ACCUMULATOR_TYPE ds = decompression_scale[scale_offset];+ #else+ ACCUMULATOR_TYPE ds = d_scales[fi % DECOMPRESSION_SCALE_LENGTH];+ #endif+ #else+ ACCUMULATOR_TYPE ds = ACCUMULATOR_VAL_ONE;+ #endif++ // Zero point+ #if DECOMPRESSION_ZP_TERM+ #if DECOMPRESSION_ZP_SCALAR+ ACCUMULATOR_TYPE dzp = DECOMPRESSION_ZP_VALUE;+ #elif DECOMPRESSION_ZP_GROUPS_NUM > 1+ const uint zp_offset = (offset_ofm % DECOMPRESSION_ZP_BATCH_NUM) * DECOMPRESSION_ZP_BATCH_PITCH ++ ((kii + ki * TILE_K + ni * TILE_IFM * SIMD) / DECOMPRESSION_ZP_GROUP_SIZE) * DECOMPRESSION_ZP_FEATURE_PITCH;+ ACCUMULATOR_TYPE dzp = decompression_zp[zp_offset];+ #else+ ACCUMULATOR_TYPE dzp = d_zps[fi % DECOMPRESSION_ZP_LENGTH];+ #endif+ #else+ ACCUMULATOR_TYPE dzp = ACCUMULATOR_VAL_ZERO;+ #endif++ w[W_IDX] = (w[W_IDX] - dzp) * ds;+ }+ }++ // Multiply-accumulate via sub-group shuffle broadcast+ unroll_for(uint kii = 0; kii < TILE_K; ++kii) {+ const uint total_k = ki * TILE_K + kii;+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ INPUT0_TYPE in_val = _sub_group_shuffle(((INPUT0_TYPE*)(&in_0[bi]))[total_k / SIMD], total_k % SIMD);+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+#if DECOMPRESSION_SCALE_POST_OP+ #if TILE_OFM > 1+ ((ACCUMULATOR_TYPE*)(&acc_tmp[bi]))[fi] += in_val * ((ACCUMULATOR_TYPE*)(&wei))[W_IDX];+ #else+ acc_tmp[bi] += in_val * ((ACCUMULATOR_TYPE*)(&wei))[W_IDX];+ #endif+#else+ #if TILE_OFM > 1+ ((ACCUMULATOR_TYPE*)(&acc[bi]))[fi] += in_val * ((ACCUMULATOR_TYPE*)(&wei))[W_IDX];+ #else+ acc[bi] += in_val * ((ACCUMULATOR_TYPE*)(&wei))[W_IDX];+ #endif+#endif+ }+ }+ }+ weights_offset += TILE_K_OFM_PACKED * TILE_OFM_PER_OSV_SIZE * SIMD;++ // Apply deferred scale mid-loop when scale group < TILE_IFM*SIMD+#if DECOMPRESSION_SCALE_POST_OP && (TILE_IFM * SIMD > DECOMPRESSION_SCALE_GROUP_SIZE)+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+ const uint offset_ofm = out_f + fi * SIMD + sglid;+ #if DECOMPRESSION_SCALE_GROUPS_NUM > 1+ const uint scale_offset = (offset_ofm % DECOMPRESSION_SCALE_BATCH_NUM) * DECOMPRESSION_SCALE_BATCH_PITCH ++ ((ni * TILE_IFM * SIMD + ki * TILE_K) / DECOMPRESSION_SCALE_GROUP_SIZE) * DECOMPRESSION_SCALE_FEATURE_PITCH;+ ACCUMULATOR_TYPE ds = decompression_scale[scale_offset];+ #else+ ACCUMULATOR_TYPE ds = d_scales[fi % DECOMPRESSION_SCALE_LENGTH];+ #endif+ #if TILE_OFM > 1+ ((ACCUMULATOR_TYPE*)(&acc[bi]))[fi] += ((ACCUMULATOR_TYPE*)(&acc_tmp[bi]))[fi] * ds;+ acc_tmp[bi][fi] = 0;+ #else+ acc[bi] += acc_tmp[bi] * ds;+ acc_tmp[bi] = 0;+ #endif+ }+ }+#endif+ } // end ki loop++ // Apply deferred scale at end of outer loop iteration+#if DECOMPRESSION_SCALE_POST_OP && (TILE_IFM * SIMD <= DECOMPRESSION_SCALE_GROUP_SIZE)+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+ const uint offset_ofm = out_f + fi * SIMD + sglid;+ #if DECOMPRESSION_SCALE_GROUPS_NUM > 1+ const uint scale_offset = (offset_ofm % DECOMPRESSION_SCALE_BATCH_NUM) * DECOMPRESSION_SCALE_BATCH_PITCH ++ ((ni * TILE_IFM * SIMD) / DECOMPRESSION_SCALE_GROUP_SIZE) * DECOMPRESSION_SCALE_FEATURE_PITCH;+ ACCUMULATOR_TYPE ds = decompression_scale[scale_offset];+ #else+ ACCUMULATOR_TYPE ds = d_scales[fi % DECOMPRESSION_SCALE_LENGTH];+ #endif+ #if TILE_OFM > 1+ ((ACCUMULATOR_TYPE*)(&acc[bi]))[fi] += ((ACCUMULATOR_TYPE*)(&acc_tmp[bi]))[fi] * ds;+ acc_tmp[bi][fi] = 0;+ #else+ acc[bi] += acc_tmp[bi] * ds;+ acc_tmp[bi] = 0;+ #endif+ }+ }+#endif+ } // end ni loop++ // ================================================================+ // Leftovers: remaining IFM elements not divisible by (TILE_IFM * SIMD)+ // ================================================================+#if MAIN_LOOP_ELEMENTS_COUNT % (TILE_IFM * SIMD) != 0+ #define LEFTOVER_IFM (MAIN_LOOP_ELEMENTS_COUNT % (TILE_IFM * SIMD))+ {+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ in_0[bi] = INPUT_BLOCK_READ(input, input_offset);+ input_offset += TILE_IN_B_PITCH;+ }+ input_offset += TILE_IFM * SIMD - TILE_IN_B_PITCH * FORCED_TILE_B;++ unroll_for(uint ki = 0; ki < CEIL_DIV(LEFTOVER_IFM, TILE_K); ++ki) {+ FILTER_PACKED_VEC_TYPE wei_packed = FILTER_BLOCK_READ(weights, weights_offset);+ wei = UNPACK_INT4(ACCUMULATOR_TYPE, *((INT4_PACKED_TYPE*)&wei_packed));++ ACCUMULATOR_TYPE* w = (ACCUMULATOR_TYPE*)(&wei);+ unroll_for(uint kii = 0; kii < TILE_K; ++kii) {+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+ const uint offset_ofm = out_f + fi * SIMD + sglid;+ #if DECOMPRESSION_SCALE_GROUPS_NUM > 1+ const uint scale_offset = (offset_ofm % DECOMPRESSION_SCALE_BATCH_NUM) * DECOMPRESSION_SCALE_BATCH_PITCH ++ ((kii + ki * TILE_K + iterations * TILE_IFM * SIMD) / DECOMPRESSION_SCALE_GROUP_SIZE) * DECOMPRESSION_SCALE_FEATURE_PITCH;+ ACCUMULATOR_TYPE ds = decompression_scale[scale_offset];+ #else+ ACCUMULATOR_TYPE ds = d_scales[fi % DECOMPRESSION_SCALE_LENGTH];+ #endif++ #if DECOMPRESSION_ZP_TERM+ #if DECOMPRESSION_ZP_SCALAR+ ACCUMULATOR_TYPE dzp = DECOMPRESSION_ZP_VALUE;+ #elif DECOMPRESSION_ZP_GROUPS_NUM > 1+ const uint zp_offset = (offset_ofm % DECOMPRESSION_ZP_BATCH_NUM) * DECOMPRESSION_ZP_BATCH_PITCH ++ ((kii + ki * TILE_K + iterations * TILE_IFM * SIMD) / DECOMPRESSION_ZP_GROUP_SIZE) * DECOMPRESSION_ZP_FEATURE_PITCH;+ ACCUMULATOR_TYPE dzp = decompression_zp[zp_offset];+ #else+ ACCUMULATOR_TYPE dzp = d_zps[fi % DECOMPRESSION_ZP_LENGTH];+ #endif+ #else+ ACCUMULATOR_TYPE dzp = ACCUMULATOR_VAL_ZERO;+ #endif+ w[W_IDX] = (w[W_IDX] - dzp) * ds;+ }+ }+ weights_offset += TILE_K_OFM_PACKED * TILE_OFM_PER_OSV_SIZE * SIMD;++ unroll_for(uint kii = 0; kii < TILE_K; ++kii) {+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ const uint total_k = ki * TILE_K + kii;+ if (total_k < LEFTOVER_IFM) {+ INPUT0_TYPE in_val = _sub_group_shuffle(((INPUT0_TYPE*)(&in_0[bi]))[total_k / SIMD], total_k % SIMD);+ #if TILE_OFM > 1+ ((ACCUMULATOR_TYPE*)(&acc[bi]))[fi] += in_val * ((ACCUMULATOR_TYPE*)(&wei))[W_IDX];+ #else+ acc[bi] += in_val * ((ACCUMULATOR_TYPE*)(&wei))[W_IDX];+ #endif+ }+ }+ }+ }+ }+ }+ #undef LEFTOVER_IFM+#endif++ // ================================================================+ // Post-processing: bias, activation, fused ops+ // ================================================================+ ACTIVATION_VEC_TYPE activated[FORCED_TILE_B] = { };+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ activated[bi] = TO_ACTIVATION_VEC_TYPE(acc[bi]);+ }++#if BIAS_TERM+ #if TILE_OUT_F_NUM % (TILE_OFM * SIMD) == 0+ BIAS_VEC_TYPE bias = BIAS_BLOCK_READ(biases, out_f);+ #else+ BIAS_VEC_TYPE bias = 0;+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+ ((BIAS_TYPE*)(&bias))[fi] = biases[out_f + sglid + fi * SIMD];+ }+ #endif+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ activated[bi] += TO_ACTIVATION_VEC_TYPE(bias);+ }+#endif++ OUTPUT_VEC_TYPE result[FORCED_TILE_B] = { };+#if HAS_FUSED_OPS+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ #if TILE_OFM > 1+ unroll_for(uint fi = 0; fi < TILE_OFM; ++fi) {+ FUSED_OPS_VEC;+ result[bi][fi] = FUSED_OPS_RESULT_VEC;+ }+ #else+ FUSED_OPS_SCALAR;+ result[bi] = FUSED_OPS_RESULT_SCALAR;+ #endif+ }+#else+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ result[bi] = TO_OUTPUT_VEC_TYPE(ACTIVATION_TYPED(activated[bi], ACTIVATION_PARAMS_TYPED));+ }+#endif++ // ================================================================+ // Write results (tile variant matches actual element count — no bounds check)+ // ================================================================+ uint output_offset = out_f * TILE_OUT_F_PITCH + out_b * TILE_OUT_B_PITCH + OUTPUT_OFFSET;++ if (USE_BLOCK_WRITE && (TILE_OUT_F_NUM % (TILE_OFM * SIMD) == 0 || out_f + (TILE_OFM * SIMD) <= TILE_OUT_F_NUM)) {+ unroll_for(uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ OUTPUT_BLOCK_WRITE(output, output_offset, result[bi]);+ output_offset += TILE_OUT_B_PITCH;+ }+ } else {+ output_offset += sglid;+ for (uint bi = 0; bi < FORCED_TILE_B; ++bi) {+ for (uint fi = 0; fi < TILE_OFM; ++fi) {+ if (TILE_OUT_F_NUM % (TILE_OFM * SIMD) == 0 || out_f + fi * SIMD + sglid < TILE_OUT_F_NUM) {+ output[output_offset] = ((OUTPUT_TYPE*)(&result[bi]))[fi];+ }+ output_offset += SIMD;+ }+ output_offset += TILE_OUT_B_PITCH - TILE_OFM * SIMD;+ }+ }+}++#undef DYN_B_FUNC_BATCHED+#undef DYN_B_FUNC_NAMEdiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cppindex cfa4506045b..0baf7000ef5 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cpp@@ -3,6 +3,7 @@//#include "fully_connected_kernel_bf_tiled.h"+#include "fully_connected_kernel_bf_tiled_dyn_b.h"#include "kernel_selector_utils.h"#include "swiglu/swiglu_kernel_base.h"#include <vector>@@ -16,6 +17,31 @@ static constexpr size_t min_quantize_grp_size = (simd * 2); // SIMD * (min valuestatic constexpr size_t min_slm_size = 256;static std::vector<size_t> available_quantize_grp_size = {128, 64, 32};+// Per-direction bitmasks of batch sizes where dyn_b is beneficial (based on MTL profiling).+// IFM << OFM (e.g. 3584→18944): exclude {20, 24}+static constexpr uint64_t dyn_b_batches_ifm_lt_ofm =+ (1ULL << 2) | (1ULL << 3) | (1ULL << 4) | (1ULL << 5) | (1ULL << 6) | (1ULL << 7) | (1ULL << 8) |+ (1ULL << 9) | (1ULL << 10) | (1ULL << 11) | (1ULL << 12) | (1ULL << 13) |+ (1ULL << 14) | (1ULL << 15) | (1ULL << 16) | (1ULL << 17) | (1ULL << 18) | (1ULL << 19) |+ (1ULL << 21) | (1ULL << 22) | (1ULL << 23) |+ (1ULL << 25) | (1ULL << 26) | (1ULL << 27) | (1ULL << 28) | (1ULL << 29) | (1ULL << 30) | (1ULL << 31) | (1ULL << 32);+// IFM >> OFM (e.g. 18944→3584): exclude {3, 6, 7, 8, 24, 25, 30, 31, 32}+static constexpr uint64_t dyn_b_batches_ifm_gt_ofm =+ (1ULL << 2) | (1ULL << 4) | (1ULL << 5) |+ (1ULL << 9) | (1ULL << 10) | (1ULL << 11) | (1ULL << 12) | (1ULL << 13) |+ (1ULL << 14) | (1ULL << 15) | (1ULL << 16) | (1ULL << 17) | (1ULL << 18) | (1ULL << 19) |+ (1ULL << 20) | (1ULL << 21) | (1ULL << 22) | (1ULL << 23) |+ (1ULL << 26) | (1ULL << 27) | (1ULL << 28) | (1ULL << 29);++static inline bool is_dyn_b_batch_beneficial(size_t batch, size_t ifm, size_t ofm, bool swiglu) {+ if (swiglu || batch < 2 || batch > 32)+ return false;+ if (std::min(ifm, ofm) < simd)+ return false;+ uint64_t mask = (ifm < ofm) ? dyn_b_batches_ifm_lt_ofm : dyn_b_batches_ifm_gt_ofm;+ return (mask >> batch) & 1ULL;+}+namespace kernel_selector {namespace fc_kernel_bf_tiled_utils {@@ -843,75 +869,153 @@ JitConstants FullyConnected_bf_tiled::GetJitConstants(const fully_connected_parareturn jit;}+// Sets up the runtime dispatch lambda that selects between default/SLM/dyn_b kernels+// based on actual batch size at inference time.+void FullyConnected_bf_tiled::SetDispatchDataFunc(KernelData& kd, const DispatchIndices& idx) const {+ kd.update_dispatch_data_func = [this, idx](const Params& params, KernelData& kd) {+ const auto& prim_params = static_cast<const fully_connected_params&>(params);+ size_t output_batch = get_output_aligned_bf_size(prim_params, false).first;++ // Choose shape agnostic kernel based on batch size:+ // - default version for batches <= 240+ // - SLM version for batches >= 256+ // - dyn_b version for small batches (2-32)+ const auto default_alignment = 16;+ // We can use SLM version if `output_batch + default_alignment > min_slm_size(256)`+ // because memory and batch are aligned (whether 16 or 64 elements)+ bool use_slm = (idx.slm >= 0) && (output_batch + default_alignment > min_slm_size);+ bool use_dyn_b = !use_slm && (idx.dyn_b >= 0)+ && is_dyn_b_batch_beneficial(output_batch,+ prim_params.weights.IFM().v,+ prim_params.weights.OFM().v,+ is_swiglu_fused(prim_params));++ int execute = use_slm ? idx.slm : (use_dyn_b ? idx.dyn_b : idx.default_fc);++ // Skip all FC kernels except the executing one+ int32_t total_kernels = static_cast<int32_t>(kd.kernels.size());+ for (int32_t i = idx.default_fc; i < total_kernels; i++)+ kd.kernels[i].skip_execution = (i != execute);++ const char* mode_str = use_slm ? "SLM" : (use_dyn_b ? "DynB" : "Default");+ GPU_DEBUG_TRACE_DETAIL << "FC bf tiled: " << mode_str << " shape-agnostic kernel version "+ << "will be used for batch size = " << output_batch << "\n";++ // Configure workgroups for the selected kernel+ if (use_dyn_b) {+ size_t effective_batch = std::max(output_batch, (size_t)1);+ size_t tile_b = FullyConnected_bf_tiled_dyn_b::SelectTileB(effective_batch);+ FullyConnected_bf_tiled_dyn_b dyn_b_impl;+ auto dyn_b_tparams = dyn_b_impl.GetTuneParams(prim_params);+ auto threads = get_output_aligned_bf_size(prim_params, true,+ static_cast<uint32_t>(tile_b),+ static_cast<int32_t>(dyn_b_tparams.tile_ofm * simd));+ kd.kernels[execute].params.workGroups.global = {threads.second * threads.first * simd, 1, 1};+ kd.kernels[execute].params.workGroups.local = {simd, 1, 1};+ } else {+ auto dispatchData = SetDefault(prim_params, -1,+ use_slm ? static_cast<int>(KernelType::SLM) : static_cast<int>(KernelType::DEFAULT));+ kd.kernels[execute].params.workGroups.global = dispatchData.gws;+ kd.kernels[execute].params.workGroups.local = dispatchData.lws;+ }++ kd.kernels[execute].skip_execution = KernelData::SkipKernelExecution(prim_params);++ auto& input = prim_params.inputs[0];+ if (prim_params.outputs[0].GetLayout() == DataLayout::bfyx)+ OPENVINO_ASSERT(input.X().pad.Total() == 0 && input.Y().pad.Total() == 0,+ "[GPU] Invalid padding in spatial axes observed in FC bf tiled.");+ else+ OPENVINO_ASSERT(input.Feature().pad.Total(true) == 0,+ "[GPU] Invalid padding in f axis observed in FC bf tiled.");++ // Pre-quantizing kernel was generated. Update the kernel and intermediate buffers or disable it.+ if (idx.quantize >= 0) {+ if (use_slm) {+ size_t quantize_grp_size = get_dynamic_quantize_group_size(prim_params);+ kd.kernels[idx.quantize].skip_execution = false;+ auto slm_dispatch = SetDefault(prim_params, -1, static_cast<int>(KernelType::SLM));+ size_t input_f = get_input_bf_size(prim_params).second;+ size_t input_size = input_f * slm_dispatch.tile_m * slm_dispatch.gws[2];+ OPENVINO_ASSERT(quantize_grp_size != 0, "Error: quantize_grp_size is zero.");+ // half type of de_quan_scale and activation sum for each quantized group+ size_t quan_var_size = (input_size / quantize_grp_size) * 2 * 2;++ if (kd.internalBuffers[0].byte_count < input_size ||+ kd.internalBuffers[1].byte_count < quan_var_size) {+ kd.internalBuffers.clear();+ // quantized input is char type+ kd.internalBuffers.push_back(input_size);+ // float type of de_quan_scale and activation sum for each quantized group+ kd.internalBuffers.push_back(quan_var_size);+ }++ kd.kernels[0].params.workGroups.global = {(std::max((input_size / quantize_grp_size), (size_t)1)), 1, 1};+ kd.kernels[0].params.workGroups.local = {1, 1, 1};+ } else {+ kd.kernels[idx.quantize].skip_execution = true;+ }+ }+ };+}++// Reconstruct DispatchIndices for deserialization path (load()).+// At build time, GetMultiKernelsData returns indices explicitly.+// Layout:+// dyn-quantize path: quantize(0) | default(1) | [optional:slm] | [optional:dyn_b]+// non-dyn-quantize: default(0) | [optional:slm] | [optional:dyn_b]+// dyn_b is the last kernel when it has no INTERNAL_BUFFER args but the default_fc kernel does+// (only possible in dyn-quantize path where default_fc uses quantized internal buffers).+// In non-dyn-quantize path, no kernel has INTERNAL_BUFFER, so extra kernels are SLM only.void FullyConnected_bf_tiled::GetUpdateDispatchDataFunc(KernelData& kd) const {if (kd.kernels.size() == 1) {Parent::GetUpdateDispatchDataFunc(kd);- } else {- kd.update_dispatch_data_func = [this](const Params& params, KernelData& kd) {- const auto& prim_params = static_cast<const fully_connected_params&>(params);-- size_t quantize_grp_size = get_dynamic_quantize_group_size(prim_params);- size_t output_batch = get_output_aligned_bf_size(prim_params, false).first;- // Get index of the added shape-agnostic kernel- // [optional:dyn_quant kernel] | default kernel | [optional:slm kernel]- int32_t quantize_kernel_idx = (kd.internalBuffers.empty()) ? -1 : 0;- int32_t default_kernel_idx = quantize_kernel_idx + 1;- int32_t slm_kernel_idx = (static_cast<int32_t>(kd.kernels.size()) > default_kernel_idx + 1) ? default_kernel_idx + 1 : -1;-- // Choose one of the two shape agnostic kernels: N == added kernel number- // - kd.kernels[N-1] for batches <= 240 (default version)- // - kd.kernels[N] for batches >= 256 (slm version)- const auto default_alignment = 16;- // We can use SLM version if `output_batch + default_alignment > min_slm_size(256)` because memory and batch are aligned (whether 16 or 64 elements)- const auto execute_type = ((slm_kernel_idx >= 0) && (output_batch + default_alignment > min_slm_size)) ? KernelType::SLM : KernelType::DEFAULT;- const auto execute_kernel_idx = (execute_type == KernelType::SLM) ? slm_kernel_idx : default_kernel_idx;- const auto skip_kernel_idx = (execute_type == KernelType::SLM) ? default_kernel_idx : slm_kernel_idx;-- // Check default or SLM version FC, and disable remain version- if (skip_kernel_idx >= 0)- kd.kernels[skip_kernel_idx].skip_execution = true;-- GPU_DEBUG_TRACE_DETAIL << "FC bf tiled: " << (execute_type == KernelType::SLM ? "SLM" : "Default") << " shape-agnostic kernel version "- << "will be used for batch size = " << output_batch << "\n";-- auto dispatchData = SetDefault(prim_params, -1, static_cast<int>(execute_type));- kd.kernels[execute_kernel_idx].params.workGroups.global = dispatchData.gws;- kd.kernels[execute_kernel_idx].params.workGroups.local = dispatchData.lws;- kd.kernels[execute_kernel_idx].skip_execution = KernelData::SkipKernelExecution(prim_params);-- auto& input = prim_params.inputs[0];- if (prim_params.outputs[0].GetLayout() == DataLayout::bfyx)- OPENVINO_ASSERT(input.X().pad.Total() == 0 && input.Y().pad.Total() == 0, "[GPU] Invalid padding in spatial axes observed in FC bf tiled.");- else- OPENVINO_ASSERT(input.Feature().pad.Total(true) == 0, "[GPU] Invalid padding in f axis observed in FC bf tiled.");+ return;+ }- if (quantize_kernel_idx >= 0) {- // Pre-quantizing kernel was generated. Update the kernel and intermediate buffers or disable it.- if (execute_type == KernelType::DEFAULT) {- kd.kernels[quantize_kernel_idx].skip_execution = true;- } else {- kd.kernels[quantize_kernel_idx].skip_execution = false;- size_t input_f = get_input_bf_size(prim_params).second;- size_t input_size = input_f * dispatchData.tile_m * dispatchData.gws[2];- OPENVINO_ASSERT(quantize_grp_size != 0, "Error: quantize_grp_size is zero.");- // half type of de_quan_scale and activation sum for each quantized group- size_t quan_var_size = (input_size / quantize_grp_size) * 2 * 2;-- if (kd.internalBuffers[0].byte_count < input_size ||- kd.internalBuffers[1].byte_count < quan_var_size) {- kd.internalBuffers.clear();- // quantized input is char type- kd.internalBuffers.push_back(input_size);- // float type of de_quan_scale and activation sum for each quantized group- kd.internalBuffers.push_back(quan_var_size);- }+ DispatchIndices idx;+ idx.quantize = kd.internalBuffers.empty() ? -1 : 0;+ idx.default_fc = idx.quantize + 1;++ int32_t total = static_cast<int32_t>(kd.kernels.size());+ if (total > idx.default_fc + 1) {+ // Check if default_fc kernel uses INTERNAL_BUFFER (dyn-quantize path)+ bool default_has_internal_buffer = false;+ for (const auto& arg : kd.kernels[idx.default_fc].params.arguments) {+ if (arg.t == ArgumentDescriptor::Types::INTERNAL_BUFFER) {+ default_has_internal_buffer = true;+ break;+ }+ }- kd.kernels[0].params.workGroups.global = {(std::max((input_size / quantize_grp_size), (size_t)1)), 1, 1};- kd.kernels[0].params.workGroups.local = {1, 1, 1};+ if (default_has_internal_buffer) {+ // dyn-quantize path: last kernel without INTERNAL_BUFFER is dyn_b+ int32_t last = total - 1;+ bool last_has_internal_buffer = false;+ for (const auto& arg : kd.kernels[last].params.arguments) {+ if (arg.t == ArgumentDescriptor::Types::INTERNAL_BUFFER) {+ last_has_internal_buffer = true;+ break;}}- };+ if (!last_has_internal_buffer) {+ idx.dyn_b = last;+ if (last > idx.default_fc + 1)+ idx.slm = idx.default_fc + 1;+ } else {+ idx.slm = idx.default_fc + 1;+ }+ } else {+ // non-dyn-quantize path: extra kernels are SLM only (dyn_b never has INTERNAL_BUFFER+ // and neither do other kernels, so we cannot distinguish — but dyn_b is always last+ // and is only added when SLM is also present in this path)+ idx.slm = idx.default_fc + 1;+ if (total > idx.default_fc + 2)+ idx.dyn_b = total - 1;+ }}++ SetDispatchDataFunc(kd, idx);}KernelsData FullyConnected_bf_tiled::GetTunedKernelsDataByIndex(const Params ¶ms,@@ -953,48 +1057,65 @@ KernelsData FullyConnected_bf_tiled::GetTunedKernelsDataByIndex(const Params &paKernelsData kernels_data;if (should_dynamic_quantize(fc_params)) {- // Use seperate 2 kernels for dynamic quantizing : quantizing_kernel + fc_kernel+ // Use seperate kernels for dynamic quantizing : quantizing_kernel + fc_kernel// 1st kernel : Dynamic quantizing by dynamic_quantize_grp_size// 2nd kernel : fully connected kernel with KernelType::DEFAULT. Quantized inputs and scale values could be used.// 3rd kernel : (optional) fully connected shape_agnostic kernel with KernelType::SLM. Quantized inputs and scale values would be used.- kernels_data = GetMultiKernelsData(params,- fc_params.inputs[0].GetLayout(),- weights_layout,- tparams.exec_options,- autoTuneIndex);+ // 4th kernel : (optional) FC with dyn_b dispatch (original F16 input, no dynamic quantization)+ auto [multi_kd, multi_idx] = GetMultiKernelsData(params,+ fc_params.inputs[0].GetLayout(),+ weights_layout,+ tparams.exec_options,+ autoTuneIndex);+ kernels_data = std::move(multi_kd);OPENVINO_ASSERT(!kernels_data.empty() && !kernels_data[0].kernels.empty(), "[GPU] Error to create multi kernel for dynamic quantizing.");if (params.is_shape_agnostic)- GetUpdateDispatchDataFunc(kernels_data[0]);+ SetDispatchDataFunc(kernels_data[0], multi_idx);} else {kernels_data = GetCommonKernelsData(params,- fc_params.inputs[0].GetLayout(),- weights_layout,- tparams.exec_options,- autoTuneIndex,- 0);+ fc_params.inputs[0].GetLayout(),+ weights_layout,+ tparams.exec_options,+ autoTuneIndex,+ 0);if (params.is_shape_agnostic) {- auto tparams = GetAutoTuneParams(fc_params, KernelType::SLM, autoTuneIndex);- auto can_select_slm_kernel = tparams.kernel_type == KernelType::SLM;-- if (!can_select_slm_kernel)- return kernels_data;-- auto slm_kernel = GetCommonKernelsData(params,- fc_params.inputs[0].GetLayout(),- weights_layout,- tparams.exec_options,- autoTuneIndex,- 1);-- if (slm_kernel.empty() || slm_kernel[0].kernels.empty())- return kernels_data;+ DispatchIndices idx;+ idx.default_fc = 0;++ // Try to add SLM kernel for large batches+ auto slm_tparams = GetAutoTuneParams(fc_params, KernelType::SLM, autoTuneIndex);+ if (slm_tparams.kernel_type == KernelType::SLM) {+ auto slm_kernel = GetCommonKernelsData(params,+ fc_params.inputs[0].GetLayout(),+ weights_layout,+ slm_tparams.exec_options,+ autoTuneIndex,+ 1);+ if (!slm_kernel.empty() && !slm_kernel[0].kernels.empty()) {+ kernels_data[0].kernels.push_back(slm_kernel[0].kernels.back());+ idx.slm = static_cast<int32_t>(kernels_data[0].kernels.size()) - 1;+ }+ }- kernels_data[0].kernels.push_back(slm_kernel[0].kernels.back());+ // Try to add dyn_b kernel for batch-optimal runtime dispatch.+ // Only add when SLM is present — deserialization relies on default_fc having+ // INTERNAL_BUFFER args (dyn-quantize path) to distinguish dyn_b from SLM.+ // In non-dyn-quantize path, no kernel has INTERNAL_BUFFER, so dyn_b is only+ // identifiable when it follows an SLM kernel (3-kernel layout: default|slm|dyn_b).+ if (idx.slm >= 0) {+ FullyConnected_bf_tiled_dyn_b dyn_b_impl;+ auto dyn_b_kd = dyn_b_impl.GetKernelsData(params);+ if (!dyn_b_kd.empty() && !dyn_b_kd[0].kernels.empty()) {+ kernels_data[0].kernels.push_back(dyn_b_kd[0].kernels.back());+ idx.dyn_b = static_cast<int32_t>(kernels_data[0].kernels.size()) - 1;+ }+ }// Update default update_dispatch_data_func function- GetUpdateDispatchDataFunc(kernels_data[0]);+ if (idx.slm >= 0 || idx.dyn_b >= 0)+ SetDispatchDataFunc(kernels_data[0], idx);}}@@ -1028,15 +1149,17 @@ KernelsData FullyConnected_bf_tiled::GetKernelsData(const Params& params) const}-KernelsData FullyConnected_bf_tiled::GetMultiKernelsData(const Params ¶ms,- DataLayout dl,- WeightsLayout wl,- const std::string exeMode,- int autoTuneIndex) const {+std::pair<KernelsData, FullyConnected_bf_tiled::DispatchIndices>+FullyConnected_bf_tiled::GetMultiKernelsData(const Params ¶ms,+ DataLayout dl,+ WeightsLayout wl,+ const std::string exeMode,+ int autoTuneIndex) const {if (!Validate(params)) {- return KernelsData();+ return {KernelsData(), {}};}+ DispatchIndices idx;const auto& fc_params = static_cast<const fully_connected_params&>(params);size_t quantize_grp_size = get_dynamic_quantize_group_size(fc_params);@@ -1061,7 +1184,7 @@ KernelsData FullyConnected_bf_tiled::GetMultiKernelsData(const Params ¶ms,kd.weightsReorderParams,GetSupportedKey());if (!succeed) {- return {};+ return {{}, {}};}int inputs_count = 1;@@ -1076,6 +1199,7 @@ KernelsData FullyConnected_bf_tiled::GetMultiKernelsData(const Params ¶ms,const DispatchData dispatchData = SetDefault(new_params, autoTuneIndex, kernel_number);// Dynamic-quantize kernel+ idx.quantize = kernel_number;{auto& quan_kernel = kd.kernels[0];DispatchData dyn_quan_dispatch = dispatchData;@@ -1122,6 +1246,7 @@ KernelsData FullyConnected_bf_tiled::GetMultiKernelsData(const Params ¶ms,kd.internalBufferDataType = Datatype::F16;// FC kernel for dynamic quantized input with KernelType::DEFAULT+ idx.default_fc = kernel_number;{auto entry_point = GetEntryPoint(kernelName, fc_params.layerID, params, kernel_number);auto cldnn_jit = GetJitConstants(new_params, dispatchData);@@ -1156,6 +1281,7 @@ KernelsData FullyConnected_bf_tiled::GetMultiKernelsData(const Params ¶ms,auto can_select_slm_kernel = slm_params.kernel_type == KernelType::SLM;// FC kernel for dynamic quantized input with KernelType::SLMif (params.is_shape_agnostic && can_select_slm_kernel) {+ idx.slm = kernel_number;kd.kernels.resize(kernel_number + 1);auto entry_point = GetEntryPoint(kernelName, fc_params.layerID, params, kernel_number);@@ -1183,9 +1309,43 @@ KernelsData FullyConnected_bf_tiled::GetMultiKernelsData(const Params ¶ms,sa_kernel.params.arguments.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, 0});sa_kernel.params.arguments.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, 1});+ kernel_number++;+ }++ // FC kernel with dyn_b dispatch (no dynamic quantization, reads original F16 input)+ if (params.is_shape_agnostic && FullyConnected_bf_tiled_dyn_b::IsBeneficial(fc_params)) {+ idx.dyn_b = kernel_number;+ FullyConnected_bf_tiled_dyn_b dyn_b_impl;+ auto dyn_b_dispatch = dyn_b_impl.SetDefault(new_params);+ auto dyn_b_jit_constants = dyn_b_impl.GetJitConstants(new_params, dyn_b_dispatch);++ kd.kernels.resize(kernel_number + 1);++ auto dyn_b_entry_point = GetEntryPoint("fully_connected_gpu_bf_tiled_dyn_b", fc_params.layerID, params, kernel_number);+ auto dyn_b_jit = CreateJit("fully_connected_gpu_bf_tiled_dyn_b", dyn_b_jit_constants, dyn_b_entry_point);++ auto& dyn_b_kernel = kd.kernels[kernel_number];+ dyn_b_kernel.params.workGroups.global = dyn_b_dispatch.gws;+ dyn_b_kernel.params.workGroups.local = dyn_b_dispatch.lws;+ dyn_b_kernel.skip_execution = false;++ FillCLKernelData(dyn_b_kernel,+ dyn_b_dispatch,+ params.engineInfo,+ "fully_connected_gpu_bf_tiled_dyn_b",+ dyn_b_jit,+ dyn_b_entry_point,+ EXE_MODE_DEFAULT,+ true,+ !fc_params.bias.empty(),+ inputs_count,+ GetFusedPrimitiveInputsCount(params),+ 1,+ fc_params.is_shape_agnostic);+ // dyn_b reads original F16 input (no INTERNAL_BUFFER args)}kd.autoTuneIndex = autoTuneIndex;- return {kd};+ return {{kd}, idx};}} // namespace kernel_selectordiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.hindex 8f2f32c9a6b..fea0e8b7909 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.h+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.h@@ -31,11 +31,20 @@ public:ParamsKey GetSupportedKey() const override;DeviceFeaturesKey get_required_device_features_key(const Params& params) const override;- KernelsData GetMultiKernelsData(const Params ¶ms,- DataLayout dl,- WeightsLayout wl,- const std::string exeMode = EXE_MODE_DEFAULT,- int autoTuneIndex = -1) const;+ // Indices of kernel slots in the multi-kernel KernelData.+ // -1 means the kernel is not present.+ struct DispatchIndices {+ int32_t quantize = -1; // dynamic quantization pre-processing kernel+ int32_t default_fc = -1; // FC with KernelType::DEFAULT+ int32_t slm = -1; // FC with KernelType::SLM (large batches)+ int32_t dyn_b = -1; // FC with dyn_b dispatch (small batches)+ };++ std::pair<KernelsData, DispatchIndices> GetMultiKernelsData(const Params ¶ms,+ DataLayout dl,+ WeightsLayout wl,+ const std::string exeMode = EXE_MODE_DEFAULT,+ int autoTuneIndex = -1) const;struct tune_params {tune_params(unsigned tile_b,@@ -82,6 +91,7 @@ protected:JitConstants GetJitConstants(const fully_connected_params& params, const DispatchData& dispatchData) const override;bool Validate(const Params& params) const override;void GetUpdateDispatchDataFunc(KernelData& kd) const override;+ void SetDispatchDataFunc(KernelData& kd, const DispatchIndices& idx) const;tune_params GetAutoTuneParams(const fully_connected_params& params, KernelType preffered_kernel_type = KernelType::DEFAULT, int idx = -1) const;diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled_dyn_b.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled_dyn_b.cppnew file mode 100644index 00000000000..f9026f4fb10--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled_dyn_b.cpp@@ -0,0 +1,381 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "fully_connected_kernel_bf_tiled_dyn_b.h"+#include "kernel_selector_utils.h"+#include <vector>+#include <functional>+#include "common_types.h"++static constexpr size_t simd = 16;++namespace kernel_selector {++using namespace fc_kernel_bf_tiled_utils;++FullyConnected_bf_tiled_dyn_b::FullyConnected_bf_tiled_dyn_b()+ : FullyConnectedKernelBase("fully_connected_gpu_bf_tiled_dyn_b") {}++ParamsKey FullyConnected_bf_tiled_dyn_b::GetSupportedKey() const {+ ParamsKey k;+ k.EnableInputDataType(Datatype::F16);+ k.EnableOutputDataType(Datatype::F16);+ k.EnableOutputDataType(Datatype::F32);+ k.EnableOutputDataType(Datatype::INT8);+ k.EnableOutputDataType(Datatype::UINT8);+ k.EnableInputWeightsType(WeightsType::UINT4);+ k.EnableInputWeightsType(WeightsType::INT4);+ k.EnableInputLayout(DataLayout::bf);+ k.EnableInputLayout(DataLayout::bfyx);+ k.EnableOutputLayout(DataLayout::bf);+ k.EnableOutputLayout(DataLayout::bfyx);+ k.EnableBatching();+ k.EnableBiasPerFeature();+ k.EnableNonBiasTerm();+ k.EnableTensorOffset();+ k.EnableTensorPitches();+ k.EnableDifferentTypes();+ k.EnableDifferentInputWeightsTypes();+ k.EnableWeightsCompression();+ k.EnableDynamicShapesSupport();+ return k;+}++DeviceFeaturesKey FullyConnected_bf_tiled_dyn_b::get_required_device_features_key(const Params& params) const {+ auto k = get_common_subgroups_device_features_key(params);+ k.requires_subgroup_shuffle();+ return k;+}++size_t FullyConnected_bf_tiled_dyn_b::SelectTileB(size_t batch_size) {+ // For small batches, tile = batch (exact, no tail)+ if (batch_size <= 8)+ return batch_size;++ // Find largest exact divisor in [8..4] (no tail needed)+ for (size_t t = 8; t >= 4; --t) {+ if (batch_size % t == 0)+ return t;+ }++ // No good exact divisor (primes, 2*prime, etc.):+ // Use TILE_B=8 for maximum throughput; CL entry point handles the tail.+ return 8;+}++bool FullyConnected_bf_tiled_dyn_b::IsBeneficial(const fully_connected_params& params) {+ auto& weights = params.weights;+ auto wt = weights.GetDType();++ // INT4 compressed, F16 input, shape_agnostic only+ if (wt != WeightsType::UINT4 && wt != WeightsType::INT4)+ return false;+ if (!params.compressed)+ return false;+ if (params.inputs[0].GetDType() != Datatype::F16)+ return false;+ if (!params.is_shape_agnostic)+ return false;++ // No SwiGLU support+ if (is_swiglu_fused(params))+ return false;++ // Only beneficial for imbalanced IFM/OFM with sufficient dimension size+ auto ifm = weights.IFM().v;+ auto ofm = weights.OFM().v;+ if (std::min(ifm, ofm) < simd)+ return false;+ if (!(2 * ifm < ofm || ifm > 2 * ofm))+ return false;++ return true;+}++bool FullyConnected_bf_tiled_dyn_b::Validate(const Params& params) const {+ if (!Parent::Validate(params)) {+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ }++ auto& fc_params = static_cast<const fully_connected_params&>(params);+ auto& input = fc_params.inputs[0];+ auto& output = fc_params.outputs[0];+ auto& weights = fc_params.weights;++ // Only INT4 compressed weights+ auto wt = weights.GetDType();+ if (wt != WeightsType::UINT4 && wt != WeightsType::INT4)+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ if (!fc_params.compressed)+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ // F16 input only+ if (input.GetDType() != Datatype::F16)+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ // IFM must be known and even (for INT4 packing)+ if (input.is_dynamic()) {+ auto ifm_size = get_input_bf_size(fc_params).second;+ if (ifm_size == 0)+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ }+ if (weights.IFM().v % 2 != 0)+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ // Block reads: batch pitch must be even for F16+ if (input.Batch().pitch % 2 != 0 && (input.Batch().v > 1 || fc_params.is_shape_agnostic))+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ // For 3D: feature pitch must be even+ if (output.GetLayout() == DataLayout::bfyx && input.Feature().pitch % 2 != 0+ && (input.Feature().v > 1 || fc_params.is_shape_agnostic))+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ // No padding on spatial dimensions+ if (input.GetLayout() == DataLayout::bfyx) {+ if (input.X().pad.Total() != 0 || input.Y().pad.Total() != 0)+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ }++ // No 4D output+ if (output.GetLayout() == DataLayout::bfyx) {+ if (input.X().v > 1)+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ }++ // Reject dynamic quantization targets (let bf_tiled SLM handle those)+ if (is_weight_dyn_quantizable(fc_params) && should_dynamic_quantize(fc_params))+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ // This kernel is designed for dynamic batch dispatch only.+ // For static shapes, bf_tiled already selects optimal TILE_B at compile time.+ if (!fc_params.is_shape_agnostic)+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ // SwiGLU fused op is not supported (requires OUTER_OFM=2 / SWIGLU_LENGTH handling)+ if (is_swiglu_fused(fc_params))+ DO_NOT_USE_THIS_KERNEL(params.layerID);++ // Only beneficial when IFM and OFM are highly imbalanced with sufficient size.+ // For near-square shapes (IFM ≈ OFM), bf_tiled's dispatch is already efficient.+ // Accept when: min(IFM,OFM) >= SIMD and (2*IFM < OFM or IFM > 2*OFM)+ {+ auto ifm = weights.IFM().v;+ auto ofm = weights.OFM().v;+ if (std::min(ifm, ofm) < simd)+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ if (!(2 * ifm < ofm || ifm > 2 * ofm))+ DO_NOT_USE_THIS_KERNEL(params.layerID);+ }++ return true;+}+++FullyConnected_bf_tiled_dyn_b::tune_params+FullyConnected_bf_tiled_dyn_b::GetTuneParams(const fully_connected_params& params) const {+ // Same base config as static_b16 (optimized for INT4 on iGPU)+ if (params.weights.GetLayout() == WeightsLayout::os_iyx_osv16)+ return tune_params(1, 1, 4, 1, 1, EXE_MODE_DEFAULT);+ else if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2)+ return tune_params(2, 1, 2, 1, 1, EXE_MODE_DEFAULT);+ else // os_is_yx_osv32_isv2 (default)+ return tune_params(2, 1, 4, 1, 1, EXE_MODE_DEFAULT);+}++FullyConnected_bf_tiled_dyn_b::DispatchData+FullyConnected_bf_tiled_dyn_b::SetDefault(const fully_connected_params& params, int autoTuneIndex, int kernel_number) const {+ auto dispatchData = Parent::SetDefault(params);+ auto tparams = GetTuneParams(params);++ // For initial compilation use TILE_B=8 as default (will be updated at runtime)+ constexpr size_t default_tile_b = 8;+ auto bf_size = get_output_aligned_bf_size(params, false);+ size_t batch = bf_size.first;+ size_t tile_b = (batch > 0) ? SelectTileB(batch) : default_tile_b;++ auto threads = get_output_aligned_bf_size(params, true,+ static_cast<uint32_t>(tile_b),+ static_cast<int32_t>(tparams.tile_ofm * simd));+ auto batch_threads = threads.first;+ auto feature_threads = threads.second;++ dispatchData.gws[0] = feature_threads * batch_threads * simd;+ dispatchData.gws[1] = 1;+ dispatchData.gws[2] = 1;++ dispatchData.lws[0] = simd;+ dispatchData.lws[1] = 1;+ dispatchData.lws[2] = 1;++ dispatchData.tile_m = static_cast<uint32_t>(tile_b);+ dispatchData.tile_n = tparams.tile_ofm;+ dispatchData.tile_mk = tparams.tile_ifm;+ dispatchData.tile_nk = tparams.tile_k;+ dispatchData.outer_n = 1;+ dispatchData.tile_ms = tparams.dispatch_bsv;+ dispatchData.tile_ns = tparams.dispatch_fsv;+ dispatchData.use_slm = false;++ return dispatchData;+}++void FullyConnected_bf_tiled_dyn_b::GetUpdateDispatchDataFunc(KernelData& kd) const {+ kd.update_dispatch_data_func = [this](const Params& params, KernelData& kd) {+ const auto& prim_params = static_cast<const fully_connected_params&>(params);++ auto dispatchData = SetDefault(prim_params);+ kd.kernels[0].params.workGroups.global = dispatchData.gws;+ kd.kernels[0].params.workGroups.local = dispatchData.lws;+ kd.kernels[0].skip_execution = KernelData::SkipKernelExecution(prim_params);+ };+}++KernelsPriority FullyConnected_bf_tiled_dyn_b::GetKernelsPriority(const Params& params) const {+ // Low priority: dyn_b is integrated into bf_tiled as a runtime sub-kernel.+ // Standalone is only used when explicitly forced via force_implementations (tests).+ return FORCE_PRIORITY_9;+}++JitConstants FullyConnected_bf_tiled_dyn_b::GetJitConstants(const fully_connected_params& params,+ const DispatchData& dispatchData) const {+ JitConstants jit = Parent::GetJitConstants(params, dispatchData);++ size_t tile_k_ofm = dispatchData.tile_nk * dispatchData.tile_n;+ size_t tile_k_ofm_packed = tile_k_ofm;++ bool add_decompress_scale_post_op = false;+ WeightsType weights_dt = params.weights.GetDType();+ if (weights_dt == WeightsType::UINT4 || weights_dt == WeightsType::INT4) {+ tile_k_ofm_packed /= 2;+ jit.Merge(make_int4_packed_type_jit_constant("INT4_PACKED_TYPE", weights_dt, tile_k_ofm));+ const size_t scale_group_size = get_scale_group_size(params);+ if (scale_group_size % simd == 0)+ add_decompress_scale_post_op = true;+ }++ // W_IDX and TILE_OFM_PER_OSV_SIZE based on weight layout+ if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2) {+ jit.AddConstant(MakeJitConstant("W_IDX", "fi * TILE_K + kii"));+ } else if (params.weights.GetLayout() == WeightsLayout::os_iyx_osv16) {+ jit.AddConstant(MakeJitConstant("W_IDX", "fi * TILE_K + kii"));+ } else if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2) {+ jit.AddConstant(MakeJitConstant("W_IDX", "fi * TILE_K + kii"));+ } else {+ jit.AddConstant(MakeJitConstant("W_IDX", "kii * TILE_OFM + fi"));+ }++ if (params.weights.GetLayout() == WeightsLayout::os_iyx_osv16 && dispatchData.tile_n == 2) {+ jit.AddConstant(MakeJitConstant("TILE_OFM_PER_OSV_SIZE", 0.5f));+ } else if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2 && dispatchData.tile_n == 1) {+ jit.AddConstant(MakeJitConstant("TILE_OFM_PER_OSV_SIZE", 2));+ } else if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2 && dispatchData.tile_n == 2) {+ jit.AddConstant(MakeJitConstant("TILE_OFM_PER_OSV_SIZE", 2));+ } else {+ jit.AddConstant(MakeJitConstant("TILE_OFM_PER_OSV_SIZE", 1));+ }++ if (add_decompress_scale_post_op)+ jit.AddConstant(MakeJitConstant("DECOMPRESSION_SCALE_POST_OP", 1));++ jit.AddConstant(MakeJitConstant("DYNAMIC_QUANTIZE", 0));+ jit.AddConstant(MakeJitConstant("IFM_SIZE", get_input_bf_size(params).second));+ jit.AddConstant(MakeJitConstant("SIMD", simd));++ // TILE_B is not used directly in the kernel dispatch,+ // but needed for CONST_LOOP unroll generation (max tile = 8).+ jit.AddConstant(MakeJitConstant("TILE_OFM", dispatchData.tile_n));+ jit.AddConstant(MakeJitConstant("TILE_IFM", dispatchData.tile_mk));+ jit.AddConstant(MakeJitConstant("TILE_K", dispatchData.tile_nk));+ jit.AddConstant(MakeJitConstant("TILE_K_OFM", tile_k_ofm));+ jit.AddConstant(MakeJitConstant("TILE_K_OFM_PACKED", tile_k_ofm_packed));+ jit.AddConstant(MakeJitConstant("DISPATCH_BSV", dispatchData.tile_ms));+ jit.AddConstant(MakeJitConstant("DISPATCH_FSV", dispatchData.tile_ns));++ // Generate CONST_LOOP macros up to max FORCED_TILE_B = 8+ jit.Merge(MakeConstantLoopUnrollJitConstants(8));++ bool realign_fp16_offset = params.inputs[0].GetDType() == Datatype::F16 && params.inputs[0].GetFirstElementOffset() % 2 != 0;+ jit.AddConstant(MakeJitConstant("REALIGN_FP16_OFFSET", realign_fp16_offset));++ auto activation_dt = GetActivationType(params);+ auto accumulator_dt = GetAccumulatorType(params);+ jit.Merge(MakeTypeJitConstants(activation_dt, "ACTIVATION"));+ jit.Merge(MakeActivationJitConstants(params.activations, activation_dt, "_TYPED"));+ jit.Merge(MakeTypeJitConstants(accumulator_dt, "ACCUMULATOR"));++ // Output layout constants+ if (params.outputs[0].GetLayout() == DataLayout::bfyx) {+ auto tile_in_b_pitch = (params.inputs[0].Feature().pitch == 0)+ ? get_input_bf_size(params).second+ : params.inputs[0].Feature().pitch;+ jit.AddConstant(MakeJitConstant("TILE_OUT_F_NUM", params.outputs[0].Y().v));+ jit.AddConstant(MakeJitConstant("TILE_OUT_F_PITCH", params.outputs[0].Y().pitch));+ jit.AddConstant(MakeJitConstant("TILE_IN_B_PITCH", tile_in_b_pitch));+ jit.AddConstant(MakeJitConstant("TILE_OUT_B_PITCH", params.outputs[0].Feature().pitch));+ jit.AddConstant(MakeJitConstant("OUTPUT_3D", true));+ jit.AddConstant(MakeJitConstant("BATCH_SIZE", "(OUTPUT_BATCH_NUM * OUTPUT_FEATURE_NUM)"));+ } else {+ auto tile_in_b_pitch = (params.inputs[0].Batch().pitch == 0)+ ? get_input_bf_size(params).second+ : params.inputs[0].Batch().pitch;+ jit.AddConstant(MakeJitConstant("TILE_OUT_F_NUM", params.outputs[0].Feature().v));+ jit.AddConstant(MakeJitConstant("TILE_OUT_F_PITCH", params.outputs[0].Feature().pitch));+ jit.AddConstant(MakeJitConstant("TILE_IN_B_PITCH", tile_in_b_pitch));+ jit.AddConstant(MakeJitConstant("TILE_OUT_B_PITCH", params.outputs[0].Batch().pitch));+ jit.AddConstant(MakeJitConstant("BATCH_SIZE", "(OUTPUT_BATCH_NUM)"));+ }++ // Fused ops setup+ if (!params.fused_ops.empty()) {+ std::vector<std::string> idx_order_scalar = { "(out_b + bi)", "(out_f + sglid)", "0", "0" };+ std::vector<std::string> idx_order_vec = { "(out_b + bi)", "(out_f + sglid + fi * SIMD)", "0", "0" };+ if (params.outputs[0].GetLayout() == DataLayout::bfyx) {+ idx_order_scalar = { "(out_b + bi) / OUTPUT_FEATURE_NUM", "(out_b + bi) % OUTPUT_FEATURE_NUM", "(out_f + sglid)", "0" };+ idx_order_vec = { "(out_b + bi) / OUTPUT_FEATURE_NUM", "(out_b + bi) % OUTPUT_FEATURE_NUM", "(out_f + sglid + fi * SIMD)", "0" };+ }++ FusedOpsConfiguration conf_scalar = { "_SCALAR", idx_order_scalar, "activated[bi]", activation_dt, 1 };+ FusedOpsConfiguration conf_vec = { "_VEC", idx_order_vec, "activated[bi][fi]", activation_dt, 1 };+ jit.Merge(MakeFusedOpsJitConstants(params, { conf_scalar, conf_vec }));+ }++ return jit;+}++KernelsData FullyConnected_bf_tiled_dyn_b::GetKernelsData(const Params& params) const {+ auto& fc_params = static_cast<const fully_connected_params&>(params);+ auto tparams = GetTuneParams(fc_params);++ // Determine optimal weight layout+ WeightsLayout weights_layout = WeightsLayout::os_iyx_osv16;+ auto output_f = get_output_aligned_bf_size(fc_params, false).second;+ if (fc_params.compressed && fc_params.inputs[0].GetDType() == Datatype::F16+ && (fc_params.weights.GetDType() == WeightsType::INT4 || fc_params.weights.GetDType() == WeightsType::UINT4)) {+ if ((fc_params.weights.GetLayout() == WeightsLayout::oiyx || fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2)+ && is_weight_horizontal(fc_params, output_f)) {+ weights_layout = WeightsLayout::os_is_yx_osv64_isv2;+ } else if ((fc_params.weights.GetLayout() == WeightsLayout::oiyx || fc_params.weights.GetLayout() == WeightsLayout::os_iyx_osv16)+ && is_weight_vertical(fc_params, output_f)) {+ weights_layout = WeightsLayout::os_iyx_osv16;+ } else if (fc_params.weights.GetLayout() == WeightsLayout::oiyx || fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2) {+ weights_layout = WeightsLayout::os_is_yx_osv32_isv2;+ }+ }++ auto kernels_data = GetCommonKernelsData(params,+ fc_params.inputs[0].GetLayout(),+ weights_layout,+ tparams.exec_options,+ -1,+ 0);++ if (!kernels_data.empty()) {+ GetUpdateDispatchDataFunc(kernels_data[0]);+ }++ return kernels_data;+}++} // namespace kernel_selectordiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled_dyn_b.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled_dyn_b.hnew file mode 100644index 00000000000..23cc96a8efa--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled_dyn_b.h@@ -0,0 +1,64 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "fully_connected_kernel_base.h"+#include "fully_connected_kernel_bf_tiled.h"++#include <vector>++namespace kernel_selector {++class FullyConnected_bf_tiled_dyn_b : public FullyConnectedKernelBase {+public:+ using Parent = FullyConnectedKernelBase;+ FullyConnected_bf_tiled_dyn_b();++ KernelsData GetKernelsData(const Params& params) const override;+ KernelsPriority GetKernelsPriority(const Params& params) const override;+ ParamsKey GetSupportedKey() const override;+ DeviceFeaturesKey get_required_device_features_key(const Params& params) const override;++ struct tune_params {+ tune_params(unsigned tile_ofm, unsigned tile_ifm,+ unsigned tile_k, unsigned dispatch_bsv, unsigned dispatch_fsv,+ std::string exec_options)+ : tile_ofm(tile_ofm), tile_ifm(tile_ifm),+ tile_k(tile_k), dispatch_bsv(dispatch_bsv), dispatch_fsv(dispatch_fsv),+ exec_options(exec_options) {}+ tune_params() = default;++ unsigned tile_ofm = 2;+ unsigned tile_ifm = 1;+ unsigned tile_k = 4;+ unsigned dispatch_bsv = 1;+ unsigned dispatch_fsv = 1;+ std::string exec_options;+ };++ // Select optimal TILE_B for the given batch_size.+ // Prefers exact divisor in [8..4]; falls back to 8 with tail handling.+ static size_t SelectTileB(size_t batch_size);++ // Check whether dyn_b is beneficial for the given params (IFM/OFM ratio, !swiglu, INT4 compressed F16).+ // Used by bf_tiled to decide whether to add dyn_b as a sub-kernel.+ static bool IsBeneficial(const fully_connected_params& params);++ // Exposed for bf_tiled integration (sub-kernel creation in GetMultiKernelsData)+ tune_params GetTuneParams(const fully_connected_params& params) const;+ DispatchData SetDefault(const fully_connected_params& params, int autoTuneIndex = -1, int kernel_number = 0) const override;+ JitConstants GetJitConstants(const fully_connected_params& params, const DispatchData& dispatchData) const override;++protected:+ std::vector<FusedOpType> GetSupportedFusedOps() const override {+ return { FusedOpType::ACTIVATION,+ FusedOpType::ELTWISE,+ FusedOpType::QUANTIZE };+ }+ bool Validate(const Params& params) const override;+ void GetUpdateDispatchDataFunc(KernelData& kd) const override;+};++} // namespace kernel_selectordiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_selector.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_selector.cppindex 1646dc7f7bf..df660db2c19 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_selector.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_selector.cpp@@ -20,6 +20,7 @@#include "fully_connected_kernel_imad.h"#include "fully_connected_kernel_fs_byx_fsv32.h"#include "fully_connected_kernel_bf_tiled.h"+#include "fully_connected_kernel_bf_tiled_dyn_b.h"#include "fully_connected_kernel_gemv.h"namespace kernel_selector {@@ -42,6 +43,9 @@ fully_connected_kernel_selector::fully_connected_kernel_selector() {Attach<FullyConnectedKernelIMAD>();Attach<FullyConnected_fs_byx_fsv32>();Attach<FullyConnected_bf_tiled>();+ // dyn_b is integrated into bf_tiled as a runtime-switchable sub-kernel.+ // Standalone is kept at low priority for force_implementations test support.+ Attach<FullyConnected_bf_tiled_dyn_b>();Attach<FullyConnected_GEMV>();}diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cppindex fd5428dbccd..d1ff1116cd2 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp@@ -1570,7 +1570,7 @@ public:auto inst = network->get_primitive("fc_prim");auto impl = inst->get_impl();ASSERT_TRUE(impl != NULL);- ASSERT_EQ(impl->get_kernels().size(), size_t((is_dynamic ? 3 : 2))); // shape-agnostic kernels+ ASSERT_GE(impl->get_kernels().size(), size_t((is_dynamic ? 3 : 2))); // shape-agnostic kernels}network->set_input_data("input", input_mem);@@ -1806,7 +1806,7 @@ public:auto inst = network->get_primitive("fc_prim");auto impl = inst->get_impl();ASSERT_TRUE(impl != NULL);- ASSERT_EQ(impl->get_kernels().size(), 2);+ ASSERT_GE(impl->get_kernels().size(), 2u);}network->set_input_data("input", input_mem);@@ -2121,7 +2121,7 @@ public:if (batch_num == 1) {ASSERT_EQ(fc_kernels.size(), 1);} else if (batch_num > 1) {- ASSERT_EQ(fc_kernels.size(), 2);+ ASSERT_GE(fc_kernels.size(), 2u);} else {ASSERT_TRUE(false);}@@ -2545,7 +2545,7 @@ public:ASSERT_EQ(fc_kernels.size(), 1);} else if (batch_num > 1) {- ASSERT_EQ(fc_kernels.size(), 2);+ ASSERT_GE(fc_kernels.size(), 2u);} else {ASSERT_TRUE(false);}@@ -2693,7 +2693,7 @@ public:ASSERT_EQ(fc_kernels.size(), 1);} else if (batch_num > 1) {- ASSERT_EQ(fc_kernels.size(), 2);+ ASSERT_GE(fc_kernels.size(), 2u);} else {ASSERT_TRUE(false);}@@ -5924,3 +5924,122 @@ TEST(fully_connected_gpu, cm) {// Do not validate output for CM}++struct fc_bf_tiled_dyn_b_accuracy_test : public ::testing::TestWithParam<std::tuple<long int, long int, long int, bool>> {+ tests::random_generator rg;+ void SetUp() override {+ rg = tests::random_generator(GET_SUITE_NAME);+ }++ void test_accuracy(long int batch_num, long int ifm_num, long int ofm_num, bool with_post_op, long int scales_group_size = 128) {+ auto& engine = get_test_engine();++ auto input_mem = engine.allocate_memory({ {batch_num, ifm_num}, data_types::f16, format::bfyx });+ auto weights_mem = engine.allocate_memory({ {ofm_num, ifm_num}, data_types::u4, format::bfyx });+ auto scale_mem = engine.allocate_memory({ {ofm_num, ifm_num / scales_group_size}, data_types::f16, format::bfyx });+ auto dcomp_zp_mem = engine.allocate_memory({ {ofm_num, ifm_num / scales_group_size}, data_types::f16, format::bfyx });++ auto input_data = rg.generate_random_1d<ov::float16>(batch_num * ifm_num, -1.0f, 1.0f);+ set_values(input_mem, input_data);+ auto weights_data = rg.generate_random_1d<uint8_t>(ofm_num * ifm_num / 2, 0, 10);+ set_values(weights_mem, weights_data);+ auto scale_data = rg.generate_random_1d<ov::float16>(ofm_num * ifm_num / scales_group_size, -1.0f, 1.0f);+ set_values(scale_mem, scale_data);+ auto zp_data = rg.generate_random_1d<ov::float16>(ofm_num * ifm_num / scales_group_size, -0.5f, 0.5f);+ set_values(dcomp_zp_mem, zp_data);++ auto dyn_layout = layout{ {-1, ifm_num}, data_types::f16, format::bfyx };++ cldnn::memory::ptr eltw_mem = nullptr;+ if (with_post_op) {+ eltw_mem = engine.allocate_memory({ {batch_num, ofm_num}, data_types::f16, format::bfyx });+ auto eltw_data = rg.generate_random_1d<ov::float16>(batch_num * ofm_num, -1.0f, 1.0f);+ set_values(eltw_mem, eltw_data);+ }++ auto build_topology = [&]() {+ auto fc_prim = fully_connected("fc_prim", input_info("input"), "weights", "", "scale", "dcomp_zp", data_types::f16, 2, 2);+ topology topology(+ input_layout("input", dyn_layout),+ data("weights", weights_mem),+ data("scale", scale_mem),+ data("dcomp_zp", dcomp_zp_mem),+ fc_prim+ );+ if (with_post_op) {+ topology.add(data("eltw_data", eltw_mem));+ topology.add(eltwise("eltw", { input_info("fc_prim"), input_info("eltw_data") }, eltwise_mode::sum));+ }+ return topology;+ };++ auto out_name = with_post_op ? "eltw" : "fc_prim";++ // Reference: bfyx_ref kernel+ auto get_ref_results = [&]() {+ auto topo = build_topology();+ auto config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));+ config.set_user_property(ov::hint::dynamic_quantization_group_size(0));+ ov::intel_gpu::ImplementationDesc fc_impl_desc = { format::bfyx, "fully_connected_gpu_bfyx_ref", impl_types::ocl };+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ {"fc_prim", fc_impl_desc} }));+ network network(engine, topo, config);+ network.set_input_data("input", input_mem);+ auto outputs = network.execute();+ auto output_mem = outputs.at(out_name).get_memory();+ return engine.reinterpret_buffer(*output_mem, outputs.at(out_name).get_layout());+ };++ // Test: dyn_b kernel+ auto get_dyn_b_results = [&]() {+ auto topo = build_topology();+ auto 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_user_property(ov::hint::dynamic_quantization_group_size(0));+ ov::intel_gpu::ImplementationDesc fc_impl_desc = { format::bfyx, "fully_connected_gpu_bf_tiled_dyn_b", impl_types::ocl };+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ {"fc_prim", fc_impl_desc} }));+ network network(engine, topo, config);+ network.set_input_data("input", input_mem);+ auto outputs = network.execute();+ auto output_mem = outputs.at(out_name).get_memory();+ return engine.reinterpret_buffer(*output_mem, outputs.at(out_name).get_layout());+ };++ auto ref_mem = get_ref_results();+ auto test_mem = get_dyn_b_results();++ cldnn::mem_lock<ov::float16> ref_ptr(ref_mem, get_test_stream());+ cldnn::mem_lock<ov::float16> test_ptr(test_mem, get_test_stream());++ ASSERT_EQ(ref_ptr.size(), test_ptr.size());+ for (size_t i = 0; i < ref_ptr.size(); i++) {+ ASSERT_NEAR(float(ref_ptr[i]), float(test_ptr[i]), 9.0f)+ << "Mismatch at i=" << i+ << " ref=" << float(ref_ptr[i])+ << " dyn_b=" << float(test_ptr[i]);+ }+ }+};++TEST_P(fc_bf_tiled_dyn_b_accuracy_test, compare_with_ref) {+ auto [batch_num, ifm_num, ofm_num, with_post_op] = GetParam();+ test_accuracy(batch_num, ifm_num, ofm_num, with_post_op);+}++INSTANTIATE_TEST_SUITE_P(+ fully_connected_bf_tiled_dyn_b_accuracy,+ fc_bf_tiled_dyn_b_accuracy_test,+ ::testing::Values(+ // Small batch, IFM < OFM (up/gate projection pattern)+ std::make_tuple(3L, 256L, 1024L, false),+ // Prime batch with tail, IFM > OFM (down projection pattern)+ std::make_tuple(11L, 1024L, 256L, false),+ // Aligned batch, IFM < OFM+ std::make_tuple(16L, 256L, 1024L, false),+ // Large prime batch, IFM < OFM (multi TILE_B + tail)+ std::make_tuple(29L, 256L, 1024L, false),+ // With post-op eltwise+ std::make_tuple(16L, 256L, 1024L, true)+ )+);