interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / controls / attention_nomax / kernel.cl

41 lines · 1312 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// CONTROL attention_nomax: exp(s_j) without subtracting the row max m (m is still computed).
3// attn_p: one work-item per row i writes the softmax weights pw[i][j], j <= i.
4__kernel void attn_p(__global const float *q, __global const float *k, __global float *pw,
5 const int T, const int D)
6{
7 int i = get_global_id(0);
8 if (i >= T)
9 return;
10 float m = -1e30f;
11 for (int j = 0; j <= i; j++) {
12 float acc = 0.0f;
13 for (int c = 0; c < D; c++)
14 acc += q[i * D + c] * k[j * D + c];
15 float s = acc * 0.125f;
16 pw[i * T + j] = s;
17 m = fmax(m, s);
18 }
19 float z = 0.0f;
20 for (int j = 0; j <= i; j++) {
21 float e = exp(pw[i * T + j]);
22 pw[i * T + j] = e;
23 z += e;
24 }
25 for (int j = 0; j <= i; j++)
26 pw[i * T + j] = pw[i * T + j] / z;
27}
28
29// attn_o: o[i][c] = sum_{j <= i} pw[i][j] v[j][c].
30__kernel void attn_o(__global const float *pw, __global const float *v, __global float *o,
31 const int T, const int D)
32{
33 int i = get_global_id(0);
34 int c = get_global_id(1);
35 if (i >= T || c >= D)
36 return;
37 float acc = 0.0f;
38 for (int j = 0; j <= i; j++)
39 acc += pw[i * T + j] * v[j * D + c];
40 o[i * D + c] = acc;
41}