← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_k_dot_ordered.h File Reference
#include "ckernel_engine.h"
#include "ckernel_quant.h"
#include <string.h>

Go to the source code of this file.

Macros

#define CK_DEFINE_PACKED_ORDERED_DOT(name, type, block_width, decode)

Functions

static float ck_packed_q3_k_dot_ordered (const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q4_k_dot_ordered (const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q4_k_dot_ordered_scalar (const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_0_dot_ordered (const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_1_dot_ordered (const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_k_dot_ordered (const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q6_k_dot_ordered (const float *input, const unsigned char *weight, size_t width)

Macro Definition Documentation

◆ CK_DEFINE_PACKED_ORDERED_DOT

#define CK_DEFINE_PACKED_ORDERED_DOT ( name,
type,
block_width,
decode )
Value:
static inline float name(const float *input, const unsigned char *weight, size_t width) \
{ \
double sum = 0.0; \
for (size_t first = 0; first < width; first += block_width, weight += sizeof(type)) { \
type block; \
float values[block_width]; \
memcpy(&block, weight, sizeof(block)); \
decode(&block, values, block_width); \
for (size_t channel = 0; channel < block_width; ++channel) \
sum += (double)input[first + channel] * (double)values[channel]; \
} \
return (float)sum; \
}

Definition at line 11 of file packed_k_dot_ordered.h.

11#define CK_DEFINE_PACKED_ORDERED_DOT(name, type, block_width, decode) \
12static inline float name(const float *input, const unsigned char *weight, size_t width) \
13{ \
14 double sum = 0.0; \
15 for (size_t first = 0; first < width; first += block_width, weight += sizeof(type)) { \
16 type block; \
17 float values[block_width]; \
18 memcpy(&block, weight, sizeof(block)); \
19 decode(&block, values, block_width); \
20 for (size_t channel = 0; channel < block_width; ++channel) \
21 sum += (double)input[first + channel] * (double)values[channel]; \
22 } \
23 return (float)sum; \
24}

Referenced by ck_packed_q5_0_dot_ordered(), ck_packed_q5_1_dot_ordered(), and ck_packed_q6_k_dot_ordered().

Function Documentation

◆ ck_packed_q3_k_dot_ordered()

float ck_packed_q3_k_dot_ordered ( const float * input,
const unsigned char * weight,
size_t width )
inlinestatic

Definition at line 54 of file packed_k_dot_ordered.h.

56{
57 double sum = 0.0;
58 for (size_t first = 0; first < width; first += QK_K, weight += sizeof(block_q3_K)) {
59 block_q3_K block;
60 memcpy(&block, weight, sizeof(block));
61 const float d = CK_FP16_TO_FP32(block.d);
62 for (unsigned group = 0; group < 16; ++group) {
63 const unsigned lo = (block.scales[group % 8] >> (4 * (group / 8))) & 15u;
64 const unsigned hi = (block.scales[8 + group % 4] >> (2 * (group / 4))) & 3u;
65 const float scale = d * ((int)(lo | (hi << 4)) - 32);
66 const unsigned plane = group / 2;
67 const unsigned lane_start = (group % 2) * 16;
68 for (unsigned lane = lane_start; lane < lane_start + 16; ++lane) {
69 const int low = (block.qs[(plane / 4) * 32 + lane] >> (2 * (plane % 4))) & 3u;
70 const int code = low - ((block.hmask[lane] & (1u << plane)) ? 0 : 4);
71 const float value = scale * code;
72 sum += (double)input[first + group * 16 + lane - lane_start] * value;
73 }
74 }
75 }
76 return (float)sum;
77}
#define CK_FP16_TO_FP32(x)
#define QK_K
uint8_t scales[12]
uint8_t qs[256/4]
uint8_t hmask[256/8]

References CK_FP16_TO_FP32, block_q3_K::d, block_q3_K::hmask, QK_K, block_q3_K::qs, and block_q3_K::scales.

Referenced by expert_dot(), and linear_rows_q3_k_checked_f32().

◆ ck_packed_q4_k_dot_ordered()

float ck_packed_q4_k_dot_ordered ( const float * input,
const unsigned char * weight,
size_t width )
inlinestatic

Definition at line 129 of file packed_k_dot_ordered.h.

131{
132#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
133 if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("f16c"))
134 return ck_packed_q4_k_dot_ordered_avx2(input, weight, width);
135#endif
136 return ck_packed_q4_k_dot_ordered_scalar(input, weight, width);
137}
static float ck_packed_q4_k_dot_ordered_scalar(const float *input, const unsigned char *weight, size_t width)

References ck_packed_q4_k_dot_ordered_scalar().

Referenced by expert_dot(), and linear_rows_q4_k_checked_f32().

◆ ck_packed_q4_k_dot_ordered_scalar()

float ck_packed_q4_k_dot_ordered_scalar ( const float * input,
const unsigned char * weight,
size_t width )
inlinestatic

◆ ck_packed_q5_0_dot_ordered()

float ck_packed_q5_0_dot_ordered ( const float * input,
const unsigned char * weight,
size_t width )
inlinestatic

◆ ck_packed_q5_1_dot_ordered()

float ck_packed_q5_1_dot_ordered ( const float * input,
const unsigned char * weight,
size_t width )
inlinestatic

◆ ck_packed_q5_k_dot_ordered()

float ck_packed_q5_k_dot_ordered ( const float * input,
const unsigned char * weight,
size_t width )
inlinestatic

Definition at line 33 of file packed_k_dot_ordered.h.

35{
36 double sum = 0.0;
37 for (size_t first = 0; first < width; first += QK_K, weight += sizeof(block_q5_K)) {
38 block_q5_K block;
39 memcpy(&block, weight, sizeof(block));
40 const float d = CK_FP16_TO_FP32(block.d), dmin = CK_FP16_TO_FP32(block.dmin);
41 uint8_t scales[8], mins[8];
42 unpack_q4_k_scales(block.scales, scales, mins);
43 for (int group = 0; group < 8; ++group) {
44 const float scale = d * scales[group], minimum = dmin * mins[group];
45 for (int lane = 0; lane < 32; ++lane) {
46 const float value = scale * q5_k_quant_value(&block, group, lane) - minimum;
47 sum += (double)input[first + group * 32 + lane] * value;
48 }
49 }
50 }
51 return (float)sum;
52}
static uint8_t q5_k_quant_value(const block_q5_K *block, int subblock, int i)
static void unpack_q4_k_scales(const uint8_t *scales, uint8_t *sc, uint8_t *m)
Unpack Q4_K sub-block scales and mins.
uint8_t scales[12]

References CK_FP16_TO_FP32, block_q5_K::d, block_q5_K::dmin, q5_k_quant_value(), QK_K, block_q5_K::scales, and unpack_q4_k_scales().

Referenced by linear_rows_q5_k_checked_f32().

◆ ck_packed_q6_k_dot_ordered()

float ck_packed_q6_k_dot_ordered ( const float * input,
const unsigned char * weight,
size_t width )
inlinestatic