char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
81b0d882a
char/aliceai-t5-moe
1#pragma once 2 3#include <cassert> 4#include <cstddef> 5#include <functional> 6 7namespace spacemit_kernels { 8 9#define BLOCK_QNK_LEN 256 10 11template < int N > struct nrow_block_q2_k { 12// [4bit scale + 4bit zp] * N * 16 13uint8_t scales [N * BLOCK_QNK_LEN /16 ]; 14// [b0, b16, b32, b48] [b1, b17, b33, b49] ... [b15, b31, b47, b63] 15// [b64, b80, b96, b112] ...[b79, b95, b111, b127] 16// [b128, b144, b160, b176] ...[b143, b159, b175, b191] 17// [b192, b208, b224, b240] ...[b207, b223, b239, b255] 18uint8_t qs [N * BLOCK_QNK_LEN /4 ]; 19uint16_t scales16 [N ]; 20uint16_t zeros16 [N ]; 21}; 22 23template < int N > struct nrow_block_q3_k { 24// [8bit scale] * N * 16 25int8_t scales [N * 16 ]; 26// [b0, b1, b2, b3, b4, b5, b6, b7] ... [b248, b249, b250, b251, b252, b253, b254, b255] 27uint8_t hmask [N * BLOCK_QNK_LEN /8 ]; 28// [b0, b16, b32, b48] [b1, b17, b33, b49] ... [b15, b31, b47, b63] 29// [b64, b80, b96, b112] ...[b79, b95, b111, b127] 30// [b128, b144, b160, b176] ...[b143, b159, b175, b191] 31// [b192, b208, b224, b240] ...[b207, b223, b239, b255] 32uint8_t qs [N * BLOCK_QNK_LEN /4 ]; 33uint16_t scales16 [N ]; 34}; 35 36template < int N > struct nrow_block_mxfp4 { 37uint8_t e [N ]; 38uint8_t qh [4 * N ]; 39uint8_t qs [16 * N ]; 40}; 41 42template < int N > struct __attribute__((packed ))nrow_block_q5_1 { 43uint16_t scales16[ N ]; 44uint8_t zp[ N ]; 45// n0 [bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7] .... 46uint8_t qh[ 4 * N ]; 47// n0 [b0, b1], [b2, b3] .... [b30, b31] 48// n1 [b0, b1], [b2, b3] .... [b30, b31] 49uint8_t qs[ 16 * N ]; 50}; 51 52static_assert ( sizeof (nrow_block_q5_1 < 1 > ) == sizeof ( uint8_t ) + 22 , "wrong nrow_block_q5_1 block size/padding" ); 53 54template < int N > struct __attribute__((packed)) nrow_block_q5_0 { 55uint16_t scales16[ N ]; 56// n0 [bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7] .... 57uint8_t qh[ 4 * N ]; 58// n0 [b0, b1], [b2, b3] .... [b30, b31] 59// n1 [b0, b1], [b2, b3] .... [b30, b31] 60uint8_t qs[ 16 * N ]; 61}; 62 63static_assert ( sizeof (nrow_block_q5_0 < 1 > ) == 22 , "wrong nrow_block_q5_0 block size/padding" ); 64 65using gemm_kernel_quantize_def = std::function < 66size_t (size_t, const uint8_t * , const uint8_t * , const uint8_t * , float * , size_t, size_t, size_t, size_t) > ; 67 68using moe_gemm_kernel_quantize_def = std::function < 69size_t (size_t, const uint8_t ** , const uint8_t * , const uint8_t * , float ** , size_t, size_t, size_t, size_t) > ; 70 71namespace ime1 { 72size_t gemm_kernel_i8i4 ( size_t blk_len, 73const uint8_t * quant_a_ptr, 74const uint8_t * quant_b_data, 75const uint8_t * quant_b_zp, 76float * c_ptr, 77size_t count_m, 78size_t count_n, 79size_t k_blks, 80size_t ldc); 81 82void quantize_a_row_i8 ( size_t blk_len, const float * a_ptr, size_t count_k, uint8_t * quant_a_ptr); 83 84void quantize_a_4row_i8 ( size_t blk_len, const float * a_ptr, size_t count_k, uint8_t * quant_a_ptr); 85 86} // namespace ime1 87 88namespace ime2 { 89size_t gemm_kernel_i8i2k ( size_t blk_len, 90const uint8_t * quant_a_ptr, 91const uint8_t * quant_b_data, 92const uint8_t * quant_b_zp, 93float * c_ptr, 94size_t count_m, 95size_t count_n, 96size_t k_blks, 97size_t ldc); 98 99size_t gemm_kernel_i8i3k ( size_t blk_len, 100const uint8_t * quant_a_ptr, 101const uint8_t * quant_b_data, 102const uint8_t * quant_b_zp, 103float * c_ptr, 104size_t count_m, 105size_t count_n, 106size_t k_blks, 107size_t ldc); 108 109size_t gemm_kernel_i8i4 ( size_t blk_len, 110const uint8_t * quant_a_ptr, 111const uint8_t * quant_b_data, 112const uint8_t * quant_b_zp, 113float * c_ptr, 114size_t count_m, 115size_t count_n, 116size_t k_blks, 117size_t ldc); 118 119size_t gemm_kernel_i8i4_hp ( size_t blk_len, 120const uint8_t * quant_a_ptr, 121const uint8_t * quant_b_data, 122const uint8_t * quant_b_zp, 123float * c_ptr, 124size_t count_m, 125size_t count_n, 126size_t k_blks, 127size_t ldc); 128 129size_t moe_m2_gemm_kernel_i8i4 ( size_t blk_len, 130const uint8_t ** quant_a_ptr, 131const uint8_t * quant_b_data, 132const uint8_t * quant_b_zp, 133float ** c_ptr, 134size_t count_m, 135size_t count_n, 136size_t k_blks, 137size_t ldc); 138 139size_t gemm_kernel_i8i8 ( size_t blk_len, 140const uint8_t * quant_a_ptr, 141const uint8_t * quant_b_data, 142const uint8_t * quant_b_zp, 143float * c_ptr, 144size_t count_m, 145size_t count_n, 146size_t k_blks, 147size_t ldc); 148 149size_t gemm_kernel_i8mxfp4 ( size_t blk_len, 150const uint8_t * quant_a_ptr, 151const uint8_t * quant_b_data, 152const uint8_t * quant_b_zp, 153float * c_ptr, 154size_t count_m, 155size_t count_n, 156size_t k_blks, 157size_t ldc); 158 159size_t moe_m2_gemm_kernel_i8mxfp4 ( size_t blk_len, 160const uint8_t ** quant_a_ptr, 161const uint8_t * quant_b_data, 162const uint8_t * quant_b_zp, 163float ** c_ptr, 164size_t count_m, 165size_t count_n, 166size_t k_blks, 167size_t ldc); 168 169size_t gemm_kernel_i8i5 ( size_t blk_len, 170const uint8_t * quant_a_ptr, 171const uint8_t * quant_b_data, 172const uint8_t * quant_b_zp, 173float * c_ptr, 174size_t count_m, 175size_t count_n, 176size_t k_blks, 177size_t ldc); 178 179size_t moe_m2_gemm_kernel_i8i5 ( size_t blk_len, 180const uint8_t ** quant_a_ptr, 181const uint8_t * quant_b_data, 182const uint8_t * quant_b_zp, 183float ** c_ptr, 184size_t count_m, 185size_t count_n, 186size_t k_blks, 187size_t ldc); 188} // namespace ime2 189} // namespace spacemit_kernels