ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / fused-gemm.hpp
| 1 | #ifndef GGML_SYCL_FUSED_GEMM_HPP |
| 2 | #define GGML_SYCL_FUSED_GEMM_HPP |
| 3 | |
| 4 | #include "common.hpp" |
| 5 | |
| 6 | |
| 7 | // Shape and type gates for the kernels below. Device capability is separate: it needs a queue to ask. |
| 8 | static constexpr int GGML_SYCL_FG_MAX_N = 64; // widest N taken; each shape covers it in BN-wide tiles |
| 9 | |
| 10 | // weight formats the fused A stage decodes; K must cover whole stored blocks |
| 11 | constexpr bool ggml_sycl_fused_dequant_gemm_type_ok(ggml_type src0_type, int64_t K) { |
| 12 | // iq4_nl and q8_0 store 32 values per block; every other format here is a 256-value superblock |
| 13 | // that the A stage walks in steps of 32, so K must cover whole superblocks. |
| 14 | if (src0_type == GGML_TYPE_IQ4_NL || src0_type == GGML_TYPE_Q8_0) { |
| 15 | return K % 32 == 0; |
| 16 | } |
| 17 | const bool superblock = |
| 18 | src0_type == GGML_TYPE_Q4_K || |
| 19 | src0_type == GGML_TYPE_Q5_K || |
| 20 | src0_type == GGML_TYPE_Q6_K || |
| 21 | src0_type == GGML_TYPE_IQ3_S || |
| 22 | src0_type == GGML_TYPE_IQ4_XS || |
| 23 | src0_type == GGML_TYPE_IQ3_XXS || |
| 24 | src0_type == GGML_TYPE_IQ2_XXS || |
| 25 | src0_type == GGML_TYPE_IQ2_XS || |
| 26 | src0_type == GGML_TYPE_IQ2_S || |
| 27 | src0_type == GGML_TYPE_IQ1_S || |
| 28 | src0_type == GGML_TYPE_IQ1_M; |
| 29 | return superblock && QK_K == 256 && K % QK_K == 0; |
| 30 | } |
| 31 | |
| 32 | constexpr bool ggml_sycl_fused_dequant_gemm_shape_ok(ggml_type src0_type, int64_t M, int64_t N, int64_t K, |
| 33 | int64_t ldd) { |
| 34 | return ggml_sycl_fused_dequant_gemm_type_ok(src0_type, K) && M > 0 && N > 0 && K > 0 && |
| 35 | N <= GGML_SYCL_FG_MAX_N && |
| 36 | M <= INT32_MAX && N <= INT32_MAX && K <= INT32_MAX && ldd <= INT32_MAX; |
| 37 | } |
| 38 | |
| 39 | // grouped variant: the per-expert fused kernel is only worth it while each expert is narrow, |
| 40 | // so wider average slices are left to the per-expert library GEMM loop |
| 41 | constexpr bool ggml_sycl_grouped_dequant_gemm_shape_ok(ggml_type src0_type, int64_t M, int64_t K, |
| 42 | int64_t total_rows, int64_t n_active) { |
| 43 | return ggml_sycl_fused_dequant_gemm_shape_ok(src0_type, M, 1, K, M) && total_rows > 0 && |
| 44 | total_rows <= INT32_MAX && total_rows <= n_active * GGML_SYCL_FG_MAX_N; |
| 45 | } |
| 46 | |
| 47 | // Runtime type gate, kept out of the constexpr predicates above so those stay pure. |
| 48 | inline bool ggml_sycl_xmx_gather_type_enabled(ggml_type src0_type) { |
| 49 | switch (src0_type) { |
| 50 | case GGML_TYPE_IQ4_NL: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ4_NL ) != 0; |
| 51 | case GGML_TYPE_IQ3_S: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ3_S ) != 0; |
| 52 | case GGML_TYPE_IQ4_XS: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ4_XS ) != 0; |
| 53 | case GGML_TYPE_IQ3_XXS: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ3_XXS ) != 0; |
| 54 | case GGML_TYPE_IQ2_XXS: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ2_XXS ) != 0; |
| 55 | case GGML_TYPE_IQ2_XS: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ2_XS ) != 0; |
| 56 | case GGML_TYPE_IQ2_S: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ2_S ) != 0; |
| 57 | case GGML_TYPE_IQ1_S: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ1_S ) != 0; |
| 58 | case GGML_TYPE_IQ1_M: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_IQ1_M ) != 0; |
| 59 | case GGML_TYPE_Q8_0: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_Q8_0 ) != 0; |
| 60 | case GGML_TYPE_Q4_K: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_Q4_K ) != 0; |
| 61 | case GGML_TYPE_Q5_K: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_Q5_K ) != 0; |
| 62 | case GGML_TYPE_Q6_K: return (g_ggml_sycl_xmx_gather_types & GGML_SYCL_XMX_GATHER_Q6_K ) != 0; |
| 63 | default: return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // dst[n*ldd + m] = sum_k dequant(src0)[m*K + k] * src1[n*K + k], src1 is F32, F16 or BF16. |
| 68 | // The XMX combination is picked per call from the src1 type and its precision request src1_prec |
| 69 | // (op_params[3], [TAG_GGML_PREC]); the accumulator is f32, which meets any request. |
| 70 | // q8_0 and the k-quants are not handled here, only in the grouped path below. |
| 71 | // Returns false when the case is not handled (type, device, precision, or shape). |
| 72 | bool ggml_sycl_fused_dequant_gemm(ggml_type src0_type, bool src0_reordered, const void * src0, const void * src1, ggml_type src1_type, |
| 73 | int32_t src1_prec, float * dst, int64_t M, int64_t N, int64_t K, int64_t ldd, |
| 74 | ggml_sycl_pool & pool, dpct::queue_ptr stream); |
| 75 | |
| 76 | // One launch for every expert of a MUL_MAT_ID: rows of src1/dst are grouped by expert, expert e |
| 77 | // owns rows [expert_row_offsets[e], expert_row_offsets[e+1]) and reads its weights at |
| 78 | // src0_base + e*expert_stride. tiles is host scratch that must stay alive until the queue drains. |
| 79 | // reordered: each expert slice is in the reorder (SoA) layout of reorder_qw(). |
| 80 | // dst[n*M + m] = sum_k dequant(src0_e)[m*K + k] * src1[n*K + k] |
| 81 | // Returns false when the case is not handled (type, layout, device, precision, or shape). |
| 82 | bool ggml_sycl_grouped_dequant_gemm(ggml_type src0_type, bool reordered, const void * src0_base, size_t expert_stride, |
| 83 | const float * src1, int32_t src1_prec, float * dst, |
| 84 | const int64_t * expert_row_offsets, int64_t n_as, int64_t M, int64_t K, |
| 85 | int64_t total_rows, std::vector<ggml_sycl_gg_tile> & tiles, |
| 86 | ggml_sycl_pool & pool, dpct::queue_ptr stream); |
| 87 | |
| 88 | #endif // GGML_SYCL_FUSED_GEMM_HPP |