-
Notifications
You must be signed in to change notification settings - Fork 20.7k
cuda: fuse snake activation (mul, sin, sqr, mul, add) #22667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
am17an
merged 6 commits into
ggml-org:master
from
ServeurpersoCom:ggml/cuda-snake-fusion
May 8, 2026
+191
−0
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1efb15
cuda: fuse snake activation (mul, sin, sqr, mul, add)
ServeurpersoCom 732c7a3
cuda: address review feedback from @am17an
ServeurpersoCom 4f6c55a
cuda: snake fusion fastdiv on T_len, Suggested-by: @am17an
ServeurpersoCom 34c2d95
Update tests/test-backend-ops.cpp
ServeurpersoCom 9c0d1ba
cuda: snake fusion check add->type matches x->type
ServeurpersoCom a79debb
cuda: snake fusion check add->type matches x->type
ServeurpersoCom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #include "snake.cuh" | ||
| #include "convert.cuh" | ||
|
|
||
| // Fused Snake activation: y = x + sin^2(a * x) * inv_b | ||
| // x: [T, C] (T contiguous), a: [1, C], inv_b: [1, C] | ||
| // Supports F32, F16, BF16 data with F32 compute. | ||
|
|
||
| template <typename T> | ||
| static __global__ void snake_kernel( | ||
| const T * __restrict__ x, | ||
| const float * __restrict__ a, | ||
| const float * __restrict__ inv_b, | ||
| T * __restrict__ dst, | ||
| const int T_len, | ||
| const int C) { | ||
| const int idx = blockIdx.x * blockDim.x + threadIdx.x; | ||
| if (idx >= T_len * C) return; | ||
|
|
||
| const int c = idx / T_len; | ||
|
am17an marked this conversation as resolved.
Outdated
|
||
|
|
||
| const float xi = ggml_cuda_cast<float>(x[idx]); | ||
| const float s = sinf(a[c] * xi); | ||
| dst[idx] = ggml_cuda_cast<T>(xi + s * s * inv_b[c]); | ||
| } | ||
|
|
||
| // Internal launcher with explicit x/a/inv_b/dst tensors. | ||
| // Shared by the public op (reads dst->src) and the fusion path (explicit args). | ||
| static void launch_snake(ggml_backend_cuda_context & ctx, | ||
| const ggml_tensor * x, | ||
| const ggml_tensor * a, | ||
| const ggml_tensor * inv_b, | ||
| ggml_tensor * dst) { | ||
| const float * a_d = (const float *)a->data; | ||
| const float * inv_b_d = (const float *)inv_b->data; | ||
|
|
||
| const int T = (int)x->ne[0]; | ||
| const int C = (int)x->ne[1]; | ||
| const int total = T * C; | ||
|
|
||
| const int block_size = 256; | ||
| const int grid_size = (total + block_size - 1) / block_size; | ||
|
|
||
| cudaStream_t stream = ctx.stream(); | ||
|
|
||
| switch (x->type) { | ||
| case GGML_TYPE_F32: { | ||
| snake_kernel<<<grid_size, block_size, 0, stream>>>( | ||
| (const float *)x->data, a_d, inv_b_d, (float *)dst->data, T, C); | ||
| } break; | ||
| case GGML_TYPE_F16: { | ||
| snake_kernel<<<grid_size, block_size, 0, stream>>>( | ||
| (const half *)x->data, a_d, inv_b_d, (half *)dst->data, T, C); | ||
| } break; | ||
| case GGML_TYPE_BF16: { | ||
| snake_kernel<<<grid_size, block_size, 0, stream>>>( | ||
| (const nv_bfloat16 *)x->data, a_d, inv_b_d, (nv_bfloat16 *)dst->data, T, C); | ||
| } break; | ||
| default: | ||
| GGML_ABORT("snake: unsupported type"); | ||
| } | ||
| } | ||
|
|
||
| // Fusion entry: caller supplies x/a/inv_b explicitly from the matched | ||
| // mul -> sin -> sqr -> mul -> add pattern. The dst is the trailing add output. | ||
| void ggml_cuda_op_snake_fused(ggml_backend_cuda_context & ctx, | ||
| const ggml_tensor * x, | ||
| const ggml_tensor * a, | ||
| const ggml_tensor * inv_b, | ||
| ggml_tensor * dst) { | ||
| launch_snake(ctx, x, a, inv_b, dst); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #include "common.cuh" | ||
|
|
||
| // Fusion entry point. Caller supplies x/a/inv_b explicitly. | ||
| void ggml_cuda_op_snake_fused(ggml_backend_cuda_context & ctx, | ||
| const ggml_tensor * x, | ||
| const ggml_tensor * a, | ||
| const ggml_tensor * inv_b, | ||
| ggml_tensor * dst); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.