getrows.cu 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "getrows.cuh"
  27. #include "dequantize.cuh"
  28. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  29. static __global__ void k_get_rows(
  30. const void * src0, const int32_t * src1, dst_t * dst,
  31. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  32. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  33. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  34. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  35. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  36. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  37. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  38. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  39. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  40. if (i00 >= ne00) {
  41. return;
  42. }
  43. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  44. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  45. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  46. const int ib = i00/qk; // block index
  47. const int iqs = (i00%qk)/qr; // quant index
  48. const int iybs = i00 - i00%qk; // dst block start index
  49. const int y_offset = qr == 1 ? 1 : qk/2;
  50. // dequantize
  51. dfloat2 v;
  52. dequantize_kernel(src0_row, ib, iqs, v);
  53. dst_row[iybs + iqs + 0] = v.x;
  54. dst_row[iybs + iqs + y_offset] = v.y;
  55. }
  56. template<typename src0_t, typename dst_t>
  57. static __global__ void k_get_rows_float(
  58. const src0_t * src0, const int32_t * src1, dst_t * dst,
  59. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  60. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  61. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  62. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  63. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  64. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  65. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  66. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  67. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  68. if (i00 >= ne00) {
  69. return;
  70. }
  71. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  72. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  73. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  74. dst_row[i00] = src0_row[i00];
  75. }
  76. template<int qk, int qr, dequantize_kernel_t dq>
  77. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  78. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  79. GGML_TENSOR_BINARY_OP_LOCALS
  80. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  81. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  82. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  83. // strides in elements
  84. //const size_t s0 = nb0 / ggml_element_size(dst);
  85. const size_t s1 = nb1 / ggml_element_size(dst);
  86. const size_t s2 = nb2 / ggml_element_size(dst);
  87. const size_t s3 = nb3 / ggml_element_size(dst);
  88. const size_t s10 = nb10 / ggml_element_size(src1);
  89. const size_t s11 = nb11 / ggml_element_size(src1);
  90. const size_t s12 = nb12 / ggml_element_size(src1);
  91. //const size_t s13 = nb13 / ggml_element_size(src1);
  92. GGML_ASSERT(ne00 % 2 == 0);
  93. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  94. src0_dd, src1_dd, dst_dd,
  95. ne00, /*ne01, ne02, ne03,*/
  96. /*ne10, ne11,*/ ne12, /*ne13,*/
  97. /* s0,*/ s1, s2, s3,
  98. /* nb00,*/ nb01, nb02, nb03,
  99. s10, s11, s12/*, s13*/);
  100. GGML_UNUSED(dst);
  101. }
  102. template<typename src0_t>
  103. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  104. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  105. GGML_TENSOR_BINARY_OP_LOCALS
  106. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  107. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  108. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  109. // strides in elements
  110. //const size_t s0 = nb0 / ggml_element_size(dst);
  111. const size_t s1 = nb1 / ggml_element_size(dst);
  112. const size_t s2 = nb2 / ggml_element_size(dst);
  113. const size_t s3 = nb3 / ggml_element_size(dst);
  114. const size_t s10 = nb10 / ggml_element_size(src1);
  115. const size_t s11 = nb11 / ggml_element_size(src1);
  116. const size_t s12 = nb12 / ggml_element_size(src1);
  117. //const size_t s13 = nb13 / ggml_element_size(src1);
  118. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  119. src0_dd, src1_dd, dst_dd,
  120. ne00, /*ne01, ne02, ne03,*/
  121. /*ne10, ne11,*/ ne12, /*ne13,*/
  122. /* s0,*/ s1, s2, s3,
  123. /* nb00,*/ nb01, nb02, nb03,
  124. s10, s11, s12/*, s13*/);
  125. GGML_UNUSED(dst);
  126. }
  127. void ggml_cuda_op_get_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  128. const ggml_tensor * src0 = dst->src[0];
  129. const ggml_tensor * src1 = dst->src[1];
  130. const float * src0_d = (const float *)src0->data;
  131. const float * src1_d = (const float *)src1->data;
  132. float * dst_d = (float *)dst->data;
  133. cudaStream_t stream = ctx.stream();
  134. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  135. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  136. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  137. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  138. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  139. const int32_t * src1_i32 = (const int32_t *) src1_d;
  140. switch (src0->type) {
  141. case GGML_TYPE_F16:
  142. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  143. break;
  144. case GGML_TYPE_F32:
  145. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  146. break;
  147. case GGML_TYPE_Q4_0:
  148. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  149. break;
  150. case GGML_TYPE_Q4_1:
  151. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  152. break;
  153. case GGML_TYPE_Q5_0:
  154. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  155. break;
  156. case GGML_TYPE_Q5_1:
  157. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  158. break;
  159. case GGML_TYPE_Q8_0:
  160. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  161. break;
  162. default:
  163. // TODO: k-quants
  164. GGML_ABORT("%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  165. break;
  166. }
  167. }