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/openaccel-lab/blob/main/workloads/softmax/kernel.cl # openaccel-lab aien-dev/openaccel-lab Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `openaccel-lab / workloads / softmax / kernel.cl` 36 lines ยท 1145 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // one work-group per row; get_local_size(0) must be a power of two 3 __kernel void softmax(__global const float *x, __global float *y, const int D, __local float *red) 4 { 5 int t = get_local_id(0), nt = get_local_size(0); 6 __global const float *xr = x + (long)get_group_id(0) * D; 7 __global float *yr = y + (long)get_group_id(0) * D; 8 float acc = -INFINITY; 9 for (int k = t; k < D; k += nt) 10 acc = fmax(acc, xr[k]); 11 red[t] = acc; 12 barrier(CLK_LOCAL_MEM_FENCE); 13 for (int h = nt / 2; h > 0; h >>= 1) { 14 if (t < h) 15 red[t] = fmax(red[t], red[t + h]); 16 barrier(CLK_LOCAL_MEM_FENCE); 17 } 18 float m = red[0]; 19 barrier(CLK_LOCAL_MEM_FENCE); 20 acc = 0.0f; 21 for (int k = t; k < D; k += nt) { 22 float e = exp(xr[k] - m); 23 yr[k] = e; 24 acc += e; 25 } 26 red[t] = acc; 27 barrier(CLK_LOCAL_MEM_FENCE); 28 for (int h = nt / 2; h > 0; h >>= 1) { 29 if (t < h) 30 red[t] += red[t + h]; 31 barrier(CLK_LOCAL_MEM_FENCE); 32 } 33 float s = red[0]; 34 for (int k = t; k < D; k += nt) 35 yr[k] = yr[k] / s; 36 }