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

Modern-Apps.git / library / ml / src / main / rust / shaders / conv_point.comp

conv_point.comp182 lines · 7.0 KiBRaw
1#version 450
2//! A `1 x 1` convolution as a register-blocked matrix multiply.
3//!
4//! `out[o][p] = act(sum_i W[o][i] * in[i][p] + bias[o])`, where `p` runs over positions.
5//!
6//! # Why this exists
7//!
8//! `conv.comp` gives every output element its own invocation and reads that element's weights
9//! straight from global memory, with no reuse between invocations. For a spatial kernel that is
10//! survivable: a 3x3 is small and the cache serves it to the many pixels that share it.
11//!
12//! It is not survivable for a **wide** `1 x 1` over a **short** sequence, which is what every
13//! projection in a transformer is. Measured on a Tensor G4: the runtime achieved 15.2 GFLOP/s
14//! against a device peak of order 1000, because a 1024-to-1024 projection over 54 positions was
15//! moving about 113 MB of weights to do 113 MFLOP of work — every one of the 55,296 output
16//! elements re-reading a whole 1024-wide row.
17//!
18//! # The tiling
19//!
20//! Register-blocked like `conv_point_int8.comp`: each invocation owns four output channels by
21//! four positions and the 64 of a workgroup tile a [`TILE`] x [`TILE`] block, with no shared
22//! memory or barriers (on Mali those bought no reuse the cache did not already give). Weights are
23//! read eight at a time through the `uvec4` view when rows are 16-byte aligned, activations four
24//! positions at a time when the position count is a multiple of four. Each output sums its taps in
25//! input-channel order from zero and adds the bias last, as the shared-memory version did.
26//!
27//! # Where it is not used
28//!
29//! Grouped convolutions, and anything whose kernel is not `1 x 1`. `Builder::conv` routes to this
30//! only when both hold; everything else keeps `conv.comp`. The two want opposite memory layouts
31//! and one shader serving both would be worse at each.
32
33#include "common.glsl"
34
35/// The arena again, four fp16 elements per entry, for the wide activation loads.
36layout(std430, binding = 0) readonly buffer ArenaQuad {
37 f16vec4 arena_quad[];
38};
39
40/// Output channels and positions per workgroup: 8 x 8 invocations of 4 x 4 each.
41///
42/// **Must equal `CONV_POINT_TILE` in `nets`**, which counts the workgroups.
43#define TILE 32u
44
45/// Channels and positions per invocation.
46#define BLOCK 4u
47
48/// Channels per invocation on the split path (`p.in1 == 1`).
49#define SPLIT 2u
50
51// The wide path with two channels per invocation, for GEMMs with too few 32 x 32 tiles to fill
52// the GPU (Supertonic's text encoder: 56 positions). Twice the workgroups, each 16 channels x 32
53// positions; every output sums the same terms in the same order, so the result is identical.
54// Selected only when the wide path's alignment holds (see `nets`).
55void split_rows() {
56 uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
57 if (tile >= p.count) {
58 return;
59 }
60 uint positions = p.out_h * p.out_w;
61 uint tiles_across = (positions + TILE - 1u) / TILE;
62 uint lid = gl_LocalInvocationID.x;
63 uint channel_base = (tile / tiles_across) * (TILE / BLOCK * SPLIT) + (lid >> 3u) * SPLIT;
64 uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK;
65 if (position_base >= positions) {
66 return;
67 }
68 uint rows[SPLIT];
69 vec4 sum[SPLIT];
70 for (uint i = 0u; i < SPLIT; i++) {
71 rows[i] = p.weight + min(channel_base + i, p.out_c - 1u) * p.in_c;
72 sum[i] = vec4(0.0);
73 }
74 uint quads = positions >> 2u;
75 uint quad = (p.in0 + position_base) >> 2u;
76 for (uint k = 0u; k < p.in_c; k += 8u) {
77 uvec4 w[SPLIT];
78 for (uint i = 0u; i < SPLIT; i++) {
79 w[i] = weights128[(rows[i] + k) >> 3u];
80 }
81 for (uint t = 0u; t < 8u; t++) {
82 vec4 x = vec4(arena_quad[quad + (k + t) * quads]);
83 for (uint i = 0u; i < SPLIT; i++) {
84 float tap = unpackHalf2x16(w[i][t >> 1u])[t & 1u];
85 sum[i] += tap * x;
86 }
87 }
88 }
89 for (uint i = 0u; i < SPLIT; i++) {
90 uint channel = channel_base + i;
91 if (channel >= p.out_c) {
92 continue;
93 }
94 float offset = float(weights[p.bias + channel]);
95 for (uint j = 0u; j < BLOCK; j++) {
96 uint position = position_base + j;
97 if (position >= positions) {
98 continue;
99 }
100 uint index = channel * positions + position;
101 float value = fused_store(sum[i][j] + offset, p.act, channel, index);
102 arena[p.out_at + index] = float16_t(value);
103 }
104 }
105}
106
107void main() {
108 if (p.in1 == 1u) {
109 split_rows();
110 return;
111 }
112 uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
113 if (tile >= p.count) {
114 return;
115 }
116 uint positions = p.out_h * p.out_w;
117 uint tiles_across = (positions + TILE - 1u) / TILE;
118 uint lid = gl_LocalInvocationID.x;
119 uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK;
120 uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK;
121 if (position_base >= positions) {
122 return;
123 }
124
125 // Rows past the last channel read the last row; never stored.
126 uint rows[BLOCK];
127 vec4 sum[BLOCK];
128 for (uint i = 0u; i < BLOCK; i++) {
129 rows[i] = p.weight + min(channel_base + i, p.out_c - 1u) * p.in_c;
130 sum[i] = vec4(0.0);
131 }
132
133 bool wide = (p.in_c & 7u) == 0u && (p.weight & 7u) == 0u
134 && (positions & 3u) == 0u && (p.in0 & 3u) == 0u;
135 if (wide) {
136 uint quads = positions >> 2u;
137 uint quad = (p.in0 + position_base) >> 2u;
138 for (uint k = 0u; k < p.in_c; k += 8u) {
139 uvec4 w[BLOCK];
140 for (uint i = 0u; i < BLOCK; i++) {
141 w[i] = weights128[(rows[i] + k) >> 3u];
142 }
143 for (uint t = 0u; t < 8u; t++) {
144 vec4 x = vec4(arena_quad[quad + (k + t) * quads]);
145 for (uint i = 0u; i < BLOCK; i++) {
146 float tap = unpackHalf2x16(w[i][t >> 1u])[t & 1u];
147 sum[i] += tap * x;
148 }
149 }
150 }
151 } else {
152 // Columns past the last position read as zero and are never stored.
153 bvec4 live = lessThan(uvec4(position_base) + uvec4(0u, 1u, 2u, 3u), uvec4(positions));
154 for (uint k = 0u; k < p.in_c; k++) {
155 uint at = p.in0 + k * positions + position_base;
156 vec4 x;
157 for (uint j = 0u; j < BLOCK; j++) {
158 x[j] = live[j] ? float(arena[at + j]) : 0.0;
159 }
160 for (uint i = 0u; i < BLOCK; i++) {
161 sum[i] += float(weights[rows[i] + k]) * x;
162 }
163 }
164 }
165
166 for (uint i = 0u; i < BLOCK; i++) {
167 uint channel = channel_base + i;
168 if (channel >= p.out_c) {
169 continue;
170 }
171 float offset = float(weights[p.bias + channel]);
172 for (uint j = 0u; j < BLOCK; j++) {
173 uint position = position_base + j;
174 if (position >= positions) {
175 continue;
176 }
177 uint index = channel * positions + position;
178 float value = fused_store(sum[i][j] + offset, p.act, channel, index);
179 arena[p.out_at + index] = float16_t(value);
180 }
181 }
182}