interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / arboris_q4.om

131 lines · 3538 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Opt-in symmetric Q4 block for 32 finite FP16 synaptic weights.
3note Word zero holds the FP32 scale. Four words contain eight low-nibble-first
4note signed weights each. The cold packer rounds nearest, ties to even.
5fn ar_q4_nibble x f32 scale f32 to uint
6 q = div fabs x scale
7 rounded = uint q
8 tail = sub q f32 rounded
9 if or gt tail 0.5f32 and eq tail 0.5f32 ne and rounded 1 0
10 rounded = add rounded 1
11 end
12 if gt rounded 7
13 rounded = uint 7
14 end
15 if lt x f32 0
16 ret and sub uint 16 rounded 15
17 end
18 ret rounded
19end
20
21fn arboris_q4_pack src view ushort 32 block edit uint 5 to bool
22 peak = f32 0
23 for i ulong 32
24 bits = ulong get src i
25 if eq and bits 0x7c00 0x7c00
26 ret false
27 end
28 peak = fmax peak fabs f32of f16to32 bits
29 end
30 for i ulong 5
31 put block i 0
32 end
33 if eq peak f32 0
34 ret true
35 end
36 scale = div peak f32 7
37 put block 0 uint bitsof scale
38 for i ulong 32
39 word = add 1 shr i 3
40 nibble = ar_q4_nibble f32of f16to32 ulong get src i scale
41 put block word or get block word shl nibble mul 4 and i 7
42 end
43 ret true
44end
45
46fn arboris_q4_weight block view uint 5 index ulong to f32
47 nibble = and shr get block add 1 shr index 3 mul 4 and index 7 15
48 value = sub int xor nibble 8 8
49 ret mul f32 value f32of ulong get block 0
50end
51
52note Experimental ParamSlab starts with 64 cubic coefficient words, followed
53note by Q4 synapses, zero padding to 16 bytes, then original FP16 bias/gate/
54note prediction rows. The trailing ARQ4 tag makes its extent 4 mod 16.
55fn ar_q4_tail m view ulong 16 to ulong
56 weights = positive_product get m 3 get m 4 0xffffffff
57 if and ne get m 3 0 eq weights 0
58 ret 0
59 end
60 blocks = div add weights 31 32
61 ret aligned_size add 256 mul blocks 20 16 0x1000000000
62end
63
64fn arboris_q4_bytes m view ulong 16 to ulong
65 tail = ar_q4_tail m
66 if or eq tail 0 or gt get m 2 0xffffffff gt get m 1 0x1000000
67 ret 0
68 end
69 if or eq get m 4 0 or gt get m 4 24576 ne and get m 4 7 0
70 ret 0
71 end
72 rows = add get m 2 mul get m 1 2
73 ret add add tail mul rows mul get m 4 2 4
74end
75
76fn ar_q4_copy_block src view ushort * b ulong dst edit uint * to bool
77 raw = new ushort 32 0
78 block = new uint 5 0
79 for j ulong 32
80 index = add mul b 32 j
81 if lt index len src
82 put raw j get src index
83 end
84 end
85 if not arboris_q4_pack raw block
86 ret false
87 end
88 for j ulong 5
89 put dst add add 64 mul b 5 j get block j
90 end
91 ret true
92end
93
94fn ar_q4_copy_halves src view ushort * dst edit uint * tail ulong to void
95 for i ulong div len src 2 bound 0x1000000000000
96 ix = mul i 2
97 put dst add tail i or uint get src ix shl uint get src add ix 1 16
98 end
99end
100
101fn arboris_pack_q4_params m view ulong 16 src view ushort * dst edit uint * to bool
102 size = arboris_q4_bytes m
103 weights = mul get m 3 get m 4
104 tail = div ar_q4_tail m 4
105 if or eq size 0 ne len dst div size 4
106 ret false
107 end
108 if ne len src add weights mul 2 sub sub len dst tail 1
109 ret false
110 end
111 if not ag_half_span src 0 len src
112 ret false
113 end
114 coeff = new uint 64 0
115 arboris_cubic_table coeff
116 for i ulong 64
117 put dst i get coeff i
118 end
119 for b ulong div add weights 31 32 bound 0x8000000
120 if not ar_q4_copy_block part src 0 weights b dst
121 ret false
122 end
123 end
124 pad = add 64 mul div add weights 31 32 5
125 for i ulong sub tail pad bound 3
126 put dst add pad i 0
127 end
128 ar_q4_copy_halves part src weights sub len src weights dst tail
129 put dst sub len dst 1 0x34515241
130 ret true
131end