#version 450 //! Block-quantises a channel-major fp16 activation `(in_c, positions)` to int8 for //! `conv_point_int4_dot.comp`: one scale per position per 32-channel block, matching the int4 //! weights' `I4_BLOCK`. //! //! # Layout written at `p.out_at` (element offset, a multiple of 8) //! //! Data, `[block][half][position]`, one 16-byte cell each, so a run of positions is contiguous: //! word `2q` of the block (cell `q / 2`, word `2q % 4`) packs channels `8q + 0, 2, 4, 6` and word //! `2q + 1` channels `8q + 1, 3, 5, 7`, one signed byte each, low byte first. That is the order a //! weight word's nibbles fall into after `<< 4 & 0xF0F0F0F0` and `& 0xF0F0F0F0`. Then, from //! element `p.out_at + positions * in_c / 2`, a `[block][position]` table of f32 scales, //! `amax / 127 / 16`: the GEMM reads each nibble as a byte 16 times its value. //! //! # Push fields //! //! `p.in0` the input, `p.out_at` the output, `p.in_c` channels, `p.out_h * p.out_w` positions, //! `p.count` `positions * in_c / 32` invocations. #include "common.glsl" layout(std430, binding = 0) buffer ArenaWord { uint arena_word[]; }; layout(std430, binding = 0) buffer ArenaOct { uvec4 arena_oct[]; }; uint pack4(vec4 v, float inv) { ivec4 q = clamp(ivec4(roundEven(v * inv)), ivec4(-127), ivec4(127)); return uint(q.x & 255) | (uint(q.y & 255) << 8u) | (uint(q.z & 255) << 16u) | (uint(q.w) << 24u); } void main() { uint index = global_index(); if (index >= p.count) { return; } uint positions = p.out_h * p.out_w; uint position = index % positions; uint block = index / positions; float v[32]; float amax = 0.0; uint at = p.in0 + block * I4_BLOCK * positions + position; for (uint t = 0u; t < 32u; t++) { v[t] = float(arena[at + t * positions]); amax = max(amax, abs(v[t])); } float inv = amax > 0.0 ? 127.0 / amax : 0.0; for (uint h = 0u; h < 2u; h++) { uint k = h * 16u; arena_oct[(p.out_at >> 3u) + (block * 2u + h) * positions + position] = uvec4( pack4(vec4(v[k], v[k + 2u], v[k + 4u], v[k + 6u]), inv), pack4(vec4(v[k + 1u], v[k + 3u], v[k + 5u], v[k + 7u]), inv), pack4(vec4(v[k + 8u], v[k + 10u], v[k + 12u], v[k + 14u]), inv), pack4(vec4(v[k + 9u], v[k + 11u], v[k + 13u], v[k + 15u]), inv)); } arena_word[((p.out_at + positions * p.in_c / 2u) >> 1u) + index] = floatBitsToUint(amax / 127.0 / 16.0); }