CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / src / omega_gpu_fp8_pack.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_fp8_pack.c: host side of the FP8 e4m3 GEMV (OWN-30B Cut 6). See omega_gpu_fp8_api.h. |
| 3 | * |
| 4 | * The weight bytes keep the checkpoint's row-major [n][k] order on the device (a warp reads |
| 5 | * 32 * lane_bytes contiguous bytes of a row per step), so "packing" is: refuse the two e4m3 NaN |
| 6 | * codes, convert the bf16 scale grid to f32 times 2^OMEGA_FP8_SCALE_EXP, and stage x as f32 times |
| 7 | * 2^OMEGA_FP8_X_EXP. All three are exact (powers of two; refused on overflow). The conversions |
| 8 | * live in om/api/apish_pack.om; this file copies bytes. |
| 9 | */ |
| 10 | #include "omega_gpu_fp8_api.h" |
| 11 | #include "omega_blackwell_codegen_moe.h" |
| 12 | #include "omega_blackwell_matmul.h" |
| 13 | #include "omega_gpu_hostmath.h" |
| 14 | #include "omega_api_shells.h" |
| 15 | #include "omega_api_views_rest.h" |
| 16 | #include <math.h> |
| 17 | #include <string.h> |
| 18 | #include "omega_gpu_apiviews.h" |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | float omega_fp8_e4m3_ref(uint8_t b) { |
| 22 | uint32_t bits = omega_api_refb(b); |
| 23 | float f; |
| 24 | memcpy(&f, &bits, 4); |
| 25 | return f; |
| 26 | } |
| 27 | |
| 28 | uint32_t omega_fp8_e4m3_kernel_bits(uint8_t b) { return omega_api_kbits(b); } |
| 29 | |
| 30 | int omega_fp8_e4m3_is_nan(uint8_t b) { return omega_api_isnanb(b); } |
| 31 | |
| 32 | size_t omega_fp8_pack_scan_nan(const uint8_t *w, size_t count, size_t *first) { |
| 33 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 34 | size_t n = 0; |
| 35 | for (size_t i = 0; i < count; i++) |
| 36 | if (omega_fp8_e4m3_is_nan(w[i])) { if (n == 0 && first) *first = i; n++; } |
| 37 | return n; |
| 38 | #else |
| 39 | uint64_t r = 0, scan[2] = {0, 0}; |
| 40 | ar_check(omega_arnan(&r, w, count, scan, 2)); |
| 41 | if (scan[1] && first) *first = (size_t)scan[0]; |
| 42 | return (size_t)scan[1]; |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | int omega_fp8_pack_scales(const uint16_t *bf16, size_t count, float *out, int mutant_no_fold) { |
| 47 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 48 | for (size_t i = 0; i < count; i++) { |
| 49 | uint32_t bits = 0; |
| 50 | if (omega_api_scalef(bf16[i], mutant_no_fold, &bits) != 0) return -1; |
| 51 | memcpy(&out[i], &bits, 4); |
| 52 | } |
| 53 | return 0; |
| 54 | #else |
| 55 | uint64_t r = 0; |
| 56 | ar_check(omega_arscale(&r, (uint32_t)mutant_no_fold, bf16, count, out, count)); |
| 57 | return r ? -1 : 0; |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | void omega_fp8_pack_x_f32(const float *x, uint32_t k, float *out) { |
| 62 | if (av_fp8_pack_x(k, x, out) != 0) abort(); |
| 63 | } |
| 64 | |
| 65 | void omega_fp8_pack_x_bf16(const uint16_t *x, uint32_t k, float *out) { |
| 66 | if (av_fp8_pack_x_bf16(k, x, out) != 0) abort(); |
| 67 | } |
| 68 | |
| 69 | void omega_fp8_gemv_oracle(const uint8_t *w, const uint16_t *scale_bf16, uint32_t n, uint32_t k, |
| 70 | const float *x, double *y, double *yscale) { |
| 71 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 72 | const uint32_t kb = k / OMEGA_FP8_BLOCK; |
| 73 | for (uint32_t r = 0; r < n; r++) { |
| 74 | double acc = 0.0, mag = 0.0; |
| 75 | const uint8_t *row = w + (size_t)r * k; |
| 76 | const uint16_t *srow = scale_bf16 + (size_t)(r / OMEGA_FP8_BLOCK) * kb; |
| 77 | for (uint32_t b = 0; b < kb; b++) { |
| 78 | double s = (double)omega_bf16_to_fp32(srow[b]); |
| 79 | for (uint32_t c = b * OMEGA_FP8_BLOCK; c < (b + 1) * OMEGA_FP8_BLOCK; c++) { |
| 80 | double p = (double)omega_fp8_e4m3_ref(row[c]) * (double)x[c] * s; |
| 81 | acc += p; mag += fabs(p); |
| 82 | } |
| 83 | } |
| 84 | y[r] = acc; |
| 85 | if (yscale) yscale[r] = mag; |
| 86 | } |
| 87 | #else |
| 88 | /* The reference has three runtime input views (six native slots). Returning one f64 |
| 89 | * per call keeps that ABI; magnitude is a second pass only when requested. */ |
| 90 | const size_t kb = k / OMEGA_FP8_BLOCK; |
| 91 | for (uint32_t r = 0; r < n; r++) { |
| 92 | uint64_t bits = 0; |
| 93 | const uint8_t *row = w + (size_t)r * k; |
| 94 | const uint16_t *sc = scale_bf16 + (size_t)(r / OMEGA_FP8_BLOCK) * kb; |
| 95 | ar_check(omega_aracc(&bits, row, k, sc, kb, x, k)); |
| 96 | memcpy(&y[r], &bits, sizeof bits); |
| 97 | if (yscale) { |
| 98 | ar_check(omega_armag(&bits, row, k, sc, kb, x, k)); |
| 99 | memcpy(&yscale[r], &bits, sizeof bits); |
| 100 | } |
| 101 | } |
| 102 | #endif |
| 103 | } |