Transformer architecture implemented in TypeScript with WebGPU.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
nn/shader/softmaxnorm.wgsl

25 lines
652 B

struct MatrixBatch {
size: vec4<u32>,
data: array<vec4<f32>>,
}
@group(0) @binding(0)
var<storage, read> b: MatrixBatch;
@group(0) @binding(1)
var<storage, read_write> result: MatrixBatch;
@compute @workgroup_size(1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
result.size = b.size;
var vc = result.size.x / 4;
var resi = (global_id.z * result.size.y + global_id.y) * vc + global_id.x;
var sv = vec4(0f);
var bi = (global_id.z * b.size.y + global_id.y) * vc;
for (var i = 0u; i < vc; i++) {
sv += b.data[bi];
bi++;
}
var ss = sv.x + sv.y + sv.z + sv.w;
result.data[resi] = b.data[resi] / vec4(ss);
}