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 / evidence / FLASH-NEXT-WEIGHT-WASTE-20261118 / rowrans.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | * Measurement prototype: bit-exact row-addressable rANS coding of FP8 (E4M3) weight bytes. |
| 3 | * One static 14-bit model per tensor, one independent stream per matrix row (so rows decode in parallel). |
| 4 | * usage: rowrans <raw> <rows> <cols> [flip] prints: raw_bytes coded_bytes roundtrip(0/1) |
| 5 | * coded_bytes counts everything a decoder needs: 256 u16 frequencies + u32 offset per row + streams. */ |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <stdint.h> |
| 10 | #define SB 14 |
| 11 | #define RL (1u << 23) |
| 12 | int main(int argc, char **argv) { |
| 13 | if (argc < 4) return 2; |
| 14 | size_t R = strtoul(argv[2], 0, 10), C = strtoul(argv[3], 0, 10), n = R * C; |
| 15 | int flip = argc > 4; |
| 16 | uint8_t *in = malloc(n), *out = malloc(n), *buf = malloc(n * 2 + 64); |
| 17 | FILE *f = fopen(argv[1], "rb"); |
| 18 | if (!f || fread(in, 1, n, f) != n) return 2; |
| 19 | fclose(f); |
| 20 | uint64_t cnt[256] = {0}; |
| 21 | for (size_t i = 0; i < n; i++) cnt[in[i]]++; |
| 22 | uint32_t fr[256], cum[257], tot = 0; |
| 23 | for (int s = 0; s < 256; s++) { fr[s] = (uint32_t)((cnt[s] << SB) / n); if (cnt[s] && !fr[s]) fr[s] = 1; tot += fr[s]; } |
| 24 | while (tot != (1u << SB)) { /* fix the rounding on the largest symbols */ |
| 25 | int b = 0; for (int s = 1; s < 256; s++) if (fr[s] > fr[b]) b = s; |
| 26 | if (tot > (1u << SB)) { fr[b]--; tot--; } else { fr[b]++; tot++; } |
| 27 | } |
| 28 | cum[0] = 0; for (int s = 0; s < 256; s++) cum[s + 1] = cum[s] + fr[s]; |
| 29 | static uint8_t slot[1 << SB]; |
| 30 | for (int s = 0; s < 256; s++) for (uint32_t k = cum[s]; k < cum[s + 1]; k++) slot[k] = (uint8_t)s; |
| 31 | size_t total = 512 + 4 * R; |
| 32 | uint32_t *off = malloc(4 * (R + 1)); uint8_t *all = malloc(n * 2 + 8 * R); size_t w = 0; |
| 33 | for (size_t r = 0; r < R; r++) { |
| 34 | uint8_t *p = buf + 2 * C + 32, *e = p; uint32_t x = RL; |
| 35 | for (size_t i = C; i-- > 0;) { |
| 36 | uint32_t s = in[r * C + i], xm = ((RL >> SB) << 8) * fr[s]; |
| 37 | while (x >= xm) { *--p = (uint8_t)x; x >>= 8; } |
| 38 | x = ((x / fr[s]) << SB) + (x % fr[s]) + cum[s]; |
| 39 | } |
| 40 | p -= 4; memcpy(p, &x, 4); |
| 41 | off[r] = (uint32_t)w; memcpy(all + w, p, (size_t)(e - p)); w += (size_t)(e - p); |
| 42 | } |
| 43 | off[R] = (uint32_t)w; total += w; |
| 44 | if (flip) all[w / 2] ^= 0x10; /* negative control: one flipped bit must break the round trip */ |
| 45 | for (size_t r = 0; r < R; r++) { |
| 46 | const uint8_t *p = all + off[r], *end = all + off[r + 1]; uint32_t x; memcpy(&x, p, 4); p += 4; |
| 47 | for (size_t i = 0; i < C; i++) { |
| 48 | uint32_t k = x & ((1u << SB) - 1), s = slot[k]; |
| 49 | out[r * C + i] = (uint8_t)s; |
| 50 | x = fr[s] * (x >> SB) + k - cum[s]; |
| 51 | while (x < RL && p < end) x = (x << 8) | *p++; |
| 52 | } |
| 53 | } |
| 54 | printf("%zu %zu %d\n", n, total, memcmp(in, out, n) == 0); |
| 55 | return 0; |
| 56 | } |