Optimized BF16 GEMM Kernels for AVX-512. More...
#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "bf16_utils.h"#include "ckernel_engine.h"#include "ck_threadpool.h"Go to the source code of this file.
Macros | |
| #define | BLK_K 256 |
| #define | BLK_M 64 |
| #define | BLK_N 64 |
Functions | |
| __attribute__ ((unused)) | |
| static void | ck_bf16_convert_work (int ith, int nth, void *opaque) |
| static void | ck_bf16_round_work (int ith, int nth, void *opaque) |
| int | ck_gemm_bf16_amx_available (void) |
| static void | ck_gemm_bf16_amx_work (int ith, int nth, void *opaque) |
| int | ck_gemm_bf16_fp32out_amx_raw (const uint16_t *A, const uint16_t *B, float *C, int M, int N, int K, int accumulate) |
| static void | ck_gemm_bf16_native_work (int ith, int nth, void *opaque) |
| static void | ck_gemm_nt_bf16_exact_rows (int begin, int end, void *opaque) |
| static void | ck_gemm_nt_bf16_storage_exact_rows (int begin, int end, void *opaque) |
| static void | ck_gemv_bf16_rows (int begin, int end, void *opaque) |
| static void | ck_gemv_bf16_storage_rows (int begin, int end, void *opaque) |
| static int | ck_min_i (int a, int b) |
| static void | ck_patch_projection_bf16_native_work (int ith, int nth, void *opaque) |
| void | gemm_backward_bf16_mixed (const uint16_t *d_output, const uint16_t *input, const uint16_t *weight, float *d_input, float *d_weight, float *d_bias, int tokens, int in_dim, int out_dim) |
| void | gemm_bf16_fp32out (const uint16_t *A, const uint16_t *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_blocked_serial_bf16 (const uint16_t *A, const uint16_t *B, const uint16_t *bias, uint16_t *C, int M, int N, int K) |
| void | gemm_nn_bf16 (const uint16_t *A, const uint16_t *B, const uint16_t *bias, uint16_t *C, int M, int N, int K) |
| void | gemm_nt_bf16 (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_amx_bf16_storage (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_amx_bf16_storage_workspace (const float *A, const void *B, const float *bias, float *C, int M, int N, int K, uint16_t *a_bf16, size_t a_bf16_bytes) |
| void | gemm_nt_bf16_bf16_storage (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_bf16_storage_parallel_dispatch (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_bf16_storage_row_range (const float *A, const void *B, const float *bias, float *C, int M, int N, int K, int row_begin, int row_end) |
| void | gemm_nt_bf16_native_bf16_storage (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_parallel_dispatch (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_prefill_shape_safe_bf16_storage (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_prefill_shape_safe_bf16_storage_workspace (const float *A, const void *B, const float *bias, float *C, int M, int N, int K, uint16_t *a_bf16, size_t a_bf16_bytes) |
| void | gemm_nt_bf16_pytorch_onednn_3_12_brgemm_bf16_storage (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_pytorch_onednn_brgemm_bf16_storage (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| static void | gemm_nt_bf16_pytorch_onednn_brgemm_bf16_storage_impl (const float *A, const void *B, const float *bias, float *C, int M, int N, int K) |
| void | gemm_nt_bf16_row_range (const float *A, const void *B, const float *bias, float *C, int M, int N, int K, int row_begin, int row_end) |
| void | gemm_tn_bf16 (const uint16_t *A, const uint16_t *B, const uint16_t *bias, uint16_t *C, int M, int N, int K) |
| void | gemv_bf16 (float *y, const void *W, const float *x, int M, int K) |
| void | gemv_bf16_bf16_storage (float *y, const void *W, const float *x, int M, int K) |
| void | gemv_bf16_bf16_storage_parallel_dispatch (float *y, const void *W, const float *x, int M, int K) |
| static void | gemv_bf16_bf16_storage_row_range (float *y, const uint16_t *w, const float *x, int M, int K, int row_begin, int row_end) |
| void | gemv_bf16_parallel_dispatch (float *y, const void *W, const float *x, int M, int K) |
| static void | gemv_bf16_row_range (float *y, const uint16_t *w, const float *x, int M, int K, int row_begin, int row_end) |
| void | patch_projection_bf16_pytorch_onednn_conv3d_storage (const float *input, const void *weights, const float *bias, float *output, int batch, int out_channels, int in_channels, int temporal, int patch_h, int patch_w) |
| void | patch_projection_image_bf16_native_storage (const float *image, const void *weights_t0, const void *weights_t1, const float *bias, float *output, int channels, int image_h, int image_w, int patch_size, int out_channels, int merge_size) |
| void | patch_projection_image_bf16_pytorch_onednn_conv3d_storage (const float *image, const void *weights_t0, const void *weights_t1, const float *bias, float *output, int channels, int image_h, int image_w, int patch_size, int out_channels, int merge_size) |
Optimized BF16 GEMM Kernels for AVX-512.
After changes: make test && make llamacpp-parity-full
Layout: A: [M x K] row-major (BF16) B: [N x K] row-major, stored as [out x in] (BF16) C: [M x N] row-major (BF16 or FP32)
Key optimizations:
Definition in file gemm_kernels_bf16.c.
| #define BLK_K 256 |
Definition at line 56 of file gemm_kernels_bf16.c.
| #define BLK_M 64 |
Definition at line 54 of file gemm_kernels_bf16.c.
| #define BLK_N 64 |
Definition at line 55 of file gemm_kernels_bf16.c.
| __attribute__ | ( | (unused) | ) |
Definition at line 64 of file gemm_kernels_bf16.c.
References __attribute__(), bf16_to_float(), C, and float_to_bf16().
|
static |
Definition at line 1100 of file gemm_kernels_bf16.c.
References end, and float_to_bf16().
Referenced by gemm_nt_bf16_amx_bf16_storage_workspace().
|
static |
Definition at line 1186 of file gemm_kernels_bf16.c.
References bf16_to_float(), end, and float_to_bf16().
Referenced by gemm_nt_bf16_amx_bf16_storage_workspace().
| int ck_gemm_bf16_amx_available | ( | void | ) |
Definition at line 1196 of file gemm_kernels_bf16.c.
Referenced by ck_attention_full_bf16_sdpa_amx_range(), gemm_nt_bf16_prefill_shape_safe_bf16_storage(), and gemm_nt_bf16_prefill_shape_safe_bf16_storage_workspace().
|
static |
Definition at line 1119 of file gemm_kernels_bf16.c.
Referenced by gemm_nt_bf16_amx_bf16_storage_workspace().
| int ck_gemm_bf16_fp32out_amx_raw | ( | const uint16_t * | A, |
| const uint16_t * | B, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K, | ||
| int | accumulate | ||
| ) |
Definition at line 1205 of file gemm_kernels_bf16.c.
References C.
Referenced by ck_attention_full_bf16_sdpa_amx_range().
|
static |
Definition at line 969 of file gemm_kernels_bf16.c.
References bf16_to_float(), and float_to_bf16().
Referenced by gemm_nt_bf16_native_bf16_storage().
|
static |
Definition at line 768 of file gemm_kernels_bf16.c.
References end, and gemm_nt_bf16_row_range().
Referenced by gemm_nt_bf16_parallel_dispatch().
|
static |
Definition at line 2001 of file gemm_kernels_bf16.c.
References end, and gemm_nt_bf16_bf16_storage_row_range().
Referenced by gemm_nt_bf16_bf16_storage_parallel_dispatch().
|
static |
Definition at line 639 of file gemm_kernels_bf16.c.
References end, and gemv_bf16_row_range().
Referenced by gemv_bf16_parallel_dispatch().
|
static |
Definition at line 681 of file gemm_kernels_bf16.c.
References end, and gemv_bf16_bf16_storage_row_range().
Referenced by gemv_bf16_bf16_storage_parallel_dispatch().
|
inlinestatic |
Definition at line 58 of file gemm_kernels_bf16.c.
|
static |
Definition at line 1853 of file gemm_kernels_bf16.c.
References bf16_to_float(), end, and float_to_bf16().
Referenced by patch_projection_image_bf16_native_storage().
| void gemm_backward_bf16_mixed | ( | const uint16_t * | d_output, |
| const uint16_t * | input, | ||
| const uint16_t * | weight, | ||
| float * | d_input, | ||
| float * | d_weight, | ||
| float * | d_bias, | ||
| int | tokens, | ||
| int | in_dim, | ||
| int | out_dim | ||
| ) |
Definition at line 2043 of file gemm_kernels_bf16.c.
References bf16_to_float().
| void gemm_bf16_fp32out | ( | const uint16_t * | A, |
| const uint16_t * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 453 of file gemm_kernels_bf16.c.
References bf16_to_float(), and C.
Referenced by mlp_token_parallel_bf16(), and mlp_token_parallel_bf16_fp32act().
| void gemm_blocked_serial_bf16 | ( | const uint16_t * | A, |
| const uint16_t * | B, | ||
| const uint16_t * | bias, | ||
| uint16_t * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 424 of file gemm_kernels_bf16.c.
References C.
| void gemm_nn_bf16 | ( | const uint16_t * | A, |
| const uint16_t * | B, | ||
| const uint16_t * | bias, | ||
| uint16_t * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 808 of file gemm_kernels_bf16.c.
References bf16_to_float(), C, and float_to_bf16().
| void gemm_nt_bf16 | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 748 of file gemm_kernels_bf16.c.
References C, and gemm_nt_bf16_row_range().
Referenced by gemm_nt_bf16_parallel_dispatch().
| void gemm_nt_bf16_amx_bf16_storage | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 1311 of file gemm_kernels_bf16.c.
References C, and gemm_nt_bf16_amx_bf16_storage_workspace().
Referenced by gemm_nt_bf16_prefill_shape_safe_bf16_storage().
| void gemm_nt_bf16_amx_bf16_storage_workspace | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K, | ||
| uint16_t * | a_bf16, | ||
| size_t | a_bf16_bytes | ||
| ) |
Definition at line 1258 of file gemm_kernels_bf16.c.
References C, ck_bf16_convert_work(), ck_bf16_round_work(), ck_gemm_bf16_amx_work(), ck_threadpool_dispatch_n(), ck_threadpool_global(), and ck_threadpool_n_threads().
Referenced by gemm_nt_bf16_amx_bf16_storage(), and gemm_nt_bf16_prefill_shape_safe_bf16_storage_workspace().
| void gemm_nt_bf16_bf16_storage | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 2034 of file gemm_kernels_bf16.c.
References C, and gemm_nt_bf16_bf16_storage_parallel_dispatch().
| void gemm_nt_bf16_bf16_storage_parallel_dispatch | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 2010 of file gemm_kernels_bf16.c.
References C, ck_gemm_nt_bf16_storage_exact_rows(), ck_threadpool_global(), ck_threadpool_n_threads(), ck_threadpool_parallel_for_n(), and gemm_nt_bf16_bf16_storage_row_range().
Referenced by gemm_nt_bf16_bf16_storage().
| void gemm_nt_bf16_bf16_storage_row_range | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K, | ||
| int | row_begin, | ||
| int | row_end | ||
| ) |
Definition at line 1985 of file gemm_kernels_bf16.c.
References bf16_to_float(), C, float_to_bf16(), and gemm_nt_bf16_row_range().
Referenced by ck_gemm_nt_bf16_storage_exact_rows(), and gemm_nt_bf16_bf16_storage_parallel_dispatch().
| void gemm_nt_bf16_native_bf16_storage | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 1071 of file gemm_kernels_bf16.c.
References C, ck_gemm_bf16_native_work(), ck_threadpool_dispatch_n(), ck_threadpool_global(), and ck_threadpool_n_threads().
Referenced by gemm_nt_bf16_prefill_shape_safe_bf16_storage(), and gemm_nt_bf16_prefill_shape_safe_bf16_storage_workspace().
| void gemm_nt_bf16_parallel_dispatch | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 777 of file gemm_kernels_bf16.c.
References C, ck_gemm_nt_bf16_exact_rows(), ck_threadpool_global(), ck_threadpool_n_threads(), ck_threadpool_parallel_for_n(), and gemm_nt_bf16().
| void gemm_nt_bf16_prefill_shape_safe_bf16_storage | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 1347 of file gemm_kernels_bf16.c.
References C, ck_gemm_bf16_amx_available(), gemm_nt_bf16_amx_bf16_storage(), and gemm_nt_bf16_native_bf16_storage().
| void gemm_nt_bf16_prefill_shape_safe_bf16_storage_workspace | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K, | ||
| uint16_t * | a_bf16, | ||
| size_t | a_bf16_bytes | ||
| ) |
Definition at line 1333 of file gemm_kernels_bf16.c.
References C, ck_gemm_bf16_amx_available(), gemm_nt_bf16_amx_bf16_storage_workspace(), and gemm_nt_bf16_native_bf16_storage().
| void gemm_nt_bf16_pytorch_onednn_3_12_brgemm_bf16_storage | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 1551 of file gemm_kernels_bf16.c.
References C, and gemm_nt_bf16_pytorch_onednn_brgemm_bf16_storage_impl().
| void gemm_nt_bf16_pytorch_onednn_brgemm_bf16_storage | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 1536 of file gemm_kernels_bf16.c.
References C, and gemm_nt_bf16_pytorch_onednn_brgemm_bf16_storage_impl().
|
static |
Definition at line 1430 of file gemm_kernels_bf16.c.
References bf16_to_float(), C, and float_to_bf16().
Referenced by gemm_nt_bf16_pytorch_onednn_3_12_brgemm_bf16_storage(), and gemm_nt_bf16_pytorch_onednn_brgemm_bf16_storage().
| void gemm_nt_bf16_row_range | ( | const float * | A, |
| const void * | B, | ||
| const float * | bias, | ||
| float * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K, | ||
| int | row_begin, | ||
| int | row_end | ||
| ) |
Definition at line 720 of file gemm_kernels_bf16.c.
References bf16_to_float(), C, and float_to_bf16().
Referenced by ck_gemm_nt_bf16_exact_rows(), gemm_nt_bf16(), and gemm_nt_bf16_bf16_storage_row_range().
| void gemm_tn_bf16 | ( | const uint16_t * | A, |
| const uint16_t * | B, | ||
| const uint16_t * | bias, | ||
| uint16_t * | C, | ||
| int | M, | ||
| int | N, | ||
| int | K | ||
| ) |
Definition at line 875 of file gemm_kernels_bf16.c.
References bf16_to_float(), C, and float_to_bf16().
| void gemv_bf16 | ( | float * | y, |
| const void * | W, | ||
| const float * | x, | ||
| int | M, | ||
| int | K | ||
| ) |
Definition at line 622 of file gemm_kernels_bf16.c.
References gemv_bf16_row_range().
Referenced by gemv_bf16_parallel_dispatch().
| void gemv_bf16_bf16_storage | ( | float * | y, |
| const void * | W, | ||
| const float * | x, | ||
| int | M, | ||
| int | K | ||
| ) |
Definition at line 712 of file gemm_kernels_bf16.c.
References gemv_bf16_bf16_storage_parallel_dispatch().
| void gemv_bf16_bf16_storage_parallel_dispatch | ( | float * | y, |
| const void * | W, | ||
| const float * | x, | ||
| int | M, | ||
| int | K | ||
| ) |
Definition at line 688 of file gemm_kernels_bf16.c.
References ck_gemv_bf16_storage_rows(), ck_threadpool_global(), ck_threadpool_n_threads(), ck_threadpool_parallel_for_n(), and gemv_bf16_bf16_storage_row_range().
Referenced by gemv_bf16_bf16_storage().
|
static |
Definition at line 669 of file gemm_kernels_bf16.c.
References bf16_to_float(), float_to_bf16(), and gemv_bf16_row_range().
Referenced by ck_gemv_bf16_storage_rows(), and gemv_bf16_bf16_storage_parallel_dispatch().
| void gemv_bf16_parallel_dispatch | ( | float * | y, |
| const void * | W, | ||
| const float * | x, | ||
| int | M, | ||
| int | K | ||
| ) |
Definition at line 646 of file gemm_kernels_bf16.c.
References ck_gemv_bf16_rows(), ck_threadpool_global(), ck_threadpool_n_threads(), ck_threadpool_parallel_for_n(), and gemv_bf16().
|
static |
Definition at line 600 of file gemm_kernels_bf16.c.
References bf16_to_float(), and float_to_bf16().
Referenced by ck_gemv_bf16_rows(), gemv_bf16(), and gemv_bf16_bf16_storage_row_range().
| void patch_projection_bf16_pytorch_onednn_conv3d_storage | ( | const float * | input, |
| const void * | weights, | ||
| const float * | bias, | ||
| float * | output, | ||
| int | batch, | ||
| int | out_channels, | ||
| int | in_channels, | ||
| int | temporal, | ||
| int | patch_h, | ||
| int | patch_w | ||
| ) |
Definition at line 1564 of file gemm_kernels_bf16.c.
References bf16_to_float(), and float_to_bf16().
Referenced by patch_projection_image_bf16_pytorch_onednn_conv3d_storage().
| void patch_projection_image_bf16_native_storage | ( | const float * | image, |
| const void * | weights_t0, | ||
| const void * | weights_t1, | ||
| const float * | bias, | ||
| float * | output, | ||
| int | channels, | ||
| int | image_h, | ||
| int | image_w, | ||
| int | patch_size, | ||
| int | out_channels, | ||
| int | merge_size | ||
| ) |
Definition at line 1940 of file gemm_kernels_bf16.c.
References ck_patch_projection_bf16_native_work(), ck_threadpool_dispatch_n(), ck_threadpool_global(), and ck_threadpool_n_threads().
| void patch_projection_image_bf16_pytorch_onednn_conv3d_storage | ( | const float * | image, |
| const void * | weights_t0, | ||
| const void * | weights_t1, | ||
| const float * | bias, | ||
| float * | output, | ||
| int | channels, | ||
| int | image_h, | ||
| int | image_w, | ||
| int | patch_size, | ||
| int | out_channels, | ||
| int | merge_size | ||
| ) |
Definition at line 1750 of file gemm_kernels_bf16.c.
References patch_projection_bf16_pytorch_onednn_conv3d_storage().