Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tools/ampere_corpus.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tools / ampere_corpus.c` 154 lines · 6450 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Test-only IR capture. Linked into the existing host-only corpus producers; 3 * no driver API is called here. A refusal is legal for an explicit FP8 marker, 4 * or for a training R5 opcode SM86 does not implement (op > BW_IR_MUFU_LG2). 5 * 6 * The decisions are seeded Omega (om/tools/ampcorp.om through src/cudapp_om_ampcorp.c): the folder 7 * check, the IR metadata, the refusal classification, the check of the encoder outcome and the text of 8 * the paths and of the message. This file keeps the I/O: getenv, the SM86 encoder call, open/fdopen/ 9 * fwrite, the serial counter and abort. The former C is the oracle behind the default-OFF switch 10 * CUDAPP_MIGRATION_PENDING_C_TOOLS_AMPCORP (tests/tools-om/ampcorp.sh). */ 11 #include "omega_ampere_codegen.h" 12 #include "omega_blackwell_codegen_tc.h" 13 #include "omega_blackwell_codegen_qmma.h" 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 21 #ifdef CUDAPP_MIGRATION_PENDING_C_TOOLS_AMPCORP 22 static int is_safe_corpus_dir(const char *dir) 23 { 24 if (!dir || !*dir) return 0; 25 if (strchr(dir, '\\')) return 0; 26 for (const char *component = dir; *component;) { 27 while (*component == '/') ++component; 28 const char *start = component; 29 while (*component && *component != '/') ++component; 30 if ((size_t)(component - start) == 2 && 31 start[0] == '.' && start[1] == '.') return 0; 32 } 33 return 1; 34 } 35 36 static void save(const char *dir, unsigned serial, const char *suffix, const void *data, size_t size) 37 { 38 char path[4096]; 39 int n = snprintf(path, sizeof(path), "%s/%ld-%u.%s", dir, (long)getpid(), serial, suffix); 40 if (n < 0 || (size_t)n >= sizeof(path)) abort(); 41 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); 42 if (fd < 0) { perror(path); abort(); } 43 FILE *f = fdopen(fd, "wb"); 44 if (!f) { perror(path); close(fd); abort(); } 45 if (fwrite(data, 1, size, f) != size || fclose(f) != 0) abort(); 46 } 47 48 void omega_ampere_corpus_capture(const BlackwellIRProgram *prog, const uint8_t *sm121, size_t len) 49 { 50 const char *dir = getenv("OMEGA_AMPERE_CORPUS_DIR"); 51 if (!is_safe_corpus_dir(dir)) return; 52 static unsigned serial; 53 const unsigned id = serial++; 54 uint8_t code[16384]; 55 uint32_t metadata[BW_MAX_IR_INSNS][2]; 56 int fp8 = 0, blackwell_only = 0; 57 for (size_t i = 0; i < prog->count; ++i) { 58 const BlackwellIRInsn *in = &prog->insns[i]; 59 metadata[i][0] = (uint32_t)in->op; 60 metadata[i][1] = in->imm; 61 if (in->op > BW_IR_MUFU_LG2) blackwell_only = 1; 62 if ((in->op == BW_IR_HMMA_BF16 && in->imm == OMEGA_QMMA_MARK) || 63 (in->op == BW_IR_FMNMX_MAX && in->imm == OMEGA_QMMA_MARK_F2E4)) fp8 = 1; 64 } 65 memset(code, 0xa5, sizeof(code)); 66 size_t bytes = 0; 67 int rc = omega_gpu_encode_program_target(prog, 86, code, sizeof(code), &bytes); 68 /* SM121 already holds the published bytes. SM86 refuses the new opcodes in 69 * its preflight and writes nothing. There is no post-encode patch. */ 70 if (fp8 || blackwell_only) { 71 if (rc != OMEGA_GPU_ENCODE_UNSUPPORTED || bytes != 0) abort(); 72 for (size_t i = 0; i < sizeof(code); ++i) if (code[i] != 0xa5) abort(); 73 save(dir, id, "refused", metadata, prog->count * sizeof(metadata[0])); 74 return; 75 } 76 if (rc != 0 || bytes != len) { 77 fprintf(stderr, "SM86 unexpectedly refused an existing kernel (%zu instructions, rc %d)\n", prog->count, rc); 78 abort(); 79 } 80 save(dir, id, "sm121.bin", sm121, len); 81 save(dir, id, "sm86.bin", code, bytes); 82 save(dir, id, "ir", metadata, prog->count * sizeof(metadata[0])); 83 } 84 #else 85 #include "cudapp_native.h" 86 87 /* The unit's arrays (sizes fixed by om/tools/ampcorp.om). The folder buffer holds any environment 88 * string: Linux caps one at MAX_ARG_STRLEN (131072 bytes) with its name and zero. Not reentrant, as 89 * the former C's serial counter already was not. */ 90 static uint64_t g_facts[16]; 91 static uint8_t g_dir[131072]; 92 static uint64_t g_ins[2048]; 93 static uint8_t g_code[16384]; 94 static uint8_t g_out[20480]; 95 96 static uint64_t decide(void) 97 { 98 uint64_t r = 0; 99 if (omega_ampcorp(&r, g_facts, 16, g_dir, sizeof(g_dir), g_ins, 2048, g_code, sizeof(g_code), 100 g_out, sizeof(g_out)) != OMEGA_OK) abort(); 101 return r; 102 } 103 104 static void save(const char *path, const void *data, size_t size) 105 { 106 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); 107 if (fd < 0) { perror(path); abort(); } 108 FILE *f = fdopen(fd, "wb"); 109 if (!f) { perror(path); close(fd); abort(); } 110 if (fwrite(data, 1, size, f) != size || fclose(f) != 0) abort(); 111 } 112 113 void omega_ampere_corpus_capture(const BlackwellIRProgram *prog, const uint8_t *sm121, size_t len) 114 { 115 const char *dir = getenv("OMEGA_AMPERE_CORPUS_DIR"); 116 if (!dir) return; 117 size_t dn = strlen(dir); 118 if (dn >= sizeof(g_dir) || prog->count > BW_MAX_IR_INSNS) abort(); 119 memcpy(g_dir, dir, dn); 120 memset(g_facts, 0, sizeof(g_facts)); 121 g_facts[1] = dn; 122 if (!decide()) return; 123 static unsigned serial; 124 const unsigned id = serial++; 125 for (size_t i = 0; i < prog->count; ++i) { 126 g_ins[2 * i] = (uint32_t)prog->insns[i].op; 127 g_ins[2 * i + 1] = prog->insns[i].imm; 128 } 129 memset(g_code, 0xa5, sizeof(g_code)); 130 size_t bytes = 0; 131 int rc = omega_gpu_encode_program_target(prog, 86, g_code, sizeof(g_code), &bytes); 132 const uint64_t facts[16] = { 1, dn, prog->count, (uint64_t)(int64_t)rc, bytes, len, (uint64_t)(long)getpid(), id, 133 BW_IR_MUFU_LG2, BW_IR_HMMA_BF16, OMEGA_QMMA_MARK, BW_IR_FMNMX_MAX, 134 OMEGA_QMMA_MARK_F2E4, (uint64_t)(int64_t)OMEGA_GPU_ENCODE_UNSUPPORTED }; 135 memcpy(g_facts, facts, sizeof(facts)); 136 uint64_t r = decide(); 137 const char *path = (const char *)g_out + 8192; 138 switch (r & 255) { 139 case 1: 140 save(path, g_out, prog->count * 8); 141 return; 142 case 2: 143 save(path, sm121, len); 144 save((const char *)g_out + 12288, g_code, bytes); 145 save((const char *)g_out + 16384, g_out, prog->count * 8); 146 return; 147 case 4: 148 fwrite(path, 1, (size_t)(r >> 8), stderr); 149 abort(); 150 default: /* 3: the refusal wrote bytes or did not refuse; 5: the path does not fit 4096 bytes */ 151 abort(); 152 } 153 } 154 #endif