quantize.cu 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * llama.cpp - commit 8962422b1c6f9b8b15f5aeaea42600bcc2d44177 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  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. #include "quantize.cuh"
  27. #include <cstdint>
  28. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int64_t kx, const int64_t kx0_padded) {
  29. const int64_t ix0 = (int64_t)blockDim.x*blockIdx.x + threadIdx.x;
  30. if (ix0 >= kx0_padded) {
  31. return;
  32. }
  33. const int64_t ix1 = blockIdx.y;
  34. const int64_t i_padded = ix1*kx0_padded + ix0;
  35. block_q8_1 * y = (block_q8_1 *) vy;
  36. const int64_t ib = i_padded / QK8_1; // block index
  37. const int64_t iqs = i_padded % QK8_1; // quant index
  38. const float xi = ix0 < kx ? x[ix1*kx + ix0] : 0.0f;
  39. float amax = fabsf(xi);
  40. float sum = xi;
  41. amax = warp_reduce_max(amax);
  42. sum = warp_reduce_sum(sum);
  43. const float d = amax / 127;
  44. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  45. y[ib].qs[iqs] = q;
  46. if (iqs > 0) {
  47. return;
  48. }
  49. reinterpret_cast<half&>(y[ib].ds.x) = d;
  50. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  51. }
  52. template <mmq_q8_1_ds_layout ds_layout>
  53. static __global__ void quantize_mmq_q8_1(
  54. const float * __restrict__ x, void * __restrict__ vy, const int64_t kx0, const int64_t kx1, const int64_t kx0_padded) {
  55. constexpr int vals_per_scale = ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6 ? 64 : 32;
  56. constexpr int vals_per_sum = ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6 ? 16 : 32;
  57. const int64_t ix0 = ((int64_t)blockDim.x*blockIdx.x + threadIdx.x)*4;
  58. if (ix0 >= kx0_padded) {
  59. return;
  60. }
  61. const float4 * x4 = (const float4 *) x;
  62. const int64_t ix1 = kx1*blockIdx.z + blockIdx.y;
  63. block_q8_1_mmq * y = (block_q8_1_mmq *) vy;
  64. const int64_t ib0 = blockIdx.z*((int64_t)gridDim.y*gridDim.x*blockDim.x/QK8_1); // first block of channel
  65. const int64_t ib = ib0 + (ix0 / (4*QK8_1))*kx1 + blockIdx.y; // block index in channel
  66. const int64_t iqs = ix0 % (4*QK8_1); // quant index in block
  67. // Load 4 floats per thread and calculate max. abs. value between them:
  68. const float4 xi = ix0 < kx0 ? x4[(ix1*kx0 + ix0)/4] : make_float4(0.0f, 0.0f, 0.0f, 0.0f);
  69. float amax = fabsf(xi.x);
  70. amax = fmaxf(amax, fabsf(xi.y));
  71. amax = fmaxf(amax, fabsf(xi.z));
  72. amax = fmaxf(amax, fabsf(xi.w));
  73. // Exchange max. abs. value between vals_per_scale/4 threads.
  74. #pragma unroll
  75. for (int mask = vals_per_scale/8; mask > 0; mask >>= 1) {
  76. amax = fmaxf(amax, __shfl_xor_sync(0xFFFFFFFF, amax, mask, WARP_SIZE));
  77. }
  78. float sum;
  79. if (ds_layout != MMQ_Q8_1_DS_LAYOUT_D4) {
  80. sum = xi.x + xi.y + xi.z + xi.w;
  81. // Exchange calculate sum across vals_per_sum/4 threads.
  82. #pragma unroll
  83. for (int mask = vals_per_sum/8; mask > 0; mask >>= 1) {
  84. sum += __shfl_xor_sync(0xFFFFFFFF, sum, mask, WARP_SIZE);
  85. }
  86. }
  87. const float d_inv = 127.0f / amax;
  88. char4 q;
  89. q.x = roundf(xi.x*d_inv);
  90. q.y = roundf(xi.y*d_inv);
  91. q.z = roundf(xi.z*d_inv);
  92. q.w = roundf(xi.w*d_inv);
  93. // Write back 4 int8 values as a single 32 bit value for better memroy bandwidth:
  94. char4 * yqs4 = (char4 *) y[ib].qs;
  95. yqs4[iqs/4] = q;
  96. if (ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6) {
  97. if (iqs % 16 != 0 || iqs >= 96) {
  98. return;
  99. }
  100. y[ib].d2s6[2 + iqs/16] = sum;
  101. if (iqs % 64 != 0) {
  102. return;
  103. }
  104. const float d = 1.0f / d_inv;
  105. y[ib].d2s6[iqs/64] = d;
  106. return;
  107. }
  108. if (iqs % 32 != 0) {
  109. return;
  110. }
  111. const float d = 1.0f / d_inv;
  112. if (ds_layout == MMQ_Q8_1_DS_LAYOUT_DS4) {
  113. y[ib].ds4[iqs/32] = make_half2(d, sum);
  114. } else {
  115. y[ib].d4[iqs/32] = d;
  116. }
  117. }
  118. void quantize_row_q8_1_cuda(
  119. const float * x, void * vy, const int64_t kx0, const int64_t kx1, const int64_t channels,
  120. const int64_t kx0_padded, const ggml_type type_x, cudaStream_t stream) {
  121. GGML_ASSERT(kx0_padded % QK8_1 == 0);
  122. const int64_t block_num_x = (kx0_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  123. const dim3 num_blocks(block_num_x, kx1*channels, 1);
  124. const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE, 1, 1);
  125. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx0, kx0_padded);
  126. GGML_UNUSED(type_x);
  127. }
  128. void quantize_mmq_q8_1_cuda(
  129. const float * x, void * vy, const int64_t kx0, const int64_t kx1, const int64_t channels,
  130. const int64_t kx0_padded, const ggml_type type_x, cudaStream_t stream) {
  131. GGML_ASSERT(kx0_padded % (4*QK8_1) == 0);
  132. const int64_t block_num_x = (kx0_padded + 4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ - 1) / (4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ);
  133. const dim3 num_blocks(block_num_x, kx1, channels);
  134. const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);
  135. switch (mmq_get_q8_1_ds_layout(type_x)) {
  136. case MMQ_Q8_1_DS_LAYOUT_D4:
  137. quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D4>
  138. <<<num_blocks, block_size, 0, stream>>>(x, vy, kx0, kx1, kx0_padded);
  139. break;
  140. case MMQ_Q8_1_DS_LAYOUT_DS4:
  141. quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4>
  142. <<<num_blocks, block_size, 0, stream>>>(x, vy, kx0, kx1, kx0_padded);
  143. break;
  144. case MMQ_Q8_1_DS_LAYOUT_D2S6:
  145. quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D2S6>
  146. <<<num_blocks, block_size, 0, stream>>>(x, vy, kx0, kx1, kx0_padded);
  147. break;
  148. default:
  149. GGML_ABORT("fatal error");
  150. break;
  151. }
  152. }