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-CONTROL-FIX-20261118 / rowrans_strict.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | * Measurement instrument: the encoder of ../FLASH-NEXT-WEIGHT-WASTE-20261118/rowrans.c (same model, same streams, |
| 3 | * same coded byte count) with a decoder that reports WHY a corrupted stream is or is not caught. |
| 4 | * |
| 5 | * The historical tool flips bit 4 of stream byte w/2 and calls the flip "detected" only when the decoded bytes |
| 6 | * differ. It never checks the rANS end conditions, so a flip whose effect stays in the decoder state (and never |
| 7 | * reaches a decoded symbol before the row ends) is reported as a miss. This tool decodes every row and records: |
| 8 | * bytes_equal decoded bytes identical to the input (the historical verdict) |
| 9 | * init_bad rows whose first 4 bytes are outside the valid state interval [RL, 256*RL) |
| 10 | * starved rows whose decoder wanted a renormalisation byte after the row's last byte |
| 11 | * leftover rows that ended with unread bytes |
| 12 | * term_bad rows whose final state is not RL, the encoder's fixed start state |
| 13 | * strict_ok = bytes_equal && all four counts are zero. equiv = bytes_equal && strict_ok on a stream that differs |
| 14 | * from the encoder's own stream: a second valid encoding of the same bytes (expected 0, see README). |
| 15 | * |
| 16 | * usage: rowrans_strict RAW ROWS COLS mid clean decode + the historical flip (byte w/2, mask 0x10) |
| 17 | * rowrans_strict RAW ROWS COLS all clean decode + every single-bit flip of every stream byte |
| 18 | * Output: key=value lines; exit 0 when the run completed (the verdicts are in the output), 2 on usage/IO error. */ |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <stdint.h> |
| 23 | #define SB 14 |
| 24 | #define RL (1u << 23) |
| 25 | |
| 26 | static uint32_t fr[256], cum[257]; |
| 27 | static uint8_t slot[1 << SB]; |
| 28 | |
| 29 | typedef struct { size_t changed; int init_bad, starved, leftover, term_bad; uint32_t term; } rowres; |
| 30 | |
| 31 | /* Decode one row with the historical arithmetic; out may be NULL with ref given (count changes only). */ |
| 32 | static rowres decode_row(const uint8_t *p, const uint8_t *end, size_t C, const uint8_t *ref, uint8_t *out) { |
| 33 | rowres q = {0, 0, 0, 0, 0, 0}; |
| 34 | uint32_t x = 0; |
| 35 | if (end - p < 4) { q.init_bad = 1; q.changed = C; return q; } |
| 36 | memcpy(&x, p, 4); p += 4; |
| 37 | q.init_bad = !(x >= RL && (uint64_t)x < ((uint64_t)RL << 8)); |
| 38 | for (size_t i = 0; i < C; i++) { |
| 39 | uint32_t k = x & ((1u << SB) - 1), s = slot[k]; |
| 40 | if (out) out[i] = (uint8_t)s; |
| 41 | if (s != ref[i]) q.changed++; |
| 42 | x = fr[s] * (x >> SB) + k - cum[s]; |
| 43 | while (x < RL && p < end) x = (x << 8) | *p++; |
| 44 | if (x < RL) q.starved = 1; /* wanted a byte past the end of the row */ |
| 45 | } |
| 46 | q.leftover = p != end; |
| 47 | q.term_bad = x != RL; |
| 48 | q.term = x; |
| 49 | return q; |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char **argv) { |
| 53 | if (argc != 5 || (strcmp(argv[4], "mid") && strcmp(argv[4], "all"))) return 2; |
| 54 | size_t R = strtoul(argv[2], 0, 10), C = strtoul(argv[3], 0, 10), n = R * C; |
| 55 | int all_mode = !strcmp(argv[4], "all"); |
| 56 | uint8_t *in = malloc(n), *out = malloc(n), *buf = malloc(n * 2 + 64); |
| 57 | FILE *f = fopen(argv[1], "rb"); |
| 58 | if (!in || !out || !buf || !f || fread(in, 1, n, f) != n || fgetc(f) != EOF) return 2; |
| 59 | fclose(f); |
| 60 | /* ---- encoder: identical to FLASH-NEXT-WEIGHT-WASTE-20261118/rowrans.c ---- */ |
| 61 | uint64_t cnt[256] = {0}; |
| 62 | for (size_t i = 0; i < n; i++) cnt[in[i]]++; |
| 63 | uint32_t tot = 0; |
| 64 | 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]; } |
| 65 | while (tot != (1u << SB)) { |
| 66 | int b = 0; for (int s = 1; s < 256; s++) if (fr[s] > fr[b]) b = s; |
| 67 | if (tot > (1u << SB)) { fr[b]--; tot--; } else { fr[b]++; tot++; } |
| 68 | } |
| 69 | cum[0] = 0; for (int s = 0; s < 256; s++) cum[s + 1] = cum[s] + fr[s]; |
| 70 | for (int s = 0; s < 256; s++) for (uint32_t k = cum[s]; k < cum[s + 1]; k++) slot[k] = (uint8_t)s; |
| 71 | size_t total = 512 + 4 * R; |
| 72 | uint32_t *off = malloc(4 * (R + 1)); uint8_t *all = malloc(n * 2 + 8 * R); size_t w = 0; |
| 73 | if (!off || !all) return 2; |
| 74 | for (size_t r = 0; r < R; r++) { |
| 75 | uint8_t *p = buf + 2 * C + 32, *e = p; uint32_t x = RL; |
| 76 | for (size_t i = C; i-- > 0;) { |
| 77 | uint32_t s = in[r * C + i], xm = ((RL >> SB) << 8) * fr[s]; |
| 78 | while (x >= xm) { *--p = (uint8_t)x; x >>= 8; } |
| 79 | x = ((x / fr[s]) << SB) + (x % fr[s]) + cum[s]; |
| 80 | } |
| 81 | p -= 4; memcpy(p, &x, 4); |
| 82 | off[r] = (uint32_t)w; memcpy(all + w, p, (size_t)(e - p)); w += (size_t)(e - p); |
| 83 | } |
| 84 | off[R] = (uint32_t)w; total += w; |
| 85 | printf("raw=%zu coded=%zu stream_bytes=%zu rows=%zu cols=%zu\n", n, total, w, R, C); |
| 86 | /* ---- clean control: the unflipped stream must pass every check ---- */ |
| 87 | size_t c_changed = 0; int c_bad = 0; |
| 88 | for (size_t r = 0; r < R; r++) { |
| 89 | rowres q = decode_row(all + off[r], all + off[r + 1], C, in + r * C, out + r * C); |
| 90 | c_changed += q.changed; c_bad += q.init_bad + q.starved + q.leftover + q.term_bad; |
| 91 | } |
| 92 | int clean_ok = c_changed == 0 && c_bad == 0 && memcmp(in, out, n) == 0; |
| 93 | printf("clean bytes_equal=%d strict_ok=%d\n", memcmp(in, out, n) == 0, clean_ok); |
| 94 | if (!all_mode) { |
| 95 | /* ---- the historical flip, decoded in full ---- */ |
| 96 | size_t pos = w / 2, r = 0; |
| 97 | while (off[r + 1] <= pos) r++; |
| 98 | all[pos] ^= 0x10; |
| 99 | size_t changed = 0, rows_changed = 0; int ib = 0, st = 0, lo = 0, tb = 0; uint32_t term = RL; |
| 100 | for (size_t rr = 0; rr < R; rr++) { |
| 101 | rowres q = decode_row(all + off[rr], all + off[rr + 1], C, in + rr * C, out + rr * C); |
| 102 | changed += q.changed; rows_changed += q.changed != 0; |
| 103 | ib += q.init_bad; st += q.starved; lo += q.leftover; tb += q.term_bad; |
| 104 | if (rr == r) term = q.term; |
| 105 | } |
| 106 | all[pos] ^= 0x10; |
| 107 | int beq = memcmp(in, out, n) == 0, sok = beq && !ib && !st && !lo && !tb; |
| 108 | const char *cls = !beq ? "decoded_bytes_changed" |
| 109 | : (ib || st || lo || tb) ? "decoded_equal_end_state_invalid" : "equivalent_encoding"; |
| 110 | printf("flip pos=%zu mask=0x10 row=%zu byte_in_row=%zu row_len=%u from_row_end=%zu\n", pos, r, |
| 111 | pos - off[r], off[r + 1] - off[r], (size_t)off[r + 1] - pos); |
| 112 | printf("flip bytes_equal=%d changed_bytes=%zu rows_changed=%zu init_bad=%d starved=%d leftover=%d term_bad=%d " |
| 113 | "term_state=%u hist_detected=%d strict_detected=%d class=%s\n", |
| 114 | beq, changed, rows_changed, ib, st, lo, tb, term, !beq, !sok, cls); |
| 115 | return 0; |
| 116 | } |
| 117 | /* ---- every single-bit flip of every stream byte; rows are independent, so only the hit row is decoded ---- */ |
| 118 | size_t flips = 0, hist_miss = 0, strict_miss = 0, equiv = 0, k_init = 0, k_starved = 0, k_left = 0, k_term = 0; |
| 119 | size_t miss_hist_from_end[9] = {0}; /* historical misses by distance from row end: 1..8, >8 */ |
| 120 | for (size_t r = 0; r < R; r++) { |
| 121 | const uint8_t *in_r = in + r * C; |
| 122 | for (size_t pos = off[r]; pos < off[r + 1]; pos++) { |
| 123 | for (int b = 0; b < 8; b++) { |
| 124 | all[pos] ^= (uint8_t)(1u << b); |
| 125 | rowres q = decode_row(all + off[r], all + off[r + 1], C, in_r, NULL); |
| 126 | all[pos] ^= (uint8_t)(1u << b); |
| 127 | flips++; |
| 128 | if (q.changed) continue; |
| 129 | hist_miss++; |
| 130 | size_t d = off[r + 1] - pos; miss_hist_from_end[d > 8 ? 8 : d - 1]++; |
| 131 | k_init += q.init_bad; k_starved += q.starved; k_left += q.leftover; k_term += q.term_bad; |
| 132 | if (!(q.init_bad || q.starved || q.leftover || q.term_bad)) { strict_miss++; equiv++; } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | printf("all flips=%zu hist_missed=%zu strict_missed=%zu equivalent_encoding=%zu\n", flips, hist_miss, strict_miss, equiv); |
| 137 | printf("all hist_missed_caught_by init_bad=%zu starved=%zu leftover=%zu term_bad=%zu\n", k_init, k_starved, k_left, k_term); |
| 138 | printf("all hist_missed_by_bytes_from_row_end"); |
| 139 | for (int d = 0; d < 8; d++) printf(" %d:%zu", d + 1, miss_hist_from_end[d]); |
| 140 | printf(" >8:%zu\n", miss_hist_from_end[8]); |
| 141 | return 0; |
| 142 | } |