binbcast.cu 12 KB

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