binbcast.cu 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - 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. #include <cstdint>
  28. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  29. return b;
  30. GGML_UNUSED(a);
  31. }
  32. static __device__ __forceinline__ float op_add(const float a, const float b) {
  33. return a + b;
  34. }
  35. static __device__ __forceinline__ float op_sub(const float a, const float b) {
  36. return a - b;
  37. }
  38. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  39. return a * b;
  40. }
  41. static __device__ __forceinline__ float op_div(const float a, const float b) {
  42. return a / b;
  43. }
  44. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  45. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  46. int ne0, int ne1, int ne2, int ne3,
  47. int ne10, int ne11, int ne12, int ne13,
  48. /*int s0, */ int s1, int s2, int s3,
  49. /*int s00,*/ int s01, int s02, int s03,
  50. /*int s10,*/ int s11, int s12, int s13) {
  51. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  52. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  53. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  54. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  55. if (i0s >= 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. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  68. const int i10 = i0 % ne10;
  69. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  70. }
  71. }
  72. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  73. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  74. int ne0, int ne1, int ne2, int ne3,
  75. int ne10, int ne11, int ne12, int ne13,
  76. /*int s0, */ int s1, int s2, int s3,
  77. /*int s00,*/ int s01, int s02, int s03,
  78. /*int s10,*/ int s11, int s12, int s13) {
  79. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  80. const int i3 = i/(ne2*ne1*ne0);
  81. const int i2 = (i/(ne1*ne0)) % ne2;
  82. const int i1 = (i/ne0) % ne1;
  83. const int i0 = i % ne0;
  84. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  85. return;
  86. }
  87. const int i11 = i1 % ne11;
  88. const int i12 = i2 % ne12;
  89. const int i13 = i3 % ne13;
  90. const size_t i_src0 = i3*s03 + i2*s02 + i1*s01;
  91. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  92. const size_t i_dst = i3*s3 + i2*s2 + i1*s1;
  93. const src0_t * src0_row = src0 + i_src0;
  94. const src1_t * src1_row = src1 + i_src1;
  95. dst_t * dst_row = dst + i_dst;
  96. const int i10 = i0 % ne10;
  97. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  98. }
  99. template <typename T>
  100. static __global__ void k_repeat_back(
  101. const T * __restrict__ src, T * __restrict__ dst, const int64_t ne00, const int64_t ne01, const int64_t ne02,
  102. const int64_t ne0, const int64_t ne1, const int64_t ne2) {
  103. const int64_t tid0 = (int64_t) blockIdx.x*blockDim.x + threadIdx.x;
  104. const int64_t tid1 = (int64_t) blockIdx.y*blockDim.y + threadIdx.y;
  105. const int64_t tid2 = (int64_t) blockIdx.z*blockDim.z + threadIdx.z;
  106. if (tid0 >= ne0) {
  107. return;
  108. }
  109. T sum = 0;
  110. for (int64_t i2 = tid2; i2 < ne02; i2 += ne2) {
  111. for (int64_t i1 = tid1; i1 < ne01; i1 += ne1) {
  112. for (int64_t i0 = tid0; i0 < ne00; i0 += ne0) {
  113. sum += src[i2*ne01*ne00 + i1*ne00 + i0];
  114. }
  115. }
  116. }
  117. dst[tid2*ne1*ne0 + tid1*ne0 + tid0] = sum;
  118. }
  119. template<float (*bin_op)(const float, const float)>
  120. struct bin_bcast_cuda {
  121. template<typename src0_t, typename src1_t, typename dst_t>
  122. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  123. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  124. cudaStream_t stream) {
  125. GGML_TENSOR_BINARY_OP_LOCALS
  126. int nr0 = ne10/ne0;
  127. int nr1 = ne11/ne1;
  128. int nr2 = ne12/ne2;
  129. int nr3 = ne13/ne3;
  130. int nr[4] = { nr0, nr1, nr2, nr3 };
  131. // collapse dimensions until first broadcast dimension
  132. int64_t cne[] = {ne0, ne1, ne2, ne3};
  133. int64_t cne0[] = {ne00, ne01, ne02, ne03};
  134. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  135. size_t cnb[] = {nb0, nb1, nb2, nb3};
  136. size_t cnb0[] = {nb00, nb01, nb02, nb03};
  137. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  138. auto collapse = [](int64_t cne[]) {
  139. cne[0] *= cne[1];
  140. cne[1] = cne[2];
  141. cne[2] = cne[3];
  142. cne[3] = 1;
  143. };
  144. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  145. cnb[1] *= cne[1];
  146. cnb[2] *= cne[2];
  147. cnb[3] *= cne[3];
  148. };
  149. if (ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
  150. for (int i = 0; i < 4; i++) {
  151. if (nr[i] != 1) {
  152. break;
  153. }
  154. if (i > 0) {
  155. collapse_nb(cnb, cne);
  156. collapse_nb(cnb0, cne0);
  157. collapse_nb(cnb1, cne1);
  158. collapse(cne);
  159. collapse(cne0);
  160. collapse(cne1);
  161. }
  162. }
  163. }
  164. {
  165. int64_t ne0 = cne[0];
  166. int64_t ne1 = cne[1];
  167. int64_t ne2 = cne[2];
  168. int64_t ne3 = cne[3];
  169. //int64_t ne00 = cne0[0]; GGML_UNUSED(ne00);
  170. //int64_t ne01 = cne0[1]; GGML_UNUSED(ne01);
  171. //int64_t ne02 = cne0[2]; GGML_UNUSED(ne02);
  172. //int64_t ne03 = cne0[3]; GGML_UNUSED(ne03);
  173. int64_t ne10 = cne1[0];
  174. int64_t ne11 = cne1[1];
  175. int64_t ne12 = cne1[2];
  176. int64_t ne13 = cne1[3];
  177. size_t nb0 = cnb[0];
  178. size_t nb1 = cnb[1];
  179. size_t nb2 = cnb[2];
  180. size_t nb3 = cnb[3];
  181. size_t nb00 = cnb0[0];
  182. size_t nb01 = cnb0[1];
  183. size_t nb02 = cnb0[2];
  184. size_t nb03 = cnb0[3];
  185. size_t nb10 = cnb1[0];
  186. size_t nb11 = cnb1[1];
  187. size_t nb12 = cnb1[2];
  188. size_t nb13 = cnb1[3];
  189. size_t s0 = nb0 / sizeof(dst_t);
  190. size_t s1 = nb1 / sizeof(dst_t);
  191. size_t s2 = nb2 / sizeof(dst_t);
  192. size_t s3 = nb3 / sizeof(dst_t);
  193. size_t s10 = nb10 / sizeof(src1_t);
  194. size_t s11 = nb11 / sizeof(src1_t);
  195. size_t s12 = nb12 / sizeof(src1_t);
  196. size_t s13 = nb13 / sizeof(src1_t);
  197. size_t s00 = nb00 / sizeof(src0_t);
  198. size_t s01 = nb01 / sizeof(src0_t);
  199. size_t s02 = nb02 / sizeof(src0_t);
  200. size_t s03 = nb03 / sizeof(src0_t);
  201. GGML_ASSERT(nb0 % sizeof(dst_t) == 0);
  202. GGML_ASSERT(nb1 % sizeof(dst_t) == 0);
  203. GGML_ASSERT(nb2 % sizeof(dst_t) == 0);
  204. GGML_ASSERT(nb3 % sizeof(dst_t) == 0);
  205. GGML_ASSERT(nb00 % sizeof(src0_t) == 0);
  206. GGML_ASSERT(nb01 % sizeof(src0_t) == 0);
  207. GGML_ASSERT(nb02 % sizeof(src0_t) == 0);
  208. GGML_ASSERT(nb03 % sizeof(src0_t) == 0);
  209. GGML_ASSERT(nb10 % sizeof(src1_t) == 0);
  210. GGML_ASSERT(nb11 % sizeof(src1_t) == 0);
  211. GGML_ASSERT(nb12 % sizeof(src1_t) == 0);
  212. GGML_ASSERT(nb13 % sizeof(src1_t) == 0);
  213. GGML_ASSERT(s0 == 1);
  214. GGML_ASSERT(s00 == 1);
  215. GGML_ASSERT(s10 == 1);
  216. const int block_size = 128;
  217. int64_t hne0 = std::max(ne0/2LL, 1LL);
  218. dim3 block_dims;
  219. block_dims.x = std::min<unsigned int>(hne0, block_size);
  220. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  221. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  222. dim3 block_nums(
  223. (hne0 + block_dims.x - 1) / block_dims.x,
  224. (ne1 + block_dims.y - 1) / block_dims.y,
  225. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  226. );
  227. if (block_nums.z > 65535) {
  228. // this is the maximum number of blocks in z dimension, fallback to 1D grid kernel
  229. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  230. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  231. src0_dd, src1_dd, dst_dd,
  232. ne0, ne1, ne2, ne3,
  233. ne10, ne11, ne12, ne13,
  234. /* s0, */ s1, s2, s3,
  235. /* s00, */ s01, s02, s03,
  236. /* s10, */ s11, s12, s13);
  237. } else {
  238. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  239. src0_dd, src1_dd, dst_dd,
  240. ne0, ne1, ne2, ne3,
  241. ne10, ne11, ne12, ne13,
  242. /* s0, */ s1, s2, s3,
  243. /* s00, */ s01, s02, s03,
  244. /* s10, */ s11, s12, s13);
  245. }
  246. }
  247. }
  248. };
  249. template <typename T>
  250. static void repeat_back_cuda(
  251. const T * src, T * dst, const int64_t ne00, const int64_t ne01, const int64_t ne02,
  252. const int64_t ne0, const int64_t ne1, const int64_t ne2, cudaStream_t stream) {
  253. const dim3 block_dims(WARP_SIZE, 1, 1);
  254. const dim3 block_nums((ne0 + WARP_SIZE - 1) / WARP_SIZE, ne1, ne2);
  255. k_repeat_back<T><<<block_nums, block_dims, 0, stream>>>(src, dst, ne00, ne01, ne02, ne0, ne1, ne2);
  256. }
  257. template<class op>
  258. static void ggml_cuda_op_bin_bcast(
  259. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  260. const void * src0_dd, const void * src1_dd, void * dst_dd, cudaStream_t stream) {
  261. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  262. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  263. op()(src0, src1, dst, (const float *)src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
  264. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  265. op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (half *) dst_dd, stream);
  266. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  267. op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
  268. } else {
  269. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  270. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  271. GGML_ABORT("fatal error");
  272. }
  273. }
  274. void ggml_cuda_op_repeat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  275. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, dst->src[0], dst, nullptr, dst->src[0]->data, dst->data, ctx.stream());
  276. }
  277. void ggml_cuda_op_add(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  278. 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());
  279. }
  280. void ggml_cuda_op_sub(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  281. 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());
  282. }
  283. void ggml_cuda_op_mul(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  284. 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());
  285. }
  286. void ggml_cuda_op_div(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  287. 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());
  288. }
  289. void ggml_cuda_op_repeat_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  290. const ggml_tensor * src0 = dst->src[0];
  291. GGML_ASSERT(src0->type == dst->type);
  292. GGML_ASSERT(ggml_is_contiguous(src0));
  293. GGML_ASSERT(ggml_is_contiguous(dst));
  294. GGML_ASSERT(ggml_can_repeat(dst, src0));
  295. cudaStream_t stream = ctx.stream();
  296. const int64_t ne00 = src0->ne[0];
  297. const int64_t ne01 = src0->ne[1];
  298. const int64_t ne02 = src0->ne[2];
  299. GGML_ASSERT(src0->ne[3] == 1);
  300. const int64_t ne0 = dst->ne[0];
  301. const int64_t ne1 = dst->ne[1];
  302. const int64_t ne2 = dst->ne[2];
  303. GGML_ASSERT(dst->ne[3] == 1);
  304. switch (dst->type) {
  305. case GGML_TYPE_F32: {
  306. const float * src0_d = (const float *) src0->data;
  307. float * dst_d = (float *) dst->data;
  308. repeat_back_cuda<float>(src0_d, dst_d, ne00, ne01, ne02, ne0, ne1, ne2, stream);
  309. } break;
  310. default: {
  311. GGML_ASSERT(false);
  312. } break;
  313. }
  314. }