#version 450 //! A convolution whose weights are int8, dequantised by one scale per output channel. //! //! `out[o][y][x] = act(scale[o] * sum(int8_w * in) + bias[o])` //! //! # Why int8 at all //! //! Only where the weights dominate a download. SMaLL-100 is 330 million parameters: 660 MB at //! fp16 and 330 MB here, against the 1.14 GB the ncnn build of the same model shipped. Every //! other network in this runtime is small enough that fp16 costs nothing worth reclaiming, and //! they keep using `conv.comp`. //! //! # What is quantised and what is not //! //! Weights only. The export quantises its activations too, dynamically, per tensor — a //! `DynamicQuantizeLinear` before every `MatMulInteger`, with a zero point to carry the //! asymmetry. This does not, and that is both simpler and *more* accurate: activations stay //! fp16 in the arena, so the only error introduced is the weights'. It costs nothing, because //! the weights are what take the space. //! //! Read from the export rather than assumed: the weight zero points are per-column vectors and //! every entry of every one of them is **0**, so the quantisation is symmetric and a value is //! just `int8 * scale`. //! //! # The scale multiplies once //! //! One scale per output column, which is what onnxruntime's dynamic quantiser emits — not the //! per-tensor scalar this was first written for. Either way it applies to the finished //! accumulator rather than to every tap, so the inner loop is an integer-to-float convert and a //! multiply-add, exactly as many operations as the fp16 version. A per-tap scale would not work //! this way, which is why the rank is checked. //! //! `p.act_weight` holds the scale tensor's offset in the weights buffer, as an fp16 element index; //! output channel `oc` reads its own entry. `p.weight` holds the int8 tensor's offset as a //! **32-bit word** index, because that is the view it is unpacked through. #include "common.glsl" #include "conv_tiled.glsl" // The register-blocked path (`p.in1 == 1`, ungrouped only): 4 output channels x 8 columns // of one row per invocation. Same accumulation order as `main`. void tiled_int8() { uint x_blocks = (p.out_w + BLOCK_X - 1u) / BLOCK_X; uint oc_blocks = (p.out_c + BLOCK_OC - 1u) / BLOCK_OC; uint invocation = global_index(); if (invocation >= oc_blocks * p.out_h * x_blocks) { return; } uint ox0 = (invocation % x_blocks) * BLOCK_X; uint rest = invocation / x_blocks; uint oy = rest % p.out_h; uint channel_base = (rest / p.out_h) * BLOCK_OC; uint taps = p.kh * p.kw; uint wrow[BLOCK_OC]; vec4 lo[BLOCK_OC]; vec4 hi[BLOCK_OC]; for (uint i = 0u; i < BLOCK_OC; i++) { wrow[i] = min(channel_base + i, p.out_c - 1u) * p.in_c * taps; lo[i] = vec4(0.0); hi[i] = vec4(0.0); } uint plane = p.in_h * p.in_w; int ix0 = int(ox0 * p.stride_w) - int(p.pad_l); bool interior = p.stride_w == 1u && ix0 >= 0 && ix0 + int(BLOCK_X - 1u + (p.kw - 1u) * p.dil_w) < int(p.in_w); for (uint ic = 0u; ic < p.in_c; ic++) { uint channel = p.in0 + ic * plane; for (uint ky = 0u; ky < p.kh; ky++) { int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t); if (iy < 0 || iy >= int(p.in_h)) { continue; } uint row = channel + uint(iy) * p.in_w; uint tap0 = ic * taps + ky * p.kw; for (uint kx = 0u; kx < p.kw; kx++) { vec4 xl, xh; columns_at(row, ix0 + int(kx * p.dil_w), interior, xl, xh); for (uint i = 0u; i < BLOCK_OC; i++) { float w = float(int8_at(p.weight, wrow[i] + tap0 + kx)); lo[i] += w * xl; hi[i] += w * xh; } } } } uint positions = p.out_h * p.out_w; for (uint i = 0u; i < BLOCK_OC; i++) { uint oc = channel_base + i; if (oc >= p.out_c) { continue; } float scale = float(weights[p.act_weight + oc]); float bias = float(weights[p.bias + oc]); for (uint j = 0u; j < BLOCK_X; j++) { if (ox0 + j >= p.out_w) { continue; } uint index = oc * positions + oy * p.out_w + ox0 + j; float total = j < 4u ? lo[i][j] : hi[i][j - 4u]; arena[p.out_at + index] = float16_t(quant_store(fused_store(total * scale + bias, p.act, oc, index))); } } } // A grouped single-position 1x1 (`p.in1 == 2`, maia's attention-bias expansion): one output per // invocation, its group's taps read 16 at a time through the `uvec4` and quad views but summed // one by one in `main`'s order, so the result is identical. `nets` checks the alignment. void grouped_rows() { uint oc = global_index(); if (oc >= p.out_c) { return; } uint per_group = p.in_c / p.group; uint group = oc / (p.out_c / p.group); uint row16 = (p.weight + ((oc * per_group) >> 2u)) >> 2u; uint quad = (p.in0 + group * per_group) >> 2u; float total = 0.0; for (uint k = 0u; k < per_group; k += 16u) { uvec4 w = weights128[row16 + (k >> 4u)]; for (uint q = 0u; q < 4u; q++) { vec4 x = vec4(arena_quad[quad + (k >> 2u) + q]); for (uint t = 0u; t < 4u; t++) { total += float(bitfieldExtract(int(w[q]), int(t << 3u), 8)) * x[t]; } } } float scale = float(weights[p.act_weight + oc]); float biased = total * scale + float(weights[p.bias + oc]); arena[p.out_at + oc] = float16_t(quant_store(fused_store(biased, p.act, oc, oc))); } void main() { if (p.in1 == 1u) { tiled_int8(); return; } if (p.in1 == 2u) { grouped_rows(); return; } uint index = global_index(); if (index >= p.count) { return; } uint oc, oy, ox; unpack(index, oc, oy, ox); uint per_group = p.in_c / p.group; uint group = oc / (p.out_c / p.group); uint base = group * per_group; float total = 0.0; for (uint ic = 0u; ic < per_group; ic++) { uint channel = base + ic; for (uint ky = 0u; ky < p.kh; ky++) { int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t); if (iy < 0 || iy >= int(p.in_h)) { continue; } for (uint kx = 0u; kx < p.kw; kx++) { int ix = int(ox * p.stride_w + kx * p.dil_w) - int(p.pad_l); if (ix < 0 || ix >= int(p.in_w)) { continue; } uint at = ((oc * per_group + ic) * p.kh + ky) * p.kw + kx; float value = float(arena[p.in0 + (channel * p.in_h + uint(iy)) * p.in_w + uint(ix)]); total += float(int8_at(p.weight, at)) * value; } } } float scale = float(weights[p.act_weight + oc]); float biased = total * scale + float(weights[p.bias + oc]); arena[p.out_at + index] = float16_t(quant_store(fused_store(biased, p.act, oc, index))); }