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/softmaxgrad.wgsl

26 lines
653 B

struct MatrixBatch {
size: vec4<u32>,
data: array<vec4<f32>>,
}
@group(0) @binding(0)
var<storage, read> m: MatrixBatch;
@group(0) @binding(1)
var<storage, read> dldm: MatrixBatch;
@group(0) @binding(2)
var<storage, read_write> grad: MatrixBatch;
@compute @workgroup_size(1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
grad.size = m.size;
let vc = m.size.x / 4;
let ro = (global_id.z * m.size.y + global_id.y) * vc;
var s = 0f;
for (var x = 0u; x < vc; x++) {
s += dot(m.data[ro + x], dldm.data[ro + x]);
}
grad.data[ro + global_id.x] = m.data[ro + global_id.x] * (dldm.data[ro + global_id.x] - vec4(s));
}