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 / gpu / nvidia / native / models / mxdot.om

163 lines · 4095 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Block-scaled dot products and C = A B^T on the CPU for three formats:
3note fmt 0 NVFP4 (E2M1, UE4M3 scale per 16), fmt 1 MXFP4 (E2M1, E8M0 per 32),
4note fmt 2 MXFP8 E4M3 (E8M0 per 32). Needs fp8.om and nvfp4.om in the unit.
5note A is m rows and B is n rows, each row k elements, K-contiguous (TN, as the
6note cuBLASLt block-scaled path). Rows start on block boundaries. Element bytes
7note live in one 4096-byte array (FP4: two per byte, low nibble first), scales
8note in one 256-byte array, block-major per row. d holds fmt m n k.
9note Each block term is computed exactly: the element products are integers
10note (E2M1 times 2, E4M3 times 2^9), summed in a long, then times the two scales
11note as one binary64 power of two. Terms are added left to right in binary64
12note (round to nearest even). Error bound for nb blocks, u = 2^-53:
13note |acc - exact| <= gamma(nb-1) sum|term|, gamma(j) = j u/(1 - j u).
14note Wolfram: Max inner products {16 144 225, 32 144, 32 229376^2} ->
15note {518400, 4608, 1683627180032} < 2^53.
16note Refusals trap 5: NaN element (E4M3 127/255), E8M0 255, UE4M3 above 126,
17note a bad fmt, k not a multiple of the block, or arrays too small.
18fn e2m1sx nib uint to long
19v = long e2m1x2 and nib 7
20if eq and nib 8 8
21ret neg v
22end
23ret v
24end
25fn e4m3sx code uint to long
26ex = and shr code 3 15
27m = long and code 7
28if eq and code 127 127
29trap 5
30end
31v = m
32if gt ex 0
33v = shl or m 8 sub long ex 1
34end
35if eq and code 128 128
36ret neg v
37end
38ret v
39end
40fn pow2f64 k long to f64
41if lt k -1022
42trap 5
43end
44if gt k 1023
45trap 5
46end
47ret bitcast f64 shl ulong add k 1023 52
48end
49fn e8m0exp sc uint to long
50if eq sc 255
51trap 5
52end
53ret sub long sc 127
54end
55fn fp4get a view uchar 4096 i ulong to uint
56b = uint get a shr i 1
57ret and shr b uint mul and i 1 4 15
58end
59fn nvterm a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 ia ulong ib ulong to f64
60acc = long 0
61for i ulong 16
62x = e2m1sx fp4get a add mul ia 16 i
63y = e2m1sx fp4get b add mul ib 16 i
64acc = add acc mul x y
65end
66ca = uint get sa ia
67cb = uint get sb ib
68w = mul long mul ue4m3sig ca ue4m3sig cb acc
69p = add add ue4m3exp ca ue4m3exp cb -2
70ret mul f64 w pow2f64 p
71end
72fn mx4term a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 ia ulong ib ulong to f64
73acc = long 0
74for i ulong 32
75x = e2m1sx fp4get a add mul ia 32 i
76y = e2m1sx fp4get b add mul ib 32 i
77acc = add acc mul x y
78end
79p = add add e8m0exp uint get sa ia e8m0exp uint get sb ib -2
80ret mul f64 acc pow2f64 p
81end
82fn mx8term a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 ia ulong ib ulong to f64
83acc = long 0
84for i ulong 32
85x = e4m3sx uint get a add mul ia 32 i
86y = e4m3sx uint get b add mul ib 32 i
87acc = add acc mul x y
88end
89p = add add e8m0exp uint get sa ia e8m0exp uint get sb ib -18
90ret mul f64 acc pow2f64 p
91end
92fn mxblk fmt ulong to ulong
93if eq fmt 0
94ret 16
95end
96if gt fmt 2
97trap 5
98end
99ret 32
100end
101fn mxterm a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 d view ulong 4 ij ulong to f64
102fmt = get d 0
103n = get d 2
104kb = div get d 3 mxblk fmt
105ia = mul div ij n kb
106ib = mul rem ij n kb
107acc = 0.0f64
108for t ulong kb
109if eq fmt 0
110acc = add acc nvterm a sa b sb add ia t add ib t
111continue
112end
113if eq fmt 1
114acc = add acc mx4term a sa b sb add ia t add ib t
115continue
116end
117acc = add acc mx8term a sa b sb add ia t add ib t
118end
119ret acc
120end
121fn mxcheck d view ulong 4 to void
122fmt = get d 0
123bs = mxblk fmt
124m = get d 1
125n = get d 2
126k = get d 3
127if eq k 0
128trap 5
129end
130if ne rem k bs 0
131trap 5
132end
133note Divide the capacity before comparing so full-width dimensions cannot wrap products.
134if ne n 0
135if gt m div ulong 256 n
136trap 5
137end
138end
139kb = div k bs
140if gt m div ulong 256 kb
141trap 5
142end
143if gt n div ulong 256 kb
144trap 5
145end
146eb = k
147if lt fmt 2
148eb = div k 2
149end
150if gt m div ulong 4096 eb
151trap 5
152end
153if gt n div ulong 4096 eb
154trap 5
155end
156end
157fn mxmm a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 c edit f64 256 d view ulong 4 to void
158mxcheck d
159mn = mul get d 1 get d 2
160for ij ulong mn
161put c ij mxterm a sa b sb d ij
162end
163end