concat.cu 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - 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 "concat.cuh"
  27. // contiguous kernels
  28. static __global__ void concat_f32_dim0(const float * x, const float * y, float * dst, const int ne0, const int ne00) {
  29. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  30. if (nidx >= ne0) {
  31. return;
  32. }
  33. int offset_dst =
  34. nidx +
  35. blockIdx.y * ne0 +
  36. blockIdx.z * ne0 * gridDim.y;
  37. if (nidx < ne00) { // src0
  38. int offset_src =
  39. nidx +
  40. blockIdx.y * ne00 +
  41. blockIdx.z * ne00 * gridDim.y;
  42. dst[offset_dst] = x[offset_src];
  43. } else {
  44. int offset_src =
  45. (nidx - ne00) +
  46. blockIdx.y * (ne0 - ne00) +
  47. blockIdx.z * (ne0 - ne00) * gridDim.y;
  48. dst[offset_dst] = y[offset_src];
  49. }
  50. }
  51. static __global__ void concat_f32_dim1(const float * x, const float * y, float * dst, const int ne0, const int ne01) {
  52. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  53. if (nidx >= ne0) {
  54. return;
  55. }
  56. int offset_dst =
  57. nidx +
  58. blockIdx.y * ne0 +
  59. blockIdx.z * ne0 * gridDim.y;
  60. if (blockIdx.y < ne01) { // src0
  61. int offset_src =
  62. nidx +
  63. blockIdx.y * ne0 +
  64. blockIdx.z * ne0 * ne01;
  65. dst[offset_dst] = x[offset_src];
  66. } else {
  67. int offset_src =
  68. nidx +
  69. (blockIdx.y - ne01) * ne0 +
  70. blockIdx.z * ne0 * (gridDim.y - ne01);
  71. dst[offset_dst] = y[offset_src];
  72. }
  73. }
  74. static __global__ void concat_f32_dim2(const float * x, const float * y, float * dst, const int ne0, const int ne02) {
  75. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  76. if (nidx >= ne0) {
  77. return;
  78. }
  79. int offset_dst =
  80. nidx +
  81. blockIdx.y * ne0 +
  82. blockIdx.z * ne0 * gridDim.y;
  83. if (blockIdx.z < ne02) { // src0
  84. int offset_src =
  85. nidx +
  86. blockIdx.y * ne0 +
  87. blockIdx.z * ne0 * gridDim.y;
  88. dst[offset_dst] = x[offset_src];
  89. } else {
  90. int offset_src =
  91. nidx +
  92. blockIdx.y * ne0 +
  93. (blockIdx.z - ne02) * ne0 * gridDim.y;
  94. dst[offset_dst] = y[offset_src];
  95. }
  96. }
  97. static void concat_f32_cuda(const float * x, const float * y, float * dst, int ne00, int ne01, int ne02, int ne0, int ne1, int ne2, int dim, cudaStream_t stream) {
  98. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  99. dim3 gridDim(num_blocks, ne1, ne2);
  100. if (dim == 0) {
  101. concat_f32_dim0<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne00);
  102. return;
  103. }
  104. if (dim == 1) {
  105. concat_f32_dim1<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne01);
  106. return;
  107. }
  108. concat_f32_dim2<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  109. }
  110. // non-contiguous kernel (slow)
  111. template <int dim>
  112. static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE)
  113. concat_f32_non_cont(
  114. const char * src0,
  115. const char * src1,
  116. char * dst,
  117. int64_t ne00,
  118. int64_t ne01,
  119. int64_t ne02,
  120. int64_t ne03,
  121. uint64_t nb00,
  122. uint64_t nb01,
  123. uint64_t nb02,
  124. uint64_t nb03,
  125. int64_t /*ne10*/,
  126. int64_t /*ne11*/,
  127. int64_t /*ne12*/,
  128. int64_t /*ne13*/,
  129. uint64_t nb10,
  130. uint64_t nb11,
  131. uint64_t nb12,
  132. uint64_t nb13,
  133. int64_t ne0,
  134. int64_t /*ne1*/,
  135. int64_t /*ne2*/,
  136. int64_t /*ne3*/,
  137. uint64_t nb0,
  138. uint64_t nb1,
  139. uint64_t nb2,
  140. uint64_t nb3){
  141. static_assert(dim >= 0 && dim <= 3, "dim must be between 0 and 3");
  142. const int64_t i3 = blockIdx.z;
  143. const int64_t i2 = blockIdx.y;
  144. const int64_t i1 = blockIdx.x;
  145. const float * x;
  146. for (int64_t i0 = threadIdx.x; i0 < ne0; i0 += blockDim.x) {
  147. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  148. x = (const float *)(src0 + (i3 )*nb03 + (i2 )*nb02 + (i1 )*nb01 + (i0 )*nb00);
  149. } else {
  150. if constexpr (dim == 0) {
  151. x = (const float *) (src1 + i3 * nb13 + i2 * nb12 + i1 * nb11 + (i0 - ne00) * nb10);
  152. } else if constexpr (dim == 1) {
  153. x = (const float *) (src1 + i3 * nb13 + i2 * nb12 + (i1 - ne01) * nb11 + i0 * nb10);
  154. } else if constexpr (dim == 2) {
  155. x = (const float *) (src1 + i3 * nb13 + (i2 - ne02) * nb12 + i1 * nb11 + i0 * nb10);
  156. } else if constexpr (dim == 3) {
  157. x = (const float *) (src1 + (i3 - ne03) * nb13 + i2 * nb12 + i1 * nb11 + i0 * nb10);
  158. }
  159. }
  160. float * y = (float *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  161. *y = *x;
  162. }
  163. }
  164. void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  165. const ggml_tensor * src0 = dst->src[0];
  166. const ggml_tensor * src1 = dst->src[1];
  167. cudaStream_t stream = ctx.stream();
  168. const int32_t dim = ((int32_t *) dst->op_params)[0];
  169. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  170. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  171. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  172. if (ggml_is_contiguous(src0) && ggml_is_contiguous(src1)) {
  173. const float * src0_d = (const float *)src0->data;
  174. const float * src1_d = (const float *)src1->data;
  175. float * dst_d = (float *)dst->data;
  176. if (dim != 3) {
  177. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  178. concat_f32_cuda(
  179. src0_d + i3 * (src0->nb[3] / 4),
  180. src1_d + i3 * (src1->nb[3] / 4),
  181. dst_d + i3 * ( dst->nb[3] / 4),
  182. src0->ne[0], src0->ne[1], src0->ne[2],
  183. dst->ne[0], dst->ne[1], dst->ne[2], dim, stream);
  184. }
  185. } else {
  186. const size_t size0 = ggml_nbytes(src0);
  187. const size_t size1 = ggml_nbytes(src1);
  188. CUDA_CHECK(cudaMemcpyAsync(dst_d, src0_d, size0, cudaMemcpyDeviceToDevice, stream));
  189. CUDA_CHECK(cudaMemcpyAsync(dst_d + size0/4, src1_d, size1, cudaMemcpyDeviceToDevice, stream));
  190. }
  191. } else {
  192. dim3 grid_dim(dst->ne[1], dst->ne[2], dst->ne[3]);
  193. auto launch_kernel = [&](auto dim) {
  194. concat_f32_non_cont<dim><<<grid_dim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(
  195. (const char *) src0->data, (const char *) src1->data, (char *) dst->data,
  196. src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
  197. src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
  198. src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3],
  199. src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3],
  200. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
  201. dst->nb[0], dst->nb[1], dst->nb[2], dst->nb[3]);
  202. };
  203. switch (dim) {
  204. case 0:
  205. launch_kernel(std::integral_constant<int, 0>{});
  206. break;
  207. case 1:
  208. launch_kernel(std::integral_constant<int, 1>{});
  209. break;
  210. case 2:
  211. launch_kernel(std::integral_constant<int, 2>{});
  212. break;
  213. case 3:
  214. launch_kernel(std::integral_constant<int, 3>{});
  215. break;
  216. default:
  217. GGML_ABORT("Invalid dim: %d", dim);
  218. break;
  219. }
  220. }
  221. }