interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-opencl / kernels / moe_add_id_glu.cl

80 lines · 3118 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3//------------------------------------------------------------------------------
4// add_id(gate) + add_id(up) + swiglu_oai, fused
5//
6// gpt-oss-class MoE FFNs run three full passes over the same
7// [n_ff, n_expert_used, n_tokens] f32 tensor: a per-expert bias add on the gate
8// matmul output, the same on the up matmul output, then swiglu_oai over the
9// two. Both bias adds are in-place, so each costs a full read plus a full write
10// of a tensor that is only read once more. Folding them into the swiglu pass
11// leaves two reads and one write instead of six passes.
12//
13// Grouping matches kernel_add_id: group 0 = expert slot (i1), group 1 = token
14// (i2). For a contiguous destination that addressing is identical to the flat
15// row walk kernel_swiglu_oai uses, since row i1 + i2*ne1 sits at
16// i1*nb1 + i2*ne1*nb1.
17//------------------------------------------------------------------------------
18kernel void kernel_add_id_add_id_swiglu_oai(
19 global char * src_g,
20 ulong offset_g,
21 global char * src_gb,
22 ulong offset_gb,
23 global char * src_u,
24 ulong offset_u,
25 global char * src_ub,
26 ulong offset_ub,
27 global char * src_ids,
28 ulong offset_ids,
29 global char * dst,
30 ulong offsetd,
31 ulong nb01_g,
32 ulong nb02_g,
33 ulong nb01_u,
34 ulong nb02_u,
35 ulong nb11_g,
36 ulong nb11_u,
37 ulong nb21,
38 ulong nbd1,
39 ulong nbd2,
40 int ne0,
41 float limit,
42 float alpha
43) {
44 src_g = (global char *)(src_g + offset_g);
45 src_gb = (global char *)(src_gb + offset_gb);
46 src_u = (global char *)(src_u + offset_u);
47 src_ub = (global char *)(src_ub + offset_ub);
48 src_ids = (global char *)(src_ids + offset_ids);
49 dst = (global char *)(dst + offsetd);
50
51 const int i1 = get_group_id(0);
52 const int i2 = get_group_id(1);
53
54 // The ids tensor is a view into a [n_expert, n_tokens] buffer, so its row
55 // stride is nb21 and the k selected ids are NOT contiguous per token.
56 const int i11 = *((global const int *) (src_ids + i1*sizeof(int) + i2*nb21));
57
58 global const float * g_row = (global const float *)(src_g + i1*nb01_g + i2*nb02_g);
59 global const float * u_row = (global const float *)(src_u + i1*nb01_u + i2*nb02_u);
60 global float * d_row = (global float *)(dst + i1*nbd1 + i2*nbd2);
61
62 for (int i0 = get_local_id(0); i0 < ne0; i0 += get_local_size(0)) {
63 float x0 = g_row[i0];
64 float x1 = u_row[i0];
65 if (i11 != -1) {
66 global const float * gb_row = (global const float *)(src_gb + i11*nb11_g);
67 global const float * ub_row = (global const float *)(src_ub + i11*nb11_u);
68 x0 += gb_row[i0];
69 x1 += ub_row[i0];
70 }
71
72 x0 = min(x0, limit);
73 x1 = max(min(x1, limit), -limit);
74
75 float out_glu = x0 / (1.0f + exp(-x0 * alpha));
76 out_glu = out_glu * (1.0f + x1);
77
78 d_row[i0] = out_glu;
79 }
80}