cpy-utils.cuh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #pragma once
  2. #include "ggml-common.h"
  3. #include "convert.cuh"
  4. static __device__ __forceinline__ int best_index_int8(int n, const int8_t * val, float x) {
  5. if (x <= val[0]) return 0;
  6. if (x >= val[n-1]) return n-1;
  7. int ml = 0, mu = n-1;
  8. while (mu-ml > 1) {
  9. int mav = (ml+mu)/2;
  10. if (x < val[mav]) mu = mav; else ml = mav;
  11. }
  12. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  13. }
  14. static __device__ void quantize_f32_q4_0_block(const float * __restrict__ x, block_q4_0 * __restrict__ y) {
  15. float amax = 0.0f;
  16. float vmax = 0.0f;
  17. for (int j = 0; j < QK4_0; ++j) {
  18. const float v = x[j];
  19. if (amax < fabsf(v)) {
  20. amax = fabsf(v);
  21. vmax = v;
  22. }
  23. }
  24. const float d = vmax / -8;
  25. const float id = d ? 1.0f/d : 0.0f;
  26. y->d = d;
  27. for (int j = 0; j < QK4_0/2; ++j) {
  28. const float x0 = x[0 + j]*id;
  29. const float x1 = x[QK4_0/2 + j]*id;
  30. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  31. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  32. y->qs[j] = xi0;
  33. y->qs[j] |= xi1 << 4;
  34. }
  35. }
  36. static __device__ void quantize_f32_q4_1_block(const float * __restrict__ x, block_q4_1 * __restrict__ y) {
  37. float vmin = FLT_MAX;
  38. float vmax = -FLT_MAX;
  39. for (int j = 0; j < QK4_1; ++j) {
  40. const float v = x[j];
  41. if (v < vmin) vmin = v;
  42. if (v > vmax) vmax = v;
  43. }
  44. const float d = (vmax - vmin) / ((1 << 4) - 1);
  45. const float id = d ? 1.0f/d : 0.0f;
  46. y->dm.x = d;
  47. y->dm.y = vmin;
  48. for (int j = 0; j < QK4_1/2; ++j) {
  49. const float x0 = (x[0 + j] - vmin)*id;
  50. const float x1 = (x[QK4_1/2 + j] - vmin)*id;
  51. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  52. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  53. y->qs[j] = xi0;
  54. y->qs[j] |= xi1 << 4;
  55. }
  56. }
  57. static __device__ void quantize_f32_q5_0_block(const float * __restrict__ x, block_q5_0 * __restrict__ y) {
  58. float amax = 0.0f;
  59. float vmax = 0.0f;
  60. for (int j = 0; j < QK5_0; ++j) {
  61. const float v = x[j];
  62. if (amax < fabsf(v)) {
  63. amax = fabsf(v);
  64. vmax = v;
  65. }
  66. }
  67. const float d = vmax / -16;
  68. const float id = d ? 1.0f/d : 0.0f;
  69. y->d = d;
  70. uint32_t qh = 0;
  71. for (int j = 0; j < QK5_0/2; ++j) {
  72. const float x0 = x[0 + j]*id;
  73. const float x1 = x[QK5_0/2 + j]*id;
  74. const uint8_t xi0 = min(31, (int8_t)(x0 + 16.5f));
  75. const uint8_t xi1 = min(31, (int8_t)(x1 + 16.5f));
  76. y->qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  77. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  78. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  79. }
  80. memcpy(y->qh, &qh, sizeof(qh));
  81. }
  82. static __device__ void quantize_f32_q5_1_block(const float * __restrict__ x, block_q5_1 * __restrict__ y) {
  83. float min = x[0];
  84. float max = x[0];
  85. for (int j = 1; j < QK5_1; ++j) {
  86. const float v = x[j];
  87. min = v < min ? v : min;
  88. max = v > max ? v : max;
  89. }
  90. const float d = (max - min) / 31;
  91. const float id = d ? 1.0f/d : 0.0f;
  92. y->dm.x = d;
  93. y->dm.y = min;
  94. uint32_t qh = 0;
  95. for (int j = 0; j < QK5_1/2; ++j) {
  96. const float x0 = (x[0 + j] - min)*id;
  97. const float x1 = (x[QK5_1/2 + j] - min)*id;
  98. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  99. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  100. y->qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  101. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  102. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2);
  103. }
  104. memcpy(y->qh, &qh, sizeof(qh));
  105. }
  106. static __device__ void quantize_f32_q8_0_block(const float * __restrict__ x, block_q8_0 * __restrict__ y) {
  107. float amax = 0.0f; // absolute max
  108. for (int j = 0; j < QK8_0; j++) {
  109. const float v = x[j];
  110. amax = fmaxf(amax, fabsf(v));
  111. }
  112. const float d = amax / ((1 << 7) - 1);
  113. const float id = d ? 1.0f/d : 0.0f;
  114. y->d = d;
  115. for (int j = 0; j < QK8_0; ++j) {
  116. const float x0 = x[j]*id;
  117. y->qs[j] = roundf(x0);
  118. }
  119. }
  120. static __device__ void quantize_f32_iq4_nl_block(const float * __restrict__ x, block_iq4_nl * __restrict__ y) {
  121. float amax = 0.0f;
  122. float vmax = 0.0f;
  123. for (int j = 0; j < QK4_NL; ++j) {
  124. const float v = x[j];
  125. if (amax < fabsf(v)) {
  126. amax = fabsf(v);
  127. vmax = v;
  128. }
  129. }
  130. float d = vmax / kvalues_iq4nl[0];
  131. const float id = d ? 1.0f/d : 0.0f;
  132. float sumqx = 0, sumq2 = 0;
  133. for (int j = 0; j < QK4_NL/2; ++j) {
  134. const float x0 = x[0 + j]*id;
  135. const float x1 = x[QK4_NL/2 + j]*id;
  136. const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl, x0);
  137. const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl, x1);
  138. y->qs[j] = xi0 | (xi1 << 4);
  139. const float v0 = kvalues_iq4nl[xi0];
  140. const float v1 = kvalues_iq4nl[xi1];
  141. const float w0 = x[0 + j]*x[0 + j];
  142. const float w1 = x[QK4_NL/2 + j]*x[QK4_NL/2 + j];
  143. sumqx += w0*v0*x[j] + w1*v1*x[QK4_NL/2 + j];
  144. sumq2 += w0*v0*v0 + w1*v1*v1;
  145. }
  146. y->d = sumq2 > 0 ? sumqx/sumq2 : d;
  147. }
  148. // Wrapper functions for cpy.cu compatibility
  149. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  150. quantize_f32_q4_0_block((const float *)cxi, (block_q4_0 *)cdsti);
  151. }
  152. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  153. quantize_f32_q4_1_block((const float *)cxi, (block_q4_1 *)cdsti);
  154. }
  155. static __device__ void cpy_blck_f32_q5_0(const char * cxi, char * cdsti) {
  156. quantize_f32_q5_0_block((const float *)cxi, (block_q5_0 *)cdsti);
  157. }
  158. static __device__ void cpy_blck_f32_q5_1(const char * cxi, char * cdsti) {
  159. quantize_f32_q5_1_block((const float *)cxi, (block_q5_1 *)cdsti);
  160. }
  161. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  162. quantize_f32_q8_0_block((const float *)cxi, (block_q8_0 *)cdsti);
  163. }
  164. static __device__ void cpy_blck_f32_iq4_nl(const char * cxi, char * cdsti) {
  165. quantize_f32_iq4_nl_block((const float *)cxi, (block_iq4_nl *)cdsti);
  166. }
  167. template<typename src_t, typename dst_t>
  168. static __device__ void cpy_1_scalar(const char * cxi, char * cdsti) {
  169. *(dst_t *) cdsti = ggml_cuda_cast<dst_t>(*(const src_t *) cxi);
  170. }