1#include "softcap.cuh"
 2
 3static __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(scale * x[i]) * softcap;
11}
12
13static 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<<<num_blocks, CUDA_SOFTCAP_BLOCK_SIZE, 0, stream>>>(x, dst, scale, softcap, k);
16}
17
18// fused GGML_OP_SCALE + GGML_UNARY_OP_TANH + GGML_OP_SCALE
19void 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(&scale,   (float *) src->op_params + 0, sizeof(float));
31    memcpy(&softcap, (float *) dst->op_params + 0, sizeof(float));
32
33    softcap_f32_cuda(src0_d, dst_d, scale, softcap, ggml_nelements(src0), stream);
34}