| 1 | #include "softcap.cuh" |
| 2 | |
| 3 | static __global__ void softcap_f32(const float * x, float * dst, const float scale, const float softcap, const int k) { |
| 4 | const int i = blockDim.x*blockIdx.x + threadIdx.x; |
| 5 | |
| 6 | if (i >= k) { |
| 7 | return; |
| 8 | } |
| 9 | |
| 10 | dst[i] = tanhf(a: scale * x[i]) * softcap; |
| 11 | } |
| 12 | |
| 13 | static void softcap_f32_cuda(const float * x, float * dst, const float scale, const float softcap, const int k, cudaStream_t stream) { |
| 14 | const int num_blocks = (k + CUDA_SOFTCAP_BLOCK_SIZE - 1) / CUDA_SOFTCAP_BLOCK_SIZE; |
| 15 | softcap_f32<<<gridDim: num_blocks, CUDA_SOFTCAP_BLOCK_SIZE, sharedMem: 0, stream>>>(x, dst, scale, softcap, k); |
| 16 | } |
| 17 | |
| 18 | // fused GGML_OP_SCALE + GGML_UNARY_OP_TANH + GGML_OP_SCALE |
| 19 | void ggml_cuda_op_softcap(ggml_backend_cuda_context & ctx, ggml_tensor * dst, ggml_tensor * src) { |
| 20 | const ggml_tensor * src0 = src->src[0]; |
| 21 | const float * src0_d = (const float *)src0->data; |
| 22 | float * dst_d = (float *)dst->data; |
| 23 | cudaStream_t stream = ctx.stream(); |
| 24 | |
| 25 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 26 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 27 | |
| 28 | float scale; |
| 29 | float softcap; |
| 30 | memcpy(dest: &scale, src: (float *) src->op_params + 0, n: sizeof(float)); |
| 31 | memcpy(dest: &softcap, src: (float *) dst->op_params + 0, n: sizeof(float)); |
| 32 | |
| 33 | softcap_f32_cuda(src0_d, dst_d, scale, softcap, ggml_nelements(src0), stream); |
| 34 | } |
| 35 | |