Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / maxpool.comp
maxpool.comp50 lines · 1.7 KiBRaw
| 1 | #version 450 |
| 2 | //! Max pooling. 33 uses, all of them 2x2 stride 2 in U^2-Netp. |
| 3 | //! |
| 4 | //! The kernel and stride still come from push constants rather than being hardcoded to |
| 5 | //! 2, because that costs nothing here and keeps the shader honest about what the plan |
| 6 | //! actually asked for. |
| 7 | //! |
| 8 | //! ONNX marks all 33 `ceil_mode=1`; this floors. Every pooled extent in U^2-Netp is even |
| 9 | //! — 320 halves five times to 10 — so the two agree, and `nets::Builder::max_pool_2x2` |
| 10 | //! refuses an odd extent rather than letting the difference pass silently. |
| 11 | //! |
| 12 | //! There is no padding: all 33 have `pads = [0, 0, 0, 0]`, so no `-inf` initial value is |
| 13 | //! needed and every window is fully inside the input. |
| 14 | |
| 15 | #include "common.glsl" |
| 16 | |
| 17 | void main() { |
| 18 | uint index = global_index(); |
| 19 | if (index >= p.count) { |
| 20 | return; |
| 21 | } |
| 22 | uint oc, oy, ox; |
| 23 | unpack(index, oc, oy, ox); |
| 24 | |
| 25 | uint plane = p.in0 + oc * p.in_h * p.in_w; |
| 26 | uint top = oy * p.stride_h; |
| 27 | uint left = ox * p.stride_w; |
| 28 | |
| 29 | // The most negative fp16, rather than a literal -inf: every one of the 33 pools has |
| 30 | // `pads = [0, 0, 0, 0]` and an even extent, so this initial value is never actually |
| 31 | // the answer, and a real -inf would be relying on GLSL's division-by-zero behaviour |
| 32 | // for a value that cannot be observed. |
| 33 | float best = -65504.0; |
| 34 | for (uint ky = 0u; ky < p.kh; ky++) { |
| 35 | uint iy = top + ky; |
| 36 | if (iy >= p.in_h) { |
| 37 | continue; |
| 38 | } |
| 39 | uint row = plane + iy * p.in_w; |
| 40 | for (uint kx = 0u; kx < p.kw; kx++) { |
| 41 | uint ix = left + kx; |
| 42 | if (ix >= p.in_w) { |
| 43 | continue; |
| 44 | } |
| 45 | best = max(best, float(arena[row + ix])); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | arena[p.out_at + index] = float16_t(best); |
| 50 | } |