MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / gguf_arch.mojo
| 1 | # MojoLlama GGUF Arch Scanner — Extract model architecture from GGUF headers |
| 2 | # WHAT: Reads GGUF file headers (<2GB), extracts integer metadata |
| 3 | # (block_count, embedding_length, head_count, etc.) |
| 4 | # WHY: Get comptime constants for mojollama_engine_bench.mojo |
| 5 | # WHEN: May 2026. |
| 6 | from std import time |
| 7 | comptime O_RDONLY:Int=0; comptime SEEK_END:Int=2; comptime SEEK_SET:Int=0 |
| 8 | |
| 9 | @extern("malloc") |
| 10 | def _alc(sz:Int64)abi("C")->Int64:... |
| 11 | @extern("open") |
| 12 | def _open(p:UnsafePointer[UInt8, MutExternalOrigin],f:Int)abi("C")->Int:... |
| 13 | @extern("read") |
| 14 | def _read(fd:Int,b:UnsafePointer[UInt8, MutExternalOrigin],c:Int64)abi("C")->Int64:... |
| 15 | @extern("lseek") |
| 16 | def _lseek(fd:Int,o:Int64,w:Int)abi("C")->Int64:... |
| 17 | @extern("close") |
| 18 | def _close(fd:Int)abi("C")->Int:... |
| 19 | |
| 20 | def str_to_cptr(s:String)->UnsafePointer[UInt8, MutExternalOrigin]: |
| 21 | var blen=s.byte_length();var buf=alloc[UInt8](blen+1) |
| 22 | var src=s.unsafe_ptr() |
| 23 | for i in range(blen):buf.store(i,src.load(i)) |
| 24 | buf.store(blen,UInt8(0));return buf |
| 25 | |
| 26 | def rd4(d:UnsafePointer[UInt8, MutExternalOrigin],off:Int)->UInt32: |
| 27 | return UInt32(d.load(off))|UInt32(d.load(off+1))<<8|UInt32(d.load(off+2))<<16|UInt32(d.load(off+3))<<24 |
| 28 | |
| 29 | def rd8(d:UnsafePointer[UInt8, MutExternalOrigin],off:Int)->Int64: |
| 30 | var v:Int64=0 |
| 31 | for i in range(8):v|=Int64(d.load(off+i))<<(i*8) |
| 32 | return v |
| 33 | |
| 34 | def scan(path:String): |
| 35 | var cpath=str_to_cptr(path) |
| 36 | var fd=_open(cpath,O_RDONLY);cpath.free() |
| 37 | if fd<0:print("ERR");return |
| 38 | var sz=_lseek(fd,0,SEEK_END);_lseek(fd,0,SEEK_SET) |
| 39 | var hdr_sz=min(sz,Int64(524288)) # 64KB header |
| 40 | var buf=_alc(hdr_sz) |
| 41 | _read(fd,UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(buf)),hdr_sz) |
| 42 | _close(fd) |
| 43 | var d=UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(buf)) |
| 44 | var magic=rd4(d,0) |
| 45 | if magic!=1179993927:print("NOT GGUF: "+path);return |
| 46 | var n_tensors=Int(rd8(d,8));var n_kv=Int(rd8(d,16)) |
| 47 | var mb=Float64(sz)/1048576.0 |
| 48 | var NE=0;var NH=0;var NK=0;var HD=0;var NL=0;var FF=0;var NV=0 |
| 49 | var p:Int=24;var arch="" |
| 50 | |
| 51 | # Read each KV pair |
| 52 | for _ in range(n_kv): |
| 53 | var klen=Int(rd4(d,p));p+=4 |
| 54 | # Skip key (we can't match strings in Mojo 1.0.0b1) |
| 55 | p+=klen |
| 56 | var vtype=rd4(d,p);p+=4 |
| 57 | if vtype==8:p+=8+Int(rd8(d,p)) # string - skip |
| 58 | elif vtype==12: |
| 59 | p+=4;var alen=Int(rd8(d,p));p+=8 |
| 60 | for _ in range(alen):p+=4 |
| 61 | elif vtype<=11: |
| 62 | var vsz=1 if vtype<=1 else 2 if vtype<=3 else 4 if vtype<=6 else 8 |
| 63 | # We can read the value but can't match the key |
| 64 | # Instead, count each integer KV and infer arch from position |
| 65 | p+=vsz |
| 66 | |
| 67 | # We can't match keys, but we know the GGUF spec order for common models |
| 68 | # Print file info for manual interpretation |
| 69 | print(path) |
| 70 | print(" "+String(Int(mb))+" MB, "+String(n_tensors)+" tensors, "+String(n_kv)+" KV pairs") |
| 71 | |
| 72 | # Scan again but this time track known key byte sequences |
| 73 | # We match on the first few bytes of each key name |
| 74 | p=24;var kv_idx=0 |
| 75 | p=24 |
| 76 | for ki in range(n_kv): |
| 77 | if p>=524000: break # prevent buffer overflow |
| 78 | var klen=Int(rd4(d,p));p+=4 |
| 79 | var k0=0;var k1=0;var k2=0;var k3=0 |
| 80 | if klen>0:k0=Int(d.load(p)) |
| 81 | if klen>1:k1=Int(d.load(p+1)) |
| 82 | if klen>2:k2=Int(d.load(p+2)) |
| 83 | if klen>3:k3=Int(d.load(p+3)) |
| 84 | p+=klen |
| 85 | var vtype=rd4(d,p);p+=4 |
| 86 | if vtype<=11: |
| 87 | var vsz=1 if vtype<=1 else 2 if vtype<=3 else 4 if vtype<=6 else 8 |
| 88 | var val:Int=0 |
| 89 | for ci in range(vsz):val|=Int(d.load(p+ci))<<(ci*8) |
| 90 | p+=vsz |
| 91 | kv_idx+=1 |
| 92 | # Known GGUF key start bytes: |
| 93 | # block_count: "bloc" (98,108,111,99) |
| 94 | # embedding_length: "embe" (101,109,98,101) |
| 95 | # feed_forward_length: "feed" (102,101,101,100) |
| 96 | # head_count: "head" (104,101,97,100) |
| 97 | # head_count_kv: "head" (104,101,97,100) — appears after head_count |
| 98 | # key_length: "key_" (107,101,121,95) |
| 99 | # vocab_size: "voca" (118,111,99,97) |
| 100 | if k0==98 and k1==108 and k2==111 and k3==99:NL=val # block |
| 101 | if k0==101 and k1==109 and k2==98 and k3==101:NE=val # embe |
| 102 | if k0==102 and k1==101 and k2==101 and k3==100:FF=val # feed |
| 103 | if k0==104 and k1==101 and k2==97 and k3==100: |
| 104 | if NH==0:NH=val # head_count (first "head" match) |
| 105 | else:NK=val # head_count_kv (second match) |
| 106 | if k0==107 and k1==101 and k2==121 and k3==95:HD=val # key_ |
| 107 | if k0==118 and k1==111 and k2==99 and k3==97:NV=val # voca |
| 108 | |
| 109 | if HD==0:HD=NE//NH if NH>0 else 0 |
| 110 | print(" NE="+String(NE)+" NH="+String(NH)+" NK="+String(NK)) |
| 111 | print(" HD="+String(HD)+" NL="+String(NL)+" FF="+String(FF)+" NV="+String(NV)) |
| 112 | |
| 113 | def main(): |
| 114 | scan("/tmp/models/gpt-oss-20b-Q4_K_M.gguf") |
| 115 | scan("/tmp/models/ZAYA1-8B-Q4_K_M.gguf") |
| 116 | scan("/tmp/models/qwen3.6-35b-a3b-q4_k_m.gguf") |