sourcelibs/Quant/demos/formats.xtl
1⍝!/usr/bin/env xetal
2⍝│ One weight matrix stored five ways, and how far each moves a layer's
3⍝│ output. Run with "just demo-lib Quant formats".
4
5"qz:" u̲se< "Quant"
6
7⍝│ A 16 x 16 weight matrix and an input, by a fixed ripple (no chance).
8W ← 16 16 r̲eshape 0.4 × s̲in 2.3 × f̲loat r̲ange 256
9x ← c̲os 0.9 × f̲loat r̲ange 16
10y ← x '+ '× i̲nner W
11⍝│ Each format's weights, and the largest change in the layer's output.
12ᵘe̲rr ← { V → 'm̲ax r̲/ a̲bs y − x '+ '× i̲nner V }
13q ← 0.7 qz:t̲ernary W
14ᵘr̲ound ← { a → (f̲loat f̲loor 0.5 + 1000000.0 × a) ÷ 1000000.0 }
15⍝│ FP16, bfloat16, INT8, INT4, ternary (times its one scale).
16ᵘr̲ound (ᵘe̲rr 10.0 qz:f̲loats W) c̲at (ᵘe̲rr 7.0 qz:f̲loats W) c̲at (ᵘe̲rr 8 qz:i̲nt W) c̲at (ᵘe̲rr 4 qz:i̲nt W) c̲at ᵘe̲rr (W qz:s̲cale q) × q
17⍝│ The ternary layer by additions only, then its one multiply: the same output.
18ᵘr̲ound ((W qz:s̲cale q) × x qz:a̲dds q) − x '+ '× i̲nner (W qz:s̲cale q) × q