unary.cu 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "unary.cuh"
  27. static __global__ void neg_f32(const float * x, float * dst, const int k) {
  28. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  29. if (i >= k) {
  30. return;
  31. }
  32. dst[i] = -x[i];
  33. }
  34. static __global__ void step_f32(const float * x, float * dst, const int k) {
  35. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  36. if (i >= k) {
  37. return;
  38. }
  39. dst[i] = x[i] > 0.0f;
  40. }
  41. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  42. const float GELU_COEF_A = 0.044715f;
  43. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  44. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  45. if (i >= k) {
  46. return;
  47. }
  48. float xi = x[i];
  49. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  50. }
  51. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  52. const float GELU_QUICK_COEF = -1.702f;
  53. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  54. if (i >= k) {
  55. return;
  56. }
  57. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  58. }
  59. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  60. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  61. if (i >= k) {
  62. return;
  63. }
  64. dst[i] = x[i] / (1.0f + expf(-x[i]));
  65. }
  66. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  67. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  68. if (i >= k) {
  69. return;
  70. }
  71. dst[i] = tanhf(x[i]);
  72. }
  73. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  74. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  75. if (i >= k) {
  76. return;
  77. }
  78. dst[i] = fmaxf(x[i], 0);
  79. }
  80. static __global__ void sigmoid_f32(const float * x, float * dst, const int k) {
  81. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  82. if (i >= k) {
  83. return;
  84. }
  85. dst[i] = 1.0f / (1.0f + expf(-x[i]));
  86. }
  87. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  88. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  89. if (i >= k) {
  90. return;
  91. }
  92. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  93. }
  94. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  95. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  96. if (i >= k) {
  97. return;
  98. }
  99. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  100. }
  101. static __global__ void exp_f32(const float * x, float * dst, const int k) {
  102. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  103. if (i >= k) {
  104. return;
  105. }
  106. dst[i] = expf(x[i]);
  107. }
  108. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  109. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  110. if (i >= k) {
  111. return;
  112. }
  113. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  114. }
  115. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  116. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  117. if (i >= k) {
  118. return;
  119. }
  120. dst[i] = x[i] * x[i];
  121. }
  122. static __global__ void sqrt_f32(const float * x, float * dst, const int k) {
  123. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  124. if (i >= k) {
  125. return;
  126. }
  127. dst[i] = sqrtf(x[i]);
  128. }
  129. static __global__ void sin_f32(const float * x, float * dst, const int k) {
  130. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  131. if (i >= k) {
  132. return;
  133. }
  134. dst[i] = sinf(x[i]);
  135. }
  136. static __global__ void cos_f32(const float * x, float * dst, const int k) {
  137. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  138. if (i >= k) {
  139. return;
  140. }
  141. dst[i] = cosf(x[i]);
  142. }
  143. static void neg_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  144. const int num_blocks = (k + CUDA_NEG_BLOCK_SIZE - 1) / CUDA_NEG_BLOCK_SIZE;
  145. neg_f32<<<num_blocks, CUDA_NEG_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  146. }
  147. static void step_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  148. const int num_blocks = (k + CUDA_STEP_BLOCK_SIZE - 1) / CUDA_STEP_BLOCK_SIZE;
  149. step_f32<<<num_blocks, CUDA_STEP_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  150. }
  151. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  152. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  153. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  154. }
  155. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  156. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  157. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  158. }
  159. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  160. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  161. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  162. }
  163. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  164. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  165. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  166. }
  167. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  168. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  169. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  170. }
  171. static void sigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  172. const int num_blocks = (k + CUDA_SIGMOID_BLOCK_SIZE - 1) / CUDA_SIGMOID_BLOCK_SIZE;
  173. sigmoid_f32<<<num_blocks, CUDA_SIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  174. }
  175. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  176. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  177. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  178. }
  179. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  180. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  181. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  182. }
  183. static void exp_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  184. const int num_blocks = (k + CUDA_EXP_BLOCK_SIZE - 1) / CUDA_EXP_BLOCK_SIZE;
  185. exp_f32<<<num_blocks, CUDA_EXP_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  186. }
  187. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  188. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  189. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  190. }
  191. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  192. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  193. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  194. }
  195. static void sqrt_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  196. const int num_blocks = (k + CUDA_SQRT_BLOCK_SIZE - 1) / CUDA_SQRT_BLOCK_SIZE;
  197. sqrt_f32<<<num_blocks, CUDA_SQRT_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  198. }
  199. static void sin_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  200. const int num_blocks = (k + CUDA_SIN_BLOCK_SIZE - 1) / CUDA_SIN_BLOCK_SIZE;
  201. sin_f32<<<num_blocks, CUDA_SIN_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  202. }
  203. static void cos_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  204. const int num_blocks = (k + CUDA_COS_BLOCK_SIZE - 1) / CUDA_COS_BLOCK_SIZE;
  205. cos_f32<<<num_blocks, CUDA_COS_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  206. }
  207. void ggml_cuda_op_neg(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  208. const ggml_tensor * src0 = dst->src[0];
  209. const float * src0_d = (const float *)src0->data;
  210. float * dst_d = (float *)dst->data;
  211. cudaStream_t stream = ctx.stream();
  212. GGML_ASSERT(ggml_is_contiguous(src0));
  213. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  214. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  215. neg_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  216. }
  217. void ggml_cuda_op_step(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  218. const ggml_tensor * src0 = dst->src[0];
  219. const float * src0_d = (const float *)src0->data;
  220. float * dst_d = (float *)dst->data;
  221. cudaStream_t stream = ctx.stream();
  222. GGML_ASSERT(ggml_is_contiguous(src0));
  223. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  224. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  225. step_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  226. }
  227. void ggml_cuda_op_gelu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  228. const ggml_tensor * src0 = dst->src[0];
  229. const float * src0_d = (const float *)src0->data;
  230. float * dst_d = (float *)dst->data;
  231. cudaStream_t stream = ctx.stream();
  232. GGML_ASSERT(ggml_is_contiguous(src0));
  233. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  234. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  235. gelu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  236. }
  237. void ggml_cuda_op_silu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  238. const ggml_tensor * src0 = dst->src[0];
  239. const float * src0_d = (const float *)src0->data;
  240. float * dst_d = (float *)dst->data;
  241. cudaStream_t stream = ctx.stream();
  242. GGML_ASSERT(ggml_is_contiguous(src0));
  243. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  244. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  245. silu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  246. }
  247. void ggml_cuda_op_gelu_quick(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  248. const ggml_tensor * src0 = dst->src[0];
  249. const float * src0_d = (const float *)src0->data;
  250. float * dst_d = (float *)dst->data;
  251. cudaStream_t stream = ctx.stream();
  252. GGML_ASSERT(ggml_is_contiguous(src0));
  253. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  254. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  255. gelu_quick_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  256. }
  257. void ggml_cuda_op_tanh(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  258. const ggml_tensor * src0 = dst->src[0];
  259. const float * src0_d = (const float *)src0->data;
  260. float * dst_d = (float *)dst->data;
  261. cudaStream_t stream = ctx.stream();
  262. GGML_ASSERT(ggml_is_contiguous(src0));
  263. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  264. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  265. tanh_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  266. }
  267. void ggml_cuda_op_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  268. const ggml_tensor * src0 = dst->src[0];
  269. const float * src0_d = (const float *)src0->data;
  270. float * dst_d = (float *)dst->data;
  271. cudaStream_t stream = ctx.stream();
  272. GGML_ASSERT(ggml_is_contiguous(src0));
  273. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  274. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  275. relu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  276. }
  277. void ggml_cuda_op_sigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  278. const ggml_tensor * src0 = dst->src[0];
  279. const float * src0_d = (const float *)src0->data;
  280. float * dst_d = (float *)dst->data;
  281. cudaStream_t stream = ctx.stream();
  282. GGML_ASSERT(ggml_is_contiguous(src0));
  283. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  284. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  285. sigmoid_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  286. }
  287. void ggml_cuda_op_hardsigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  288. const ggml_tensor * src0 = dst->src[0];
  289. const float * src0_d = (const float *)src0->data;
  290. float * dst_d = (float *)dst->data;
  291. cudaStream_t stream = ctx.stream();
  292. GGML_ASSERT(ggml_is_contiguous(src0));
  293. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  294. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  295. hardsigmoid_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  296. }
  297. void ggml_cuda_op_hardswish(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  298. const ggml_tensor * src0 = dst->src[0];
  299. const float * src0_d = (const float *)src0->data;
  300. float * dst_d = (float *)dst->data;
  301. cudaStream_t stream = ctx.stream();
  302. GGML_ASSERT(ggml_is_contiguous(src0));
  303. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  304. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  305. hardswish_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  306. }
  307. void ggml_cuda_op_exp(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  308. const ggml_tensor * src0 = dst->src[0];
  309. const float * src0_d = (const float *)src0->data;
  310. float * dst_d = (float *)dst->data;
  311. cudaStream_t stream = ctx.stream();
  312. GGML_ASSERT(ggml_is_contiguous(src0));
  313. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  314. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  315. exp_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  316. }
  317. void ggml_cuda_op_leaky_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  318. const ggml_tensor * src0 = dst->src[0];
  319. const float * src0_d = (const float *)src0->data;
  320. float * dst_d = (float *)dst->data;
  321. cudaStream_t stream = ctx.stream();
  322. GGML_ASSERT(ggml_is_contiguous(src0));
  323. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  324. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  325. float negative_slope;
  326. memcpy(&negative_slope, dst->op_params, sizeof(float));
  327. leaky_relu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), negative_slope, stream);
  328. }
  329. void ggml_cuda_op_sqr(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  330. const ggml_tensor * src0 = dst->src[0];
  331. const float * src0_d = (const float *)src0->data;
  332. float * dst_d = (float *)dst->data;
  333. cudaStream_t stream = ctx.stream();
  334. GGML_ASSERT(ggml_is_contiguous(src0));
  335. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  336. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  337. sqr_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  338. }
  339. void ggml_cuda_op_sqrt(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  340. const ggml_tensor * src0 = dst->src[0];
  341. const float * src0_d = (const float *)src0->data;
  342. float * dst_d = (float *)dst->data;
  343. cudaStream_t stream = ctx.stream();
  344. GGML_ASSERT(ggml_is_contiguous(src0));
  345. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  346. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  347. sqrt_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  348. }
  349. void ggml_cuda_op_sin(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  350. const ggml_tensor * src0 = dst->src[0];
  351. const float * src0_d = (const float *)src0->data;
  352. float * dst_d = (float *)dst->data;
  353. cudaStream_t stream = ctx.stream();
  354. GGML_ASSERT(ggml_is_contiguous(src0));
  355. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  356. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  357. sin_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  358. }
  359. void ggml_cuda_op_cos(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  360. const ggml_tensor * src0 = dst->src[0];
  361. const float * src0_d = (const float *)src0->data;
  362. float * dst_d = (float *)dst->data;
  363. cudaStream_t stream = ctx.stream();
  364. GGML_ASSERT(ggml_is_contiguous(src0));
  365. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  366. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  367. cos_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  368. }