im2col.cu 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "im2col.cuh"
  2. template <typename T>
  3. static __global__ void im2col_kernel(
  4. const float * x, T * dst, int64_t batch_offset,
  5. int64_t offset_delta, int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH, int64_t pelements, int64_t CHW,
  6. int s0, int s1, int p0, int p1, int d0, int d1) {
  7. const int64_t i = threadIdx.x + blockIdx.x * blockDim.x;
  8. if (i >= pelements) {
  9. return;
  10. }
  11. const int64_t ksize = OW * (KH > 1 ? KW : 1);
  12. const int64_t kx = i / ksize;
  13. const int64_t kd = kx * ksize;
  14. const int64_t ky = (i - kd) / OW;
  15. const int64_t ix = i % OW;
  16. const int64_t oh = blockIdx.y;
  17. const int64_t batch = blockIdx.z / IC;
  18. const int64_t ic = blockIdx.z % IC;
  19. const int64_t iiw = ix * s0 + kx * d0 - p0;
  20. const int64_t iih = oh * s1 + ky * d1 - p1;
  21. const int64_t offset_dst =
  22. ((batch * OH + oh) * OW + ix) * CHW +
  23. (ic * (KW * KH) + ky * KW + kx);
  24. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  25. dst[offset_dst] = 0.0f;
  26. } else {
  27. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  28. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  29. }
  30. }
  31. template <typename T>
  32. static void im2col_cuda(const float * x, T* dst,
  33. int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
  34. int64_t batch, int64_t batch_offset, int64_t offset_delta,
  35. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  36. const int parallel_elements = OW * KW * KH;
  37. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  38. dim3 block_nums(num_blocks, OH, batch * IC);
  39. im2col_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  40. }
  41. static void im2col_cuda_f16(const float * x, half * dst,
  42. int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
  43. int64_t batch, int64_t batch_offset, int64_t offset_delta,
  44. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  45. im2col_cuda<half>(x, dst, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, offset_delta, s0, s1, p0, p1, d0, d1, stream);
  46. }
  47. static void im2col_cuda_f32(const float * x, float * dst,
  48. int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
  49. int64_t batch, int64_t batch_offset, int64_t offset_delta,
  50. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  51. im2col_cuda<float>(x, dst, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, offset_delta, s0, s1, p0, p1, d0, d1, stream);
  52. }
  53. void ggml_cuda_op_im2col(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  54. const ggml_tensor * src0 = dst->src[0];
  55. const ggml_tensor * src1 = dst->src[1];
  56. const float * src1_d = (const float *)src1->data;
  57. float * dst_d = (float *)dst->data;
  58. cudaStream_t stream = ctx.stream();
  59. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  60. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  61. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  62. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  63. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  64. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  65. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  66. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  67. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  68. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  69. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  70. const int64_t IH = is_2D ? src1->ne[1] : 1;
  71. const int64_t IW = src1->ne[0];
  72. const int64_t KH = is_2D ? src0->ne[1] : 1;
  73. const int64_t KW = src0->ne[0];
  74. const int64_t OH = is_2D ? dst->ne[2] : 1;
  75. const int64_t OW = dst->ne[1];
  76. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  77. const int64_t batch = src1->ne[3];
  78. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  79. if(dst->type == GGML_TYPE_F16) {
  80. im2col_cuda_f16(src1_d, (half *) dst_d, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, stream);
  81. } else {
  82. im2col_cuda_f32(src1_d, (float *) dst_d, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, stream);
  83. }
  84. }