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-vulkan/vulkan-shaders/cross_entropy_loss_back.comp # 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-vulkan / vulkan-shaders / cross_entropy_loss_back.comp` 75 lines ยท 2191 bytes Raw Blame History 1 #version 450 2 3 #include "generic_head.glsl" 4 #include "types.glsl" 5 6 #extension GL_EXT_control_flow_attributes : enable 7 8 layout(constant_id = 0) const uint BLOCK_SIZE = 32; 9 layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; 10 11 layout (binding = 0) readonly buffer G {A_TYPE data_g[];}; 12 layout (binding = 1) readonly buffer X {B_TYPE data_x[];}; 13 layout (binding = 2) readonly buffer Y {B_TYPE data_y[];}; 14 layout (binding = 3) writeonly buffer D {D_TYPE data_d[];}; 15 16 shared FLOAT_TYPE tmp[BLOCK_SIZE]; 17 18 FLOAT_TYPE wg_reduce_max(FLOAT_TYPE v) { 19 const uint tid = gl_LocalInvocationID.x; 20 tmp[tid] = v; 21 barrier(); 22 [[unroll]] for (uint s = BLOCK_SIZE / 2; s > 0; s >>= 1) { 23 if (tid < s) { 24 tmp[tid] = max(tmp[tid], tmp[tid + s]); 25 } 26 barrier(); 27 } 28 v = tmp[0]; 29 barrier(); 30 return v; 31 } 32 33 FLOAT_TYPE wg_reduce_sum(FLOAT_TYPE v) { 34 const uint tid = gl_LocalInvocationID.x; 35 tmp[tid] = v; 36 barrier(); 37 [[unroll]] for (uint s = BLOCK_SIZE / 2; s > 0; s >>= 1) { 38 if (tid < s) { 39 tmp[tid] += tmp[tid + s]; 40 } 41 barrier(); 42 } 43 v = tmp[0]; 44 barrier(); 45 return v; 46 } 47 48 void main() { 49 const uint row = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x; 50 const uint tid = gl_LocalInvocationID.x; 51 52 if (row >= p.KY) { 53 return; 54 } 55 56 const uint off = row * p.KX; 57 const FLOAT_TYPE d_by_nrows = FLOAT_TYPE(data_g[0]) / FLOAT_TYPE(p.KY); 58 59 FLOAT_TYPE max_logit = FLOAT_TYPE(uintBitsToFloat(0xFF800000)); 60 for (uint i = tid; i < p.KX; i += BLOCK_SIZE) { 61 max_logit = max(max_logit, FLOAT_TYPE(data_x[off + i])); 62 } 63 max_logit = wg_reduce_max(max_logit); 64 65 FLOAT_TYPE sum_exp = FLOAT_TYPE(0.0f); 66 for (uint i = tid; i < p.KX; i += BLOCK_SIZE) { 67 sum_exp += exp(FLOAT_TYPE(data_x[off + i]) - max_logit); 68 } 69 const FLOAT_TYPE inv_sum = FLOAT_TYPE(1.0f) / wg_reduce_sum(sum_exp); 70 71 for (uint i = tid; i < p.KX; i += BLOCK_SIZE) { 72 const FLOAT_TYPE sm = exp(FLOAT_TYPE(data_x[off + i]) - max_logit) * inv_sum; 73 data_d[off + i] = D_TYPE((sm - FLOAT_TYPE(data_y[off + i])) * d_by_nrows); 74 } 75 }