ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
quantization.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <limits>
6#include <vector>
7
8#include "ggml_types.h"
9#include "gguf_parser.h" // Include for GGML_QK_K
10
11// Define RESTRICT macro based on compiler
12#ifdef _MSC_VER
13#define RESTRICT
14#else
15#define RESTRICT __restrict__
16#endif
17
28// Forward declarations
29struct block_q2_K;
30struct block_q3_K;
31struct block_q4_K;
32struct block_q5_K;
33struct block_q6_K;
34
41float fp16_to_fp32(uint16_t h, bool is_gguf_scale_field = false);
42
49
50#pragma pack(push, 1)
51
64static_assert(sizeof(block_q4_K) == 2 + 2 + 12 + 128, "Size mismatch for standard block_q4_K");
65
80static_assert(sizeof(block_q5_K) == 2 + 2 + 12 + 32 + 128, "Size mismatch for block_q5_K");
81
94static_assert(sizeof(block_q6_K) == 128 + 64 + 16 + 2, "Size mismatch for block_q6_K");
95
108static_assert(sizeof(block_q2_K) == 2 + 2 + 16 + 64, "Size mismatch for block_q2_K");
109
123static_assert(sizeof(block_q3_K) == 32 + 64 + 12 + 2 + 2, "Size mismatch for block_q3_K");
124
133
141static_assert(sizeof(block_q8_0) == sizeof(uint16_t) + GGML_QK8_0, "Size mismatch for block_q8_0");
142
143#pragma pack(pop)
144
150const char* ggml_type_name(GGMLType type);
151
158
165
173void dequantize_q2_k(const void* q_data, float* f_data,
175 bool log_details_for_this_block = false);
176
185 int num_elements, bool log_this_block = false);
186
193
197void dequantize_vector_q5k_to_f32(const std::vector<block_q5_K>& q_weights,
198 std::vector<float>& f32_weights,
199 size_t total_num_elements);
200
209 int num_elements, bool log_this_block = false);
210
218void dequantize_vector_q6k_to_f32(const std::vector<block_q6_K>& q_weights,
219 std::vector<float>& f32_weights,
220 size_t total_num_elements,
221 int log_first_n_blocks = 0);
222
229void dequantize_q3_k(const void* q_data, float* f_data,
231
238void handle_i8_tensor(const void* i8_data, float* f_data, size_t num_elements);
239
246void quantize_q4_k_m(const float* f_data, void* q_data, int num_elements);
247
254void quantize_q6_k(const float* f_data, void* q_data, int num_elements);
255
261std::vector<block_q8_K> quantize_fp32_to_q8_K(const std::vector<float>& f_data);
262
271float vec_dot_q6_k_q8_k_cpu(int n, const std::vector<block_q6_K>& x,
272 const std::vector<block_q8_K>& y,
273 bool log_this_call);
274
284void matvec_q6k_q8k_cpu(const std::vector<block_q6_K>& mat_q6k,
285 const std::vector<block_q8_K>& vec_q8k,
286 std::vector<float>& out_f32, int rows, int cols,
287 bool log_calls);
288
297float vec_dot_q4_k_q8_k_cpu(int n, const std::vector<block_q4_K>& x_vec,
298 const std::vector<block_q8_K>& y_vec,
299 bool log_this_call);
300
310void matvec_q4k_q8k_cpu(const std::vector<block_q4_K>& mat_q4k,
311 const std::vector<block_q8_K>& vec_q8k,
312 std::vector<float>& out_f32, int rows, int cols,
313 bool log_calls);
314
320void dequantize_q8_0_block(const block_q8_0* qblock, float* output);
321
329void dequantize_vector_q4k_to_f32(const std::vector<block_q4_K>& q_weights,
330 std::vector<float>& f32_weights,
331 size_t total_num_elements,
332 int log_first_n_blocks = 0);
333
341void dequantize_vector_q8_0_to_f32(const std::vector<block_q8_0>& q_weights,
342 std::vector<float>& f32_weights,
343 size_t total_num_elements,
344 int log_first_n_blocks = 0);
Type definitions for GGML (Georgi Gerganov Machine Learning) library.
GGMLType
Enumeration of GGML tensor data types.
Definition ggml_types.h:21
Parser for GGUF (GPT-Generated Unified Format) files.
constexpr size_t GGML_QK8_0
Definition gguf_parser.h:43
constexpr size_t GGML_QK_K
Block size constants for different quantization formats.
Definition gguf_parser.h:42
int n
size_t j
enum ember_ir::@44::Value::Type type
uint16_t fp32_to_fp16(float f)
Converts a 32-bit float to 16-bit floating point.
size_t ggml_type_block_size(GGMLType type)
Gets the block size for a GGML type.
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 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=0)
Dequantizes a vector of Q8_0 blocks to a vector of float32.
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.
void dequantize_q2_k(const void *q_data, float *f_data, int num_weights_in_block, bool log_details_for_this_block=false)
Dequantizes a Q2_K quantized block to float32.
void dequantize_q5_k(const block_q5_K *qblock, float *RESTRICT output_f32)
Dequantizes a Q5_K block (256 weights) to float32, as ggml's dequantize_row_q5_K.
void handle_i8_tensor(const void *i8_data, float *f_data, size_t num_elements)
Handles conversion of int8 tensor data to float32.
void dequantize_q4_k_m(const block_q4_K *qblock, float *RESTRICT output_f32, int num_elements, bool log_this_block=false)
Dequantizes a Q4_K quantized block to float32.
float vec_dot_q6_k_q8_k_cpu(int n, const std::vector< block_q6_K > &x, const std::vector< block_q8_K > &y, bool log_this_call)
Computes dot product between Q6_K and Q8_K vectors on CPU.
void quantize_q4_k_m(const float *f_data, void *q_data, int num_elements)
Quantizes float32 data to Q4_K format.
void quantize_q6_k(const float *f_data, void *q_data, int num_elements)
Quantizes float32 data to Q6_K format.
std::vector< block_q8_K > quantize_fp32_to_q8_K(const std::vector< float > &f_data)
Quantizes float32 data to Q8_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.
float fp16_to_fp32(uint16_t h, bool is_gguf_scale_field=false)
Converts a 16-bit floating point number to 32-bit float.
#define RESTRICT
void dequantize_q8_0_block(const block_q8_0 *qblock, float *output)
Dequantizes a Q8_0 block to float32.
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=0)
Dequantizes a vector of Q4_K blocks to a vector of float32.
void dequantize_q6_k(const block_q6_K *qblock, float *RESTRICT output_f32, int num_elements, bool log_this_block=false)
Dequantizes a Q6_K quantized block to float32.
void dequantize_q3_k(const void *q_data, float *f_data, int num_weights_in_block)
Dequantizes a Q3_K quantized block to float32.
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=0)
Dequantizes a vector of Q6_K blocks to a vector of float32.
2-bit K-quantized block structure
uint16_t dmin
uint16_t d
uint8_t qs[GGML_QK_K/4]
uint8_t scales[GGML_QK_K/16]
3-bit K-quantized block structure
uint8_t scales[12]
uint16_t dmin
uint8_t hmask[GGML_QK_K/8]
uint16_t d
uint8_t qs[GGML_QK_K/4]
4-bit K-quantized block structure
uint16_t d
uint8_t scales[12]
uint8_t qs[GGML_QK_K/2]
uint16_t dmin
5-bit K-quantized block structure (ggml's block_q5_K)
uint16_t d
uint16_t dmin
uint8_t qh[GGML_QK_K/8]
uint8_t scales[12]
uint8_t qs[GGML_QK_K/2]
6-bit K-quantized block structure
int8_t scales[GGML_QK_K/16]
uint16_t d
uint8_t ql[GGML_QK_K/2]
uint8_t qh[GGML_QK_K/4]
Simple 8-bit quantized block structure.
uint16_t d
int8_t qs[GGML_QK8_0]
8-bit K-quantized block structure with block sums
int16_t bsums[GGML_QK_K/16]
int8_t qs[GGML_QK_K]
uint16_t d