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 / apiviews_host_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #define _POSIX_C_SOURCE 200809L |
| 3 | /* Host-data loops for elementwise, matmul bf16, gdn, fp8 and scan. |
| 4 | * Build with CUDAPP_MIGRATION_PENDING_APIVIEWS_DIFF so each call is Omega |
| 5 | * versus the C oracle. Frozen constants are from the unit's integer rule |
| 6 | * and from Wolfram, not copied back out of a passing run. */ |
| 7 | #include "omega_gpu_apiviews.h" |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <time.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | static int checks; |
| 16 | static int fails; |
| 17 | |
| 18 | static void u32eq(const char *name, uint32_t got, uint32_t want) { |
| 19 | checks++; |
| 20 | if (got != want) { |
| 21 | fprintf(stderr, "FAIL %s got %u want %u\n", name, got, want); |
| 22 | fails++; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | static void u64eq(const char *name, uint64_t got, uint64_t want) { |
| 27 | checks++; |
| 28 | if (got != want) { |
| 29 | fprintf(stderr, "FAIL %s got %llu want %llu\n", name, |
| 30 | (unsigned long long)got, (unsigned long long)want); |
| 31 | fails++; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | static uint32_t bits(float f) { |
| 36 | uint32_t u; |
| 37 | memcpy(&u, &f, 4); |
| 38 | return u; |
| 39 | } |
| 40 | |
| 41 | /* WolframLanguageEvaluator: gdn hk = Quotient[h, Quotient[vh, 16]] |
| 42 | * -> 0, 127, 2048, 2175, 4096, 4224. */ |
| 43 | static void gdn_cases(void) { |
| 44 | float src[3] = {1.0f, 2.0f, 3.0f}; |
| 45 | float dst[4]; |
| 46 | float ng[2] = {1.0f, 4.0f}; |
| 47 | float dt[2] = {0.5f, 0.25f}; |
| 48 | float sc[4]; |
| 49 | u32eq("gdn0", av_gdn_channel(0, 0, 128), 0); |
| 50 | u32eq("gdn127", av_gdn_channel(0, 127, 128), 127); |
| 51 | u32eq("gdn2048", av_gdn_channel(0, 128, 128), 2048); |
| 52 | u32eq("gdn2175", av_gdn_channel(0, 255, 128), 2175); |
| 53 | u32eq("gdn4096", av_gdn_channel(0, 256, 128), 4096); |
| 54 | u32eq("gdn4224", av_gdn_channel(1, 256, 128), 4224); |
| 55 | av_gdn_slot(dst, src); |
| 56 | u32eq("slot0", bits(dst[0]), 1065353216u); |
| 57 | u32eq("slot3", bits(dst[3]), 0); |
| 58 | u32eq("scales", (uint32_t)av_gdn_scales(2, ng, dt, sc), 0); |
| 59 | u32eq("scale0", bits(sc[0]), 1065353216u); |
| 60 | u32eq("scale1", bits(sc[1]), 1056964608u); |
| 61 | } |
| 62 | |
| 63 | /* 1.0 -> code 56, 0.5 -> 48, 4.0 -> 72, |x|>=448 -> 126, -1.0 -> 184. |
| 64 | * 4.0 is the e4m3 mutant witness: code 72 is above a lowered 64 cap. */ |
| 65 | static void e4m3_cases(void) { |
| 66 | u32eq("e4m3zero", av_e4m3(0.0f), 0); |
| 67 | u32eq("e4m3one", av_e4m3(1.0f), 56); |
| 68 | u32eq("e4m3half", av_e4m3(0.5f), 48); |
| 69 | u32eq("e4m3four", av_e4m3(4.0f), 72); |
| 70 | u32eq("e4m3sat", av_e4m3(448.0f), 126); |
| 71 | u32eq("e4m3over", av_e4m3(512.0f), 126); |
| 72 | u32eq("e4m3neg", av_e4m3(-1.0f), 184); |
| 73 | } |
| 74 | |
| 75 | static void bf16_cases(void) { |
| 76 | float in[4] = {1.0f, 0.0f, -1.0f, 0.5f}; |
| 77 | uint16_t out[4]; |
| 78 | float wide[4]; |
| 79 | u32eq("bf16", (uint32_t)av_bf16_n(0, 4, in, out), 0); |
| 80 | u32eq("bf16one", out[0], 0x3f80); |
| 81 | u32eq("bf16zero", out[1], 0); |
| 82 | u32eq("bf16neg", out[2], 0xbf80); |
| 83 | u32eq("bf16half", out[3], 0x3f00); |
| 84 | u32eq("widen", (uint32_t)av_widen_n(4, out, wide), 0); |
| 85 | u32eq("widenone", bits(wide[0]), 1065353216u); |
| 86 | u32eq("bf16empty", (uint32_t)av_bf16_n(0, 0, in, out), 0); |
| 87 | /* md != 0: finite takes the round arm, NaN takes the class arm. */ |
| 88 | { |
| 89 | uint32_t nanb = 0x7fc00000u; |
| 90 | memcpy(&in[0], &nanb, 4); |
| 91 | } |
| 92 | u32eq("bf16nan", (uint32_t)av_bf16_n(1, 1, in, out), 0); |
| 93 | u32eq("bf16nanbits", out[0], 0x7fc0u); |
| 94 | in[0] = 1.0f; |
| 95 | u32eq("bf16md", (uint32_t)av_bf16_n(1, 1, in, out), 0); |
| 96 | u32eq("bf16mdone", out[0], 0x3f80); |
| 97 | } |
| 98 | |
| 99 | static void fp8_cases(void) { |
| 100 | uint64_t ent[2]; |
| 101 | uint16_t w[128 * 128]; |
| 102 | uint8_t q[128 * 128]; |
| 103 | uint16_t scale[1]; |
| 104 | uint32_t i; |
| 105 | av_fp8_group(ent, 0, 10, 20, 4, 8); |
| 106 | u64eq("fp8wv", ent[0], 10); |
| 107 | u64eq("fp8sv", ent[1], 20); |
| 108 | av_fp8_group(ent, 3, 10, 20, 4, 8); |
| 109 | u64eq("fp8wv3", ent[0], 22); |
| 110 | u64eq("fp8sv3", ent[1], 44); |
| 111 | for (i = 0; i < 128u * 128u; i++) w[i] = 0x3f80; |
| 112 | u32eq("fp8q", (uint32_t)av_fp8_quantize(128, 128, w, q, scale), 0); |
| 113 | u32eq("fp8qbad", (uint32_t)av_fp8_quantize(129, 128, w, q, scale), 1); |
| 114 | } |
| 115 | |
| 116 | static void ew_cases(void) { |
| 117 | uint64_t rec[8]; |
| 118 | float cs[4] = {1.0f, 2.0f, 3.0f, 4.0f}; |
| 119 | float sn[4] = {0.5f, 0.25f, 0.125f, 0.0625f}; |
| 120 | float tab[8]; |
| 121 | u32eq("ew0", (uint32_t)av_ew_rec(0, 100, 0, 0, rec), 0); |
| 122 | u64eq("ew0n", rec[0], 100); |
| 123 | u64eq("ew0g", rec[1], 1); |
| 124 | u64eq("ew0t", rec[6], 128); |
| 125 | u32eq("ewcap", (uint32_t)av_ew_rec(0, 200000, 0, 0, rec), 0); |
| 126 | u64eq("ewcapn", rec[0], 131072); |
| 127 | u32eq("ew0bad", (uint32_t)av_ew_rec(0, 0, 0, 0, rec), 1); |
| 128 | u32eq("ewodd", (uint32_t)av_ew_rec(1, 4, 3, 0, rec), 1); |
| 129 | u32eq("ewdim0", (uint32_t)av_ew_rec(2, 4, 0, 0, rec), 1); |
| 130 | /* head_dim 16 is the ewarg mutant witness (lowered cap of 8). */ |
| 131 | u32eq("ewhead", (uint32_t)av_ew_rec(1, 4, 16, 0, rec), 0); |
| 132 | u64eq("ewhf", rec[1], 8); |
| 133 | u32eq("rope", (uint32_t)av_rope_head(4, cs, sn, tab), 0); |
| 134 | u32eq("rope0", bits(tab[0]), 1065353216u); |
| 135 | u32eq("ropeempty", (uint32_t)av_rope_head(0, cs, sn, tab), 0); |
| 136 | /* kind 2 is the rmsnorm launch record. p1 = 0 and 16385 refuse; 1 and 16384 are the edges. */ |
| 137 | u32eq("ewrms", (uint32_t)av_ew_rec(2, 4, 7, 128, rec), 0); |
| 138 | u64eq("ewrmsn", rec[0], 4); |
| 139 | u64eq("ewrmsg", rec[1], 1); |
| 140 | u64eq("ewrmsinv", rec[2], 1006632960ull); |
| 141 | u64eq("ewrmsp0", rec[3], 7); |
| 142 | u32eq("ewrmscap", (uint32_t)av_ew_rec(2, 2000, 0, 1, rec), 0); |
| 143 | u64eq("ewrmscapn", rec[0], 1024); |
| 144 | u64eq("ewrms1", rec[2], 1065353216ull); |
| 145 | u32eq("ewrmsedge", (uint32_t)av_ew_rec(2, 1, 0, 16384, rec), 0); |
| 146 | u64eq("ewrmsedgeinv", rec[2], 947912704ull); |
| 147 | u32eq("ewrmsover", (uint32_t)av_ew_rec(2, 1, 0, 16385, rec), 1); |
| 148 | u32eq("ewkind", (uint32_t)av_ew_rec(3, 1, 0, 1, rec), 1); |
| 149 | /* Different NaN payloads: class match, not bit match. */ |
| 150 | { |
| 151 | float na, nb; |
| 152 | uint32_t ua = 0x7fc00000u, ub = 0x7fc00001u; |
| 153 | memcpy(&na, &ua, 4); |
| 154 | memcpy(&nb, &ub, 4); |
| 155 | u32eq("nanclass", (uint32_t)av_f32_eq(na, nb), 1); |
| 156 | u32eq("nanreal", (uint32_t)av_f32_eq(na, 1.0f), 0); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void scan_cases(void) { |
| 161 | float in[32]; |
| 162 | float out[32]; |
| 163 | uint32_t i; |
| 164 | for (i = 0; i < 32; i++) in[i] = 0.0f; |
| 165 | u32eq("scann0", (uint32_t)av_scan_tile(0, 0, in, out), 1); |
| 166 | u32eq("scann33", (uint32_t)av_scan_tile(0, 33, in, out), 1); |
| 167 | u32eq("scanop", (uint32_t)av_scan_tile(4, 1, in, out), 1); |
| 168 | in[0] = 1.0f; |
| 169 | u32eq("scansum", (uint32_t)av_scan_tile(0, 1, in, out), 0); |
| 170 | u32eq("scanslot0", bits(out[0]), 1065353216u); |
| 171 | u32eq("scanpre", (uint32_t)av_scan_tile(3, 1, in, out), 0); |
| 172 | u32eq("scanpre0", bits(out[0]), 1065353216u); |
| 173 | /* +0 and -0 under max: the scan mutant flips the both-zero fmax arm. */ |
| 174 | in[0] = 0.0f; |
| 175 | in[1] = -0.0f; |
| 176 | u32eq("scanmax0", (uint32_t)av_scan_tile(1, 2, in, out), 0); |
| 177 | u32eq("scanmin", (uint32_t)av_scan_tile(2, 1, in, out), 0); |
| 178 | } |
| 179 | |
| 180 | static void f32_of(uint32_t u, float *f) { memcpy(f, &u, 4); } |
| 181 | |
| 182 | static void tail_cases(void) { |
| 183 | float scratch[4] = {0, 0, 0, 0}; |
| 184 | u32eq("pack refuse", (uint32_t)(av_gdn_pack_tail(8, 1, scratch, scratch) == -1), 1); |
| 185 | const uint32_t vh = 16u, conv = 4096u + vh * 128u; |
| 186 | float *tail = calloc((size_t)3u * conv, sizeof *tail); |
| 187 | float *dev = calloc((size_t)vh * 384u * 4u, sizeof *dev); |
| 188 | float *back = calloc((size_t)3u * conv, sizeof *back); |
| 189 | f32_of(1065353216u, &tail[0]); |
| 190 | f32_of(1073741824u, &tail[conv]); |
| 191 | f32_of(1056964608u, &tail[2u * conv]); |
| 192 | u32eq("pack16", (uint32_t)av_gdn_pack_tail(vh, conv, tail, dev), 0); |
| 193 | u32eq("slot0", bits(dev[0]), 1065353216u); |
| 194 | u32eq("slot1", bits(dev[1]), 1073741824u); |
| 195 | u32eq("slot2", bits(dev[2]), 1056964608u); |
| 196 | u32eq("slot3", bits(dev[3]), 0); |
| 197 | u32eq("unpack16", (uint32_t)av_gdn_unpack_tail(vh, conv, back, dev), 0); |
| 198 | u32eq("back0", bits(back[0]), 1065353216u); |
| 199 | const uint32_t vh2 = 32u, conv2 = 4096u + vh2 * 128u; |
| 200 | float *tail2 = calloc((size_t)3u * conv2, sizeof *tail2); |
| 201 | float *dev2 = calloc((size_t)vh2 * 384u * 4u, sizeof *dev2); |
| 202 | f32_of(1065353216u, &tail2[0]); |
| 203 | u32eq("pack32", (uint32_t)av_gdn_pack_tail(vh2, conv2, tail2, dev2), 0); |
| 204 | dev2[(1u * 384u) * 4u] = 99.0f; |
| 205 | u32eq("disagree", (uint32_t)(av_gdn_unpack_tail(vh2, conv2, tail2, dev2) == -1), 1); |
| 206 | free(tail); |
| 207 | free(dev); |
| 208 | free(back); |
| 209 | free(tail2); |
| 210 | free(dev2); |
| 211 | } |
| 212 | |
| 213 | static void fold_cases(void) { |
| 214 | float src[4] = {1.0f, -2.0f, 0.5f, 0.0f}, dst[4], ng, y; |
| 215 | uint16_t b = 0x3f80; |
| 216 | float a0 = 0.0f, a1 = 1.0f; |
| 217 | u32eq("fp8x", (uint32_t)av_fp8_pack_x(4, src, dst), 0); |
| 218 | u32eq("fold1", bits(dst[0]), 1602224128u); |
| 219 | u32eq("fold0", bits(dst[3]), 0); |
| 220 | u32eq("fp8xb", (uint32_t)av_fp8_pack_x_bf16(1, &b, &y), 0); |
| 221 | { |
| 222 | float nanv; |
| 223 | f32_of(0x7fc00000u, &nanv); |
| 224 | u32eq("negnan", (uint32_t)av_gdn_negexp(1, &nanv, &ng), 0); |
| 225 | u32eq("negnanclass", (uint32_t)(((bits(ng) & 0x7fffffffu) > 0x7f800000u)), 1); |
| 226 | } |
| 227 | u32eq("foldb", bits(y), 1602224128u); |
| 228 | u32eq("negexp0", (uint32_t)av_gdn_negexp(1, &a0, &ng), 0); |
| 229 | u32eq("neg0", bits(ng), 3212836864u); |
| 230 | u32eq("negexp1", (uint32_t)av_gdn_negexp(1, &a1, &ng), 0); |
| 231 | } |
| 232 | |
| 233 | static void bench(void) { |
| 234 | float in[32]; |
| 235 | float out[32]; |
| 236 | uint16_t w[128 * 128]; |
| 237 | uint8_t q[128 * 128]; |
| 238 | uint16_t scale[1]; |
| 239 | struct timespec a, c; |
| 240 | uint32_t i; |
| 241 | uint64_t ns; |
| 242 | for (i = 0; i < 32; i++) in[i] = 1.0f; |
| 243 | clock_gettime(CLOCK_MONOTONIC, &a); |
| 244 | for (i = 0; i < 200; i++) av_scan_tile(0, 32, in, out); |
| 245 | clock_gettime(CLOCK_MONOTONIC, &c); |
| 246 | ns = (uint64_t)(c.tv_sec - a.tv_sec) * 1000000000ull + (uint64_t)(c.tv_nsec - a.tv_nsec); |
| 247 | printf("bench scan_tile 200 x 32: %llu ns\n", (unsigned long long)ns); |
| 248 | for (i = 0; i < 128u * 128u; i++) w[i] = 0x3f80; |
| 249 | clock_gettime(CLOCK_MONOTONIC, &a); |
| 250 | for (i = 0; i < 200; i++) av_fp8_quantize(128, 128, w, q, scale); |
| 251 | clock_gettime(CLOCK_MONOTONIC, &c); |
| 252 | ns = (uint64_t)(c.tv_sec - a.tv_sec) * 1000000000ull + (uint64_t)(c.tv_nsec - a.tv_nsec); |
| 253 | printf("bench fp8_quantize 200 x 128x128: %llu ns\n", (unsigned long long)ns); |
| 254 | clock_gettime(CLOCK_MONOTONIC, &a); |
| 255 | for (i = 0; i < 200; i++) av_fp8_pack_x(4, in, out); |
| 256 | clock_gettime(CLOCK_MONOTONIC, &c); |
| 257 | ns = (uint64_t)(c.tv_sec - a.tv_sec) * 1000000000ull + (uint64_t)(c.tv_nsec - a.tv_nsec); |
| 258 | printf("bench fp8_pack_x 200 x 4: %llu ns\n", (unsigned long long)ns); |
| 259 | } |
| 260 | |
| 261 | int main(int argc, char **argv) { |
| 262 | gdn_cases(); |
| 263 | e4m3_cases(); |
| 264 | bf16_cases(); |
| 265 | fp8_cases(); |
| 266 | ew_cases(); |
| 267 | scan_cases(); |
| 268 | tail_cases(); |
| 269 | fold_cases(); |
| 270 | if (argc > 1 && strcmp(argv[1], "--bench") == 0) bench(); |
| 271 | if (fails) { |
| 272 | fprintf(stderr, "FAIL apiviews host %d/%d\n", fails, checks); |
| 273 | return 1; |
| 274 | } |
| 275 | printf("PASS apiviews host %d checks\n", checks); |
| 276 | return 0; |
| 277 | } |