concat.cu 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "concat.cuh"
  2. static __global__ void concat_f32_dim0(const float * x, const float * y, float * dst, const int ne0, const int ne00) {
  3. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  4. if (nidx >= ne0) {
  5. return;
  6. }
  7. int offset_dst =
  8. nidx +
  9. blockIdx.y * ne0 +
  10. blockIdx.z * ne0 * gridDim.y;
  11. if (nidx < ne00) { // src0
  12. int offset_src =
  13. nidx +
  14. blockIdx.y * ne00 +
  15. blockIdx.z * ne00 * gridDim.y;
  16. dst[offset_dst] = x[offset_src];
  17. } else {
  18. int offset_src =
  19. (nidx - ne00) +
  20. blockIdx.y * (ne0 - ne00) +
  21. blockIdx.z * (ne0 - ne00) * gridDim.y;
  22. dst[offset_dst] = y[offset_src];
  23. }
  24. }
  25. static __global__ void concat_f32_dim1(const float * x, const float * y, float * dst, const int ne0, const int ne01) {
  26. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  27. if (nidx >= ne0) {
  28. return;
  29. }
  30. int offset_dst =
  31. nidx +
  32. blockIdx.y * ne0 +
  33. blockIdx.z * ne0 * gridDim.y;
  34. if (blockIdx.y < ne01) { // src0
  35. int offset_src =
  36. nidx +
  37. blockIdx.y * ne0 +
  38. blockIdx.z * ne0 * ne01;
  39. dst[offset_dst] = x[offset_src];
  40. } else {
  41. int offset_src =
  42. nidx +
  43. (blockIdx.y - ne01) * ne0 +
  44. blockIdx.z * ne0 * (gridDim.y - ne01);
  45. dst[offset_dst] = y[offset_src];
  46. }
  47. }
  48. static __global__ void concat_f32_dim2(const float * x, const float * y, float * dst, const int ne0, const int ne02) {
  49. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  50. if (nidx >= ne0) {
  51. return;
  52. }
  53. int offset_dst =
  54. nidx +
  55. blockIdx.y * ne0 +
  56. blockIdx.z * ne0 * gridDim.y;
  57. if (blockIdx.z < ne02) { // src0
  58. int offset_src =
  59. nidx +
  60. blockIdx.y * ne0 +
  61. blockIdx.z * ne0 * gridDim.y;
  62. dst[offset_dst] = x[offset_src];
  63. } else {
  64. int offset_src =
  65. nidx +
  66. blockIdx.y * ne0 +
  67. (blockIdx.z - ne02) * ne0 * gridDim.y;
  68. dst[offset_dst] = y[offset_src];
  69. }
  70. }
  71. 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) {
  72. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  73. dim3 gridDim(num_blocks, ne1, ne2);
  74. if (dim == 0) {
  75. concat_f32_dim0<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne00);
  76. return;
  77. }
  78. if (dim == 1) {
  79. concat_f32_dim1<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne01);
  80. return;
  81. }
  82. concat_f32_dim2<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  83. }
  84. void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  85. const ggml_tensor * src0 = dst->src[0];
  86. const ggml_tensor * src1 = dst->src[1];
  87. const float * src0_d = (const float *)src0->data;
  88. const float * src1_d = (const float *)src1->data;
  89. float * dst_d = (float *)dst->data;
  90. cudaStream_t stream = ctx.stream();
  91. const int32_t dim = ((int32_t *) dst->op_params)[0];
  92. GGML_ASSERT(ggml_is_contiguous(src0));
  93. GGML_ASSERT(ggml_is_contiguous(src1));
  94. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  95. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  96. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  97. if (dim != 3) {
  98. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  99. concat_f32_cuda(
  100. src0_d + i3 * (src0->nb[3] / 4),
  101. src1_d + i3 * (src1->nb[3] / 4),
  102. dst_d + i3 * ( dst->nb[3] / 4),
  103. src0->ne[0], src0->ne[1], src0->ne[2],
  104. dst->ne[0], dst->ne[1], dst->ne[2], dim, stream);
  105. }
  106. } else {
  107. const size_t size0 = ggml_nbytes(src0);
  108. const size_t size1 = ggml_nbytes(src1);
  109. CUDA_CHECK(cudaMemcpyAsync(dst_d, src0_d, size0, cudaMemcpyDeviceToDevice, stream));
  110. CUDA_CHECK(cudaMemcpyAsync(dst_d + size0/4, src1_d, size1, cudaMemcpyDeviceToDevice, stream));
  111. }
  112. }