unary.cu 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * llama.cpp - commit 1e6f6554aa11fa10160a5fda689e736c3c34169f - 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 gelu_f32(const float * x, float * dst, const int k) {
  28. const float GELU_COEF_A = 0.044715f;
  29. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  30. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  31. if (i >= k) {
  32. return;
  33. }
  34. float xi = x[i];
  35. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  36. }
  37. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  38. const float GELU_QUICK_COEF = -1.702f;
  39. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  40. if (i >= k) {
  41. return;
  42. }
  43. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  44. }
  45. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  46. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  47. if (i >= k) {
  48. return;
  49. }
  50. dst[i] = x[i] / (1.0f + expf(-x[i]));
  51. }
  52. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  53. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  54. if (i >= k) {
  55. return;
  56. }
  57. dst[i] = tanhf(x[i]);
  58. }
  59. static __global__ void relu_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] = fmaxf(x[i], 0);
  65. }
  66. static __global__ void sigmoid_f32(const float * x, float * dst, const int k) {
  67. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  68. if (i >= k) {
  69. return;
  70. }
  71. dst[i] = 1.0f / (1.0f + expf(-x[i]));
  72. }
  73. static __global__ void hardsigmoid_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] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  79. }
  80. static __global__ void hardswish_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] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  86. }
  87. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  88. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  89. if (i >= k) {
  90. return;
  91. }
  92. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  93. }
  94. static __global__ void sqr_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] * x[i];
  100. }
  101. static __global__ void sqrt_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] = sqrtf(x[i]);
  107. }
  108. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  109. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  110. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  111. }
  112. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  113. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  114. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  115. }
  116. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  117. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  118. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  119. }
  120. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  121. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  122. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  123. }
  124. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  125. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  126. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  127. }
  128. static void sigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  129. const int num_blocks = (k + CUDA_SIGMOID_BLOCK_SIZE - 1) / CUDA_SIGMOID_BLOCK_SIZE;
  130. sigmoid_f32<<<num_blocks, CUDA_SIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  131. }
  132. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  133. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  134. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  135. }
  136. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  137. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  138. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  139. }
  140. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  141. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  142. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  143. }
  144. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  145. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  146. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  147. }
  148. static void sqrt_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  149. const int num_blocks = (k + CUDA_SQRT_BLOCK_SIZE - 1) / CUDA_SQRT_BLOCK_SIZE;
  150. sqrt_f32<<<num_blocks, CUDA_SQRT_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  151. }
  152. void ggml_cuda_op_gelu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  153. const ggml_tensor * src0 = dst->src[0];
  154. const float * src0_d = (const float *)src0->data;
  155. float * dst_d = (float *)dst->data;
  156. cudaStream_t stream = ctx.stream();
  157. GGML_ASSERT(ggml_is_contiguous(src0));
  158. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  159. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  160. gelu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  161. }
  162. void ggml_cuda_op_silu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  163. const ggml_tensor * src0 = dst->src[0];
  164. const float * src0_d = (const float *)src0->data;
  165. float * dst_d = (float *)dst->data;
  166. cudaStream_t stream = ctx.stream();
  167. GGML_ASSERT(ggml_is_contiguous(src0));
  168. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  169. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  170. silu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  171. }
  172. void ggml_cuda_op_gelu_quick(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  173. const ggml_tensor * src0 = dst->src[0];
  174. const float * src0_d = (const float *)src0->data;
  175. float * dst_d = (float *)dst->data;
  176. cudaStream_t stream = ctx.stream();
  177. GGML_ASSERT(ggml_is_contiguous(src0));
  178. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  179. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  180. gelu_quick_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  181. }
  182. void ggml_cuda_op_tanh(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  183. const ggml_tensor * src0 = dst->src[0];
  184. const float * src0_d = (const float *)src0->data;
  185. float * dst_d = (float *)dst->data;
  186. cudaStream_t stream = ctx.stream();
  187. GGML_ASSERT(ggml_is_contiguous(src0));
  188. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  189. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  190. tanh_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  191. }
  192. void ggml_cuda_op_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  193. const ggml_tensor * src0 = dst->src[0];
  194. const float * src0_d = (const float *)src0->data;
  195. float * dst_d = (float *)dst->data;
  196. cudaStream_t stream = ctx.stream();
  197. GGML_ASSERT(ggml_is_contiguous(src0));
  198. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  199. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  200. relu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  201. }
  202. void ggml_cuda_op_sigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  203. const ggml_tensor * src0 = dst->src[0];
  204. const float * src0_d = (const float *)src0->data;
  205. float * dst_d = (float *)dst->data;
  206. cudaStream_t stream = ctx.stream();
  207. GGML_ASSERT(ggml_is_contiguous(src0));
  208. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  209. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  210. sigmoid_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  211. }
  212. void ggml_cuda_op_hardsigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  213. const ggml_tensor * src0 = dst->src[0];
  214. const float * src0_d = (const float *)src0->data;
  215. float * dst_d = (float *)dst->data;
  216. cudaStream_t stream = ctx.stream();
  217. GGML_ASSERT(ggml_is_contiguous(src0));
  218. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  219. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  220. hardsigmoid_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  221. }
  222. void ggml_cuda_op_hardswish(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  223. const ggml_tensor * src0 = dst->src[0];
  224. const float * src0_d = (const float *)src0->data;
  225. float * dst_d = (float *)dst->data;
  226. cudaStream_t stream = ctx.stream();
  227. GGML_ASSERT(ggml_is_contiguous(src0));
  228. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  229. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  230. hardswish_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  231. }
  232. void ggml_cuda_op_leaky_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  233. const ggml_tensor * src0 = dst->src[0];
  234. const float * src0_d = (const float *)src0->data;
  235. float * dst_d = (float *)dst->data;
  236. cudaStream_t stream = ctx.stream();
  237. GGML_ASSERT(ggml_is_contiguous(src0));
  238. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  239. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  240. float negative_slope;
  241. memcpy(&negative_slope, dst->op_params, sizeof(float));
  242. leaky_relu_f32_cuda(src0_d, dst_d, ggml_nelements(src0), negative_slope, stream);
  243. }
  244. void ggml_cuda_op_sqr(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  245. const ggml_tensor * src0 = dst->src[0];
  246. const float * src0_d = (const float *)src0->data;
  247. float * dst_d = (float *)dst->data;
  248. cudaStream_t stream = ctx.stream();
  249. GGML_ASSERT(ggml_is_contiguous(src0));
  250. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  251. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  252. sqr_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  253. }
  254. void ggml_cuda_op_sqrt(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  255. const ggml_tensor * src0 = dst->src[0];
  256. const float * src0_d = (const float *)src0->data;
  257. float * dst_d = (float *)dst->data;
  258. cudaStream_t stream = ctx.stream();
  259. GGML_ASSERT(ggml_is_contiguous(src0));
  260. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  261. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  262. sqrt_f32_cuda(src0_d, dst_d, ggml_nelements(src0), stream);
  263. }