interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / mojo_engine / gguf_extract.c

179 lines · 6724 bytes Raw Blame History
1/* gguf_extract.c — GGUF → raw .bin extractor (standalone CLI)
2 WHAT: Reads any GGUF file, extracts all tensors to <out_dir>/<name>.bin
3 WHY: Standalone binary called from Mojo. Avoids @extern("write") stdlib conflict.
4 USAGE: ./gguf_extract <model.gguf> <output_dir>
5 WHEN: May 2026.
6*/
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <stdint.h>
11#include <sys/stat.h>
12#include <unistd.h>
13#include <fcntl.h>
14
15#define GGUF_ALIGN 32
16
17static uint32_t r4(const uint8_t *p) {
18 return (uint32_t)p[0] | (uint32_t)p[1]<<8 | (uint32_t)p[2]<<16 | (uint32_t)p[3]<<24;
19}
20static uint64_t r8(const uint8_t *p) {
21 uint64_t v = 0;
22 for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (i*8);
23 return v;
24}
25static uint64_t min_u64(uint64_t a, uint64_t b) { return a < b ? a : b; }
26
27int main(int argc, char **argv) {
28 if (argc < 2) {
29 fprintf(stderr, "Usage: gguf_extract <model.gguf> [output_dir]\n");
30 return 1;
31 }
32 const char *path = argv[1];
33 char out_dir[1024];
34 if (argc >= 3) {
35 snprintf(out_dir, sizeof(out_dir), "%s", argv[2]);
36 } else {
37 // Default output dir: /tmp/<modelname>_weights/
38 const char *base = strrchr(path, '/');
39 base = base ? base + 1 : path;
40 snprintf(out_dir, sizeof(out_dir), "/tmp/%.*s_weights", (int)(strlen(base)-5), base);
41 }
42
43 // Read up to 4MB for headers
44 int fd = open(path, O_RDONLY);
45 if (fd < 0) { perror("open"); return 1; }
46 uint64_t fsz = (uint64_t)lseek(fd, 0, SEEK_END);
47 lseek(fd, 0, SEEK_SET);
48 uint64_t chunk = min_u64(fsz, 4194304ULL);
49 uint8_t *buf = (uint8_t*)malloc(chunk);
50 if (!buf) { close(fd); return 1; }
51 if (read(fd, buf, chunk) != (ssize_t)chunk) { free(buf); close(fd); return 1; }
52 close(fd);
53
54 if (r4(buf) != 0x46554747) { fprintf(stderr, "Not GGUF\n"); free(buf); return 1; }
55 uint64_t n_tensors = r8(buf + 8);
56 uint64_t n_kv = r8(buf + 16);
57 printf("GGUF: %lu tensors, %lu KV, %.0fMB\n", n_tensors, n_kv, fsz/1048576.0);
58
59 // Create output dir
60 mkdir(out_dir, 0755);
61
62 uint64_t pos = 24;
63 // Skip KV pairs
64 for (uint64_t k = 0; k < n_kv; k++) {
65 uint64_t klen = r8(buf + pos); pos += 8;
66 pos += klen;
67 uint32_t vtype = r4(buf + pos); pos += 4;
68 switch (vtype) {
69 case 0: case 1: pos += 1; break;
70 case 2: case 3: pos += 2; break;
71 case 4: case 5: case 6: pos += 4; break;
72 case 7: pos += 1; break;
73 case 8: { uint64_t sl = r8(buf + pos); pos += 8 + sl; break; }
74 case 9: {
75 uint32_t at = r4(buf + pos); pos += 4;
76 uint64_t al = r8(buf + pos); pos += 8;
77 for (uint64_t a = 0; a < al; a++) {
78 if (at == 8) { uint64_t sl = r8(buf + pos); pos += 8 + sl; }
79 else { int esz = (at <= 1) ? 1 : (at <= 3) ? 2 : 4; pos += esz; }
80 }
81 break;
82 }
83 case 10: case 11: case 12: pos += 8; break;
84 default: pos += 8; break;
85 }
86 }
87 uint64_t ti_start = pos;
88
89 // First pass: find total tensor info size
90 uint64_t ti_end = ti_start;
91 for (uint64_t t = 0; t < n_tensors; t++) {
92 if (ti_end + 8 > chunk) break;
93 uint64_t nl = r8(buf + ti_end); ti_end += 8;
94 ti_end += nl; // no alignment (GGUF v3)
95 if (ti_end + 4 > chunk) break;
96 uint32_t nd = r4(buf + ti_end); ti_end += 4;
97 ti_end += nd * 8;
98 ti_end += 12; // dtype(4) + offset(8)
99 }
100 uint64_t ti_total = ti_end - ti_start;
101 uint64_t data_start = (ti_start + ti_total + GGUF_ALIGN - 1) & ~(GGUF_ALIGN - 1);
102 printf("Data at offset %lu\n", data_start);
103
104 // Reopen file for reading tensor data
105 fd = open(path, O_RDONLY);
106 if (fd < 0) { free(buf); return 1; }
107
108 // Second pass: extract each tensor
109 uint64_t tp = ti_start;
110 uint64_t extracted = 0, errors = 0;
111 uint64_t total_bytes = 0;
112
113 for (uint64_t t = 0; t < n_tensors; t++) {
114 uint64_t nl = r8(buf + tp); tp += 8;
115 char name[1024];
116 if (nl > 1023) nl = 1023;
117 memcpy(name, buf + tp, nl);
118 name[nl] = '\0';
119 tp += nl;
120
121 uint32_t nd = r4(buf + tp); tp += 4;
122 uint64_t dims[4] = {1,1,1,1};
123 for (int d = 0; d < nd && d < 4; d++) { dims[d] = r8(buf + tp); tp += 8; }
124 uint32_t dtype = r4(buf + tp); tp += 4;
125 uint64_t toff = r8(buf + tp); tp += 8;
126
127 uint64_t ne = dims[0]*dims[1]*dims[2]*dims[3];
128 uint64_t dsz = 0;
129 switch (dtype) {
130 case 0: dsz = ne * 4; break; // F32
131 case 1: dsz = ne * 2; break; // F16
132 case 2: dsz = ((ne+31)/32)*18; break; // Q4_0
133 case 3: dsz = ((ne+31)/32)*20; break; // Q4_1
134 case 6: dsz = ((ne+31)/32)*34; break; // Q8_0
135 case 10: dsz = ((ne+255)/256)*144; break; // Q4_K
136 case 12: dsz = ((ne+255)/256)*240; break; // Q6_K
137 case 39: dsz = ((ne+31)/32)*34; break; // MXFP4
138 case 47: dsz = ((ne+31)/32)*34; break; // MXFP4
139 default: dsz = ne * 2; break;
140 }
141
142 // Build output filename: replace '.' and '/' with '_'
143 char fpath[2048];
144 int nw = snprintf(fpath, sizeof(fpath), "%s/", out_dir);
145 for (int i = 0; name[i]; i++) {
146 if (name[i] == '.' || name[i] == '/') fpath[nw++] = '_';
147 else fpath[nw++] = name[i];
148 }
149 memcpy(fpath + nw, ".bin", 5);
150
151 // Read tensor data from file
152 uint64_t file_pos = data_start + toff;
153 FILE *out = fopen(fpath, "wb");
154 if (!out) { errors++; continue; }
155
156 uint8_t tmp[131072]; // 128KB chunks
157 uint64_t remaining = dsz;
158 while (remaining > 0) {
159 uint64_t chunk = remaining > sizeof(tmp) ? sizeof(tmp) : remaining;
160 uint64_t got = (uint64_t)pread(fd, tmp, chunk, file_pos);
161 if (got != chunk) { fprintf(stderr, " short read %s\n", name); break; }
162 fwrite(tmp, 1, got, out);
163 file_pos += got; remaining -= got;
164 }
165 fclose(out);
166 extracted++; total_bytes += dsz;
167
168 char sz_str[32];
169 if (dsz > 1048576) snprintf(sz_str, sizeof(sz_str), "%.1fMB", dsz/1048576.0);
170 else if (dsz > 1024) snprintf(sz_str, sizeof(sz_str), "%.0fKB", dsz/1024.0);
171 else snprintf(sz_str, sizeof(sz_str), "%luB", dsz);
172 printf(" [%lu/%lu] %s (%s)\n", t+1, n_tensors, fpath + strlen(out_dir) + 1, sz_str);
173 }
174 close(fd); free(buf);
175
176 printf("\n[DONE] %lu tensors extracted to %s/ (%.1fMB total)\n",
177 extracted, out_dir, total_bytes/1048576.0);
178 return errors ? 1 : 0;
179}