vdot.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include <cstdio>
  2. #include <vector>
  3. #include <random>
  4. #include <chrono>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <cassert>
  8. #include <cstring>
  9. #include <array>
  10. #include <ggml.h>
  11. constexpr int kVecSize = 1 << 18;
  12. float drawFromGaussianPdf(std::mt19937& rndm) {
  13. constexpr double kScale = 1./(1. + std::mt19937::max());
  14. constexpr double kTwoPiTimesScale = 6.28318530717958647692*kScale;
  15. static float lastX;
  16. static bool haveX = false;
  17. if (haveX) { haveX = false; return lastX; }
  18. auto r = sqrt(-2*log(1 - kScale*rndm()));
  19. auto phi = kTwoPiTimesScale * rndm();
  20. lastX = r*sin(phi);
  21. haveX = true;
  22. return r*cos(phi);
  23. }
  24. void fillRandomGaussianFloats(std::vector<float>& values, std::mt19937& rndm, float mean = 0) {
  25. for (auto& v : values) v = mean + drawFromGaussianPdf(rndm);
  26. }
  27. // Copy-pasted from ggml.c
  28. #define QK4_0 32
  29. typedef struct {
  30. float d; // delta
  31. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  32. } block_q4_0;
  33. static_assert(sizeof(block_q4_0) == sizeof(float) + QK4_0 / 2, "wrong q4_0 block size/padding");
  34. #define QK4_1 32
  35. typedef struct {
  36. float d; // delta
  37. float m; // min
  38. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  39. } block_q4_1;
  40. static_assert(sizeof(block_q4_1) == sizeof(float) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  41. // Copy-pasted from ggml.c
  42. #define QK8_0 32
  43. typedef struct {
  44. float d; // delta
  45. int8_t qs[QK8_0]; // quants
  46. } block_q8_0;
  47. static_assert(sizeof(block_q8_0) == sizeof(float) + QK8_0, "wrong q8_0 block size/padding");
  48. // "Scalar" dot product between the quantized vector x and float vector y
  49. inline double dot(int n, const block_q4_0* x, const float* y) {
  50. const static float kValues[16] = {-8.f, -7.f, -6.f, -5.f, -4.f, -3.f, -2.f, -1.f, 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f};
  51. constexpr uint32_t kMask1 = 0x0f0f0f0f;
  52. uint32_t u1, u2;
  53. auto q1 = (const uint8_t*)&u1;
  54. auto q2 = (const uint8_t*)&u2;
  55. double sum = 0;
  56. for (int i=0; i<n; ++i) {
  57. float d = x->d;
  58. auto u = (const uint32_t*)x->qs;
  59. float s = 0;
  60. for (int k=0; k<4; ++k) {
  61. u1 = u[k] & kMask1;
  62. u2 = (u[k] >> 4) & kMask1;
  63. s += y[0]*kValues[q1[0]] + y[1]*kValues[q2[0]] +
  64. y[2]*kValues[q1[1]] + y[3]*kValues[q2[1]] +
  65. y[4]*kValues[q1[2]] + y[5]*kValues[q2[2]] +
  66. y[6]*kValues[q1[3]] + y[7]*kValues[q2[3]];
  67. y += 8;
  68. }
  69. sum += s*d;
  70. ++x;
  71. }
  72. return sum;
  73. }
  74. // Alternative version of the above. Faster on my Mac (~45 us vs ~55 us per dot product),
  75. // but about the same on X86_64 (Ryzen 7950X CPU).
  76. inline double dot3(int n, const block_q4_0* x, const float* y) {
  77. const static std::pair<float,float> kValues[256] = {
  78. {-8.f, -8.f}, {-7.f, -8.f}, {-6.f, -8.f}, {-5.f, -8.f}, {-4.f, -8.f}, {-3.f, -8.f}, {-2.f, -8.f}, {-1.f, -8.f},
  79. { 0.f, -8.f}, { 1.f, -8.f}, { 2.f, -8.f}, { 3.f, -8.f}, { 4.f, -8.f}, { 5.f, -8.f}, { 6.f, -8.f}, { 7.f, -8.f},
  80. {-8.f, -7.f}, {-7.f, -7.f}, {-6.f, -7.f}, {-5.f, -7.f}, {-4.f, -7.f}, {-3.f, -7.f}, {-2.f, -7.f}, {-1.f, -7.f},
  81. { 0.f, -7.f}, { 1.f, -7.f}, { 2.f, -7.f}, { 3.f, -7.f}, { 4.f, -7.f}, { 5.f, -7.f}, { 6.f, -7.f}, { 7.f, -7.f},
  82. {-8.f, -6.f}, {-7.f, -6.f}, {-6.f, -6.f}, {-5.f, -6.f}, {-4.f, -6.f}, {-3.f, -6.f}, {-2.f, -6.f}, {-1.f, -6.f},
  83. { 0.f, -6.f}, { 1.f, -6.f}, { 2.f, -6.f}, { 3.f, -6.f}, { 4.f, -6.f}, { 5.f, -6.f}, { 6.f, -6.f}, { 7.f, -6.f},
  84. {-8.f, -5.f}, {-7.f, -5.f}, {-6.f, -5.f}, {-5.f, -5.f}, {-4.f, -5.f}, {-3.f, -5.f}, {-2.f, -5.f}, {-1.f, -5.f},
  85. { 0.f, -5.f}, { 1.f, -5.f}, { 2.f, -5.f}, { 3.f, -5.f}, { 4.f, -5.f}, { 5.f, -5.f}, { 6.f, -5.f}, { 7.f, -5.f},
  86. {-8.f, -4.f}, {-7.f, -4.f}, {-6.f, -4.f}, {-5.f, -4.f}, {-4.f, -4.f}, {-3.f, -4.f}, {-2.f, -4.f}, {-1.f, -4.f},
  87. { 0.f, -4.f}, { 1.f, -4.f}, { 2.f, -4.f}, { 3.f, -4.f}, { 4.f, -4.f}, { 5.f, -4.f}, { 6.f, -4.f}, { 7.f, -4.f},
  88. {-8.f, -3.f}, {-7.f, -3.f}, {-6.f, -3.f}, {-5.f, -3.f}, {-4.f, -3.f}, {-3.f, -3.f}, {-2.f, -3.f}, {-1.f, -3.f},
  89. { 0.f, -3.f}, { 1.f, -3.f}, { 2.f, -3.f}, { 3.f, -3.f}, { 4.f, -3.f}, { 5.f, -3.f}, { 6.f, -3.f}, { 7.f, -3.f},
  90. {-8.f, -2.f}, {-7.f, -2.f}, {-6.f, -2.f}, {-5.f, -2.f}, {-4.f, -2.f}, {-3.f, -2.f}, {-2.f, -2.f}, {-1.f, -2.f},
  91. { 0.f, -2.f}, { 1.f, -2.f}, { 2.f, -2.f}, { 3.f, -2.f}, { 4.f, -2.f}, { 5.f, -2.f}, { 6.f, -2.f}, { 7.f, -2.f},
  92. {-8.f, -1.f}, {-7.f, -1.f}, {-6.f, -1.f}, {-5.f, -1.f}, {-4.f, -1.f}, {-3.f, -1.f}, {-2.f, -1.f}, {-1.f, -1.f},
  93. { 0.f, -1.f}, { 1.f, -1.f}, { 2.f, -1.f}, { 3.f, -1.f}, { 4.f, -1.f}, { 5.f, -1.f}, { 6.f, -1.f}, { 7.f, -1.f},
  94. {-8.f, 0.f}, {-7.f, 0.f}, {-6.f, 0.f}, {-5.f, 0.f}, {-4.f, 0.f}, {-3.f, 0.f}, {-2.f, 0.f}, {-1.f, 0.f},
  95. { 0.f, 0.f}, { 1.f, 0.f}, { 2.f, 0.f}, { 3.f, 0.f}, { 4.f, 0.f}, { 5.f, 0.f}, { 6.f, 0.f}, { 7.f, 0.f},
  96. {-8.f, 1.f}, {-7.f, 1.f}, {-6.f, 1.f}, {-5.f, 1.f}, {-4.f, 1.f}, {-3.f, 1.f}, {-2.f, 1.f}, {-1.f, 1.f},
  97. { 0.f, 1.f}, { 1.f, 1.f}, { 2.f, 1.f}, { 3.f, 1.f}, { 4.f, 1.f}, { 5.f, 1.f}, { 6.f, 1.f}, { 7.f, 1.f},
  98. {-8.f, 2.f}, {-7.f, 2.f}, {-6.f, 2.f}, {-5.f, 2.f}, {-4.f, 2.f}, {-3.f, 2.f}, {-2.f, 2.f}, {-1.f, 2.f},
  99. { 0.f, 2.f}, { 1.f, 2.f}, { 2.f, 2.f}, { 3.f, 2.f}, { 4.f, 2.f}, { 5.f, 2.f}, { 6.f, 2.f}, { 7.f, 2.f},
  100. {-8.f, 3.f}, {-7.f, 3.f}, {-6.f, 3.f}, {-5.f, 3.f}, {-4.f, 3.f}, {-3.f, 3.f}, {-2.f, 3.f}, {-1.f, 3.f},
  101. { 0.f, 3.f}, { 1.f, 3.f}, { 2.f, 3.f}, { 3.f, 3.f}, { 4.f, 3.f}, { 5.f, 3.f}, { 6.f, 3.f}, { 7.f, 3.f},
  102. {-8.f, 4.f}, {-7.f, 4.f}, {-6.f, 4.f}, {-5.f, 4.f}, {-4.f, 4.f}, {-3.f, 4.f}, {-2.f, 4.f}, {-1.f, 4.f},
  103. { 0.f, 4.f}, { 1.f, 4.f}, { 2.f, 4.f}, { 3.f, 4.f}, { 4.f, 4.f}, { 5.f, 4.f}, { 6.f, 4.f}, { 7.f, 4.f},
  104. {-8.f, 5.f}, {-7.f, 5.f}, {-6.f, 5.f}, {-5.f, 5.f}, {-4.f, 5.f}, {-3.f, 5.f}, {-2.f, 5.f}, {-1.f, 5.f},
  105. { 0.f, 5.f}, { 1.f, 5.f}, { 2.f, 5.f}, { 3.f, 5.f}, { 4.f, 5.f}, { 5.f, 5.f}, { 6.f, 5.f}, { 7.f, 5.f},
  106. {-8.f, 6.f}, {-7.f, 6.f}, {-6.f, 6.f}, {-5.f, 6.f}, {-4.f, 6.f}, {-3.f, 6.f}, {-2.f, 6.f}, {-1.f, 6.f},
  107. { 0.f, 6.f}, { 1.f, 6.f}, { 2.f, 6.f}, { 3.f, 6.f}, { 4.f, 6.f}, { 5.f, 6.f}, { 6.f, 6.f}, { 7.f, 6.f},
  108. {-8.f, 7.f}, {-7.f, 7.f}, {-6.f, 7.f}, {-5.f, 7.f}, {-4.f, 7.f}, {-3.f, 7.f}, {-2.f, 7.f}, {-1.f, 7.f},
  109. { 0.f, 7.f}, { 1.f, 7.f}, { 2.f, 7.f}, { 3.f, 7.f}, { 4.f, 7.f}, { 5.f, 7.f}, { 6.f, 7.f}, { 7.f, 7.f}
  110. };
  111. double sum = 0;
  112. for (int i=0; i<n; ++i) {
  113. float d = x->d;
  114. auto q = x->qs;
  115. float s = 0;
  116. for (int k=0; k<4; ++k) {
  117. s += y[0]*kValues[q[0]].first + y[1]*kValues[q[0]].second +
  118. y[2]*kValues[q[1]].first + y[3]*kValues[q[1]].second +
  119. y[4]*kValues[q[2]].first + y[5]*kValues[q[2]].second +
  120. y[6]*kValues[q[3]].first + y[7]*kValues[q[3]].second;
  121. y += 8; q += 4;
  122. }
  123. sum += s*d;
  124. ++x;
  125. }
  126. return sum;
  127. }
  128. inline double dot41(int n, const block_q4_1* x, const float* y) {
  129. const static float kValues[16] = {0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f};
  130. constexpr uint32_t kMask1 = 0x0f0f0f0f;
  131. uint32_t u1, u2;
  132. auto q1 = (const uint8_t*)&u1;
  133. auto q2 = (const uint8_t*)&u2;
  134. double sum = 0;
  135. for (int i=0; i<n; ++i) {
  136. auto u = (const uint32_t*)x->qs;
  137. float s = 0, s1 = 0;
  138. for (int k=0; k<4; ++k) {
  139. u1 = u[k] & kMask1;
  140. u2 = (u[k] >> 4) & kMask1;
  141. s += y[0]*kValues[q1[0]] + y[1]*kValues[q2[0]] +
  142. y[2]*kValues[q1[1]] + y[3]*kValues[q2[1]] +
  143. y[4]*kValues[q1[2]] + y[5]*kValues[q2[2]] +
  144. y[6]*kValues[q1[3]] + y[7]*kValues[q2[3]];
  145. s1 += y[0] + y[1] + y[2] + y[3] + y[4] + y[5] + y[6] + y[7];
  146. y += 8;
  147. }
  148. sum += s*x->d + s1*x->m;
  149. ++x;
  150. }
  151. return sum;
  152. }
  153. // Copy-pasted from ggml.c
  154. static void quantize_row_q8_0_reference(const float *x, block_q8_0 *y, int k) {
  155. assert(k % QK8_0 == 0);
  156. const int nb = k / QK8_0;
  157. for (int i = 0; i < nb; i++) {
  158. float amax = 0.0f; // absolute max
  159. for (int l = 0; l < QK8_0; l++) {
  160. const float v = x[i*QK8_0 + l];
  161. amax = std::max(amax, fabsf(v));
  162. }
  163. const float d = amax / ((1 << 7) - 1);
  164. const float id = d ? 1.0f/d : 0.0f;
  165. y[i].d = d;
  166. for (int l = 0; l < QK8_0; ++l) {
  167. const float v = x[i*QK8_0 + l]*id;
  168. y[i].qs[l] = roundf(v);
  169. }
  170. }
  171. }
  172. // Copy-pasted from ggml.c
  173. static void dot_q4_q8(const int n, float* s, const void* vx, const void* vy) {
  174. const int nb = n / QK8_0;
  175. const block_q4_0* x = (const block_q4_0*)vx;
  176. const block_q8_0* y = (const block_q8_0*)vy;
  177. float sumf = 0;
  178. for (int i = 0; i < nb; i++) {
  179. const float d0 = x[i].d;
  180. const float d1 = y[i].d;
  181. const uint8_t * p0 = x[i].qs;
  182. const int8_t * p1 = y[i].qs;
  183. int sumi = 0;
  184. for (int j = 0; j < QK8_0/2; j++) {
  185. const uint8_t v0 = p0[j];
  186. const int i0 = (int8_t) (v0 & 0xf) - 8;
  187. const int i1 = (int8_t) (v0 >> 4) - 8;
  188. const int i2 = p1[2*j + 0];
  189. const int i3 = p1[2*j + 1];
  190. sumi += i0*i2 + i1*i3;
  191. }
  192. sumf += d0*d1*sumi;
  193. }
  194. *s = sumf;
  195. }
  196. int main(int argc, char** argv) {
  197. int nloop = argc > 1 ? atoi(argv[1]) : 10;
  198. bool scalar = argc > 2 ? atoi(argv[2]) : false;
  199. bool useQ4_1 = argc > 3 ? atoi(argv[3]) : false;
  200. if (scalar && useQ4_1) {
  201. printf("It is not possible to use Q4_1 quantization and scalar implementations\n");
  202. return 1;
  203. }
  204. std::mt19937 rndm(1234);
  205. std::vector<float> x1(kVecSize), y1(kVecSize);
  206. int n4 = useQ4_1 ? kVecSize / QK4_1 : kVecSize / QK4_0; n4 = 64*((n4 + 63)/64);
  207. int n8 = kVecSize / QK8_0; n8 = 64*((n8 + 63)/64);
  208. auto funcs = useQ4_1 ? ggml_internal_get_quantize_fn(GGML_TYPE_Q4_1) : ggml_internal_get_quantize_fn(GGML_TYPE_Q4_0);
  209. std::vector<block_q4_0> q40;
  210. std::vector<block_q4_1> q41;
  211. if (useQ4_1) q41.resize(n4);
  212. else q40.resize(n4);
  213. std::vector<block_q8_0> q8(n8);
  214. std::vector<int64_t> H(16, 0);
  215. double sumt = 0, sumt2 = 0, maxt = 0;
  216. double sumqt = 0, sumqt2 = 0, maxqt = 0;
  217. double sum = 0, sumq = 0, exactSum = 0;
  218. for (int iloop=0; iloop<nloop; ++iloop) {
  219. // Fill vector x with random numbers
  220. fillRandomGaussianFloats(x1, rndm);
  221. // Fill vector y with random numbers
  222. fillRandomGaussianFloats(y1, rndm);
  223. // Compute the exact dot product
  224. for (int k=0; k<kVecSize; ++k) exactSum += x1[k]*y1[k];
  225. // quantize x.
  226. // Note, we do not include this in the timing as in practical application
  227. // we already have the quantized model weights.
  228. if (useQ4_1) {
  229. funcs.quantize_row_q(x1.data(), q41.data(), kVecSize);
  230. } else {
  231. funcs.quantize_row_q(x1.data(), q40.data(), kVecSize);
  232. }
  233. // Now measure time the dot product needs using the "scalar" version above
  234. auto t1 = std::chrono::high_resolution_clock::now();
  235. if (useQ4_1) sum += dot41(kVecSize / QK4_1, q41.data(), y1.data());
  236. else sum += dot(kVecSize / QK4_0, q40.data(), y1.data());
  237. auto t2 = std::chrono::high_resolution_clock::now();
  238. auto t = 1e-3*std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count();
  239. sumt += t; sumt2 += t*t; maxt = std::max(maxt, t);
  240. // And now measure the time needed to quantize y and perform the dot product with the quantized y
  241. t1 = std::chrono::high_resolution_clock::now();
  242. float result;
  243. if (scalar) {
  244. quantize_row_q8_0_reference(y1.data(), q8.data(), kVecSize);
  245. dot_q4_q8(kVecSize, &result, q40.data(), q8.data());
  246. }
  247. else {
  248. funcs.quantize_row_q_dot(y1.data(), q8.data(), kVecSize);
  249. if (useQ4_1) funcs.vec_dot_q(kVecSize, &result, q41.data(), q8.data());
  250. else funcs.vec_dot_q(kVecSize, &result, q40.data(), q8.data());
  251. }
  252. sumq += result;
  253. t2 = std::chrono::high_resolution_clock::now();
  254. t = 1e-3*std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count();
  255. sumqt += t; sumqt2 += t*t; maxqt = std::max(maxqt, t);
  256. }
  257. // Report the time (and the average of the dot products so the compiler does not come up with the idea
  258. // of optimizing away the function calls after figuring that the result is not used).
  259. sum /= nloop; sumq /= nloop;
  260. exactSum /= nloop;
  261. printf("Exact result: <dot> = %g\n",exactSum);
  262. printf("<dot> = %g, %g\n",sum,sumq);
  263. sumt /= nloop; sumt2 /= nloop; sumt2 -= sumt*sumt;
  264. if (sumt2 > 0) sumt2 = sqrt(sumt2);
  265. printf("time = %g +/- %g us. maxt = %g us\n",sumt,sumt2,maxt);
  266. sumqt /= nloop; sumqt2 /= nloop; sumqt2 -= sumqt*sumqt;
  267. if (sumqt2 > 0) sumqt2 = sqrt(sumqt2);
  268. printf("timeq = %g +/- %g us. maxt = %g us\n",sumqt,sumqt2,maxqt);
  269. return 0;
  270. }