concat.cu 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "concat.cuh"
  2. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  3. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  4. if (nidx >= ne0) {
  5. return;
  6. }
  7. // operation
  8. int offset_dst =
  9. nidx +
  10. blockIdx.y * ne0 +
  11. blockIdx.z * ne0 * gridDim.y;
  12. if (blockIdx.z < ne02) { // src0
  13. int offset_src =
  14. nidx +
  15. blockIdx.y * ne0 +
  16. blockIdx.z * ne0 * gridDim.y;
  17. dst[offset_dst] = x[offset_src];
  18. } else {
  19. int offset_src =
  20. nidx +
  21. blockIdx.y * ne0 +
  22. (blockIdx.z - ne02) * ne0 * gridDim.y;
  23. dst[offset_dst] = y[offset_src];
  24. }
  25. }
  26. static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
  27. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  28. dim3 gridDim(num_blocks, ne1, ne2);
  29. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  30. }
  31. void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  32. const ggml_tensor * src0 = dst->src[0];
  33. const ggml_tensor * src1 = dst->src[1];
  34. const float * src0_d = (const float *)src0->data;
  35. const float * src1_d = (const float *)src1->data;
  36. float * dst_d = (float *)dst->data;
  37. cudaStream_t stream = ctx.stream();
  38. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  39. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  40. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  41. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  42. concat_f32_cuda(src0_d + i3 * (src0->nb[3] / 4), src1_d + i3 * (src1->nb[3] / 4), dst_d + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], stream);
  43. }
  44. }