Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/snake.cu # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / snake.cu` 72 lines ยท 2821 bytes Raw Blame History 1 #include "snake.cuh" 2 #include "convert.cuh" 3 4 // Fused Snake activation: y = x + sin^2(a * x) * inv_b 5 // x: [T, C] (T contiguous), a: [1, C], inv_b: [1, C] 6 // Supports F32, F16, BF16 data with F32 compute. 7 8 template 9 static __global__ void snake_kernel( 10 const T * __restrict__ x, 11 const float * __restrict__ a, 12 const float * __restrict__ inv_b, 13 T * __restrict__ dst, 14 const int total, 15 const uint3 T_len_fastdiv) { 16 const int idx = blockIdx.x * blockDim.x + threadIdx.x; 17 if (idx >= total) return; 18 19 const int c = (int) fastdiv((uint32_t) idx, T_len_fastdiv); 20 21 const float xi = ggml_cuda_cast(x[idx]); 22 const float s = sinf(a[c] * xi); 23 dst[idx] = ggml_cuda_cast(xi + s * s * inv_b[c]); 24 } 25 26 // Internal launcher with explicit x/a/inv_b/dst tensors. 27 // Shared by the public op (reads dst->src) and the fusion path (explicit args). 28 static void launch_snake(ggml_backend_cuda_context & ctx, 29 const ggml_tensor * x, 30 const ggml_tensor * a, 31 const ggml_tensor * inv_b, 32 ggml_tensor * dst) { 33 const float * a_d = (const float *)a->data; 34 const float * inv_b_d = (const float *)inv_b->data; 35 36 const int T = (int)x->ne[0]; 37 const int C = (int)x->ne[1]; 38 const int total = T * C; 39 const uint3 T_len_fastdiv = init_fastdiv_values((uint64_t) T); 40 41 const int block_size = 256; 42 const int grid_size = (total + block_size - 1) / block_size; 43 44 cudaStream_t stream = ctx.stream(); 45 46 switch (x->type) { 47 case GGML_TYPE_F32: { 48 snake_kernel<<>>( 49 (const float *)x->data, a_d, inv_b_d, (float *)dst->data, total, T_len_fastdiv); 50 } break; 51 case GGML_TYPE_F16: { 52 snake_kernel<<>>( 53 (const half *)x->data, a_d, inv_b_d, (half *)dst->data, total, T_len_fastdiv); 54 } break; 55 case GGML_TYPE_BF16: { 56 snake_kernel<<>>( 57 (const nv_bfloat16 *)x->data, a_d, inv_b_d, (nv_bfloat16 *)dst->data, total, T_len_fastdiv); 58 } break; 59 default: 60 GGML_ABORT("snake: unsupported type"); 61 } 62 } 63 64 // Fusion entry: caller supplies x/a/inv_b explicitly from the matched 65 // mul -> sin -> sqr -> mul -> add pattern. The dst is the trailing add output. 66 void ggml_cuda_op_snake_fused(ggml_backend_cuda_context & ctx, 67 const ggml_tensor * x, 68 const ggml_tensor * a, 69 const ggml_tensor * inv_b, 70 ggml_tensor * dst) { 71 launch_snake(ctx, x, a, inv_b, dst); 72 }