Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / ml / src / main / rust / shaders / conv_point_widened.glsl

conv_point_widened.glsl146 lines · 5.5 KiBRaw
1//! `conv_point_int8.comp`'s wide paths over weights the runtime widened to fp16 at upload
2//! (`p.in1` = 3: four channels per lane, = 4: two). Every int8 value is exact in fp16, so each
3//! tap is the same float the int8 paths unpack, summed in the same order: bit-identical, without
4//! the per-tap `bitfieldExtract` + int-to-float that made those paths ALU-bound. `p.weight` is
5//! still a 32-bit word offset, of a 16-byte-aligned fp16 row block; `in_c % 16 == 0`.
6
7/// Four positions starting at element `at`, which need not be quad-aligned.
8vec4 shifted_quad(uint at, uint last_quad) {
9 uint q = at >> 2u;
10 uint s = at & 3u;
11 f16vec4 a = arena_quad[q];
12 f16vec4 b = arena_quad[min(q + 1u, last_quad)];
13 f16vec4 h = s == 0u ? a
14 : s == 1u ? f16vec4(a.yzw, b.x)
15 : s == 2u ? f16vec4(a.zw, b.xy)
16 : f16vec4(a.w, b.xyz);
17 return vec4(h);
18}
19
20void store_row(uint channel, uint position_base, uint positions, vec4 sum) {
21 if (channel >= p.out_c) {
22 return;
23 }
24 float scale = float(weights[p.act_weight + channel]);
25 float offset = float(weights[p.bias + channel]);
26 for (uint j = 0u; j < BLOCK; j++) {
27 if (position_base + j >= positions) {
28 continue;
29 }
30 uint index = channel * positions + position_base + j;
31 float value = quant_store(fused_store(sum[j] * scale + offset, p.act, channel, index));
32 arena[p.out_at + index] = float16_t(value);
33 }
34}
35
36void widened_block() {
37 uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
38 if (tile >= p.count) {
39 return;
40 }
41 uint positions = p.out_h * p.out_w;
42 uint tiles_across = (positions + TILE - 1u) / TILE;
43 uint lid = gl_LocalInvocationID.x;
44 uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK;
45 uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK;
46 if (position_base >= positions) {
47 return;
48 }
49 uint rows8[BLOCK];
50 vec4 sum[BLOCK];
51 for (uint i = 0u; i < BLOCK; i++) {
52 uint row = min(channel_base + i, p.out_c - 1u);
53 rows8[i] = (p.weight + ((row * p.in_c) >> 1u)) >> 2u;
54 sum[i] = vec4(0.0);
55 }
56 if ((positions & 3u) == 0u && (p.in0 & 3u) == 0u) {
57 uint quads = positions >> 2u;
58 uint quad = (p.in0 + position_base) >> 2u;
59 for (uint k = 0u; k < p.in_c; k += 8u) {
60 uvec4 w[BLOCK];
61 for (uint i = 0u; i < BLOCK; i++) {
62 w[i] = weights128[rows8[i] + (k >> 3u)];
63 }
64 for (uint t = 0u; t < 8u; t++) {
65 vec4 x = vec4(arena_quad[quad + (k + t) * quads]);
66 for (uint i = 0u; i < BLOCK; i++) {
67 sum[i] += unpackHalf2x16(w[i][t >> 1u])[t & 1u] * x;
68 }
69 }
70 }
71 } else {
72 uint first = p.in0 + position_base;
73 uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u;
74 for (uint k = 0u; k < p.in_c; k += 8u) {
75 uvec4 w[BLOCK];
76 for (uint i = 0u; i < BLOCK; i++) {
77 w[i] = weights128[rows8[i] + (k >> 3u)];
78 }
79 for (uint t = 0u; t < 8u; t++) {
80 vec4 x = shifted_quad(first + (k + t) * positions, last_quad);
81 for (uint i = 0u; i < BLOCK; i++) {
82 sum[i] += unpackHalf2x16(w[i][t >> 1u])[t & 1u] * x;
83 }
84 }
85 }
86 }
87 for (uint i = 0u; i < BLOCK; i++) {
88 store_row(channel_base + i, position_base, positions, sum[i]);
89 }
90}
91
92void widened_split() {
93 uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
94 if (tile >= p.count) {
95 return;
96 }
97 uint positions = p.out_h * p.out_w;
98 uint tiles_across = (positions + TILE - 1u) / TILE;
99 uint lid = gl_LocalInvocationID.x;
100 uint channel_base = (tile / tiles_across) * (TILE / BLOCK * SPLIT) + (lid >> 3u) * SPLIT;
101 uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK;
102 if (position_base >= positions) {
103 return;
104 }
105 uint rows8[SPLIT];
106 vec4 sum[SPLIT];
107 for (uint i = 0u; i < SPLIT; i++) {
108 uint row = min(channel_base + i, p.out_c - 1u);
109 rows8[i] = (p.weight + ((row * p.in_c) >> 1u)) >> 2u;
110 sum[i] = vec4(0.0);
111 }
112 if ((positions & 3u) == 0u && (p.in0 & 3u) == 0u) {
113 uint quads = positions >> 2u;
114 uint quad = (p.in0 + position_base) >> 2u;
115 for (uint k = 0u; k < p.in_c; k += 8u) {
116 uvec4 w[SPLIT];
117 for (uint i = 0u; i < SPLIT; i++) {
118 w[i] = weights128[rows8[i] + (k >> 3u)];
119 }
120 for (uint t = 0u; t < 8u; t++) {
121 vec4 x = vec4(arena_quad[quad + (k + t) * quads]);
122 for (uint i = 0u; i < SPLIT; i++) {
123 sum[i] += unpackHalf2x16(w[i][t >> 1u])[t & 1u] * x;
124 }
125 }
126 }
127 } else {
128 uint first = p.in0 + position_base;
129 uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u;
130 for (uint k = 0u; k < p.in_c; k += 8u) {
131 uvec4 w[SPLIT];
132 for (uint i = 0u; i < SPLIT; i++) {
133 w[i] = weights128[rows8[i] + (k >> 3u)];
134 }
135 for (uint t = 0u; t < 8u; t++) {
136 vec4 x = shifted_quad(first + (k + t) * positions, last_quad);
137 for (uint i = 0u; i < SPLIT; i++) {
138 sum[i] += unpackHalf2x16(w[i][t >> 1u])[t & 1u] * x;
139 }
140 }
141 }
142 }
143 for (uint i = 0u; i < SPLIT; i++) {
144 store_row(channel_base + i, position_base, positions, sum[i]);
145 }
146}