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 / gpu / engine / bonsai_nvfp4.om

515 lines · 12135 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note om/gpu/engine/bonsai_nvfp4.om: Canonical NVIDIA NVFP4 (E2M1 + UE4M3/16, alpha=1/256, 4.50 bpw)
3note - Builds exact LUT[272] (256 UE4M3*alpha + 16 E2M1) & 65536-entry FP16->(UE4M3, E2M1_mag) table on CPU (<2ms)
4note - Converts all 25.6B PTQ1_0 weights -> canonical NVFP4 [N, K/2] + [N, K/16] ON GPU via tPtq1ToNvfp4 (<0.2s!)
5note - Full-APU 20 ARMv9 CPU workers execute canonical 4-bit E2M1 + UE4M3 NVFP4 in parallel with 48 Blackwell SMs!
6
7global bonUseNvfp4 ulong 1
8global bonNvLutMap long 1
9global bonNvLutVa ulong 1
10global bonNvFp16Map long 1
11global bonNvFp16Va ulong 1
12global bonNvRawMap long 1
13global bonNvRawVa ulong 1
14global bonNvPktMap long 1
15
16global bonNvLutCpu f32 272
17global bonNvFp16Cpu uint 65536
18global bonNvBestErr f32 1
19global bonNvBestSc ulong 1
20global bonNvBestNib ulong 1
21
22note Shared slab allocation state & Full-APU CPU buffers (used by both PTQ1_0 and NVFP4)
23global bonCurMap long 1
24global bonCurOff ulong 1
25global bonCurVa ulong 1
26global bonTResVa ulong 1
27
28global bonCpuX f32 5120
29global bonCpuX4 f32 20480
30global bonCpuRowAcc f32 4
31global bonCpuLut uchar 1280
32global bonCpuLutF f32 1280
33global bonCpuRawBuf uchar 4194304
34global bonCpuRowStart ulong 1
35global bonCpuRowCnt ulong 1
36global bonCpuNvW uchar 4194304
37global bonCpuNvS uchar 524288
38
39text bonMsgModeNv4 "quant_format : NVFP4 Canonical (E2M1 + UE4M3/16, 4.50 bpw) + Hadamard-1024 + BF16-GDN + Full-APU (20 CPU + 48 SM)\n"
40
41fn bonSlabAllocMap bytes ulong to bool
42al = mul div add bytes 4095 4096 4096
43cnt = get engSlabCnt 0
44if gt cnt 0
45last = sub cnt 1
46used = get engSlabUsed last
47if le add used al ENG_SLAB_SZ
48put engSlabUsed last add used al
49put bonCurMap 0 get engSlabMap last
50put bonCurOff 0 used
51put bonCurVa 0 add get engSlabVa last used
52ret true
53end
54end
55if ge cnt 48
56ret false
57end
58sm = nvalloc ENG_SLAB_SZ true
59if le sm 0
60ret false
61end
62sva = mapaddr sm
63put engSlabMap cnt sm
64put engSlabVa cnt sva
65put engSlabUsed cnt al
66put engSlabCnt 0 add cnt 1
67put bonCurMap 0 sm
68put bonCurOff 0 0
69put bonCurVa 0 sva
70ret true
71end
72
73fn bonNvBuildUe4m3 c ulong to f32
74if ge c 127
75ret f32 0.0f32
76end
77se = shr c 3
78sm = and c 7
79if eq se 0
80ret mul f32 sm f32 0.00000762939453125f32
81end
82mant = f32 add 8 sm
83expI = sub long se 18
84v = mant
85while gt expI 0 bound 16
86v = mul v f32 2.0f32
87expI = sub expI 1
88end
89while lt expI 0 bound 24
90v = mul v f32 0.5f32
91expI = add expI 1
92end
93ret v
94end
95
96fn bonNvFillCpuLut to void
97for c ulong 256
98put bonNvLutCpu c bonNvBuildUe4m3 c
99end
100put bonNvLutCpu 256 f32 0.0f32
101put bonNvLutCpu 257 f32 0.5f32
102put bonNvLutCpu 258 f32 1.0f32
103put bonNvLutCpu 259 f32 1.5f32
104put bonNvLutCpu 260 f32 2.0f32
105put bonNvLutCpu 261 f32 3.0f32
106put bonNvLutCpu 262 f32 4.0f32
107put bonNvLutCpu 263 f32 6.0f32
108put bonNvLutCpu 264 f32 0.0f32
109put bonNvLutCpu 265 f32 -0.5f32
110put bonNvLutCpu 266 f32 -1.0f32
111put bonNvLutCpu 267 f32 -1.5f32
112put bonNvLutCpu 268 f32 -2.0f32
113put bonNvLutCpu 269 f32 -3.0f32
114put bonNvLutCpu 270 f32 -4.0f32
115put bonNvLutCpu 271 f32 -6.0f32
116end
117
118fn bonNvBinSearch target f32 to ulong
119lo = ulong 1
120hi = ulong 126
121while lt add lo 1 hi bound 8
122mid = shr add lo hi 1
123if le get bonNvLutCpu mid target
124lo = mid
125else
126hi = mid
127end
128end
129ret lo
130end
131
132fn bonNvCheckCand ad f32 nib ulong cc ulong to void
133if gt cc 126
134ret
135end
136err = sub mul get bonNvLutCpu cc get bonNvLutCpu add 256 nib ad
137if lt err f32 0.0f32
138err = sub f32 0.0f32 err
139end
140if lt err get bonNvBestErr 0
141put bonNvBestErr 0 err
142put bonNvBestSc 0 cc
143put bonNvBestNib 0 nib
144end
145end
146
147fn bonNvBestEntry d f32 to uint
148if eq d f32 0.0f32
149ret 0
150end
151negD = lt d f32 0.0f32
152ad = d
153if negD
154ad = sub f32 0.0f32 ad
155end
156if gt ad f32 100.0f32
157ret 0
158end
159put bonNvBestErr 0 f32 1000000000.0f32
160put bonNvBestSc 0 1
161put bonNvBestNib 0 2
162for ni ulong 2
163lo = bonNvBinSearch div ad get bonNvLutCpu add 258 ni
164bonNvCheckCand ad add 2 ni lo
165bonNvCheckCand ad add 2 ni add lo 1
166end
167mPos = get bonNvBestNib 0
168mNeg = or mPos 8
169if negD
170ret uint or get bonNvBestSc 0 or shl mNeg 8 shl mPos 12
171end
172ret uint or get bonNvBestSc 0 or shl mPos 8 shl mNeg 12
173end
174
175fn bonNvInitGpu to bool
176bonNvFillCpuLut
177put bonNvLutMap 0 nvalloc 4096 false
178if le get bonNvLutMap 0 0
179ret false
180end
181put bonNvLutVa 0 mapaddr get bonNvLutMap 0
182for i ulong 272
183engPutU32Io mul i 4 bitcast uint get bonNvLutCpu i
184end
185if ne mapwrite get bonNvLutMap 0 0 engIoBuf 0 1088 1088
186ret false
187end
188put bonNvFp16Map 0 nvalloc 262144 false
189if le get bonNvFp16Map 0 0
190ret false
191end
192put bonNvFp16Va 0 mapaddr get bonNvFp16Map 0
193for h ulong 65536
194expF = and shr h 10 31
195ent = uint 0
196if lt expF 31
197ent = bonNvBestEntry bitcast f32 ggFp16ToF32Bits h
198end
199put bonNvFp16Cpu h ent
200engPutU32Io mul h 4 ent
201end
202if ne mapwrite get bonNvFp16Map 0 0 engIoBuf 0 262144 262144
203ret false
204end
205put bonNvRawMap 0 nvalloc 16777216 false
206if le get bonNvRawMap 0 0
207ret false
208end
209put bonNvRawVa 0 mapaddr get bonNvRawMap 0
210put bonNvPktMap 0 nvalloc 65536 false
211if le get bonNvPktMap 0 0
212ret false
213end
214ret true
215end
216
217fn bonNvRunConv nBlks ulong dstWVa ulong dstSVa ulong to bool
218put gbused 0 0
219gbsetup
220gbmth 0 40 4
221gbemit 0
222gbemit 0
223gbemit 0
224gbemit 2281701376
225cb = mapaddr get gbmem 1
226gbdriver cb
227gbdma cb 224
228for j ulong 224
229gbemit get gbcb j
230end
231put engBindN 0 0
232engPtr get bonNvRawVa 0
233engPtr dstWVa
234engPtr dstSVa
235engPtr get bonNvFp16Va 0
236engArg uint nBlks
237engPrepQmdGy 27 uint div add nBlks 255 256 1 256
238words = get engBindN 0
239endva = add cb add 896 mul words 4
240put gbcb 212 gbhi endva
241put gbcb 213 gblo endva
242gbdma add cb 896 uint words
243for j2 ulong words
244gbemit get engBind j2
245end
246q = mapaddr get gbmem 2
247gbinline q false
248gbdma add q 8192 1
249gbemit 1
250gbinline add q 4096 true
251gbtail
252tot = get gbused 0
253if ne mapwrite get bonNvPktMap 0 0 gbcmd 0 tot long tot
254ret false
255end
256if lt mapset get gbmem 2 8192 4 0 0
257ret false
258end
259if lt mapset get gbmem 3 0 4 0 0
260ret false
261end
262if lt mapset get gbmem 3 16 4 0 0
263ret false
264end
265if not nvsend get bonNvPktMap 0 0 uint div tot 4
266ret false
267end
268if not engWaitFast 30000000000
269ret false
270end
271ret eq mapget get gbmem 3 0 4 1145324612
272end
273
274fn bonNvReadRawChunk fd int foff ulong rawB ulong to bool
275rdDone = ulong 0
276while lt rdDone rawB bound 64
277ch = sub rawB rdDone
278if gt ch 1048576
279ch = ulong 1048576
280end
281got = pread fd engIoBuf 0 ch long add foff rdDone
282if le got 0
283ret false
284end
285if ne mapwrite get bonNvRawMap 0 rdDone engIoBuf 0 ulong got got
286ret false
287end
288rdDone = add rdDone ulong got
289end
290ret true
291end
292
293fn bonNvStreamRange tid ulong nRows ulong dstWVa ulong dstSVa ulong to bool
294nBlksTot = div mul get ggDim0 tid nRows 128
295foff = add get ggDataStart 0 get ggTOff tid
296doneBlks = ulong 0
297while lt doneBlks nBlksTot bound 256
298cb = sub nBlksTot doneBlks
299if gt cb 524288
300cb = ulong 524288
301end
302if not bonNvReadRawChunk get ggFd 0 add foff mul doneBlks 28 mul cb 28
303ret false
304end
305if not bonNvRunConv cb add dstWVa mul doneBlks 64 add dstSVa mul doneBlks 8
306ret false
307end
308doneBlks = add doneBlks cb
309end
310ret true
311end
312
313fn bonLoadNvfp4_1 tid ulong to bool
314K = get ggDim0 tid
315N = get ggDim1 tid
316wB = div mul N K 2
317sB = div mul N K 16
318if not bonSlabAllocMap add wB sB
319ret false
320end
321wVa = get bonCurVa 0
322put bonTResVa 0 wVa
323ret bonNvStreamRange tid N wVa add wVa wB
324end
325
326fn bonLoadNvfp4_2 t0 ulong t1 ulong to bool
327K = get ggDim0 t0
328totN = add get ggDim1 t0 get ggDim1 t1
329wB = div mul totN K 2
330if not bonSlabAllocMap add wB div mul totN K 16
331ret false
332end
333wVa = get bonCurVa 0
334sVa = add wVa wB
335put bonTResVa 0 wVa
336if not bonNvStreamRange t0 get ggDim1 t0 wVa sVa
337ret false
338end
339wVa = add wVa div mul get ggDim1 t0 K 2
340sVa = add sVa div mul get ggDim1 t0 K 16
341ret bonNvStreamRange t1 get ggDim1 t1 wVa sVa
342end
343
344fn bonLoadNvfp4_3 t0 ulong t1 ulong t2 ulong to bool
345K = get ggDim0 t0
346totN = add add get ggDim1 t0 get ggDim1 t1 get ggDim1 t2
347wB = div mul totN K 2
348if not bonSlabAllocMap add wB div mul totN K 16
349ret false
350end
351wVa = get bonCurVa 0
352sVa = add wVa wB
353put bonTResVa 0 wVa
354if not bonNvStreamRange t0 get ggDim1 t0 wVa sVa
355ret false
356end
357wVa = add wVa div mul get ggDim1 t0 K 2
358sVa = add sVa div mul get ggDim1 t0 K 16
359if not bonNvStreamRange t1 get ggDim1 t1 wVa sVa
360ret false
361end
362wVa = add wVa div mul get ggDim1 t1 K 2
363sVa = add sVa div mul get ggDim1 t1 K 16
364ret bonNvStreamRange t2 get ggDim1 t2 wVa sVa
365end
366
367fn bonLoadNvfp4_Slice tid ulong nRows ulong to bool
368K = get ggDim0 tid
369wB = div mul nRows K 2
370sB = div mul nRows K 16
371if not bonSlabAllocMap add wB sB
372ret false
373end
374wVa = get bonCurVa 0
375put bonTResVa 0 wVa
376ret bonNvStreamRange tid nRows wVa add wVa wB
377end
378
379note =========================================================================
380note Full-APU 20-Core CPU Canonical NVFP4 Workers (exact E2M1 + UE4M3 dot products!)
381note =========================================================================
382global bonConvMPos ulong 1
383global bonConvMNeg ulong 1
384
385fn bonCpuMapTrit tr ulong to ulong
386if eq tr 0
387ret get bonConvMNeg 0
388end
389if eq tr 2
390ret get bonConvMPos 0
391end
392ret 0
393end
394
395fn bonCpuConvPairByte b0 ulong b1 ulong tr ulong to uchar
396n0 = bonCpuMapTrit ulong get bonCpuLut add b0 tr
397n1 = bonCpuMapTrit ulong get bonCpuLut add b1 tr
398ret uchar or n0 shl n1 4
399end
400
401fn bonCpuConvBlkG1 src ulong wb ulong to void
402for m ulong 8
403b0 = mul ulong get bonCpuRawBuf add src mul m 2 5
404b1 = mul ulong get bonCpuRawBuf add src add mul m 2 1 5
405for tr ulong 5
406put bonCpuNvW add wb add mul tr 8 m bonCpuConvPairByte b0 b1 tr
407end
408end
409end
410
411fn bonCpuConvBlkG2 src ulong wb ulong to void
412for m2 ulong 4
413b0 = mul ulong get bonCpuRawBuf add src add 16 mul m2 2 5
414b1 = mul ulong get bonCpuRawBuf add src add 16 add mul m2 2 1 5
415for tr2 ulong 5
416put bonCpuNvW add wb add 40 add mul tr2 4 m2 bonCpuConvPairByte b0 b1 tr2
417end
418end
419end
420
421fn bonCpuConvBlkG3 src ulong wb ulong to void
422q0 = mul ulong get bonCpuRawBuf add src 24 5
423q1 = mul ulong get bonCpuRawBuf add src 25 5
424for tr3 ulong 4
425put bonCpuNvW add wb add 60 tr3 bonCpuConvPairByte q0 q1 tr3
426end
427end
428
429fn bonCpuConvBlkNv4 b ulong to void
430src = mul b 28
431ent = ulong get bonNvFp16Cpu or ulong get bonCpuRawBuf add src 26 shl ulong get bonCpuRawBuf add src 27 8
432put bonConvMPos 0 and shr ent 8 15
433put bonConvMNeg 0 and shr ent 12 15
434for si ulong 8
435put bonCpuNvS add mul b 8 si uchar and ent 255
436end
437bonCpuConvBlkG1 src mul b 64
438bonCpuConvBlkG2 src mul b 64
439bonCpuConvBlkG3 src mul b 64
440end
441
442fn bonWorkerConvertNvfp4 to void
443nBlks = mul get bonCpuRowCnt 0 40
444for b ulong nBlks bound 65536
445bonCpuConvBlkNv4 b
446end
447end
448
449fn bonCpuDotGrpNv4 wb ulong xb ulong to f32
450bs = f32 0.0f32
451for j ulong 8
452by = ulong get bonCpuNvW add wb j
453x0 = add xb mul j 2
454w = get bonNvLutCpu add 256 and by 15
455bs = add bs mul w get bonCpuX x0
456w = get bonNvLutCpu add 256 shr by 4
457bs = add bs mul w get bonCpuX add x0 1
458end
459ret bs
460end
461
462fn bonCpuDotRowNvfp4 rIdx ulong to f32
463acc = f32 0.0f32
464wBase = mul rIdx 2560
465sBase = mul rIdx 320
466for g ulong 320
467sc = get bonNvLutCpu ulong get bonCpuNvS add sBase g
468acc = add acc mul bonCpuDotGrpNv4 add wBase mul g 8 mul g 16 sc
469end
470ret acc
471end
472
473global bonNvGrpAcc f32 4
474
475fn bonCpuDotGrpM4Nv4 wb ulong xb ulong to void
476s0 = f32 0.0f32
477s1 = f32 0.0f32
478s2 = f32 0.0f32
479s3 = f32 0.0f32
480for j ulong 8
481by = ulong get bonCpuNvW add wb j
482x0 = add xb mul j 2
483w = get bonNvLutCpu add 256 and by 15
484s0 = add s0 mul w get bonCpuX4 x0
485s1 = add s1 mul w get bonCpuX4 add x0 5120
486s2 = add s2 mul w get bonCpuX4 add x0 10240
487s3 = add s3 mul w get bonCpuX4 add x0 15360
488w = get bonNvLutCpu add 256 shr by 4
489s0 = add s0 mul w get bonCpuX4 add x0 1
490s1 = add s1 mul w get bonCpuX4 add x0 5121
491s2 = add s2 mul w get bonCpuX4 add x0 10241
492s3 = add s3 mul w get bonCpuX4 add x0 15361
493end
494put bonNvGrpAcc 0 s0
495put bonNvGrpAcc 1 s1
496put bonNvGrpAcc 2 s2
497put bonNvGrpAcc 3 s3
498end
499
500fn bonCpuDotRowM4Nvfp4 rIdx ulong to void
501put bonCpuRowAcc 0 f32 0.0f32
502put bonCpuRowAcc 1 f32 0.0f32
503put bonCpuRowAcc 2 f32 0.0f32
504put bonCpuRowAcc 3 f32 0.0f32
505wBase = mul rIdx 2560
506sBase = mul rIdx 320
507for g ulong 320
508bonCpuDotGrpM4Nv4 add wBase mul g 8 mul g 16
509sc = get bonNvLutCpu ulong get bonCpuNvS add sBase g
510put bonCpuRowAcc 0 add get bonCpuRowAcc 0 mul get bonNvGrpAcc 0 sc
511put bonCpuRowAcc 1 add get bonCpuRowAcc 1 mul get bonNvGrpAcc 1 sc
512put bonCpuRowAcc 2 add get bonCpuRowAcc 2 mul get bonNvGrpAcc 2 sc
513put bonCpuRowAcc 3 add get bonCpuRowAcc 3 mul get bonNvGrpAcc 3 sc
514end
515end