GEMM kernels with FP16 (half-precision) weights. More...
#include <stdint.h>#include <stddef.h>#include <stdbool.h>#include "ckernel_quant.h"#include "ckernel_engine.h"#include "ck_threadpool.h"#include "ggml_runtime_compat.h"#include <dlfcn.h>#include <stdlib.h>#include <string.h>Go to the source code of this file.
Macros | |
| #define | _GNU_SOURCE |
| #define | fp16_to_fp32(x) ggml_fp16_to_fp32(x) |
| #define | fp32_to_fp16(x) ggml_fp32_to_fp16(x) |
Typedefs | |
| typedef void(* | ck_f16_ggml_build_forward_expand_fn) (struct ggml_cgraph *, struct ggml_tensor *) |
| typedef void(* | ck_f16_ggml_cpu_init_fn) (void) |
| typedef void(* | ck_f16_ggml_free_fn) (struct ggml_context *) |
| typedef float *(* | ck_f16_ggml_get_data_f32_fn) (const struct ggml_tensor *) |
| typedef void *(* | ck_f16_ggml_get_data_fn) (const struct ggml_tensor *) |
| typedef enum ggml_status(* | ck_f16_ggml_graph_compute_with_ctx_fn) (struct ggml_context *, struct ggml_cgraph *, int) |
| typedef struct ggml_context *(* | ck_f16_ggml_init_fn) (struct ggml_init_params) |
| typedef struct ggml_tensor *(* | ck_f16_ggml_mul_mat_fn) (struct ggml_context *, struct ggml_tensor *, struct ggml_tensor *) |
| typedef struct ggml_cgraph *(* | ck_f16_ggml_new_graph_fn) (struct ggml_context *) |
| typedef struct ggml_tensor *(* | ck_f16_ggml_new_tensor_2d_fn) (struct ggml_context *, enum ggml_type, int64_t, int64_t) |
Functions | |
| static float | ck_dot_f16_f16_local (const uint16_t *w, const uint16_t *x, int k) |
| static ck_f16_ggml_build_forward_expand_fn | ck_f16_resolve_ggml_build_forward_expand (void) |
| static ck_f16_ggml_cpu_init_fn | ck_f16_resolve_ggml_cpu_init (void) |
| static ck_f16_ggml_free_fn | ck_f16_resolve_ggml_free (void) |
| static ck_f16_ggml_get_data_fn | ck_f16_resolve_ggml_get_data (void) |
| static ck_f16_ggml_get_data_f32_fn | ck_f16_resolve_ggml_get_data_f32 (void) |
| static ck_f16_ggml_graph_compute_with_ctx_fn | ck_f16_resolve_ggml_graph_compute_with_ctx (void) |
| static ck_f16_ggml_init_fn | ck_f16_resolve_ggml_init (void) |
| static ck_f16_ggml_mul_mat_fn | ck_f16_resolve_ggml_mul_mat (void) |
| static ck_f16_ggml_new_graph_fn | ck_f16_resolve_ggml_new_graph (void) |
| static ck_f16_ggml_new_tensor_2d_fn | ck_f16_resolve_ggml_new_tensor_2d (void) |
| static void | ck_f32_to_f16_row_local (uint16_t *dst, const float *src, int n) |
| static void | ck_gemm_f16_input_fp16_work (int ith, int nth, void *opaque) |
| static int | ck_gemm_f16_pick_active_threads (const ck_threadpool_t *pool, int M, int N, int K) |
| static int | ck_gemm_f16_threadpool_enabled (int M, int N, int K) |
| int | ck_gemm_nt_f16_ggml_oracle (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| int | ck_gemm_nt_f16_simd_lanes (void) |
| void | convert_f16_to_f32 (float *dst, const uint16_t *src, size_t count) |
| Convert FP16 tensor to FP32. | |
| void | convert_f32_to_f16 (uint16_t *dst, const float *src, size_t count) |
| Convert FP32 tensor to FP16. | |
| float | dot_f16 (const uint16_t *w_f16, const float *x, int K) |
| void | gemm_f16 (float *Y, const uint16_t *W, const float *X, int M, int N, int K) |
| Auto-dispatch GEMM based on available SIMD. | |
| void | gemm_f16_backward (float *dX, const uint16_t *W, const float *dY, int M, int N, int K) |
| Batched backward pass. | |
| static void | gemm_f16_input_fp16_ref (float *Y, const uint16_t *W, const float *X, int M, int N, int K) |
| static int | gemm_f16_input_fp16_threadpool (float *Y, const uint16_t *W, const float *X, int M, int N, int K) |
| void | gemm_f16_ref (float *Y, const uint16_t *W, const float *X, int M, int N, int K) |
| Matrix-matrix multiply with FP16 weights (scalar reference) | |
| void | gemm_nt_f16 (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| NT GEMM wrapper for FP16 weights with the engine's standard ABI. | |
| void | gemm_nt_f16_clipped (const float *A, const void *B, const float *bias, const float *input_min, const float *input_max, const float *output_min, const float *output_max, float *C, int M, int N, int K) |
| static int | gemm_nt_f16_ggml_strict (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemv_f16 (float *y, const uint16_t *W, const float *x, int M, int K) |
| Auto-dispatch GEMV based on available SIMD. | |
| void | gemv_f16_backward (float *dX, const uint16_t *W, const float *dY, int M, int K) |
| Auto-dispatch backward. | |
| void | gemv_f16_backward_ref (float *dX, const uint16_t *W, const float *dY, int M, int K) |
| Backward pass: compute input gradient (scalar reference) | |
| void | gemv_f16_ref (float *y, const uint16_t *W, const float *x, int M, int K) |
| Matrix-vector multiply with FP16 weights (scalar reference) | |
GEMM kernels with FP16 (half-precision) weights.
After changes: make test && make llamacpp-parity-full
Implements matrix multiplication where:
Used for multimodal projection layers (mmproj-*.gguf files).
Definition in file gemm_kernels_f16.c.
| #define _GNU_SOURCE |
Definition at line 23 of file gemm_kernels_f16.c.
| #define fp16_to_fp32 | ( | x | ) | ggml_fp16_to_fp32(x) |
Definition at line 279 of file gemm_kernels_f16.c.
| #define fp32_to_fp16 | ( | x | ) | ggml_fp32_to_fp16(x) |
Definition at line 280 of file gemm_kernels_f16.c.
| typedef void(* ck_f16_ggml_build_forward_expand_fn) (struct ggml_cgraph *, struct ggml_tensor *) |
Definition at line 47 of file gemm_kernels_f16.c.
| typedef void(* ck_f16_ggml_cpu_init_fn) (void) |
Definition at line 49 of file gemm_kernels_f16.c.
| typedef void(* ck_f16_ggml_free_fn) (struct ggml_context *) |
Definition at line 43 of file gemm_kernels_f16.c.
| typedef float *(* ck_f16_ggml_get_data_f32_fn) (const struct ggml_tensor *) |
Definition at line 51 of file gemm_kernels_f16.c.
| typedef void *(* ck_f16_ggml_get_data_fn) (const struct ggml_tensor *) |
Definition at line 50 of file gemm_kernels_f16.c.
| typedef enum ggml_status(* ck_f16_ggml_graph_compute_with_ctx_fn) (struct ggml_context *, struct ggml_cgraph *, int) |
Definition at line 47 of file gemm_kernels_f16.c.
| typedef struct ggml_context *(* ck_f16_ggml_init_fn) (struct ggml_init_params) |
Definition at line 1 of file gemm_kernels_f16.c.
| typedef struct ggml_tensor *(* ck_f16_ggml_mul_mat_fn) (struct ggml_context *, struct ggml_tensor *, struct ggml_tensor *) |
Definition at line 43 of file gemm_kernels_f16.c.
| typedef struct ggml_cgraph *(* ck_f16_ggml_new_graph_fn) (struct ggml_context *) |
Definition at line 43 of file gemm_kernels_f16.c.
| typedef struct ggml_tensor *(* ck_f16_ggml_new_tensor_2d_fn) (struct ggml_context *, enum ggml_type, int64_t, int64_t) |
Definition at line 43 of file gemm_kernels_f16.c.
|
inlinestatic |
Definition at line 568 of file gemm_kernels_f16.c.
References fp16_to_fp32.
Referenced by ck_gemm_f16_input_fp16_work(), and gemm_f16_input_fp16_ref().
|
static |
Definition at line 108 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 130 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 64 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 141 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 152 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 119 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 53 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 86 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 97 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
static |
Definition at line 75 of file gemm_kernels_f16.c.
References RTLD_DEFAULT.
Referenced by gemm_nt_f16_ggml_strict().
|
inlinestatic |
Definition at line 292 of file gemm_kernels_f16.c.
References fp32_to_fp16.
Referenced by ck_gemm_f16_input_fp16_work(), and gemm_f16_input_fp16_ref().
|
static |
Definition at line 818 of file gemm_kernels_f16.c.
References ck_dot_f16_f16_local(), and ck_f32_to_f16_row_local().
Referenced by gemm_f16_input_fp16_threadpool().
|
static |
Definition at line 913 of file gemm_kernels_f16.c.
References ck_threadpool_n_threads().
Referenced by gemm_f16_input_fp16_threadpool().
|
static |
Definition at line 897 of file gemm_kernels_f16.c.
Referenced by gemm_f16_input_fp16_threadpool().
| int ck_gemm_nt_f16_ggml_oracle | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 262 of file gemm_kernels_f16.c.
References C, and gemm_nt_f16_ggml_strict().
| int ck_gemm_nt_f16_simd_lanes | ( | void | ) |
Definition at line 583 of file gemm_kernels_f16.c.
| void convert_f16_to_f32 | ( | float * | dst, |
| const uint16_t * | src, | ||
| size_t | count | ||
| ) |
Convert FP16 tensor to FP32.
Definition at line 1047 of file gemm_kernels_f16.c.
References fp16_to_fp32.
| void convert_f32_to_f16 | ( | uint16_t * | dst, |
| const float * | src, | ||
| size_t | count | ||
| ) |
Convert FP32 tensor to FP16.
Definition at line 1071 of file gemm_kernels_f16.c.
References fp32_to_fp16.
| float dot_f16 | ( | const uint16_t * | w_f16, |
| const float * | x, | ||
| int | K | ||
| ) |
Definition at line 1208 of file gemm_kernels_f16.c.
References gemv_f16().
| void gemm_f16 | ( | float * | Y, |
| const uint16_t * | W, | ||
| const float * | X, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Auto-dispatch GEMM based on available SIMD.
Definition at line 752 of file gemm_kernels_f16.c.
References gemm_f16_ref().
| void gemm_f16_backward | ( | float * | dX, |
| const uint16_t * | W, | ||
| const float * | dY, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Batched backward pass.
Definition at line 1194 of file gemm_kernels_f16.c.
References gemv_f16_backward().
|
static |
Definition at line 764 of file gemm_kernels_f16.c.
References ck_dot_f16_f16_local(), and ck_f32_to_f16_row_local().
Referenced by gemm_nt_f16().
|
static |
Definition at line 930 of file gemm_kernels_f16.c.
References ck_gemm_f16_input_fp16_work(), ck_gemm_f16_pick_active_threads(), ck_gemm_f16_threadpool_enabled(), ck_threadpool_dispatch_n(), and ck_threadpool_global().
Referenced by gemm_nt_f16().
| void gemm_f16_ref | ( | float * | Y, |
| const uint16_t * | W, | ||
| const float * | X, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Matrix-matrix multiply with FP16 weights (scalar reference)
| Y | Output matrix [M x N] |
| W | Weight matrix in FP16 [M x K] |
| X | Input matrix [K x N] |
| M | Number of output rows |
| N | Batch size |
| K | Hidden dimension |
Definition at line 699 of file gemm_kernels_f16.c.
References gemv_f16_ref().
Referenced by gemm_f16().
| void gemm_nt_f16 | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
NT GEMM wrapper for FP16 weights with the engine's standard ABI.
Contract: A: [M, K] fp32 activation matrix B: [N, K] fp16 weight matrix stored row-major (transposed layout) C: [M, N] fp32 output matrix
This wrapper follows llama.cpp's CPU F16 mul_mat contract: activation rows are rounded to FP16 first, then the dot runs as F16 x F16 with FP32 output accumulation. The lower-level gemm_f16() helper remains the direct F16-weight x FP32-activation operator for generic use.
Definition at line 970 of file gemm_kernels_f16.c.
References C, ck_strict_parity_enabled(), gemm_f16_input_fp16_ref(), gemm_f16_input_fp16_threadpool(), and gemm_nt_f16_ggml_strict().
| void gemm_nt_f16_clipped | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| const float * | input_min, | ||
| const float * | input_max, | ||
| const float * | output_min, | ||
| const float * | output_max, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 998 of file gemm_kernels_f16.c.
References C, fp16_to_fp32, and fp32_to_fp16.
|
static |
Definition at line 163 of file gemm_kernels_f16.c.
References C, ck_f16_resolve_ggml_build_forward_expand(), ck_f16_resolve_ggml_cpu_init(), ck_f16_resolve_ggml_free(), ck_f16_resolve_ggml_get_data(), ck_f16_resolve_ggml_get_data_f32(), ck_f16_resolve_ggml_graph_compute_with_ctx(), ck_f16_resolve_ggml_init(), ck_f16_resolve_ggml_mul_mat(), ck_f16_resolve_ggml_new_graph(), ck_f16_resolve_ggml_new_tensor_2d(), GGML_STATUS_SUCCESS, GGML_TYPE_F16, GGML_TYPE_F32, and ggml_init_params::mem_size.
Referenced by ck_gemm_nt_f16_ggml_oracle(), and gemm_nt_f16().
| void gemv_f16 | ( | float * | y, |
| const uint16_t * | W, | ||
| const float * | x, | ||
| int | M, | ||
| int | K | ||
| ) |
Auto-dispatch GEMV based on available SIMD.
Definition at line 673 of file gemm_kernels_f16.c.
References gemv_f16_ref().
Referenced by dot_f16().
| void gemv_f16_backward | ( | float * | dX, |
| const uint16_t * | W, | ||
| const float * | dY, | ||
| int | M, | ||
| int | K | ||
| ) |
Auto-dispatch backward.
Definition at line 1179 of file gemm_kernels_f16.c.
References gemv_f16_backward_ref().
Referenced by gemm_f16_backward().
| void gemv_f16_backward_ref | ( | float * | dX, |
| const uint16_t * | W, | ||
| const float * | dY, | ||
| int | M, | ||
| int | K | ||
| ) |
Backward pass: compute input gradient (scalar reference)
| dX | Output gradient w.r.t. input [K] |
| W | Weight matrix in FP16 format [M x K] |
| dY | Gradient w.r.t. output [M] |
| M | Number of output rows |
| K | Number of columns (input dimension) |
Definition at line 1110 of file gemm_kernels_f16.c.
References fp16_to_fp32.
Referenced by gemv_f16_backward().
| void gemv_f16_ref | ( | float * | y, |
| const uint16_t * | W, | ||
| const float * | x, | ||
| int | M, | ||
| int | K | ||
| ) |
Matrix-vector multiply with FP16 weights (scalar reference)
| y | Output vector [M] |
| W | Weight matrix in FP16 [M x K] |
| x | Input vector [K] |
| M | Number of output rows |
| K | Number of columns |
Definition at line 607 of file gemm_kernels_f16.c.
References fp16_to_fp32.
Referenced by gemm_f16_ref(), and gemv_f16().