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 / fp8_ragged_api_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * Ragged-K FP8 GEMV, API selection (host only, no device opened): which calls of the Qwen3.8-Flash-Next expert shapes run the ragged kernel. |
| 4 | * gate_proj / up_proj n = 640, k = 2560 (a multiple of 512: the plain kernel) |
| 5 | * down_proj n = 2560, k = 640 (not a multiple of 256: the ragged kernel in the device-table and record forms) |
| 6 | * Also the seeded Omega host-policy unit op 25 against its frozen C oracle (CUDAPP_API_HOSTREST_DIFF aborts on any difference) and |
| 7 | * against a truth table written here from the rule, not from the unit. |
| 8 | */ |
| 9 | #define CUDAPP_API_HOSTREST_DIFF 1 |
| 10 | #include "omega_gpu_api_hostrest.h" |
| 11 | #include "omega_gpu_fp8_api.h" |
| 12 | #include "omega_blackwell_codegen_moe.h" |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | static int g_checks, g_failed; |
| 17 | #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 18 | |
| 19 | /* the rule, from the ragged contract in omega_blackwell_codegen_moe.h */ |
| 20 | static int expected(uint64_t k, uint64_t lb, uint64_t ok, uint64_t variant, uint64_t depth) { |
| 21 | return ok && variant == 1 && depth == 0 && lb != 0 && k % (32 * lb) != 0 && k % 128 == 0; |
| 22 | } |
| 23 | |
| 24 | int main(void) { |
| 25 | /* 1. Flash-Next shapes */ |
| 26 | OmegaFp8GemvSpec gu, gu_n, dn, dn1; |
| 27 | omega_gpu_fp8_spec_for(2560, 1280, 10, &gu); /* gate and up as one 1280-row projection, 10 routed experts */ |
| 28 | omega_gpu_fp8_spec_for(2560, 640, 10, &gu_n); /* one of them */ |
| 29 | omega_gpu_fp8_spec_for(640, 2560, 10, &dn); |
| 30 | omega_gpu_fp8_spec_for(640, 2560, 1, &dn1); |
| 31 | CHECK(gu.lane_bytes == 16 && omega_fp8_gemv_steps(&gu) == 5 && !omega_gpu_fp8_ragged_for(&gu, 1) && !omega_gpu_fp8_ragged_for(&gu, 0), |
| 32 | "gate/up k=2560: plain kernel (lb %u, steps %u), ragged not used in either form", gu.lane_bytes, omega_fp8_gemv_steps(&gu)); |
| 33 | CHECK(gu_n.lane_bytes == 16 && omega_fp8_gemv_steps(&gu_n) == 5 && !omega_gpu_fp8_ragged_for(&gu_n, 1), "gate/up n=640: plain kernel"); |
| 34 | CHECK(dn.lane_bytes == 8 && omega_fp8_gemv_steps(&dn) == 0 && omega_fp8_gemv_ragged_steps(&dn) == 3, |
| 35 | "down k=640: no plain kernel (plain steps %u), ragged steps %u", omega_fp8_gemv_steps(&dn), omega_fp8_gemv_ragged_steps(&dn)); |
| 36 | CHECK(omega_gpu_fp8_ragged_for(&dn, 1) == 1, "down k=640: the device-table and record forms run the ragged kernel"); |
| 37 | CHECK(omega_gpu_fp8_ragged_for(&dn, 0) == 0, "down k=640: the host-staged form keeps the general fallback (its results do not change)"); |
| 38 | uint32_t declared = 0; |
| 39 | CHECK(omega_gpu_fp8_kernel_fits(&dn, &declared) == OMEGA_GPU_FP8_OK, "down k=640 ragged kernel fits the register file at %u warps (declared %u)", dn.warps, declared); |
| 40 | CHECK(dn.depth == 0 && dn1.depth == 0 && omega_gpu_fp8_ragged_for(&dn1, 1) == 1, "a ragged single projection never selects the deep kernel (depth %u)", dn1.depth); |
| 41 | CHECK(dn.warps == 32 && dn.rows == 1, "down k=640 grouped launch: %u warps, %u rows per warp", dn.warps, dn.rows); |
| 42 | /* the knob */ |
| 43 | omega_gpu_fp8_set_ragged(1); |
| 44 | CHECK(omega_gpu_fp8_ragged_for(&dn, 0) == 1 && omega_gpu_fp8_ragged_for(&dn, 1) == 1, "set_ragged(1): every form runs the ragged kernel"); |
| 45 | omega_gpu_fp8_set_ragged(0); |
| 46 | CHECK(omega_gpu_fp8_ragged_for(&dn, 0) == 0 && omega_gpu_fp8_ragged_for(&dn, 1) == 0, "set_ragged(0): no form runs it (the device and record forms refuse)"); |
| 47 | omega_gpu_fp8_set_ragged(-1); |
| 48 | CHECK(omega_gpu_fp8_ragged_for(&dn, 1) == 1 && omega_gpu_fp8_ragged_for(&dn, 0) == 0, "set_ragged(-1): automatic again"); |
| 49 | OmegaFp8GemvSpec dd = dn1; dd.depth = 2; |
| 50 | CHECK(!omega_gpu_fp8_ragged_for(&dd, 1), "a spec with a deep kernel is never ragged"); |
| 51 | |
| 52 | /* 2. op 25 differential and truth table */ |
| 53 | static const uint64_t ks[] = { 0, 1, 64, 127, 128, 129, 192, 255, 256, 257, 320, 384, 512, 640, 768, 896, 1024, 1152, 2048, 2560, 2688, 4096, 17408, 32768, UINT32_MAX }; |
| 54 | static const uint64_t lbs[] = { 0, 4, 8, 16, 32 }; |
| 55 | unsigned points = 0, yes = 0; |
| 56 | for (unsigned i = 0; i < sizeof ks / sizeof ks[0]; i++) |
| 57 | for (unsigned j = 0; j < sizeof lbs / sizeof lbs[0]; j++) |
| 58 | for (uint64_t ok = 0; ok < 2; ok++) |
| 59 | for (uint64_t variant = 1; variant <= 3; variant++) |
| 60 | for (uint64_t depth = 0; depth < 3; depth++) { |
| 61 | const uint64_t p[16] = { ks[i], lbs[j], ok, variant, depth }; |
| 62 | const uint64_t got = omega_ah_call(25, p); /* aborts if the Omega unit differs from the frozen C oracle */ |
| 63 | const int want = expected(ks[i], lbs[j], ok, variant, depth); |
| 64 | CHECK(got == (uint64_t)want, "op25(k=%llu lb=%llu ok=%llu variant=%llu depth=%llu) = %llu, rule says %d", |
| 65 | (unsigned long long)ks[i], (unsigned long long)lbs[j], (unsigned long long)ok, (unsigned long long)variant, (unsigned long long)depth, (unsigned long long)got, want); |
| 66 | points++; yes += want; |
| 67 | } |
| 68 | CHECK(yes > 0 && yes < points, "the grid has both answers (%u yes of %u)", yes, points); |
| 69 | printf("op 25: %u points, %u answer 1\n", points, yes); |
| 70 | printf("%s: %d checks, %d failed\n", g_failed ? "FP8 RAGGED API FAIL" : "FP8 RAGGED API PASS", g_checks, g_failed); |
| 71 | return g_failed ? 1 : 0; |
| 72 | } |