# --- T2-COPYRIGHT-BEGIN --- # t2/package/*/llama-cpp/opencl-amd.patch # Copyright (C) 2025 - 2026 The T2 SDE Project # SPDX-License-Identifier: GPL-2.0 or patched project license # --- T2-COPYRIGHT-END --- --- llama.cpp.git/./ggml/src/ggml-opencl/ggml-opencl.cpp.orig 2000-01-01 01:00:00.000000000 +0100 +++ llama.cpp.git/./ggml/src/ggml-opencl/ggml-opencl.cpp 2026-08-26 14:59:48.054440319 +0200 @@ -111,6 +111,7 @@ enum GPU_FAMILY { ADRENO, + AMD, INTEL, UNKNOWN, }; @@ -3221,7 +3222,7 @@ #endif const int gdn_sizes[4] = { 16, 32, 64, 128 }; - const int sg_size = backend_ctx->gpu_family == GPU_FAMILY::ADRENO ? 64 : backend_ctx->gpu_family == GPU_FAMILY::INTEL ? 32 : -1; + const int sg_size = backend_ctx->gpu_family == GPU_FAMILY::ADRENO ? 64 : (backend_ctx->gpu_family == GPU_FAMILY::INTEL || backend_ctx->gpu_family == GPU_FAMILY::AMD) ? 32 : -1; if (sg_size < 0) { GGML_LOG_ERROR("Unsupported GPU Family: only Adreno and Intel are supported.\n"); exit(1); @@ -5222,7 +5223,7 @@ cl_kernel k_c8 = clCreateKernel(prog_c8, "flash_attn_f32_f16_q1_vec_mq_split_c8", &err); if (err == CL_SUCCESS) { // WG = MQ_NSG(2) × Q1_WG_SIZE(=FA_SG): 128 Adreno (64), 64 Intel (32). - const size_t c8_ns2_wg = backend_ctx->gpu_family == INTEL ? 64 : 128; + const size_t c8_ns2_wg = (backend_ctx->gpu_family == INTEL || backend_ctx->gpu_family == AMD) ? 64 : 128; if (ggml_opencl_fa_kernel_fits_wg(backend_ctx, k_c8, c8_ns2_wg, "flash_attn_f32_f16_q1_vec_mq_split_c8 (ns2)", dk, dv)) { backend_ctx->fa.f32_f16_q1_vec_mq_split_c8_ns2[{dk, dv}] = k_c8; @@ -5839,6 +5840,8 @@ } } else if (strstr(dev_ctx->device_name.c_str(), "Intel")) { dev_ctx->gpu_family = GPU_FAMILY::INTEL; + } else if (strstr(dev_ctx->device_name.c_str(), "AMD")) { + dev_ctx->gpu_family = GPU_FAMILY::AMD; } else { GGML_LOG_WARN("ggml_opencl: unsupported GPU '%s'.\n", dev_ctx->device_name.c_str()); dev_ctx->gpu_family = GPU_FAMILY::UNKNOWN; @@ -13306,6 +13309,8 @@ // sizeof(size_t), &sgs, NULL)); if (backend_ctx->gpu_family == ADRENO) { sgs = 64; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 64; } else if (backend_ctx->gpu_family == INTEL) { sgs = 32; } else { @@ -13393,6 +13398,8 @@ size_t sgs; if (backend_ctx->gpu_family == ADRENO) { sgs = 64; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 64; } else if (backend_ctx->gpu_family == INTEL) { sgs = 32; } else { @@ -13474,6 +13481,7 @@ size_t sgs; if (backend_ctx->gpu_family == ADRENO) sgs = 64; + else if (backend_ctx->gpu_family == AMD) sgs = 64; else if (backend_ctx->gpu_family == INTEL) sgs = 32; else GGML_ASSERT(false && "Unsupported GPU"); @@ -13605,6 +13613,8 @@ size_t sgs = 64; if (backend_ctx->gpu_family == ADRENO) { sgs = 64; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 64; } else if (backend_ctx->gpu_family == INTEL) { sgs = 32; } else { @@ -20106,6 +20116,11 @@ nth1 = 1; kernel = backend_ctx->kernel_mul_mat_q4_0_f32_1d_16x_flat; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 1; + + kernel = backend_ctx->kernel_mul_mat_q4_0_f32_1d_16x_flat; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 1; @@ -20145,6 +20160,11 @@ global_work_size[0] = (size_t)(ne01 + 15)/16*nth0; global_work_size[1] = (size_t)ne11*nth1; global_work_size[2] = (size_t)ne12*ne13; + } else if (backend_ctx->gpu_family == AMD) { + // Set global size for AMD. It uses 16x output values. + global_work_size[0] = (size_t)(ne01 + 15)/16*nth0; + global_work_size[1] = (size_t)ne11*nth1; + global_work_size[2] = (size_t)ne12*ne13; } backend_ctx->enqueue_ndrange_kernel(kernel, 3, global_work_size, local_work_size, dst); @@ -20166,6 +20186,9 @@ if (backend_ctx->gpu_family == INTEL) { nth0 = 32; nth1 = 1; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 32; + nth1 = 1; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 1; @@ -20203,6 +20226,9 @@ if (backend_ctx->gpu_family == INTEL) { nth0 = 32; nth1 = 1; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 32; + nth1 = 1; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 1; @@ -20395,6 +20421,12 @@ kernel = backend_ctx->kernel_mul_mat_q4_0_f32_8x_flat; ndst = 8; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 1; + + kernel = backend_ctx->kernel_mul_mat_q4_0_f32_8x_flat; + ndst = 8; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 1; @@ -20431,6 +20463,16 @@ kernel = backend_ctx->kernel_mul_mat_q4_0_f32; ndst = 4; + } else if (backend_ctx->gpu_family == AMD) { + // Use 1D local size. Each workgroup is a SIMD group. Each SIMD + // group produces N_DST (4 for Q4_0 kernel) values in the result. + // The number of workgroups on dim 0 (the leading dimension) is + // the nearest multiple of 4 that covers ne0 (equals ne01). + nth0 = 16; + nth1 = 1; + + kernel = backend_ctx->kernel_mul_mat_q4_0_f32; + ndst = 4; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 1; @@ -20665,6 +20707,10 @@ nth0 = 16; nth1 = 2; ndst = nth1*4; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 2; + ndst = nth1*4; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 2; @@ -20702,6 +20748,10 @@ nth0 = 16; nth1 = 2; ndst = nth1*4; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 2; + ndst = nth1*4; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 2; @@ -20955,6 +21005,10 @@ nth0 = 16; nth1 = 2; ndst = 4; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 2; + ndst = 4; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 2; @@ -20993,6 +21047,10 @@ nth0 = 16; nth1 = 2; ndst = 1; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 2; + ndst = 1; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 2; @@ -21029,6 +21087,12 @@ ndst = nth1*2; q = extra0_mxfp4->q; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 2; + ndst = nth1*2; + + q = extra0_mxfp4->q; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 2; @@ -21064,6 +21128,10 @@ nth0 = 16; nth1 = 2; ndst = nth1*2; + } else if (backend_ctx->gpu_family == AMD) { + nth0 = 16; + nth1 = 2; + ndst = nth1*2; } else if (backend_ctx->gpu_family == ADRENO) { nth0 = 64; nth1 = 2; @@ -21692,6 +21760,10 @@ sgs = 16; nsg = 1; ndst = 8; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 16; + nsg = 1; + ndst = 8; } else if (backend_ctx->gpu_family == ADRENO) { sgs = 64; nsg = 1; @@ -22531,6 +22603,10 @@ sgs = 16; nsg = 2; ndst = 4; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 16; + nsg = 2; + ndst = 4; } else if (backend_ctx->gpu_family == ADRENO) { sgs = 64; nsg = 2; @@ -22567,6 +22643,10 @@ sgs = 16; nsg = 2; ndst = 4; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 16; + nsg = 2; + ndst = 4; } else if (backend_ctx->gpu_family == ADRENO) { sgs = 64; nsg = 2; @@ -23643,6 +23723,12 @@ ndst = 2; q = extra0_mxfp4->q; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 16; + nsg = 2; + ndst = 2; + + q = extra0_mxfp4->q; } else if (backend_ctx->gpu_family == ADRENO) { sgs = 64; nsg = 1; @@ -23684,6 +23770,10 @@ sgs = 16; nsg = 2; ndst = 2; + } else if (backend_ctx->gpu_family == AMD) { + sgs = 16; + nsg = 2; + ndst = 2; } else if (backend_ctx->gpu_family == ADRENO) { sgs = 64; nsg = 2; @@ -24162,6 +24252,10 @@ // This is the same as the initial value. nth = MIN(32, ne00); } + else if (backend_ctx->gpu_family == AMD) { + // This is the same as the initial value. + nth = MIN(32, ne00); + } else if (backend_ctx->gpu_family == ADRENO) { nth = 64; } else {