23 1.0f, 1.0625f, 1.125f, 1.1875f, 1.25f, 1.3125f, 1.375f, 1.4375f,
24 1.5f, 1.5625f, 1.625f, 1.6875f, 1.75f, 1.8125f, 1.875f, 1.9375f,
25 2.0f, 2.125f, 2.25f, 2.375f, 2.5f, 2.625f, 2.75f, 2.875f,
26 3.0f, 3.125f, 3.25f, 3.375f, 3.5f, 3.625f, 3.75f, 3.875f,
27 4.0f, 4.25f, 4.5f, 4.75f, 5.0f, 5.25f, 5.5f, 5.75f,
28 6.0f, 6.25f, 6.5f, 6.75f, 7.0f, 7.25f, 7.5f, 7.75f,
29 8.0f, 8.5f, 9.0f, 9.5f, 10.0f, 10.5f, 11.0f, 11.5f,
30 12.0f, 12.5f, 13.0f, 13.5f, 14.0f, 14.5f, 15.0f, 15.5f};
33 0.0f, -0.0078125f, -0.015625f, -0.0234375f, -0.03125f, -0.0390625f,
34 -0.046875f, -0.0546875f, -0.0625f, -0.0703125f, -0.078125f, -0.0859375f,
35 -0.09375f, -0.1015625f, -0.109375f, -0.1171875f, -0.125f, -0.140625f,
36 -0.15625f, -0.171875f, -0.1875f, -0.203125f, -0.21875f, -0.234375f,
37 -0.25f, -0.265625f, -0.28125f, -0.296875f, -0.3125f, -0.328125f,
38 -0.34375f, -0.359375f, -0.375f, -0.40625f, -0.4375f, -0.46875f,
39 -0.5f, -0.53125f, -0.5625f, -0.59375f, -0.625f, -0.65625f,
40 -0.6875f, -0.71875f, -0.75f, -0.78125f, -0.8125f, -0.84375f,
41 -0.875f, -0.9375f, -1.0f, -1.0625f, -1.125f, -1.1875f,
42 -1.25f, -1.3125f, -1.375f, -1.4375f, -1.5f, -1.5625f,
43 -1.625f, -1.6875f, -1.75f, -1.8125f};
79 std::memcpy(&
f, &
x,
sizeof(
float));
94 std::memcpy(&
x, &
f,
sizeof(
float));
108 u = (
sign << 15) | 0x7c00;
127 u = (
sign << 15) | 0x7c00;
152 *
d_val = (q[
j+4] & 0x0F) | ((q[
j-4] >> 6) << 4);
153 *
m_val = (q[
j+4] >> 4) | ((q[
j-0] >> 6) << 4);
175 <<
"Warning: dequantize_q4_k_m called with num_weights != GGML_QK_K ("
206 for (
int l = 0;
l < 32; ++
l) {
213 for (
int l = 0;
l < 32; ++
l) {
236 const float d1 = d *
static_cast<float>(
sc),
m1 = dmin *
static_cast<float>(
m);
238 const float d2 = d *
static_cast<float>(
sc),
m2 = dmin *
static_cast<float>(
m);
250 throw std::runtime_error(
"dequantize_vector_q5k_to_f32: " + std::to_string(
q_weights.size()) +
260 <<
"Warning: dequantize_q6_k called with num_weights != GGML_QK_K ("
284 for (
int l = 0;
l < 32; ++
l) {
288 const int8_t q1 = (
int8_t)(((ql[
l + 0] & 0x0F) | (((qh[
l] >> 0) & 0x03) << 4))) - 32;
289 const int8_t q2 = (
int8_t)(((ql[
l + 32] & 0x0F) | (((qh[
l] >> 2) & 0x03) << 4))) - 32;
290 const int8_t q3 = (
int8_t)(((ql[
l + 0] >> 4) | (((qh[
l] >> 4) & 0x03) << 4))) - 32;
291 const int8_t q4 = (
int8_t)(((ql[
l + 32] >> 4) | (((qh[
l] >> 6) & 0x03) << 4))) - 32;
302 size_t num_elements) {
304 for (
size_t i = 0;
i < num_elements; ++
i) {
312 throw std::invalid_argument(
313 "quantize_q4_k_m currently only supports block size " +
324 for (
int i = 0;
i < num_elements; ++
i) {
350 for (
int i = 1;
i < 16; ++
i) {
375 float min_min_err = std::numeric_limits<float>::max();
404 for (
int i = 0;
i < 16; ++
i) {
420 for (
int i = 0;
i < 8; ++
i) {
431 throw std::invalid_argument(
432 "dequantize_q2_k currently only supports block size " +
453 for (
int i = 0;
i < 8; ++
i) {
475 for (
int i = 0;
i < 4; ++
i) {
512 throw std::invalid_argument(
513 "dequantize_q3_k currently only supports block size " +
547 for (
int i = 0;
i < 4; ++
i) {
563 if (!std::isfinite(
val)) {
573 std::cout <<
"ERROR: Processed " <<
weight_index <<
" weights instead of "
585 throw std::invalid_argument(
586 "quantize_q6_k currently only supports block size " +
599 for (
int i = 0;
i < num_elements; ++
i) {
610 for (
int i = 0;
i < 16; ++
i) {
618 for (
int i = 0;
i < 16; ++
i) {
623 int idx =
sub * 16 +
i;
682 return sizeof(
float);
720 std::cout <<
" UNKNOWN GGML TYPE: " <<
static_cast<int>(
type)
722 throw std::invalid_argument(
"Unknown GGML type in ggml_type_size: " +
723 std::to_string(
static_cast<int>(
type)));
754 std::cout <<
"Warning: Unknown GGMLType in ggml_type_block_size: "
755 <<
static_cast<int>(
type) << std::endl;
763 const std::vector<float>&
f_data) {
765 throw std::runtime_error(
766 "Input vector size must be a multiple of GGML_QK_K (" +
772 const float*
x =
f_data.data();
784 const float id = (
d_fp32 != 0.f) ? 1.0f /
d_fp32 : 0.0f;
790 <<
" Input amax=" <<
amax <<
" -> d_fp32=" <<
d_fp32
791 <<
" -> Stored d_fp16=0x" << std::hex <<
y[
i].d
816 const std::vector<block_q8_K>&
y_vec,
819 throw std::runtime_error(
"vec_dot_q6_k_q8_k: n must be multiple of QK_K");
823 throw std::runtime_error(
"vec_dot_q6_k_q8_k: vector block count mismatch");
833 std::memset(
sums, 0, 8 *
sizeof(
float));
840 for (
size_t i = 0;
i <
nb; ++
i) {
848 for (
int l = 0;
l < 32; ++
l) {
850 ((ql[
l + 0] & 0xF) | (((qh[
l] >> 0) & 3) << 4)) - 32);
852 ((ql[
l + 32] & 0xF) | (((qh[
l] >> 2) & 3) << 4)) - 32);
854 ((ql[
l + 0] >> 4) | (((qh[
l] >> 4) & 3) << 4)) - 32);
856 ((ql[
l + 32] >> 4) | (((qh[
l] >> 6) & 3) << 4)) - 32);
867 for (
int l = 0;
l < 8; ++
l)
872 for (
int l = 0;
l < 8; ++
l)
890 for (
int l = 0;
l < 8; ++
l) {
902 ss_log <<
" Q6_K Scale d_q6: " <<
d_q6 <<
" (Raw FP16: 0x" << std::hex
903 <<
x[
i].d << std::dec <<
")";
909 ss_log <<
" Combined Scale d: " << d;
912 ss_log <<
" Q6_K Sub-scales (int8): ";
913 for (
int k = 0;
k < 16; ++
k)
ss_log << (
int)
x[
i].scales[
k] <<
" ";
916 ss_log <<
" Int32 Sums (aux32, before compensation): ";
921 <<
", -32 * sumi_mins: " << (-32 *
sumi_mins);
924 ss_log <<
" Block #0 Contribution to Sums (after compensation): "
930 for (
int l = 0;
l < 8; ++
l) {
941 const std::vector<block_q8_K>&
vec_q8k,
945 throw std::runtime_error(
946 "matvec_q6k_q8k_cpu: cols must be divisible by GGML_QK_K");
950 throw std::runtime_error(
"matvec_q6k_q8k_cpu: mat_q6k size mismatch");
953 throw std::runtime_error(
"matvec_q6k_q8k_cpu: vec_q8k size mismatch");
956 for (
int r = 0;
r <
rows; ++
r) {
957 const std::vector<block_q6_K>
row_q6k(
966 const std::vector<block_q8_K>&
y_vec,
972 throw std::runtime_error(
"vec_dot_q4_k_q8_k: n must be multiple of QK_K");
976 throw std::runtime_error(
"vec_dot_q4_k_q8_k: vector block count mismatch");
983 for (
size_t i = 0;
i <
nb; ++
i) {
998 for (
int k = 0;
k < 16; ++
k) {
999 int idx =
sub * 16 +
k;
1001 float q8_val =
static_cast<float>(
q8[idx]);
1007 std::stringstream
ss;
1012 ss <<
"[Q4K_Q8K] Block #0: Q8_K input (first 16): ";
1013 for (
int k = 0;
k < 16; ++
k)
ss << (
int)
q8[
k] <<
" ";
1016 ss <<
"[Q4K_Q8K] Block #0: Q4_K unpacked (first 16): ";
1026 const std::vector<block_q8_K>&
vec_q8k,
1030 throw std::runtime_error(
1031 "matvec_q4k_q8k_cpu: cols must be divisible by GGML_QK_K");
1035 throw std::runtime_error(
"matvec_q4k_q8k_cpu: mat_q4k size mismatch");
1038 throw std::runtime_error(
"matvec_q4k_q8k_cpu: vec_q8k size mismatch");
1042#pragma omp parallel for
1043 for (
int r = 0;
r <
rows; ++
r) {
1044 const std::vector<block_q4_K>
row_q4k(
1056 <<
"Error: n must be a multiple of GGML_QK_K for Q8_K dequantization."
1062 std::cerr <<
"Error: Not enough Q8_K blocks provided for dequantization."
1079 << std::hex <<
qblock->d << std::dec <<
" -> d=" << d;
1104 Logger::warning(
"[DEQUANT_VEC_Q6K] Input Q6_K weight vector is empty. Output float vector will be empty.");
1113 Logger::error(
"[DEQUANT_VEC_Q6K] Mismatch in Q6_K block count. Expected: " +
1131 Logger::warning(
"[DEQUANT_VEC_Q6K] Zero or negative elements requested for block " + std::to_string(
i) +
". Skipping.");
1159 Logger::warning(
"[DEQUANT_VEC_Q4K] Input Q4_K weight vector is empty. Output float vector will be empty.");
1168 Logger::error(
"[DEQUANT_VEC_Q4K] Mismatch in Q4_K block count. Expected: " +
1185 Logger::warning(
"[DEQUANT_VEC_Q4K] Zero or negative elements requested for block " + std::to_string(
i) +
". Skipping.");
1215 Logger::warning(
"[DEQUANT_VEC_Q8_0] Input Q8_0 weight vector is empty. Output float vector will be empty.");
1225 Logger::error(
"[DEQUANT_VEC_Q8_0] Mismatch in Q8_0 block count. Expected: " +
1242 Logger::warning(
"[DEQUANT_VEC_Q8_0] Zero or negative elements requested for block " + std::to_string(
i) +
". Skipping.");
static void debug(const std::string &message)
static void warning(const std::string &message)
static void error(const std::string &message)
GGMLType
Enumeration of GGML tensor data types.
Parser for GGUF (GPT-Generated Unified Format) files.
constexpr float TENSOR_SCALE_MAX
Constants for tensor value validation.
constexpr int8_t Q4K_OFFSET
Offset values for quantization methods.
constexpr int8_t Q6K_OFFSET
constexpr float Q8K_SCALE_FACTOR
constexpr float Q4K_SCALE_FACTOR
Scale factors for different quantization methods.
constexpr float TENSOR_SCALE_MIN
constexpr float Q6K_SCALE_FACTOR
constexpr float GGUF_EPSILON
Constants for numeric stability in calculations.
constexpr size_t GGML_QK8_0
constexpr size_t GGML_QK_K
Block size constants for different quantization formats.
constexpr float GGUF_SMALL_VAL
enum ember_ir::@44::Value::Type type
Logging utilities for the TinyLlama implementation.
uint16_t fp32_to_fp16(float f)
Converts a 32-bit float to 16-bit floating point.
void dequantize_q3_k(const void *qblock_void, float *output, int num_weights_in_block)
Dequantizes a Q3_K quantized block to float32.
size_t ggml_type_block_size(GGMLType type)
Gets the block size for a GGML type.
void dequantize_q4_k_m(const block_q4_K *qblock, float *output, int num_weights_in_block, bool log_this_block)
static void get_scale_min_k4(int j, const uint8_t *q, uint8_t *d_val, uint8_t *m_val)
void dequantize_vector_q5k_to_f32(const std::vector< block_q5_K > &q_weights, std::vector< float > &f32_weights, size_t total_num_elements)
Dequantizes a vector of Q5_K blocks to float32 (total_num_elements a multiple of GGML_QK_K)
size_t ggml_type_size(GGMLType type)
Gets the size in bytes of a GGML type.
void matvec_q6k_q8k_cpu(const std::vector< block_q6_K > &mat_q6k, const std::vector< block_q8_K > &vec_q8k, std::vector< float > &out_f32, int rows, int cols, bool log_calls)
Computes matrix-vector product between Q6_K matrix and Q8_K vector on CPU.
const char * ggml_type_name(GGMLType type)
Gets the string name of a GGML type.
void matvec_q4k_q8k_cpu(const std::vector< block_q4_K > &mat_q4k, const std::vector< block_q8_K > &vec_q8k, std::vector< float > &out_f32, int rows, int cols, bool log_calls)
Computes matrix-vector product between Q4_K matrix and Q8_K vector on CPU.
float vec_dot_q6_k_q8_k_cpu(int n, const std::vector< block_q6_K > &x_vec, const std::vector< block_q8_K > &y_vec, bool log_this_call)
Computes dot product between Q6_K and Q8_K vectors on CPU.
void handle_i8_tensor(const void *input_data, float *output_data, size_t num_elements)
Handles conversion of int8 tensor data to float32.
void quantize_q4_k_m(const float *input, void *output_qblock_void, int num_elements)
Quantizes float32 data to Q4_K format.
static void get_scale_min_indices_q4_K(int j, const uint8_t *scales, uint8_t *scale_index, uint8_t *min_index)
void dequantize_q5_k(const block_q5_K *qblock, float *output)
void dequantize_vector_q6k_to_f32(const std::vector< block_q6_K > &q_weights, std::vector< float > &f32_weights, size_t total_num_elements, int log_first_n_blocks)
Dequantizes a vector of Q6_K blocks to a vector of float32.
void dequantize_q2_k(const void *qblock_void, float *output, int num_weights_in_block)
std::vector< block_q8_K > quantize_fp32_to_q8_K(const std::vector< float > &f_data)
Quantizes float32 data to Q8_K format.
float fp16_to_fp32(uint16_t h, bool is_gguf_scale_field)
Converts a 16-bit floating point number to 32-bit float.
static std::atomic< int > g_vec_dot_q4_k_q8_k_log_count
void dequantize_vector_q8_0_to_f32(const std::vector< block_q8_0 > &q_weights, std::vector< float > &f32_weights, size_t total_num_elements, int log_first_n_blocks)
Dequantizes a vector of Q8_0 blocks to a vector of float32.
void quantize_q6_k(const float *input, void *output_qblock_void, int num_elements)
Quantizes float32 data to Q6_K format.
float vec_dot_q4_k_q8_k_cpu(int n, const std::vector< block_q4_K > &x_vec, const std::vector< block_q8_K > &y_vec, bool log_this_call)
Computes dot product between Q4_K and Q8_K vectors on CPU.
constexpr float K_MIN_VALUES[64]
void dequantize_vector_q4k_to_f32(const std::vector< block_q4_K > &q_weights, std::vector< float > &f32_weights, size_t total_num_elements, int log_first_n_blocks)
Dequantizes a vector of Q4_K blocks to a vector of float32.
void dequantize_q8_0_block(const block_q8_0 *qblock, float *output)
Dequantizes a Q8_0 block to float32.
void dequantize_q8_k(const std::vector< block_q8_K > &q_data, std::vector< float > &x, int n, bool log_this_block)
constexpr float K_SCALE_VALUES[64]
void dequantize_q6_k(const block_q6_K *qblock, float *output, int num_weights_in_block, bool log_this_block)
Weight quantization structures and functions for model compression.
2-bit K-quantized block structure
3-bit K-quantized block structure
4-bit K-quantized block structure
5-bit K-quantized block structure (ggml's block_q5_K)
6-bit K-quantized block structure
Simple 8-bit quantized block structure.
8-bit K-quantized block structure with block sums