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 / src / omega_gpu_fp8_pack.c

91 lines · 3848 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/* omega_gpu_fp8_pack.c: host side of the FP8 e4m3 GEMV (OWN-30B Cut 6). See omega_gpu_fp8_api.h.
18 *
19 * The weight bytes keep the checkpoint's row-major [n][k] order on the device (a warp reads
20 * 32 * lane_bytes contiguous bytes of a row per step), so "packing" is: refuse the two e4m3 NaN
21 * codes, convert the bf16 scale grid to f32 times 2^OMEGA_FP8_SCALE_EXP, and stage x as f32 times
22 * 2^OMEGA_FP8_X_EXP. All three are exact (powers of two; refused on overflow). */
23#include "omega_gpu_fp8_api.h"
24#include "omega_blackwell_codegen_moe.h"
25#include "omega_blackwell_matmul.h"
26#include "omega_gpu_hostmath.h"
27#include <math.h>
28#include <string.h>
29
30float omega_fp8_e4m3_ref(uint8_t b) {
31 int exponent = (b >> 3) & 0x0f;
32 float mantissa = (float)(b & 0x07);
33 float sign = (b & 0x80) ? -1.0f : 1.0f;
34 if (exponent == 0x0f && (b & 0x07) == 0x07) return NAN; /* 0x7f / 0xff */
35 if (exponent == 0) return sign * mantissa * omega_hostmath_pow2f(-9);
36 return sign * (1.0f + mantissa / 8.0f) * omega_hostmath_pow2f(exponent - 7);
37}
38
39uint32_t omega_fp8_e4m3_kernel_bits(uint8_t b) {
40 return ((uint32_t)(b & 0x7fu) << 20) | ((uint32_t)(b & 0x80u) << 24);
41}
42
43int omega_fp8_e4m3_is_nan(uint8_t b) { return (b & 0x7fu) == 0x7fu; }
44
45size_t omega_fp8_pack_scan_nan(const uint8_t *w, size_t count, size_t *first) {
46 size_t n = 0;
47 for (size_t i = 0; i < count; i++)
48 if (omega_fp8_e4m3_is_nan(w[i])) { if (n == 0 && first) *first = i; n++; }
49 return n;
50}
51
52int omega_fp8_pack_scales(const uint16_t *bf16, size_t count, float *out, int mutant_no_fold) {
53 const float fold = mutant_no_fold ? 1.0f : omega_hostmath_pow2f(OMEGA_FP8_SCALE_EXP);
54 for (size_t i = 0; i < count; i++) {
55 float s = omega_bf16_to_fp32(bf16[i]);
56 if (!(fabsf(s) <= 3.0e38f)) return -1; /* NaN or inf scale */
57 float f = s * fold;
58 if (!(fabsf(f) <= 3.0e38f)) return -1; /* fold overflow */
59 out[i] = f;
60 }
61 return 0;
62}
63
64void omega_fp8_pack_x_f32(const float *x, uint32_t k, float *out) {
65 const float fold = omega_hostmath_pow2f(OMEGA_FP8_X_EXP);
66 for (uint32_t i = 0; i < k; i++) out[i] = x[i] * fold;
67}
68
69void omega_fp8_pack_x_bf16(const uint16_t *x, uint32_t k, float *out) {
70 const float fold = omega_hostmath_pow2f(OMEGA_FP8_X_EXP);
71 for (uint32_t i = 0; i < k; i++) out[i] = omega_bf16_to_fp32(x[i]) * fold;
72}
73
74void omega_fp8_gemv_oracle(const uint8_t *w, const uint16_t *scale_bf16, uint32_t n, uint32_t k,
75 const float *x, double *y, double *yscale) {
76 const uint32_t kb = k / OMEGA_FP8_BLOCK;
77 for (uint32_t r = 0; r < n; r++) {
78 double acc = 0.0, mag = 0.0;
79 const uint8_t *row = w + (size_t)r * k;
80 const uint16_t *srow = scale_bf16 + (size_t)(r / OMEGA_FP8_BLOCK) * kb;
81 for (uint32_t b = 0; b < kb; b++) {
82 double s = (double)omega_bf16_to_fp32(srow[b]);
83 for (uint32_t c = b * OMEGA_FP8_BLOCK; c < (b + 1) * OMEGA_FP8_BLOCK; c++) {
84 double p = (double)omega_fp8_e4m3_ref(row[c]) * (double)x[c] * s;
85 acc += p; mag += fabs(p);
86 }
87 }
88 y[r] = acc;
89 if (yscale) yscale[r] = mag;
90 }
91}