Spaces:
Running
Running
Download kernels/encrelattn.wgsl from EncryptidSystems/EdgeAI: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/spaces/EncryptidSystems/EdgeAI/resolve/main/kernels/encrelattn.wgsl
- Command line
-
hf download hf://spaces/EncryptidSystems/EdgeAI/kernels/encrelattn.wgsl
-
curl -L -o encrelattn.wgsl https://huggingface.co/spaces/EncryptidSystems/EdgeAI/resolve/main/kernels/encrelattn.wgsl
2.41 kB
| // The audio encoder's local attention (decision E9). Frame t attends to frames t - PAST .. t. | |
| // The query is scaled per dim by HD^-1/2 / softplus(0) * softplus(per_dim_scale); a key at | |
| // distance d scores q.k + q.rel[PAST - d], capped as CAP * tanh(score / CAP), then softmax. | |
| // Workgroup (head, frame), one thread per head dim. | |
| override HD: u32; | |
| override PAST: u32 = 12u; | |
| override CAP: f32 = 50.0; | |
| var<storage, read> q: array<f32>; // T x H*HD | |
| var<storage, read> k: array<f32>; | |
| var<storage, read> v: array<f32>; | |
| var<storage, read> rel: array<f32>; // (PAST + 1) x H*HD | |
| var<storage, read> pds: array<f32>; // HD, per_dim_scale | |
| var<storage, read_write> o: array<f32>; // T x H*HD | |
| var<workgroup> red: array<f32, 256>; | |
| var<workgroup> sc: array<f32, 64>; | |
| fn sum(x: f32, tid: u32) -> f32 { | |
| red[tid] = x; | |
| workgroupBarrier(); | |
| for (var s = 128u; s > 0u; s >>= 1u) { | |
| if (tid < s) { | |
| red[tid] += red[tid + s]; | |
| } | |
| workgroupBarrier(); | |
| } | |
| let r = red[0]; | |
| workgroupBarrier(); | |
| return r; | |
| } | |
| fn main( wg: vec3u, nwg: vec3u, | |
| tid: u32) { | |
| let h = wg.x; | |
| let H = nwg.x; | |
| let t = wg.z * nwg.y + wg.y; | |
| let stride = H * HD; | |
| let on = tid < HD; | |
| var qd = 0.0; | |
| if (on) { | |
| let sp = log(1.0 + exp(pds[tid])); | |
| qd = q[t * stride + h * HD + tid] * inverseSqrt(f32(HD)) / log(2.0) * sp; | |
| } | |
| let first = i32(t) - i32(PAST); | |
| var mx = -3.0e38; | |
| for (var j = 0u; j <= PAST; j++) { | |
| let kt = first + i32(j); | |
| var s = 0.0; | |
| if (kt >= 0) { | |
| var part = 0.0; | |
| if (on) { | |
| let d = t - u32(kt); | |
| part = qd * (k[u32(kt) * stride + h * HD + tid] + rel[(PAST - d) * stride + h * HD + tid]); | |
| } | |
| s = sum(part, tid); | |
| s = CAP * tanh(s / CAP); | |
| mx = max(mx, s); | |
| } | |
| if (tid == 0u) { | |
| sc[j] = s; | |
| } | |
| } | |
| workgroupBarrier(); | |
| var total = 0.0; | |
| var acc = 0.0; | |
| for (var j = 0u; j <= PAST; j++) { | |
| let kt = first + i32(j); | |
| if (kt < 0) { | |
| continue; | |
| } | |
| let e = exp(sc[j] - mx); | |
| total += e; | |
| if (on) { | |
| acc += e * v[u32(kt) * stride + h * HD + tid]; | |
| } | |
| } | |
| if (on) { | |
| o[t * stride + h * HD + tid] = acc / total; | |
| } | |
| } | |