Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/gpu_lmhead_fp8_study.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / gpu_lmhead_fp8_study.c` 134 lines ยท 8338 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * gpu_lmhead_fp8_study: report-only study (OWN-30B, 2026-10-13). Does an FP8-quantised lm_head pick the same next token 19 * as the bf16 lm_head? Loads the real lm_head.weight (bf16 [151936][2048]) and model.norm.weight from the checkpoint 20 * dir, quantises the weight to e4m3 in 128x128 blocks (scale = amax / 448, rounded to bf16, the checkpoint's 21 * weight_scale_inv convention; round-to-nearest-even into e4m3, saturating), then runs both GEMVs on the chip 22 * (bf16 streaming kernel and the FP8 kernel) on N random hidden states and compares the argmax. Nothing is switched. 23 * usage: gpu_lmhead_fp8_study MODEL_DIR [--states N] [--seed S] [--out report.txt] 24 */ 25 #include "omega_gpu_bf16_gemv_api.h" 26 #include "omega_gpu_fp8_api.h" 27 #include "omega_gpu_session.h" 28 #include 29 #include 30 #include 31 #include 32 #include 33 #include 34 35 #define V 151936u 36 #define H 2048u 37 #define DATA0 89616ull /* 8 + header length of shard 4 */ 38 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 39 static float urand(uint32_t *s) { return (float)(lcg(s) >> 8) / `16777216`.0f; } 40 static float gauss(uint32_t *s) { float a = 0; for (int i = 0; i < 12; i++) a += urand(s); return a - 6.0f; } 41 static float bf16_f32(uint16_t b) { uint32_t x = (uint32_t)b << 16; float f; memcpy(&f, &x, 4); return f; } 42 static uint16_t bf16_rne(float f) { uint32_t x; memcpy(&x, &f, 4); uint32_t r = x + 0x7fffu + ((x >> 16) & 1u); return (uint16_t)(r >> 16); } 43 static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } 44 45 /* positive e4m3 values by code 0..126 (127 is NaN): increasing */ 46 static float pos[127]; 47 static uint8_t enc_e4m3(float v) { /* nearest, ties to the even code, saturating at 448 */ 48 uint8_t sign = v < 0 ? 0x80 : 0; float a = fabsf(v); 49 int lo = 0, hi = 126; 50 if (a >= pos[126]) return sign | 126; 51 while (hi - lo > 1) { int m = (lo + hi) / 2; if (pos[m] <= a) lo = m; else hi = m; } 52 float dl = a - pos[lo], dh = pos[hi] - a; 53 int c = dl < dh ? lo : dh < dl ? hi : ((lo & 1) ? hi : lo); 54 return sign | (uint8_t)c; 55 } 56 57 int main(int argc, char **argv) { 58 if (argc < 2) { printf("usage: %s MODEL_DIR [--states N] [--seed S] [--out file]\n", argv[0]); return 2; } 59 uint32_t states = 1000, seed = `20261013`; const char *out_path = NULL; 60 for (int i = 2; i < argc; i++) { 61 if (!strcmp(argv[i], "--states") && i + 1 < argc) states = (uint32_t)atoi(argv[++i]); 62 else if (!strcmp(argv[i], "--seed") && i + 1 < argc) seed = (uint32_t)atoi(argv[++i]); 63 else if (!strcmp(argv[i], "--out") && i + 1 < argc) out_path = argv[++i]; 64 } 65 for (int c = 0; c < 127; c++) { int e = c >> 3, m = c & 7; pos[c] = e == 0 ? (float)m * (1.0f / 512.0f) : ldexpf(1.0f + m / 8.0f, e - 7); } 66 char path[600]; snprintf(path, sizeof path, "%s/model-00004-of-00004.safetensors", argv[1]); 67 FILE *f = fopen(path, "rb"); if (!f) { printf("cannot open %s\n", path); return 1; } 68 uint16_t *w = malloc((size_t)V * H * 2), nw[H]; 69 fseeko(f, (off_t)DATA0, SEEK_SET); 70 if (fread(w, 2, (size_t)V * H, f) != (size_t)V * H) { printf("short read lm_head\n"); return 1; } 71 fseeko(f, (off_t)(DATA0 + 622405248ull), SEEK_SET); 72 if (fread(nw, 2, H, f) != H) { printf("short read norm\n"); return 1; } 73 fclose(f); 74 /* quantise */ 75 uint8_t *q = malloc((size_t)V * H); uint16_t *sc = malloc((size_t)(V / 128) * (H / 128) * 2); 76 double sq_err = 0, sq_w = 0; 77 for (uint32_t bi = 0; bi < V / 128; bi++) for (uint32_t bj = 0; bj < H / 128; bj++) { 78 float amax = 0; 79 for (uint32_t r = 0; r < 128; r++) for (uint32_t c = 0; c < 128; c++) { float a = fabsf(bf16_f32(w[(size_t)(bi * 128 + r) * H + bj * 128 + c])); if (a > amax) amax = a; } 80 uint16_t sb = bf16_rne(amax > 0 ? amax / 448.0f : 1.0f); float s = bf16_f32(sb); 81 sc[bi * (H / 128) + bj] = sb; 82 for (uint32_t r = 0; r < 128; r++) for (uint32_t c = 0; c < 128; c++) { 83 size_t idx = (size_t)(bi * 128 + r) * H + bj * 128 + c; float wv = bf16_f32(w[idx]); 84 uint8_t e = enc_e4m3(wv / s); q[idx] = e; 85 float d = (e & 0x80 ? -1.0f : 1.0f) * pos[e & 0x7f] * s; sq_err += (double)(d - wv) * (d - wv); sq_w += (double)wv * wv; 86 } 87 } 88 printf("quantised lm_head: weight rms error %.4f of weight rms (block-scaled e4m3)\n", sqrt(sq_err / sq_w)); 89 OmegaGpuBf16Tensor *tb = NULL; OmegaGpuFp8Tensor *tf = NULL; 90 int r1 = omega_gpu_bf16_tensor_upload(V, H, w, &tb), r2 = omega_gpu_fp8_tensor_upload(1, V, H, q, sc, &tf); 91 printf("uploads: bf16 %s, fp8 %s\n", omega_gpu_bf16_rc_name(r1), omega_gpu_fp8_rc_name(r2)); 92 if (r1 || r2) return 1; 93 (void)omega_gpu_session_set_spin_us(2000); 94 float *x = malloc(H * 4), *yb = malloc(V * 4), *yf = malloc(V * 4); 95 uint32_t s = seed, agree = 0, top5_in = 0, fails = 0; 96 uint64_t *tb_ns = malloc(states * 8), *tf_ns = malloc(states * 8); 97 double maxrel = 0; 98 uint32_t bucket[4] = { 0, 0, 0, 0 }, bucket_dis[4] = { 0, 0, 0, 0 }; /* margin <0.05, <0.2, <1, >=1 (bf16 logit gap top1-top2) */ 99 for (uint32_t it = 0; it < states; it++) { 100 for (uint32_t i = 0; i < H; i++) x[i] = gauss(&s) * bf16_f32(nw[i]); 101 OmegaGpuBf16Info ib; OmegaGpuFp8Info jf; 102 if (omega_gpu_bf16_gemv_f32(tb, x, yb, &ib) || omega_gpu_fp8_gemv_f32(tf, 0, x, yf, &jf)) { fails++; printf("state %u: chip call failed (%s)\n", it, omega_gpu_bf16_last_error()); break; } 103 tb_ns[it] = ib.elapsed_ns; tf_ns[it] = jf.elapsed_ns; 104 uint32_t ab = 0, af = 0; float m2 = -1e30f; 105 for (uint32_t v = 1; v < V; v++) { if (yb[v] > yb[ab]) ab = v; if (yf[v] > yf[af]) af = v; } 106 for (uint32_t v = 0; v < V; v++) if (v != ab && yb[v] > m2) m2 = yb[v]; 107 double margin = yb[ab] - m2; 108 int ok = ab == af; agree += ok; 109 /* is the bf16 argmax inside the fp8 top 5? */ 110 uint32_t better = 0; for (uint32_t v = 0; v < V; v++) if (yf[v] > yf[ab]) better++; 111 top5_in += better < 5; 112 int b = margin < 0.05 ? 0 : margin < 0.2 ? 1 : margin < 1.0 ? 2 : 3; bucket[b]++; if (!ok) { bucket_dis[b]++; } 113 double num = 0, den = 0; for (uint32_t v = 0; v < V; v++) { double d = (double)yf[v] - yb[v]; num += d * d; den += (double)yb[v] * yb[v]; } 114 double rel = sqrt(num / den); if (rel > maxrel) maxrel = rel; 115 if (it == 0) printf("state 0: bf16 argmax %u (%.3f), fp8 argmax %u (%.3f), margin %.4f, logit rms rel diff %.4f\n", ab, yb[ab], af, yf[af], margin, rel); 116 } 117 uint32_t n = states - fails; 118 qsort(tb_ns, n, 8, cmp_u64); qsort(tf_ns, n, 8, cmp_u64); 119 char rep[2000]; 120 snprintf(rep, sizeof rep, 121 "lm_head fp8 vs bf16 argmax study (real lm_head.weight, %u random hidden states = gaussian x model.norm.weight, seed %u)\n" 122 "argmax agreement: %u of %u (%.2f%%); bf16 argmax within fp8 top-5: %u of %u\n" 123 "logit rms relative difference, worst over states: %.4f\n" 124 "agreement by bf16 top1-top2 logit gap: <0.05: %u/%u disagree, <0.2: %u/%u, <1: %u/%u, >=1: %u/%u\n" 125 "chip medians: bf16 kernel %.1f us, fp8 kernel %.1f us\n", 126 n, seed, agree, n, 100.0 * agree / (n ? n : 1), top5_in, n, maxrel, 127 bucket_dis[0], bucket[0], bucket_dis[1], bucket[1], bucket_dis[2], bucket[2], bucket_dis[3], bucket[3], 128 n ? tb_ns[n / 2] / 1e3 : 0.0, n ? tf_ns[n / 2] / 1e3 : 0.0); 129 printf("%s", rep); 130 if (out_path) { FILE *o = fopen(out_path, "w"); if (o) { fputs(rep, o); fclose(o); } } 131 omega_gpu_bf16_tensor_free(tb); omega_gpu_fp8_tensor_free(tf); 132 omega_gpu_session_close(); 133 return fails ? 1 : 0; 134 }