concat.cu 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "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. static __global__ void concat_f32_non_cont(
  112. const char * src0,
  113. const char * src1,
  114. char * dst,
  115. int64_t ne00,
  116. int64_t ne01,
  117. int64_t ne02,
  118. int64_t ne03,
  119. uint64_t nb00,
  120. uint64_t nb01,
  121. uint64_t nb02,
  122. uint64_t nb03,
  123. int64_t /*ne10*/,
  124. int64_t /*ne11*/,
  125. int64_t /*ne12*/,
  126. int64_t /*ne13*/,
  127. uint64_t nb10,
  128. uint64_t nb11,
  129. uint64_t nb12,
  130. uint64_t nb13,
  131. int64_t ne0,
  132. int64_t /*ne1*/,
  133. int64_t /*ne2*/,
  134. int64_t /*ne3*/,
  135. uint64_t nb0,
  136. uint64_t nb1,
  137. uint64_t nb2,
  138. uint64_t nb3,
  139. int32_t dim) {
  140. const int64_t i3 = blockIdx.z;
  141. const int64_t i2 = blockIdx.y;
  142. const int64_t i1 = blockIdx.x;
  143. int64_t o[4] = {0, 0, 0, 0};
  144. o[dim] = dim == 0 ? ne00 : (dim == 1 ? ne01 : (dim == 2 ? ne02 : ne03));
  145. const float * x;
  146. for (int 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. x = (const float *)(src1 + (i3 - o[3])*nb13 + (i2 - o[2])*nb12 + (i1 - o[1])*nb11 + (i0 - o[0])*nb10);
  151. }
  152. float * y = (float *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  153. *y = *x;
  154. }
  155. }
  156. void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  157. const ggml_tensor * src0 = dst->src[0];
  158. const ggml_tensor * src1 = dst->src[1];
  159. cudaStream_t stream = ctx.stream();
  160. const int32_t dim = ((int32_t *) dst->op_params)[0];
  161. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  162. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  163. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  164. if (ggml_is_contiguous(src0) && ggml_is_contiguous(src1)) {
  165. const float * src0_d = (const float *)src0->data;
  166. const float * src1_d = (const float *)src1->data;
  167. float * dst_d = (float *)dst->data;
  168. if (dim != 3) {
  169. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  170. concat_f32_cuda(
  171. src0_d + i3 * (src0->nb[3] / 4),
  172. src1_d + i3 * (src1->nb[3] / 4),
  173. dst_d + i3 * ( dst->nb[3] / 4),
  174. src0->ne[0], src0->ne[1], src0->ne[2],
  175. dst->ne[0], dst->ne[1], dst->ne[2], dim, stream);
  176. }
  177. } else {
  178. const size_t size0 = ggml_nbytes(src0);
  179. const size_t size1 = ggml_nbytes(src1);
  180. CUDA_CHECK(cudaMemcpyAsync(dst_d, src0_d, size0, cudaMemcpyDeviceToDevice, stream));
  181. CUDA_CHECK(cudaMemcpyAsync(dst_d + size0/4, src1_d, size1, cudaMemcpyDeviceToDevice, stream));
  182. }
  183. } else {
  184. dim3 grid_dim(dst->ne[1], dst->ne[2], dst->ne[3]);
  185. concat_f32_non_cont<<<grid_dim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(
  186. (const char *)src0->data,
  187. (const char *)src1->data,
  188. ( char *)dst->data,
  189. src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
  190. src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
  191. src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3],
  192. src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3],
  193. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
  194. dst->nb[0], dst->nb[1], dst->nb[2], dst->nb[3], dim);
  195. }
  196. }