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 / safetensors.om

704 lines · 13494 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note om/gpu/engine/safetensors.om: Universal Pure Seeded-OM-2 Safetensors Tensor Registry.
3note Dynamically discovers and indexes tensors across ALL 3 checkpoint layouts with ZERO hardcoded offsets:
4note 1) Standard single-file HuggingFace: model.safetensors
5note 2) Standard sharded HuggingFace: model.safetensors.index.json -> model-00001-of-*.safetensors
6note 3) Omega per-layer packed safetensors: outside.safetensors + layers-0..L-1.safetensors
7note Stores every tensor's (file_id, byte_offset_from_file_start, byte_len, dtype, dim0, dim1)
8note and resolves any tensor by canonical name (auto-handling model.* vs model.language_model.*).
9
10const ST_MAX_FILES ulong 256
11const ST_MAX_TENSORS ulong 163840
12const ST_HASH_SZ ulong 524288
13const ST_HASH_MASK ulong 524287
14
15const DT_BF16 ulong 0
16const DT_F16 ulong 1
17const DT_F32 ulong 2
18const DT_F8_E4M3 ulong 3
19const DT_OTHER ulong 4
20const DT_I64 ulong 5
21
22global stFileNamePool uchar 16384
23global stFileNameOff ulong 256
24global stFileNameLen ulong 256
25global stFileDataOff ulong 256
26global stFileCount ulong 1
27global stLayoutMode ulong 1
28
29global stNamePool uchar 16777216
30global stNamePoolUsed ulong 1
31global stNameOff ulong 163840
32global stNameLen ulong 163840
33global stFileId ulong 163840
34global stByteOff ulong 163840
35global stByteLen ulong 163840
36global stDtype ulong 163840
37global stDim0 ulong 163840
38global stDim1 ulong 163840
39global stTensorCount ulong 1
40global stHashTab uint 524288
41
42global stHdrBuf uchar 4194304
43global stIdxBuf uchar 262400
44global stIdxLen ulong 1
45global stPathBuf uchar 1024
46global stOpenFd int 1
47global stOpenFid ulong 1
48
49text stSufSingle "/model.safetensors"
50text stSufIndex "/model.safetensors.index.json"
51text stSufOutside "/outside.safetensors"
52text stSufLayerPre "layers-"
53text stSufSt ".safetensors"
54text stKeyDtype "\"dtype\""
55text stKeyShape "\"shape\""
56text stKeyOffs "\"data_offsets\""
57text stKeyMeta "__metadata__"
58text stKeyWMap "\"weight_map\""
59text stPrefLang "model.language_model."
60text stPrefPlain "model."
61text stLayPre "layers."
62
63fn stAddFile name view uchar * nlen ulong to ulong
64cnt = get stFileCount 0
65for i ulong cnt bound 256
66if eq get stFileNameLen i nlen
67off = get stFileNameOff i
68same = true
69for j ulong nlen bound 128
70if ne get stFileNamePool add off j get name j
71same = false
72break
73end
74end
75if same
76ret i
77end
78end
79end
80if ge cnt ST_MAX_FILES
81ret ST_MAX_FILES
82end
83off = ulong 0
84if gt cnt 0
85off = add get stFileNameOff sub cnt 1 get stFileNameLen sub cnt 1
86end
87if gt add off nlen 16300
88ret ST_MAX_FILES
89end
90for j ulong nlen bound 128
91put stFileNamePool add off j get name j
92end
93put stFileNameOff cnt off
94put stFileNameLen cnt nlen
95put stFileCount 0 add cnt 1
96ret cnt
97end
98
99fn stBuildFilePath fid ulong to ulong
100dlen = get engModelDirLen 0
101for i ulong dlen bound 512
102put stPathBuf i get engModelDir i
103end
104p = dlen
105put stPathBuf p 47
106p = add p 1
107off = get stFileNameOff fid
108nlen = get stFileNameLen fid
109for i ulong nlen bound 128
110put stPathBuf p get stFileNamePool add off i
111p = add p 1
112end
113put stPathBuf p 0
114ret p
115end
116
117fn stOpenFile fid ulong to int
118ofd = get stOpenFd 0
119if and ge ofd int 0 eq get stOpenFid 0 fid
120ret ofd
121end
122if ge ofd int 0
123c0 = fclose ofd
124put stOpenFd 0 neg 1
125end
126plen = stBuildFilePath fid
127fd = int fopen stPathBuf 0 plen 0 0
128if ge fd int 0
129put stOpenFd 0 fd
130put stOpenFid 0 fid
131end
132ret fd
133end
134
135fn stCloseCached to void
136ofd = get stOpenFd 0
137if ge ofd int 0
138c0 = fclose ofd
139put stOpenFd 0 neg 1
140end
141end
142
143fn stParseU64Hdr pos ulong lim ulong to ulong
144v = ulong 0
145p = pos
146while lt p lim bound 32
147c = get stHdrBuf p
148if and ge c 48 le c 57
149v = add mul v 10 ulong sub c 48
150p = add p 1
151else
152break
153end
154end
155ret v
156end
157
158fn stSkipWsHdr pos ulong lim ulong to ulong
159p = pos
160while lt p lim bound 128
161c = get stHdrBuf p
162if or eq c 32 or eq c 9 or eq c 10 eq c 13
163p = add p 1
164else
165break
166end
167end
168ret p
169end
170
171fn stSkipDigitsHdr pos ulong lim ulong to ulong
172p = pos
173while lt p lim bound 32
174c = get stHdrBuf p
175if and ge c 48 le c 57
176p = add p 1
177else
178break
179end
180end
181ret p
182end
183
184fn stSkipSepHdr pos ulong lim ulong to ulong
185p = pos
186while lt p lim bound 16
187c = get stHdrBuf p
188if or eq c 44 eq c 32
189p = add p 1
190else
191break
192end
193end
194ret p
195end
196
197fn stParseTensorMeta tid ulong oStart ulong oEnd ulong dataOff ulong to void
198put stDtype tid DT_OTHER
199put stDim0 tid 0
200put stDim1 tid 0
201put stByteOff tid dataOff
202put stByteLen tid 0
203q = oStart
204while lt add q 14 oEnd bound 2048
205if eq get stHdrBuf q 34
206c1 = get stHdrBuf add q 1
207c2 = get stHdrBuf add q 2
208if and eq c1 100 eq c2 116
209k = add q 7
210while and lt k oEnd ne get stHdrBuf k 34 bound 16
211k = add k 1
212end
213k = add k 1
214if lt add k 2 oEnd
215x0 = get stHdrBuf k
216x1 = get stHdrBuf add k 1
217if and eq x0 66 eq x1 70
218put stDtype tid DT_BF16
219else
220if and eq x0 70 eq x1 56
221put stDtype tid DT_F8_E4M3
222else
223if and eq x0 70 eq x1 49
224put stDtype tid DT_F16
225else
226if and eq x0 70 eq x1 51
227put stDtype tid DT_F32
228else
229if and eq x0 73 eq x1 54
230put stDtype tid DT_I64
231end
232end
233end
234end
235end
236end
237end
238if and eq c1 115 eq c2 104
239k = add q 7
240while and lt k oEnd ne get stHdrBuf k 91 bound 16
241k = add k 1
242end
243k = stSkipWsHdr add k 1 oEnd
244put stDim0 tid stParseU64Hdr k oEnd
245k = stSkipSepHdr stSkipDigitsHdr k oEnd oEnd
246if lt k oEnd
247put stDim1 tid stParseU64Hdr k oEnd
248end
249end
250if and eq c1 100 eq c2 97
251k = add q 14
252while and lt k oEnd ne get stHdrBuf k 91 bound 16
253k = add k 1
254end
255k = stSkipWsHdr add k 1 oEnd
256b0 = stParseU64Hdr k oEnd
257k = stSkipSepHdr stSkipDigitsHdr k oEnd oEnd
258b1 = stParseU64Hdr k oEnd
259put stByteOff tid add dataOff b0
260put stByteLen tid sub b1 b0
261end
262end
263q = add q 1
264end
265end
266
267fn stCopyName used ulong nStart ulong nLen ulong to void
268for k ulong nLen bound 256
269put stNamePool add used k get stHdrBuf add nStart k
270end
271end
272
273fn stHashHdr nStart ulong nLen ulong to ulong
274h = ulong 14695981039346656037
275for k ulong nLen bound 256
276h = mul xor h ulong get stHdrBuf add nStart k 1099511628211
277end
278ret and h ST_HASH_MASK
279end
280
281fn stHashView name view uchar * nlen ulong to ulong
282h = ulong 14695981039346656037
283for k ulong nlen bound 256
284h = mul xor h ulong get name k 1099511628211
285end
286ret and h ST_HASH_MASK
287end
288
289fn stHashInsert tid ulong slot0 ulong to void
290slot = slot0
291for probe ulong 1024
292if eq get stHashTab slot 0
293put stHashTab slot uint add tid 1
294ret
295end
296slot = and add slot 1 ST_HASH_MASK
297end
298end
299
300fn stRecordTensor fid ulong dataOff ulong nStart ulong nLen ulong oStart ulong oEnd ulong to void
301if and eq nLen 12 eq get stHdrBuf nStart 95
302ret
303end
304if ge nLen 13
305if eq get stHdrBuf add nStart 6 118
306if eq get stHdrBuf add nStart 7 105
307ret
308end
309end
310end
311if ge nLen 4
312if eq get stHdrBuf nStart 109
313if eq get stHdrBuf add nStart 1 116
314if eq get stHdrBuf add nStart 2 112
315if eq get stHdrBuf add nStart 3 46
316ret
317end
318end
319end
320end
321end
322tid = get stTensorCount 0
323used = get stNamePoolUsed 0
324if or ge tid ST_MAX_TENSORS ge add used nLen 16770000
325ret
326end
327stCopyName used nStart nLen
328put stNameOff tid used
329put stNameLen tid nLen
330put stNamePoolUsed 0 add used nLen
331put stFileId tid fid
332stParseTensorMeta tid oStart oEnd dataOff
333stHashInsert tid stHashHdr nStart nLen
334put stTensorCount 0 add tid 1
335end
336
337fn stSkipObj oStart ulong hlen ulong to ulong
338depth = ulong 1
339p = add oStart 1
340while and lt p hlen gt depth 0 bound 4096
341ch = get stHdrBuf p
342if eq ch 123
343depth = add depth 1
344else
345if eq ch 125
346depth = sub depth 1
347end
348end
349p = add p 1
350end
351ret p
352end
353
354fn stIndexOneFile fid ulong to bool
355fd = stOpenFile fid
356if lt fd int 0
357ret false
358end
359hdr8 = new uchar 8 0
360if ne pread fd hdr8 0 8 0 8
361ret false
362end
363hlen = ulong 0
364for i ulong 8
365hlen = or hlen shl ulong get hdr8 i mul i 8
366end
367if or eq hlen 0 gt hlen 4194000
368ret false
369end
370dataOff = add hlen 8
371put stFileDataOff fid dataOff
372tot = ulong 0
373while lt tot hlen bound 1024
374got = pread fd stHdrBuf tot sub hlen tot long add 8 tot
375if le got 0
376ret false
377end
378tot = add tot ulong got
379end
380p = ulong 0
381while lt p hlen bound 4194304
382if eq get stHdrBuf p 34
383nStart = add p 1
384p = add p 1
385while and lt p hlen ne get stHdrBuf p 34 bound 256
386p = add p 1
387end
388nLen = sub p nStart
389p = stSkipWsHdr add p 1 hlen
390if and lt p hlen eq get stHdrBuf p 58
391p = stSkipWsHdr add p 1 hlen
392if and lt p hlen eq get stHdrBuf p 123
393oStart = p
394p = stSkipObj oStart hlen
395stRecordTensor fid dataOff nStart nLen oStart p
396end
397end
398else
399p = add p 1
400end
401end
402ret true
403end
404
405fn stAddFromIdx s ulong ln ulong to void
406fnBuf = new uchar 128 0
407for k ulong ln bound 128
408put fnBuf k get stIdxBuf add s k
409end
410fid = stAddFile fnBuf ln
411end
412
413fn stIsStSuf p ulong to bool
414for k ulong 12
415if ne get stIdxBuf add sub p 12 k get stSufSt k
416ret false
417end
418end
419ret true
420end
421
422fn stScanIdxChunk tot ulong to void
423p = ulong 0
424while lt p tot bound 262400
425if eq get stIdxBuf p 34
426s = add p 1
427p = add p 1
428while lt p tot bound 256
429if eq get stIdxBuf p 34
430break
431end
432p = add p 1
433end
434if lt p tot
435ln = sub p s
436if and gt ln 12 lt ln 120
437if stIsStSuf p
438stAddFromIdx s ln
439end
440end
441end
442end
443p = add p 1
444end
445end
446
447fn stFindLastNl tot ulong to ulong
448k = tot
449while gt k 0 bound 512
450k = sub k 1
451if eq get stIdxBuf k 10
452ret add k 1
453end
454end
455ret tot
456end
457
458fn stShiftCarry cut ulong tot ulong to ulong
459carry = sub tot cut
460for j ulong carry bound 512
461put stIdxBuf j get stIdxBuf add cut j
462end
463ret carry
464end
465
466fn stDiscoverFromIndex to bool
467plen = engJoinPath stSufIndex
468fd = int fopen engPathTmp 0 plen 0 0
469if lt fd int 0
470ret false
471end
472foff = ulong 0
473carry = ulong 0
474while true bound 512
475got = pread fd stIdxBuf carry sub 262144 carry long foff
476if le got 0
477break
478end
479tot = add carry ulong got
480cut = stFindLastNl tot
481stScanIdxChunk cut
482foff = add foff ulong got
483carry = stShiftCarry cut tot
484end
485if gt carry 0
486stScanIdxChunk carry
487end
488c0 = fclose fd
489ret gt get stFileCount 0 0
490end
491
492fn stBuildLayerFilename L ulong dst edit uchar 64 to ulong
493p = ulong 0
494for i ulong len stSufLayerPre bound 16
495put dst p get stSufLayerPre i
496p = add p 1
497end
498if ge L 100
499put dst p uchar add 48 div L 100
500p = add p 1
501end
502if ge L 10
503put dst p uchar add 48 rem div L 10 10
504p = add p 1
505end
506put dst p uchar add 48 rem L 10
507p = add p 1
508for i ulong len stSufSt bound 16
509put dst p get stSufSt i
510p = add p 1
511end
512ret p
513end
514
515text stNmOutside "outside.safetensors"
516text stNmSingle "model.safetensors"
517
518fn stAddOmegaLayers to void
519lnBuf = new uchar 64 0
520f0 = stAddFile stNmOutside len stNmOutside
521nL = get engSpec MS_LAYERS
522for L ulong nL bound 256
523lnLen = stBuildLayerFilename L lnBuf
524fl = stAddFile lnBuf lnLen
525end
526end
527
528fn stDiscoverLayout to bool
529plen = engJoinPath stSufOutside
530fd = int fopen engPathTmp 0 plen 0 0
531if ge fd int 0
532c0 = fclose fd
533put stLayoutMode 0 2
534stAddOmegaLayers
535ret true
536end
537if stDiscoverFromIndex
538put stLayoutMode 0 1
539ret true
540end
541plen2 = engJoinPath stSufSingle
542fd2 = int fopen engPathTmp 0 plen2 0 0
543if lt fd2 int 0
544ret false
545end
546c1 = fclose fd2
547put stLayoutMode 0 0
548f1 = stAddFile stNmSingle len stNmSingle
549ret true
550end
551
552fn stIndexModel to bool
553put stFileCount 0 0
554put stTensorCount 0 0
555put stNamePoolUsed 0 0
556put stOpenFd 0 neg 1
557put stOpenFid 0 0
558for hi ulong ST_HASH_SZ
559put stHashTab hi 0
560end
561if not stDiscoverLayout
562ret false
563end
564fc = get stFileCount 0
565for fid ulong fc bound 256
566if not stIndexOneFile fid
567stCloseCached
568ret false
569end
570end
571stCloseCached
572ret gt get stTensorCount 0 0
573end
574
575fn stFindExact name view uchar * nlen ulong to long
576slot = stHashView name nlen
577for probe ulong 1024
578ent = get stHashTab slot
579if eq ent 0
580ret neg 1
581end
582i = ulong sub ent 1
583if eq get stNameLen i nlen
584off = get stNameOff i
585same = true
586for j ulong nlen bound 256
587if ne get stNamePool add off j get name j
588same = false
589break
590end
591end
592if same
593ret long i
594end
595end
596slot = and add slot 1 ST_HASH_MASK
597end
598ret neg 1
599end
600
601fn stFindCanon suffix view uchar * slen ulong to long
602note Tries exact suffix, then "model.language_model." + suffix, then "model." + suffix
603r = stFindExact suffix slen
604if ge r 0
605ret r
606end
607tmp = new uchar 256 0
608p = ulong 0
609for i ulong len stPrefLang bound 32
610put tmp p get stPrefLang i
611p = add p 1
612end
613for i ulong slen bound 200
614put tmp p get suffix i
615p = add p 1
616end
617r2 = stFindExact tmp p
618if ge r2 0
619ret r2
620end
621p = ulong 0
622for i ulong len stPrefPlain bound 16
623put tmp p get stPrefPlain i
624p = add p 1
625end
626for i ulong slen bound 200
627put tmp p get suffix i
628p = add p 1
629end
630ret stFindExact tmp p
631end
632
633fn stBuildLayerKey L ulong suf view uchar * dst edit uchar 256 to ulong
634p = ulong 0
635for i ulong len stLayPre bound 16
636put dst p get stLayPre i
637p = add p 1
638end
639if ge L 100
640put dst p uchar add 48 div L 100
641p = add p 1
642end
643if ge L 10
644put dst p uchar add 48 rem div L 10 10
645p = add p 1
646end
647put dst p uchar add 48 rem L 10
648p = add p 1
649put dst p 46
650p = add p 1
651for i ulong len suf bound 128
652put dst p get suf i
653p = add p 1
654end
655ret p
656end
657
658fn stFindLayer L ulong suf view uchar * to long
659tmp = new uchar 256 0
660n = stBuildLayerKey L suf tmp
661ret stFindCanon tmp n
662end
663
664text stExpMid "mlp.experts."
665text stPleMid "ple.ple_embedding.ngram_embedding.shard_"
666text stPleEnd ".weight"
667
668fn stAppU64 dst edit uchar 256 p0 ulong v ulong to ulong
669p = p0
670if ge v 100
671put dst p uchar add 48 div v 100
672p = add p 1
673end
674if ge v 10
675put dst p uchar add 48 rem div v 10 10
676p = add p 1
677end
678put dst p uchar add 48 rem v 10
679ret add p 1
680end
681
682fn stFindExpert L ulong E ulong suf view uchar * to long
683tmp = new uchar 256 0
684p = stBuildLayerKey L stExpMid tmp
685p = stAppU64 tmp p E
686put tmp p 46
687p = add p 1
688for i ulong len suf bound 64
689put tmp p get suf i
690p = add p 1
691end
692ret stFindCanon tmp p
693end
694
695fn stFindPleShard L ulong P ulong to long
696tmp = new uchar 256 0
697p = stBuildLayerKey L stPleMid tmp
698p = stAppU64 tmp p P
699for i ulong len stPleEnd bound 16
700put tmp p get stPleEnd i
701p = add p 1
702end
703ret stFindCanon tmp p
704end