k_quants.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * llama.cpp - git 5bf2a2771886ee86137e01dbc7492f78fb392066
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023 Georgi Gerganov
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "ggml.h"
  28. #include <stdint.h>
  29. #include <assert.h>
  30. #include <stddef.h>
  31. // Super-block size
  32. #ifdef GGML_QKK_64
  33. #define QK_K 64
  34. #define K_SCALE_SIZE 4
  35. #else
  36. #define QK_K 256
  37. #define K_SCALE_SIZE 12
  38. #endif
  39. //
  40. // Super-block quantization structures
  41. //
  42. // 2-bit quantization
  43. // weight is represented as x = a * q + b
  44. // 16 blocks of 16 elemenets each
  45. // Effectively 2.5625 bits per weight
  46. typedef struct {
  47. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  48. uint8_t qs[QK_K/4]; // quants
  49. ggml_fp16_t d; // super-block scale for quantized scales
  50. ggml_fp16_t dmin; // super-block scale for quantized mins
  51. } block_q2_K;
  52. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  53. // 3-bit quantization
  54. // weight is represented as x = a * q
  55. // 16 blocks of 16 elemenets each
  56. // Effectively 3.4375 bits per weight
  57. #ifdef GGML_QKK_64
  58. typedef struct {
  59. uint8_t hmask[QK_K/8]; // quants - high bit
  60. uint8_t qs[QK_K/4]; // quants - low 2 bits
  61. uint8_t scales[2];
  62. ggml_fp16_t d; // super-block scale
  63. } block_q3_K;
  64. static_assert(sizeof(block_q3_K) == sizeof(ggml_fp16_t) + QK_K / 4 + QK_K / 8 + 2, "wrong q3_K block size/padding");
  65. #else
  66. typedef struct {
  67. uint8_t hmask[QK_K/8]; // quants - high bit
  68. uint8_t qs[QK_K/4]; // quants - low 2 bits
  69. uint8_t scales[12]; // scales, quantized with 6 bits
  70. ggml_fp16_t d; // super-block scale
  71. } block_q3_K;
  72. static_assert(sizeof(block_q3_K) == sizeof(ggml_fp16_t) + QK_K / 4 + QK_K / 8 + 12, "wrong q3_K block size/padding");
  73. #endif
  74. // 4-bit quantization
  75. // 16 blocks of 32 elements each
  76. // weight is represented as x = a * q + b
  77. // Effectively 4.5 bits per weight
  78. #ifdef GGML_QKK_64
  79. typedef struct {
  80. ggml_fp16_t d[2]; // super-block scales/mins
  81. uint8_t scales[2]; // 4-bit block scales/mins
  82. uint8_t qs[QK_K/2]; // 4--bit quants
  83. } block_q4_K;
  84. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + QK_K/2 + 2, "wrong q4_K block size/padding");
  85. #else
  86. typedef struct {
  87. ggml_fp16_t d; // super-block scale for quantized scales
  88. ggml_fp16_t dmin; // super-block scale for quantized mins
  89. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  90. uint8_t qs[QK_K/2]; // 4--bit quants
  91. } block_q4_K;
  92. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + K_SCALE_SIZE + QK_K/2, "wrong q4_K block size/padding");
  93. #endif
  94. // 5-bit quantization
  95. // 16 blocks of 32 elements each
  96. // weight is represented as x = a * q + b
  97. // Effectively 5.5 bits per weight
  98. #ifdef GGML_QKK_64
  99. typedef struct {
  100. ggml_fp16_t d; // super-block scale
  101. int8_t scales[QK_K/16]; // 8-bit block scales
  102. uint8_t qh[QK_K/8]; // quants, high bit
  103. uint8_t qs[QK_K/2]; // quants, low 4 bits
  104. } block_q5_K;
  105. static_assert(sizeof(block_q5_K) == sizeof(ggml_fp16_t) + QK_K/2 + QK_K/8 + QK_K/16, "wrong q5_K block size/padding");
  106. #else
  107. typedef struct {
  108. ggml_fp16_t d; // super-block scale for quantized scales
  109. ggml_fp16_t dmin; // super-block scale for quantized mins
  110. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  111. uint8_t qh[QK_K/8]; // quants, high bit
  112. uint8_t qs[QK_K/2]; // quants, low 4 bits
  113. } block_q5_K;
  114. static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_fp16_t) + K_SCALE_SIZE + QK_K/2 + QK_K/8, "wrong q5_K block size/padding");
  115. #endif
  116. // 6-bit quantization
  117. // weight is represented as x = a * q
  118. // 16 blocks of 16 elemenets each
  119. // Effectively 6.5625 bits per weight
  120. typedef struct {
  121. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  122. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  123. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  124. ggml_fp16_t d; // super-block scale
  125. } block_q6_K;
  126. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + QK_K / 16 + 3*QK_K/4, "wrong q6_K block size/padding");
  127. // This is only used for intermediate quantization and dot products
  128. typedef struct {
  129. float d; // delta
  130. int8_t qs[QK_K]; // quants
  131. int16_t bsums[QK_K/16]; // sum of quants in groups of 16
  132. } block_q8_K;
  133. static_assert(sizeof(block_q8_K) == sizeof(float) + QK_K + QK_K/16*sizeof(int16_t), "wrong q8_K block size/padding");
  134. // Quantization
  135. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k);
  136. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k);
  137. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k);
  138. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k);
  139. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k);
  140. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k);
  141. void quantize_row_q2_K(const float * restrict x, void * restrict y, int k);
  142. void quantize_row_q3_K(const float * restrict x, void * restrict y, int k);
  143. void quantize_row_q4_K(const float * restrict x, void * restrict y, int k);
  144. void quantize_row_q5_K(const float * restrict x, void * restrict y, int k);
  145. void quantize_row_q6_K(const float * restrict x, void * restrict y, int k);
  146. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k);
  147. // Dequantization
  148. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k);
  149. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k);
  150. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k);
  151. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k);
  152. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k);
  153. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k);
  154. // Dot product
  155. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);
  156. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);
  157. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);
  158. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);
  159. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);
  160. // Quantization with histogram collection
  161. size_t ggml_quantize_q2_K(const float * src, void * dst, int n, int k, int64_t * hist);
  162. size_t ggml_quantize_q3_K(const float * src, void * dst, int n, int k, int64_t * hist);
  163. size_t ggml_quantize_q4_K(const float * src, void * dst, int n, int k, int64_t * hist);
  164. size_t ggml_quantize_q5_K(const float * src, void * dst, int n, int k, int64_t * hist);
  165. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist);