ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / mmvq.hpp
| 1 | // |
| 2 | // MIT license |
| 3 | // Copyright (C) 2024 Intel Corporation |
| 4 | // SPDX-License-Identifier: MIT |
| 5 | // |
| 6 | |
| 7 | // |
| 8 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 9 | // See https://llvm.org/LICENSE.txt for license information. |
| 10 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 11 | // |
| 12 | |
| 13 | #ifndef GGML_SYCL_MMVQ_HPP |
| 14 | #define GGML_SYCL_MMVQ_HPP |
| 15 | |
| 16 | #include "common.hpp" |
| 17 | |
| 18 | // q5_K multi-column MMVQ shares weights across columns, pairs rows and fuses gate/up in the reorder |
| 19 | // layout: faster on Xe2 (BMG), so untested archs keep the per-column kernel |
| 20 | inline bool ggml_sycl_q5_k_mmvq_reuse(int device) { |
| 21 | const gpu_arch arch = ggml_sycl_info().devices[device].hw_info.arch; |
| 22 | return arch == gpu_arch::intel_gpu_bmg_g21 || arch == gpu_arch::intel_gpu_bmg_g31; |
| 23 | } |
| 24 | |
| 25 | void ggml_sycl_op_mul_mat_vec_q( |
| 26 | ggml_backend_sycl_context & ctx, |
| 27 | const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst, |
| 28 | const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i, |
| 29 | float *dst_dd_i, const int64_t row_low, const int64_t row_high, |
| 30 | const int64_t src1_ncols, const int64_t src1_padded_row_size, |
| 31 | const dpct::queue_ptr &stream); |
| 32 | |
| 33 | // Requires standard (non-reorder) block layout for src0. |
| 34 | // Returns false if src0_type isn't handled; caller should fall back. |
| 35 | bool ggml_sycl_mul_mat_vec_q_id( |
| 36 | enum ggml_type src0_type, |
| 37 | const void * vx_base, // start of stacked expert weights |
| 38 | const void * vy, // pre-quantized src1 (Q8_1) |
| 39 | const int32_t * ids_dev, // device-side int32, length n_experts_used |
| 40 | float * dst_base, |
| 41 | int ncols, |
| 42 | int nrows, |
| 43 | int n_experts_used, |
| 44 | size_t expert_weight_stride, // bytes between experts in vx_base |
| 45 | size_t dst_row_stride, // bytes between dst rows |
| 46 | size_t src1_row_stride, // 0 = shared src1, else per-expert stride in bytes |
| 47 | dpct::queue_ptr stream); |
| 48 | |
| 49 | // Reorder (SoA) variant of the fused MoE expert GEMV. |
| 50 | // vx_base: each expert slice (stride expert_weight_stride == src0->nb[2]) is a self-contained reorder/SoA layout. |
| 51 | // vy: src1 quantized with quantize_and_reorder_q8_1_soa (per-row SoA). Returns false if src0_type isn't handled. |
| 52 | bool ggml_sycl_mul_mat_vec_q_id_reorder( |
| 53 | enum ggml_type src0_type, |
| 54 | const void * vx_base, |
| 55 | const void * vy, |
| 56 | const int32_t * ids_dev, |
| 57 | float * dst_base, |
| 58 | int ncols, |
| 59 | int nrows, |
| 60 | int n_experts_used, |
| 61 | size_t expert_weight_stride, |
| 62 | size_t dst_row_stride, |
| 63 | size_t src1_row_stride, |
| 64 | dpct::queue_ptr stream); |
| 65 | |
| 66 | // Fused dense-FFN GEMV: writes glu(gate . y, up . y) instead of the two mat-vec results. |
| 67 | // vx / vgate must share shape, stride and reorder layout. Returns false if unhandled. |
| 68 | bool ggml_sycl_mul_mat_vec_q_glu_reorder( |
| 69 | enum ggml_type src0_type, |
| 70 | enum ggml_glu_op glu_op, |
| 71 | const void * vx, |
| 72 | const void * vgate, |
| 73 | const void * vy, |
| 74 | float * dst, |
| 75 | int ncols, // K, shared by both weights |
| 76 | int nrows, // output rows, i.e. weight ne[1] |
| 77 | int ncols_dst, // activation columns, 1..MMVQ_MAX_BATCH_SIZE |
| 78 | int stride_col_y_bytes, // bytes between activation columns in vy |
| 79 | int stride_col_dst, // floats between output columns in dst |
| 80 | dpct::queue_ptr stream); |
| 81 | |
| 82 | |
| 83 | // Fused dense-FFN GEMV on the XMX engines for reordered weights of one type, up * act(gate) for up to |
| 84 | // GGML_SYCL_XMX_GLU_MAX_COLS (at most) activation columns. Returns false if it does not apply, the caller falls back. |
| 85 | // vy: src1 quantized with quantize_and_reorder_q8_1_soa. |
| 86 | bool ggml_sycl_mul_mat_vec_q_glu_xmx( |
| 87 | int device, |
| 88 | enum ggml_type src0_type, |
| 89 | enum ggml_glu_op glu_op, |
| 90 | const void * vx, // up weights |
| 91 | const void * vgate, // gate weights |
| 92 | const void * vy, |
| 93 | float * dst, |
| 94 | int ncols, // K, shared by both weights |
| 95 | int nrows, // output rows, i.e. weight ne[1] |
| 96 | int ncols_dst, // activation columns |
| 97 | int stride_col_y_bytes, // bytes between activation columns in vy |
| 98 | int stride_col_dst, // floats between output columns in dst |
| 99 | dpct::queue_ptr stream); |
| 100 | |
| 101 | // Fused dense-FFN GEMV + GLU over the standard (non-reorder) layout; the gate and up |
| 102 | // weights may carry different block types (q5_K / iq4_xs, mixed included). |
| 103 | // vy: src1 quantized with plain quantize_q8_1 (padded rows). stride_col_y is in |
| 104 | // block_q8_1 units. Returns false if the pair or batch is unhandled; caller falls back. |
| 105 | bool ggml_sycl_mul_mat_vec_q_glu_plain( |
| 106 | enum ggml_type gate_type, |
| 107 | enum ggml_type up_type, |
| 108 | enum ggml_glu_op glu_op, |
| 109 | const void * vgate, |
| 110 | const void * vup, |
| 111 | const void * vy, |
| 112 | float * dst, |
| 113 | int ncols, // K, shared by both weights |
| 114 | int nrows, // output rows, i.e. weight ne[1] |
| 115 | int ncols_dst, // activation columns, 1..MMVQ_MAX_BATCH_SIZE |
| 116 | int stride_col_y, // block_q8_1 units between activation columns |
| 117 | int stride_col_dst, // floats between output columns in dst |
| 118 | dpct::queue_ptr stream); |
| 119 | |
| 120 | #endif // GGML_SYCL_MMVQ_HPP |