binbcast.cu 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "binbcast.cuh"
  2. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  3. return b;
  4. GGML_UNUSED(a);
  5. }
  6. static __device__ __forceinline__ float op_add(const float a, const float b) {
  7. return a + b;
  8. }
  9. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  10. return a * b;
  11. }
  12. static __device__ __forceinline__ float op_div(const float a, const float b) {
  13. return a / b;
  14. }
  15. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  16. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  17. int ne0, int ne1, int ne2, int ne3,
  18. int ne10, int ne11, int ne12, int ne13,
  19. /*int s0, */ int s1, int s2, int s3,
  20. /*int s00,*/ int s01, int s02, int s03,
  21. /*int s10,*/ int s11, int s12, int s13) {
  22. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  23. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  24. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  25. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  26. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  27. return;
  28. }
  29. const int i11 = i1 % ne11;
  30. const int i12 = i2 % ne12;
  31. const int i13 = i3 % ne13;
  32. const size_t i_src0 = i3*s03 + i2*s02 + i1*s01;
  33. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  34. const size_t i_dst = i3*s3 + i2*s2 + i1*s1;
  35. const src0_t * src0_row = src0 + i_src0;
  36. const src1_t * src1_row = src1 + i_src1;
  37. dst_t * dst_row = dst + i_dst;
  38. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  39. const int i10 = i0 % ne10;
  40. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  41. }
  42. }
  43. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  44. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  45. int ne0, int ne1, int ne2, int ne3,
  46. int ne10, int ne11, int ne12, int ne13,
  47. /*int s0, */ int s1, int s2, int s3,
  48. /*int s00,*/ int s01, int s02, int s03,
  49. /*int s10,*/ int s11, int s12, int s13) {
  50. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  51. const int i3 = i/(ne2*ne1*ne0);
  52. const int i2 = (i/(ne1*ne0)) % ne2;
  53. const int i1 = (i/ne0) % ne1;
  54. const int i0 = i % ne0;
  55. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  56. return;
  57. }
  58. const int i11 = i1 % ne11;
  59. const int i12 = i2 % ne12;
  60. const int i13 = i3 % ne13;
  61. const size_t i_src0 = i3*s03 + i2*s02 + i1*s01;
  62. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  63. const size_t i_dst = i3*s3 + i2*s2 + i1*s1;
  64. const src0_t * src0_row = src0 + i_src0;
  65. const src1_t * src1_row = src1 + i_src1;
  66. dst_t * dst_row = dst + i_dst;
  67. const int i10 = i0 % ne10;
  68. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  69. }
  70. template<float (*bin_op)(const float, const float)>
  71. struct bin_bcast_cuda {
  72. template<typename src0_t, typename src1_t, typename dst_t>
  73. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  74. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  75. cudaStream_t stream) {
  76. GGML_TENSOR_BINARY_OP_LOCALS
  77. int nr0 = ne10/ne0;
  78. int nr1 = ne11/ne1;
  79. int nr2 = ne12/ne2;
  80. int nr3 = ne13/ne3;
  81. int nr[4] = { nr0, nr1, nr2, nr3 };
  82. // collapse dimensions until first broadcast dimension
  83. int64_t cne[] = {ne0, ne1, ne2, ne3};
  84. int64_t cne0[] = {ne00, ne01, ne02, ne03};
  85. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  86. size_t cnb[] = {nb0, nb1, nb2, nb3};
  87. size_t cnb0[] = {nb00, nb01, nb02, nb03};
  88. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  89. auto collapse = [](int64_t cne[]) {
  90. cne[0] *= cne[1];
  91. cne[1] = cne[2];
  92. cne[2] = cne[3];
  93. cne[3] = 1;
  94. };
  95. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  96. cnb[1] *= cne[1];
  97. cnb[2] *= cne[2];
  98. cnb[3] *= cne[3];
  99. };
  100. if (ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
  101. for (int i = 0; i < 4; i++) {
  102. if (nr[i] != 1) {
  103. break;
  104. }
  105. if (i > 0) {
  106. collapse_nb(cnb, cne);
  107. collapse_nb(cnb0, cne0);
  108. collapse_nb(cnb1, cne1);
  109. collapse(cne);
  110. collapse(cne0);
  111. collapse(cne1);
  112. }
  113. }
  114. }
  115. {
  116. int64_t ne0 = cne[0];
  117. int64_t ne1 = cne[1];
  118. int64_t ne2 = cne[2];
  119. int64_t ne3 = cne[3];
  120. //int64_t ne00 = cne0[0]; GGML_UNUSED(ne00);
  121. //int64_t ne01 = cne0[1]; GGML_UNUSED(ne01);
  122. //int64_t ne02 = cne0[2]; GGML_UNUSED(ne02);
  123. //int64_t ne03 = cne0[3]; GGML_UNUSED(ne03);
  124. int64_t ne10 = cne1[0];
  125. int64_t ne11 = cne1[1];
  126. int64_t ne12 = cne1[2];
  127. int64_t ne13 = cne1[3];
  128. size_t nb0 = cnb[0];
  129. size_t nb1 = cnb[1];
  130. size_t nb2 = cnb[2];
  131. size_t nb3 = cnb[3];
  132. size_t nb00 = cnb0[0];
  133. size_t nb01 = cnb0[1];
  134. size_t nb02 = cnb0[2];
  135. size_t nb03 = cnb0[3];
  136. size_t nb10 = cnb1[0];
  137. size_t nb11 = cnb1[1];
  138. size_t nb12 = cnb1[2];
  139. size_t nb13 = cnb1[3];
  140. size_t s0 = nb0 / sizeof(dst_t);
  141. size_t s1 = nb1 / sizeof(dst_t);
  142. size_t s2 = nb2 / sizeof(dst_t);
  143. size_t s3 = nb3 / sizeof(dst_t);
  144. size_t s10 = nb10 / sizeof(src1_t);
  145. size_t s11 = nb11 / sizeof(src1_t);
  146. size_t s12 = nb12 / sizeof(src1_t);
  147. size_t s13 = nb13 / sizeof(src1_t);
  148. size_t s00 = nb00 / sizeof(src0_t);
  149. size_t s01 = nb01 / sizeof(src0_t);
  150. size_t s02 = nb02 / sizeof(src0_t);
  151. size_t s03 = nb03 / sizeof(src0_t);
  152. GGML_ASSERT(nb0 % sizeof(dst_t) == 0);
  153. GGML_ASSERT(nb1 % sizeof(dst_t) == 0);
  154. GGML_ASSERT(nb2 % sizeof(dst_t) == 0);
  155. GGML_ASSERT(nb3 % sizeof(dst_t) == 0);
  156. GGML_ASSERT(nb00 % sizeof(src0_t) == 0);
  157. GGML_ASSERT(nb01 % sizeof(src0_t) == 0);
  158. GGML_ASSERT(nb02 % sizeof(src0_t) == 0);
  159. GGML_ASSERT(nb03 % sizeof(src0_t) == 0);
  160. GGML_ASSERT(nb10 % sizeof(src1_t) == 0);
  161. GGML_ASSERT(nb11 % sizeof(src1_t) == 0);
  162. GGML_ASSERT(nb12 % sizeof(src1_t) == 0);
  163. GGML_ASSERT(nb13 % sizeof(src1_t) == 0);
  164. GGML_ASSERT(s0 == 1);
  165. GGML_ASSERT(s00 == 1);
  166. GGML_ASSERT(s10 == 1);
  167. const int block_size = 128;
  168. int64_t hne0 = std::max(ne0/2LL, 1LL);
  169. dim3 block_dims;
  170. block_dims.x = std::min<unsigned int>(hne0, block_size);
  171. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  172. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  173. dim3 block_nums(
  174. (hne0 + block_dims.x - 1) / block_dims.x,
  175. (ne1 + block_dims.y - 1) / block_dims.y,
  176. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  177. );
  178. if (block_nums.z > 65535) {
  179. // this is the maximum number of blocks in z dimension, fallback to 1D grid kernel
  180. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  181. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  182. src0_dd, src1_dd, dst_dd,
  183. ne0, ne1, ne2, ne3,
  184. ne10, ne11, ne12, ne13,
  185. /* s0, */ s1, s2, s3,
  186. /* s00, */ s01, s02, s03,
  187. /* s10, */ s11, s12, s13);
  188. } else {
  189. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  190. src0_dd, src1_dd, dst_dd,
  191. ne0, ne1, ne2, ne3,
  192. ne10, ne11, ne12, ne13,
  193. /* s0, */ s1, s2, s3,
  194. /* s00, */ s01, s02, s03,
  195. /* s10, */ s11, s12, s13);
  196. }
  197. }
  198. }
  199. };
  200. template<class op>
  201. static void ggml_cuda_op_bin_bcast(
  202. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  203. const void * src0_dd, const void * src1_dd, void * dst_dd, cudaStream_t stream) {
  204. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  205. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  206. op()(src0, src1, dst, (const float *)src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
  207. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  208. op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (half *) dst_dd, stream);
  209. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  210. op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
  211. } else {
  212. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  213. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  214. GGML_ASSERT(false);
  215. }
  216. }
  217. void ggml_cuda_op_repeat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  218. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, dst->src[0], dst, nullptr, dst->src[0]->data, dst->data, ctx.stream());
  219. }
  220. void ggml_cuda_op_add(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  221. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
  222. }
  223. void ggml_cuda_op_mul(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  224. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
  225. }
  226. void ggml_cuda_op_div(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  227. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
  228. }