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] Pass HostPayload to microkernel GEMM selection and validate payload on fuse (#39021)
Jade Cho178d03e21043
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cppindex c3213248832..76886afe77a 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cpp@@ -277,6 +277,29 @@ kernels_cache::kernels_cache(engine& engine,, _prog_id(prog_id), batch_headers(std::move(batch_headers)) { }+#ifdef ENABLE_ONEDNN_FOR_GPU+// GRF size in bytes of the device. It is needed to locate the end of the host kernel thread payload when checking that+// the microkernel registers do not overlap it. Listed explicitly (no default) so that a new architecture is flagged by -Wswitch.+static int get_grf_bytes(gpu_arch arch) {+ switch (arch) {+ case gpu_arch::gen9:+ case gpu_arch::gen11:+ case gpu_arch::xe_lp:+ case gpu_arch::xe_hp:+ case gpu_arch::xe_hpg:+ return 32;+ case gpu_arch::xe_hpc:+ case gpu_arch::xe2:+ case gpu_arch::xe3:+ case gpu_arch::xe3p:+ return 64;+ case gpu_arch::unknown:+ break;+ }+ OPENVINO_THROW("[GPU] Unknown GRF size for GPU architecture ", static_cast<int>(arch));+}+#endif // ENABLE_ONEDNN_FOR_GPU+void kernels_cache::build_batch(const batch_program& batch, compiled_kernels& compiled_kernels) {OV_ITT_SCOPED_TASK(ov::intel_gpu::itt::domains::intel_gpu_plugin, "KernelsCache::build_batch");bool dump_sources = batch.dump_custom_program;@@ -336,8 +359,13 @@ void kernels_cache::build_batch(const batch_program& batch, compiled_kernels& coOPENVINO_ASSERT(batch.kernels_counter == 1 && kernels.size() == 1);std::vector<uint8_t> binary = kernels[0]->get_binary();kernels.clear();- // Update binary and rebuild kernel- gemmstone::microkernel::fuse(binary, combined_source.c_str());+ // Update binary and rebuild kernel.+ // Passing grfBytes enables validation of the microkernel registers against the host kernel thread payload;+ // fuse() throws if they overlap. Returns false if the payload could not be checked.+ const int grf_bytes = get_grf_bytes(_device->get_info().arch);+ if (!gemmstone::microkernel::fuse(binary, combined_source.c_str(), grf_bytes)) {+ GPU_DEBUG_LOG << "[GPU] Microkernels were fused, but their registers could not be checked against the host kernel payload" << std::endl;+ }_builder->build_kernels(binary.data(), binary.size(), KernelFormat::NATIVE_BIN, "", kernels);#else // ENABLE_ONEDNN_FOR_GPUOPENVINO_THROW("[GPU] Can't compile kernel w/ microkernels as onednn is not available");diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cppindex 2bc5740c3d3..fb06ffe97b3 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cpp@@ -41,12 +41,16 @@ static size_t get_subgroup_size(gpu_arch arch) {return 8;case gpu_arch::xe2:case gpu_arch::xe3:+ case gpu_arch::xe3p:return 16;default:return 0;}}+// Host payload (cross-thread argument bytes) of the gather_matmul kernels; needs ~144 bytes at least.+constexpr int host_argument_bytes = 256;+gathermatmul_config GatherMatmulMicroGenerator::get_config(const kernel_impl_params& params) {gathermatmul_config cfg;auto desc = params.typed_desc<gather_matmul>();@@ -226,6 +230,7 @@ void GatherMatmulMicroGenerator::init_microkernels(const kernel_impl_params& parhw_info.euCount = device_info.execution_units_count;hw_info.gmdid = device_info.ip_version;hw_info.systolicAvailable = device_info.supports_immad;+ const micro::HostPayload host{static_cast<int>(get_subgroup_size(device_info.arch)), host_argument_bytes};// B (weights): [n_all_experts, N, K] — N=ofm, K=ifmconst auto& weight_shape = params.get_input_layout(gather_matmul::BGMInputIdx::WEIGHT).get_shape();@@ -291,7 +296,7 @@ void GatherMatmulMicroGenerator::init_microkernels(const kernel_impl_params& parGPU_DEBUG_TRACE_DETAIL << "GatherMatmulsizes: m=" << m << " n=" << n << " k=" << k << std::endl;try {gemmstone::SelectionObserver observer = entryObserver;- gemm_bgm = micro::select_gemm_microkernel(opts, hw_info, sizes, problem, &observer);+ gemm_bgm = micro::select_gemm_microkernel(opts, host, hw_info, sizes, problem, &observer);} catch (const std::runtime_error& ex) {OPENVINO_THROW("Can't create GatherMatmul micro kernel: ", ex.what());}diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cppindex 627dceb9202..3737cbceee5 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cpp@@ -34,6 +34,7 @@ static size_t get_subgroup_size(gpu_arch arch) {return 8;case gpu_arch::xe2:case gpu_arch::xe3:+ case gpu_arch::xe3p:return 16;default:return 0;diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cppindex bddb02a5ddb..13930a11b7c 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cpp@@ -29,6 +29,9 @@ static size_t get_subgroup_size(gpu_arch arch) {}}+// Host payload (cross-thread argument bytes) of the moe_3gemm micro kernels; needs ~112 bytes at least.+constexpr int host_argument_bytes = 256;+JitConstants MoE3GemmMicroGenerator::get_jit_constants(const kernel_impl_params& params, const micro::Package& moe_gemm, const moe_3gemm_config& cfg) const {const auto& device_info = params.get_device_info();const size_t subgroup_size = get_subgroup_size(device_info.arch);@@ -238,6 +241,7 @@ void MoE3GemmMicroGenerator::init_microkernels(const kernel_impl_params& params,hw_info.euCount = device_info.execution_units_count;hw_info.gmdid = device_info.ip_version;hw_info.systolicAvailable = device_info.supports_immad;+ const micro::HostPayload host{static_cast<int>(get_subgroup_size(device_info.arch)), host_argument_bytes};// weight layout example: u4:bfyx:4x3072x8x128:nopadconst auto& weight_shape = params.get_input_layout(wei_idx).get_shape();@@ -308,7 +312,7 @@ void MoE3GemmMicroGenerator::init_microkernels(const kernel_impl_params& params,GPU_DEBUG_TRACE_DETAIL << "sizes to select gemm : m : " << m << " n : " << n << " k : " << k << std::endl;try {/* Ask microkernel provider for microkernel */- micro::Package pkg = micro::select_gemm_microkernel(opts_moe, hw_info, sizes, problem_moe);+ micro::Package pkg = micro::select_gemm_microkernel(opts_moe, host, hw_info, sizes, problem_moe);s_gemm_cache.emplace(key, pkg);gemm_moe = std::move(pkg);GPU_DEBUG_TRACE_DETAIL << "MoE3GemmMicroGenerator::init_microkernels: create and cache new micro kernel" << std::endl;diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cppindex 164a041b8e4..e84cf07ad21 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp@@ -35,6 +35,9 @@ size_t get_subgroup_size(gpu_arch arch) {}}+// Host payload (cross-thread argument bytes) of the sdpa_micro kernel; needs ~140 bytes at least.+constexpr int host_argument_bytes = 256;+inline size_t get_d_max(size_t head_size) {for (size_t i = 32; i <= 1024; i *= 2) {if (head_size <= i) {@@ -1908,6 +1911,9 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,hw_info.gmdid = device_info.ip_version;hw_info.systolicAvailable = device_info.supports_immad;+ /* Host kernel facts: the microkernel registers must not overlap the host thread payload */+ const micro::HostPayload host{static_cast<int>(get_subgroup_size(device_info.arch)), host_argument_bytes};+/* Set up GEMMProblem structure for first GEMM: K^T * Q */micro::GEMMProblem problem;problem.Ta_ext = convert_type(K.data_type);@@ -2057,7 +2063,7 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,/* Ask microkernel provider for microkernel */try {- gemm_kq = micro::select_gemm_microkernel(opts_kq, hw_info, sizes, problem_kq, reqs_kq);+ gemm_kq = micro::select_gemm_microkernel(opts_kq, host, hw_info, sizes, problem_kq, reqs_kq);} catch (const std::runtime_error& ex) {GPU_DEBUG_TRACE_DETAIL << "Can't create KQ sdpa_micro kernel: " << ex.what() << "\n";throw;@@ -2083,7 +2089,7 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,/* Ask microkernel provider for microkernel */try {- gemm_kcq = micro::select_gemm_microkernel(opts_kq, hw_info, sizes, problem_kcq, reqs_kq);+ gemm_kcq = micro::select_gemm_microkernel(opts_kq, host, hw_info, sizes, problem_kcq, reqs_kq);} catch (const std::runtime_error& ex) {GPU_DEBUG_TRACE_DETAIL << "Can't create KcQ sdpa_micro kernel: " << ex.what() << "\n";throw;@@ -2215,7 +2221,7 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,};/* Ask microkernel provider for microkernel */try {- gemm_vs = micro::select_gemm_microkernel(opts_vs, hw_info, sizes, problem_vs, reqs_vs, adjust_vs);+ gemm_vs = micro::select_gemm_microkernel(opts_vs, host, hw_info, sizes, problem_vs, reqs_vs, adjust_vs);} catch (const std::runtime_error& ex) {GPU_DEBUG_TRACE_DETAIL << "Can't create VS sdpa_micro kernel: " << ex.what() << "\n";throw;@@ -2243,7 +2249,7 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,/* Ask microkernel provider for microkernel */try {- gemm_vcs = micro::select_gemm_microkernel(opts_vs, hw_info, sizes, problem_vcs, reqs_vs, adjust_vs);+ gemm_vcs = micro::select_gemm_microkernel(opts_vs, host, hw_info, sizes, problem_vcs, reqs_vs, adjust_vs);} catch (const std::runtime_error& ex) {GPU_DEBUG_TRACE_DETAIL << "Can't create VcS sdpa_micro kernel: " << ex.what() << "\n";throw;diff --git a/src/plugins/intel_gpu/src/kernel_selector/micro_utils.hpp b/src/plugins/intel_gpu/src/kernel_selector/micro_utils.hppindex 88d462e4a7c..9cc665bce74 100644--- a/src/plugins/intel_gpu/src/kernel_selector/micro_utils.hpp+++ b/src/plugins/intel_gpu/src/kernel_selector/micro_utils.hpp@@ -27,6 +27,7 @@ namespace micro {using Package = gemmstone::microkernel::Package;using HWInformation = gemmstone::microkernel::HWInformation;+using HostPayload = gemmstone::microkernel::HostPayload;using GEMMProblem = gemmstone::GEMMProblem;using ABOffset = gemmstone::ABOffset;using GEMMStrategy = gemmstone::GEMMStrategy;@@ -73,14 +74,25 @@ struct MicroKernelPackage {}};-inline Package select_gemm_microkernel(GEMMOptions &options, HWInformation hw_info, SizeParams sizes, const GEMMProblem &problem,- const std::vector<StrategyRequirement> &reqs = std::vector<StrategyRequirement>(),- void (*strategyAdjuster)(GEMMStrategy &strategy) = nullptr, gemmstone::SelectionObserver *observer = nullptr) {- return gemmstone::microkernel::selectGEMM(options, hw_info, sizes, problem, reqs, strategyAdjuster);+// `host` describes the host OpenCL kernel (SIMD width and cross-thread argument payload bytes). The microkernel+// registers are placed above that payload, and fuse() validates them against the actual thread payload.+inline Package select_gemm_microkernel(GEMMOptions& options,+ HostPayload host,+ HWInformation hw_info,+ SizeParams sizes,+ const GEMMProblem& problem,+ const std::vector<StrategyRequirement>& reqs = std::vector<StrategyRequirement>(),+ void (*strategyAdjuster)(GEMMStrategy& strategy) = nullptr,+ gemmstone::SelectionObserver* observer = nullptr) {+ return gemmstone::microkernel::selectGEMM(options, host, hw_info, sizes, problem, reqs, strategyAdjuster, observer);}-inline Package select_gemm_microkernel(GEMMOptions &options, HWInformation hw_info, SizeParams sizes, const GEMMProblem &problem,- gemmstone::SelectionObserver *observer) {- return gemmstone::microkernel::selectGEMM(options, hw_info, sizes, problem, {}, nullptr);+inline Package select_gemm_microkernel(GEMMOptions& options,+ HostPayload host,+ HWInformation hw_info,+ SizeParams sizes,+ const GEMMProblem& problem,+ gemmstone::SelectionObserver* observer) {+ return gemmstone::microkernel::selectGEMM(options, host, hw_info, sizes, problem, {}, nullptr, observer);}static inline int alignment_for_ld(int ld) {