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 / tests / coder / real.sh
| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # ERC1 on real routed-expert matrices of the Qwen3.8-Flash-Next FP8 checkpoint (host only, no GPU). |
| 4 | # bash tests/coder/real.sh OMC OUTDIR [CHECKPOINT_DIR] |
| 5 | # Reads only shard files that have no .aria2 file beside them. Writes OUTDIR/matrices.tsv (one row per |
| 6 | # matrix: tensor, shard, offsets, SHA-256 of the bytes read, sizes), OUTDIR/summary.txt and the log |
| 7 | # on stdout. Needs gcc for the measurement prototype rowrans.c (a read-only reference, never part of ERC1). |
| 8 | # Ends "ERC1 REAL PASS n checks" and exit 0, or "ERC1 REAL FAIL" and exit 1. |
| 9 | export LC_ALL=C |
| 10 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 11 | ROOT=$(cd "$HERE/../.." && pwd) |
| 12 | . "$HERE/lib.sh" |
| 13 | OMC=${1:?usage: real.sh OMC OUTDIR [CHECKPOINT_DIR]} |
| 14 | OUT=${2:?usage: real.sh OMC OUTDIR [CHECKPOINT_DIR]} |
| 15 | CK=${3:-$HOME/models/qwen3.8-flash-next-fp8-7fb48d70} |
| 16 | RSRC=${ROWRANS_SRC:-$HOME/work/lanes/flash-next/cudapp/evidence/FLASH-NEXT-WEIGHT-WASTE-20261118/rowrans.c} |
| 17 | NLAYERS=${NLAYERS:-6} |
| 18 | EXPERTS=${EXPERTS:-"7 311"} |
| 19 | mkdir -p "$OUT" |
| 20 | W=$OUT/work |
| 21 | mkdir -p "$W" |
| 22 | ERC=$W/erc |
| 23 | build "$OMC" "$ROOT/om/coder/erc.om" "$ERC" || { echo "ERC1 REAL FAIL: compile"; exit 1; } |
| 24 | gcc -O2 -o "$W/rowrans" "$RSRC" || { echo "ERC1 REAL FAIL: reference build"; exit 1; } |
| 25 | echo "erc sha256 $(sha256sum "$ERC" | cut -d' ' -f1)" |
| 26 | echo "rowrans.c sha256 $(sha256sum "$RSRC" | cut -d' ' -f1)" |
| 27 | |
| 28 | # ---------------------------------------------------------------- inventory of complete shards |
| 29 | INV=$OUT/inventory.tsv |
| 30 | : > "$INV" |
| 31 | shards_seen=0 |
| 32 | for f in "$CK"/model-*.safetensors; do |
| 33 | [ -e "$f.aria2" ] && continue |
| 34 | shards_seen=$((shards_seen + 1)) |
| 35 | n=$(od -An -t u8 -N8 "$f" | tr -d ' ') |
| 36 | head -c $((8 + n)) "$f" | tail -c "$n" | jq -r --arg f "$(basename "$f")" --argjson base $((8 + n)) ' |
| 37 | to_entries[] | select(.key != "__metadata__") |
| 38 | | select(.key | test("^model\\.language_model\\.layers\\.[0-9]+\\.mlp\\.experts\\.[0-9]+\\.(gate|up|down)_proj\\.weight$")) |
| 39 | | select(.value.dtype == "F8_E4M3") |
| 40 | | .key as $k | ($k | capture("layers\\.(?<l>[0-9]+)\\.mlp\\.experts\\.(?<e>[0-9]+)\\.(?<t>[a-z]+)_proj") ) as $c |
| 41 | | [$f, $k, $c.l, $c.e, $c.t, (.value.shape | map(tostring) | join("x")), ($base + .value.data_offsets[0]), (.value.data_offsets[1] - .value.data_offsets[0])] | @tsv' >> "$INV" |
| 42 | done |
| 43 | echo "complete shards read: $shards_seen; expert weight tensors listed: $(wc -l < "$INV")" |
| 44 | |
| 45 | inv_line() { grep -P "^[^\t]*\t[^\t]*\t$1\t$2\t$3\t" "$INV"; } |
| 46 | # layers where every kind exists for every chosen expert |
| 47 | layers=$(cut -f3 "$INV" | sort -n -u) |
| 48 | good="" |
| 49 | for l in $layers; do |
| 50 | all=1 |
| 51 | for e in $EXPERTS; do for t in gate up down; do |
| 52 | inv_line "$l" "$e" "$t" > /dev/null || all=0 |
| 53 | done; done |
| 54 | [ "$all" = 1 ] && good="$good $l" |
| 55 | done |
| 56 | set -- $good |
| 57 | m=$# |
| 58 | echo "layers with all three kinds for experts {$EXPERTS}: $good" |
| 59 | [ "$m" -ge 4 ] || { echo "ERC1 REAL FAIL: fewer than 4 usable layers"; exit 1; } |
| 60 | chosen="" |
| 61 | i=0 |
| 62 | while [ "$i" -lt "$NLAYERS" ]; do |
| 63 | idx=$((i * (m - 1) / (NLAYERS - 1) + 1)) |
| 64 | chosen="$chosen ${!idx}" |
| 65 | i=$((i + 1)) |
| 66 | done |
| 67 | chosen=$(echo $chosen | tr ' ' '\n' | sort -n -u | tr '\n' ' ') |
| 68 | echo "chosen layers: $chosen" |
| 69 | |
| 70 | printf 'tensor\tshard\trows\tcols\tfile_offset\tbytes\tsha256\terc_coded\tc_prototype_coded\terc_minus_c\tpacked_ratio\n' > "$OUT/matrices.tsv" |
| 71 | tot_raw=0; tot_erc=0; tot_c=0; nmat=0 |
| 72 | kinds_seen=""; layers_seen="" |
| 73 | first_gate=""; first_down="" |
| 74 | for l in $chosen; do for e in $EXPERTS; do for t in gate up down; do |
| 75 | line=$(inv_line "$l" "$e" "$t") |
| 76 | shard=$(echo "$line" | cut -f1); name=$(echo "$line" | cut -f2); shape=$(echo "$line" | cut -f6) |
| 77 | off=$(echo "$line" | cut -f7); len=$(echo "$line" | cut -f8) |
| 78 | r=${shape%x*}; c=${shape#*x} |
| 79 | [ $((r * c)) = "$len" ] || { bad "$name: shape $shape does not match $len bytes"; continue; } |
| 80 | raw=$W/m.raw |
| 81 | dd if="$CK/$shard" of="$raw" bs=1M iflag=skip_bytes,count_bytes skip="$off" count="$len" 2> /dev/null |
| 82 | sha=$(sha256sum "$raw" | cut -d' ' -f1) |
| 83 | "$ERC" encode "$raw" 0 "$r" "$c" "$W/m.erc" > "$W/m.enc.out" || { bad "$name: encode"; continue; } |
| 84 | "$ERC" encode "$CK/$shard" "$off" "$r" "$c" "$W/m2.erc" > /dev/null || { bad "$name: encode from the shard offset"; continue; } |
| 85 | cmp -s "$W/m.erc" "$W/m2.erc" || bad "$name: encode from the shard offset differs from encode of the extracted bytes" |
| 86 | "$ERC" decode "$W/m.erc" "$W/m.dec" > /dev/null || { bad "$name: decode"; continue; } |
| 87 | if cmp -s "$raw" "$W/m.dec"; then ok "$name: ${r}x${c} decode is byte-identical (sha256 $sha)"; else bad "$name: decode differs"; fi |
| 88 | ez=$(stat -c %s "$W/m.erc") |
| 89 | cz=$("$W/rowrans" "$raw" "$r" "$c" | cut -d' ' -f2) |
| 90 | check "$name: ERC1 bytes = prototype bytes + 40 + 2 * rows (the streams and table are the same size; $ez vs $cz)" [ "$((ez - cz))" = "$((40 + 2 * r))" ] |
| 91 | # a spread of rows alone, against the raw bytes |
| 92 | okrows=1 |
| 93 | for row in 0 $((r - 1)) $((r / 3)) $((r * 2 / 3)) $((r / 2 + 1)); do |
| 94 | "$ERC" decode-row "$W/m.erc" "$row" "$W/row.out" && dd if="$raw" of="$W/row.ref" bs="$c" skip="$row" count=1 2> /dev/null && cmp -s "$W/row.out" "$W/row.ref" || okrows=0 |
| 95 | done |
| 96 | [ "$okrows" = 1 ] || bad "$name: a row decoded alone differs" |
| 97 | ratio=$(aien-calc eval --no-store "$ez/$len" | sed -n 's/^decimal *\([0-9.]*\).*/\1/p') |
| 98 | printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$name" "$shard" "$r" "$c" "$off" "$len" "$sha" "$ez" "$cz" "$((ez - cz))" "$ratio" >> "$OUT/matrices.tsv" |
| 99 | tot_raw=$((tot_raw + len)); tot_erc=$((tot_erc + ez)); tot_c=$((tot_c + cz)); nmat=$((nmat + 1)) |
| 100 | kinds_seen="$kinds_seen $t"; layers_seen="$layers_seen $l" |
| 101 | [ "$t" = gate ] && [ -z "$first_gate" ] && first_gate="$off $shard $r $c $name" |
| 102 | [ "$t" = down ] && [ -z "$first_down" ] && first_down="$off $shard $r $c $name" |
| 103 | done; done; done |
| 104 | check "at least 30 matrices ($nmat)" [ "$nmat" -ge 30 ] |
| 105 | check "at least 4 layers ($(echo $layers_seen | tr ' ' '\n' | sort -u | wc -l))" [ "$(echo $layers_seen | tr ' ' '\n' | sort -u | wc -l)" -ge 4 ] |
| 106 | check "all three kinds ($(echo $kinds_seen | tr ' ' '\n' | sort -u | tr '\n' ' '))" [ "$(echo $kinds_seen | tr ' ' '\n' | sort -u | wc -l)" = 3 ] |
| 107 | |
| 108 | # ---------------------------------------------------------------- rows are independent, on real matrices |
| 109 | for spec in "$first_gate" "$first_down"; do |
| 110 | set -- $spec |
| 111 | off=$1; shard=$2; r=$3; c=$4; name=$5 |
| 112 | dd if="$CK/$shard" of="$W/ind.raw" bs=1M iflag=skip_bytes,count_bytes skip="$off" count=$((r * c)) 2> /dev/null |
| 113 | "$ERC" encode "$W/ind.raw" 0 "$r" "$c" "$W/ind.erc" > /dev/null |
| 114 | mkdir -p "$W/ord"; rm -f "$W/ord"/* |
| 115 | for row in $(seq 0 $((r - 1)) | shuf --random-source=<(prng realord 1000000)); do |
| 116 | "$ERC" decode-row "$W/ind.erc" "$row" "$W/ord/$(printf %05d "$row")" || bad "$name: row $row" |
| 117 | done |
| 118 | cat "$W/ord"/* > "$W/ind.joined" |
| 119 | check "$name: all $r rows decoded one process each in shuffled order equal the raw matrix" cmp -s "$W/ind.joined" "$W/ind.raw" |
| 120 | done |
| 121 | |
| 122 | # ---------------------------------------------------------------- negative controls on real matrices |
| 123 | for t in gate up down; do |
| 124 | line=$(inv_line "${chosen%% *}" "${EXPERTS%% *}" "$t") |
| 125 | shard=$(echo "$line" | cut -f1); name=$(echo "$line" | cut -f2); shape=$(echo "$line" | cut -f6) |
| 126 | off=$(echo "$line" | cut -f7); r=${shape%x*}; c=${shape#*x} |
| 127 | "$ERC" encode "$CK/$shard" "$off" "$r" "$c" "$W/n.erc" > /dev/null |
| 128 | K=$((r / 2)); ks=$(row_start "$W/n.erc" $K); ke=$(row_end "$W/n.erc" $K) |
| 129 | cp "$W/n.erc" "$W/n1.erc"; flipbit "$W/n1.erc" $(((ks + ke) / 2)) 4 |
| 130 | refuses "$name: stream bit flip" ROWCRC $K "$ERC" decode "$W/n1.erc" "$W/x.out" |
| 131 | cp "$W/n.erc" "$W/n2.erc"; flipbit "$W/n2.erc" $((32 + 2 * 40 + 1)) 1 |
| 132 | refuses "$name: frequency table bit flip" HDRCRC - "$ERC" decode "$W/n2.erc" "$W/x.out" |
| 133 | cp "$W/n.erc" "$W/n3.erc"; flipbit "$W/n3.erc" $((544 + 4 * K)) 6 |
| 134 | refuses "$name: row offset bit flip" HDRCRC - "$ERC" decode "$W/n3.erc" "$W/x.out" |
| 135 | sz=$(stat -c %s "$W/n.erc"); head -c $((sz - 100)) "$W/n.erc" > "$W/n4.erc" |
| 136 | refuses "$name: truncated by 100 bytes" LENGTH - "$ERC" decode "$W/n4.erc" "$W/x.out" |
| 137 | done |
| 138 | |
| 139 | # ---------------------------------------------------------------- flip scan on real rows |
| 140 | echo "$first_gate" | { read off shard r c name; "$ERC" encode "$CK/$shard" "$off" 8 "$c" "$W/fg.erc" > /dev/null; } |
| 141 | fsg=$("$ERC" flipscan "$W/fg.erc"); echo "flipscan real gate rows (8 x 2560): $fsg" |
| 142 | echo "$first_down" | { read off shard r c name; "$ERC" encode "$CK/$shard" "$off" 32 "$c" "$W/fd.erc" > /dev/null; } |
| 143 | fsd=$("$ERC" flipscan "$W/fd.erc"); echo "flipscan real down rows (32 x 640): $fsd" |
| 144 | printf 'gate 8x2560: %s\ndown 32x640: %s\n' "$fsg" "$fsd" > "$OUT/flipscan-real.txt" |
| 145 | for fs in "$fsg" "$fsd"; do |
| 146 | check "flipscan real rows: checked_undetected=0" [ "$(echo "$fs" | sed 's/.*checked_undetected=\([0-9]*\).*/\1/')" = 0 ] |
| 147 | done |
| 148 | |
| 149 | # ---------------------------------------------------------------- totals (aien-calc) |
| 150 | { |
| 151 | echo "matrices $nmat" |
| 152 | echo "raw_bytes $tot_raw" |
| 153 | echo "erc_coded_bytes $tot_erc" |
| 154 | echo "c_prototype_coded_bytes $tot_c" |
| 155 | echo "erc_minus_c_bytes $((tot_erc - tot_c))" |
| 156 | echo "chosen_layers $chosen" |
| 157 | echo "experts $EXPERTS" |
| 158 | } > "$OUT/summary.txt" |
| 159 | for q in "$tot_erc/$tot_raw" "$tot_c/$tot_raw" "($tot_erc-$tot_c)/$tot_c*100"; do |
| 160 | echo "aien-calc eval $q" >> "$OUT/summary.txt" |
| 161 | aien-calc eval "$q" >> "$OUT/summary.txt" 2>&1 |
| 162 | done |
| 163 | cat "$OUT/summary.txt" |
| 164 | rm -rf "$W/ord" "$W"/*.raw "$W"/*.dec "$W"/*.joined "$W"/*.erc "$W"/*.out |
| 165 | |
| 166 | if [ "$FAIL" = 0 ]; then echo "ERC1 REAL PASS $PASS checks"; exit 0; fi |
| 167 | echo "ERC1 REAL FAIL $FAIL of $((PASS + FAIL)) checks" |
| 168 | exit 1 |