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 / amd-kfd / oracle.py
| 1 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | """Independent host-test instrument only. Never imported by a producer or launcher. |
| 3 | |
| 4 | AQL expected bytes: Python struct's standard little-endian sizes, independent of Omega's |
| 5 | layout lowering, applied to the public HSA hsa_kernel_dispatch_packet_t definition and enum |
| 6 | bit positions in ROCR-Runtime rocm-6.2.0 src/inc/hsa.h (digest in PROVENANCE.md). |
| 7 | Kernarg expected bytes: read the actual .args entries and segment size of the assembly oracle, |
| 8 | not a copied 28-byte golden. KFD expected bytes: use the live C sizeof/offsetof oracle's table, |
| 9 | not the table that the Omega serializer consumes. Neither test touches a device. |
| 10 | """ |
| 11 | import pathlib |
| 12 | import re |
| 13 | import struct |
| 14 | import subprocess |
| 15 | import runpy |
| 16 | import sys |
| 17 | |
| 18 | |
| 19 | def table(path): |
| 20 | rows = [] |
| 21 | for line in pathlib.Path(path).read_text().splitlines(): |
| 22 | nums, label = line.split('#', 1) |
| 23 | rows.append((tuple(map(int, nums.split())), label.strip())) |
| 24 | result = [] |
| 25 | i = 0 |
| 26 | while i < len(rows): |
| 27 | (req, size, count), name = rows[i] |
| 28 | fields = rows[i + 1:i + 1 + count] |
| 29 | assert len(fields) == count and size > 0 |
| 30 | for (off, width, repeat), _ in fields: |
| 31 | assert width in (4, 8) and repeat > 0 and off + width * repeat <= size |
| 32 | result.append((req, size, name, fields)) |
| 33 | i += count + 1 |
| 34 | return result |
| 35 | |
| 36 | |
| 37 | def exercise(root, out): |
| 38 | root, out = pathlib.Path(root), pathlib.Path(out) |
| 39 | events = [] |
| 40 | exits = {} |
| 41 | controls = runpy.run_path(str(root / 'tests/amd-kfd/control.py')) |
| 42 | matches = controls['matches'] |
| 43 | |
| 44 | def caught(label, name): |
| 45 | controls['prove'](out, label, 'exit', exits[name], 0) |
| 46 | |
| 47 | def run(name, exe, args, want=0, expected=None, timeout=10): |
| 48 | cmd = [str(out / exe), *map(str, args)] |
| 49 | p = subprocess.run(cmd, capture_output=True, timeout=timeout) |
| 50 | (out / (name + '.stdout')).write_bytes(p.stdout) |
| 51 | (out / (name + '.stderr')).write_bytes(p.stderr) |
| 52 | (out / (name + '.command')).write_text(' '.join(cmd) + '\nexit=' + str(p.returncode) + '\n') |
| 53 | exits[name] = p.returncode |
| 54 | if want == 0: |
| 55 | assert matches('exit', p.returncode, 0), (name, p.returncode, p.stderr) |
| 56 | else: |
| 57 | assert not matches('exit', p.returncode, 0), (name, 'unexpected acceptance') |
| 58 | assert p.returncode == want, (name, p.returncode, want, p.stderr) |
| 59 | if expected is not None: |
| 60 | (out / (name + '.expected')).write_bytes(expected) |
| 61 | assert p.stdout == expected, (name, p.stdout.hex(), expected.hex()) |
| 62 | events.append(name) |
| 63 | return p.stdout |
| 64 | |
| 65 | # Two distinct fixed launches: all dispatch fields, high halves of every address, |
| 66 | # all three dimensions, barrier 0/1 and the three fence-scope enums are represented. |
| 67 | launches = [ |
| 68 | (1, 64, 1, 1, 1024, 1, 1, 0, 0, 0x0102030405060708, 0x1112131415161718, |
| 69 | 0x2122232425262728, 1, 2, 2), |
| 70 | (3, 8, 4, 2, 32, 8, 6, 20, 4096, 0xfedcba9876543200, 0x8877665544332200, |
| 71 | 0xffeeddccbbaa9988, 0, 1, 0), |
| 72 | ] |
| 73 | for i, v in enumerate(launches): |
| 74 | dims, wx, wy, wz, gx, gy, gz, priv, group, ko, ka, sig, barrier, acq, rel = v |
| 75 | header = 2 | barrier << 8 | acq << 9 | rel << 11 |
| 76 | expected = struct.pack('<6H5I4Q', header, dims, wx, wy, wz, 0, |
| 77 | gx, gy, gz, priv, group, ko, ka, 0, sig) |
| 78 | assert len(expected) == 64 |
| 79 | first = run('aql-' + str(i), 'aql', v, expected=expected) |
| 80 | assert run('aql-repeat-' + str(i), 'aql.repeat', v) == first |
| 81 | print('PASS AQL two fixed launches: independent HSA definition + struct.pack <6H5I4Q') |
| 82 | for i, (field, value) in enumerate(((4, 0), (0, 0), (1, 0), (13, 3), (1, 65536), |
| 83 | (7, 4294967296), (5, 2), (4, 1))): |
| 84 | v = list(launches[0]) |
| 85 | v[field] = value |
| 86 | run('aql-invalid-' + str(i), 'aql', v, 3, b'') |
| 87 | caught('grid-size-zero', 'aql-invalid-0') |
| 88 | for v in ('-1', '18446744073709551616', '0x10000000000000000', 'no', '0x', ''): |
| 89 | args = list(launches[0]) |
| 90 | args[9] = v |
| 91 | run('aql-number-' + str(len(events)), 'aql', args, 2, b'') |
| 92 | print('PASS AQL field rules and overflowing/malformed numeric refusals') |
| 93 | |
| 94 | asm = (root / 'backend/amd/vadd-gfx1151.s').read_text() |
| 95 | assert re.search(r'amdhsa.version:\s*\[\s*1,\s*2\s*\]', asm), 'requires v5 metadata 1.2' |
| 96 | size = int(re.search(r'\.kernarg_segment_size:\s*(\d+)', asm).group(1)) |
| 97 | kdsize = int(re.search(r'\.amdhsa_kernarg_size\s+(\d+)', asm).group(1)) |
| 98 | assert size == kdsize == 28 |
| 99 | assert re.search(r'\.kernarg_segment_align:\s*8\b', asm) |
| 100 | entries = re.findall(r'\{\s*\.name:\s*(\w+),\s*\.offset:\s*(\d+),\s*\.size:\s*(\d+),\s*\.value_kind:\s*(\w+)[^}]*\}', asm) |
| 101 | assert [(n, int(o), int(s), k) for n, o, s, k in entries] == [ |
| 102 | ('a', 0, 8, 'global_buffer'), ('b', 8, 8, 'global_buffer'), |
| 103 | ('c', 16, 8, 'global_buffer'), ('count', 24, 4, 'by_value')] |
| 104 | assert 'hidden_' not in asm |
| 105 | values = (0x0102030405060708, 0x8899aabbccddeeff, 0xfedcba9876543210, 1024) |
| 106 | expected = bytearray(size) |
| 107 | for (_, off, width, _), value in zip(entries, values): |
| 108 | off, width = int(off), int(width) |
| 109 | expected[off:off + width] = value.to_bytes(width, 'little') |
| 110 | first = run('kernarg', 'kernarg', values, expected=expected) |
| 111 | assert run('kernarg-repeat', 'kernarg.repeat', values) == first |
| 112 | run('kernarg-short', 'kernarg', [*values, size - 1], 3, b'') |
| 113 | run('kernarg-count-overflow', 'kernarg', [*values[:3], 4294967296], 3, b'') |
| 114 | print('PASS kernarg assembly metadata: code-object-v5 metadata 1.2; four explicit arguments; no hidden args') |
| 115 | caught('kernarg-one-byte-short', 'kernarg-short') |
| 116 | |
| 117 | src_table = root / 'backend/amd/kfd/requests.tsv' |
| 118 | native_table = out / 'native-table.tsv' |
| 119 | def matches_header(path): |
| 120 | return matches('table', path, native_table) |
| 121 | |
| 122 | assert matches_header(src_table), 'header and repository table differ' |
| 123 | oracle = table(native_table) |
| 124 | with (out / 'request-list.tsv').open('w') as report: |
| 125 | for idx, (req, size, name, fields) in enumerate(oracle): |
| 126 | if req: |
| 127 | report.write(f'{name}\t{req}\t0x{req:08x}\t{size}\n') |
| 128 | values = [] |
| 129 | expected = bytearray(size) |
| 130 | for j, ((off, width, repeat), label) in enumerate(fields): |
| 131 | # Non-palindromic values prove byte order. Pads/union unused space remain zero. |
| 132 | value = 0 if label == 'pad' else (0x0102030405060708 + j if width == 8 else 0x11223344 + j) |
| 133 | for k in range(repeat): |
| 134 | values.append(value) |
| 135 | p = off + k * width |
| 136 | expected[p:p + width] = value.to_bytes(width, 'little') |
| 137 | first = run('record-' + name, 'records', [src_table, idx, *values], expected=expected) |
| 138 | assert run('record-repeat-' + name, 'records.repeat', [src_table, idx, *values]) == first |
| 139 | run('zero-record-' + name, 'records', [src_table, idx], expected=bytes(size)) |
| 140 | print(f'PASS KFD {sum(bool(r[0]) for r in oracle)} requests + auxiliary pointed records: every serialized field vs live header oracle') |
| 141 | |
| 142 | # One request number is changed. Comparing the *same* complete live oracle catches it. |
| 143 | lines = src_table.read_text().splitlines() |
| 144 | nums, comment = lines[0].split('#', 1) |
| 145 | a, b, c = map(int, nums.split()) |
| 146 | lines[0] = f'{a + 1}\t{b}\t{c}\t# {comment.strip()}' |
| 147 | wrong_req = out / 'wrong-request.tsv' |
| 148 | wrong_req.write_text('\n'.join(lines) + '\n') |
| 149 | assert not matches_header(wrong_req) |
| 150 | # records never emits the request number, so the header comparison is the only guard for it. |
| 151 | controls['prove'](out, 'wrong-request-number', 'table', wrong_req, native_table) |
| 152 | |
| 153 | # One extra u32 field in the record extent: eight bytes becomes twelve, independently caught |
| 154 | # both by the header table compare and the exact serialized record length/bytes comparison. |
| 155 | lines = src_table.read_text().splitlines() |
| 156 | lines[0] = f'{a}\t{b + 4}\t{c}\t# {comment.strip()}' |
| 157 | wrong_size = out / 'one-field-too-large.tsv' |
| 158 | wrong_size.write_text('\n'.join(lines) + '\n') |
| 159 | assert not matches_header(wrong_size) |
| 160 | changed = run('record-off-by-one-field', 'records', [wrong_size, 0, 1, 14]) |
| 161 | assert len(changed) == 12 and len(changed) != oracle[0][1] |
| 162 | controls['prove'](out, 'record-off-by-one-field', 'table', wrong_size, native_table) |
| 163 | lines = src_table.read_text().splitlines() |
| 164 | nums, comment = lines[2].split('#', 1) |
| 165 | off, width, count = map(int, nums.split()) |
| 166 | lines[2] = f'{off + 1}\t{width}\t{count}\t# {comment.strip()}' |
| 167 | wrong_off = out / 'wrong-field-offset.tsv' |
| 168 | wrong_off.write_text('\n'.join(lines) + '\n') |
| 169 | assert not matches_header(wrong_off) |
| 170 | run('record-field-offset', 'records', [wrong_off, 0, 1, 14], 3, b'') |
| 171 | controls['prove'](out, 'field-offset-plus-one', 'table', wrong_off, native_table) |
| 172 | # Generate both upper-domain edges, including the wrap case, from the wire width. |
| 173 | indices = runpy.run_path(str(root / 'tests/amd/boundaries.py'))['record_indices']() |
| 174 | for index in indices: |
| 175 | run('record-index-' + str(index), 'records', [src_table, index], 3, b'') |
| 176 | run('record-index-past-end', 'records', [src_table, len(oracle)], 3, b'') |
| 177 | print(f'PASS records refuses generated upper-domain indices {indices} and first past-end index') |
| 178 | source = (root / 'backend/amd/kfd/records.om').read_text() |
| 179 | guard = 'if eq idx 18446744073709551615\nret 3\nend\n' |
| 180 | assert source.count(guard) == 1, 'index guard mutant did not apply' |
| 181 | mutant = out / 'records-index-mutant.om' |
| 182 | mutant.write_text(source.replace(guard, '') + (root / 'backend/amd/kfd/cli.om').read_text()) |
| 183 | binary = out / 'records-index-mutant' |
| 184 | p = subprocess.run([sys.argv[3]], input=mutant.read_bytes(), capture_output=True, timeout=120) |
| 185 | (out / 'records-index-mutant.compile.log').write_bytes(p.stderr) |
| 186 | assert p.returncode == 0 and not p.stderr, ('INVALID index mutant setup', p.returncode, p.stderr) |
| 187 | binary.write_bytes(p.stdout) |
| 188 | binary.chmod(0o700) |
| 189 | p = subprocess.run([str(binary), str(src_table), str(indices[-1])], capture_output=True, timeout=10) |
| 190 | assert p.returncode == 0 and not p.stdout and not p.stderr, ('INVALID index mutant outcome', p) |
| 191 | print(f'CONTROL index-guard-removal CAUGHT actual_exit={p.returncode} expected_exit=3 empty_stdout') |
| 192 | over_fuel = out / 'field-count-over-fuel.tsv' |
| 193 | # Keep every preceding field valid: a truncated table would refuse 3 before exhausting fuel. |
| 194 | over_fuel.write_text('0 4 1000001\n' + '0 4 1\n' * 1000001) |
| 195 | alarm = subprocess.run(['sh', str(root / 'tests/amd/loop-fuel.sh'), str(over_fuel)], capture_output=True, timeout=10) |
| 196 | (out / 'field-count-alarm.log').write_bytes(alarm.stdout + alarm.stderr) |
| 197 | assert alarm.returncode == 1 and b'LOOP-FUEL ALARM site=backend/amd/kfd/records.om:' in alarm.stdout |
| 198 | assert b'function=record' in alarm.stdout and b'compiler=' in alarm.stdout |
| 199 | controls['prove'](out, 'field-count-over-loop-fuel', 'exit', alarm.returncode, 0) |
| 200 | run('record-field-count-over-fuel', 'records', [over_fuel, 0], 104, b'', timeout=60) |
| 201 | print('MEASURED compiler-fuel reference exit=104; named source-site alarm fired before this isolated reference run') |
| 202 | |
| 203 | steps = [1, 2, 3, 4, 5] * 40 |
| 204 | first = run('ring-wrap', 'ring', steps) |
| 205 | assert first == b'RING ACCEPT body<header<write-index<doorbell<consume\n' |
| 206 | assert run('ring-repeat', 'ring.repeat', steps) == first |
| 207 | run('ring-four-steps-usage', 'ring', [1, 2, 3, 4], 64, b'') |
| 208 | caught('ring-four-steps', 'ring-four-steps-usage') |
| 209 | for name, values in [('header-before-body', [2, 1, 3, 4, 5]), |
| 210 | ('write-index-before-header', [1, 3, 2, 4, 5]), |
| 211 | ('doorbell-before-write-index', [1, 2, 4, 3, 5]), |
| 212 | ('consumer-before-doorbell', [1, 2, 3, 5, 4]), |
| 213 | ('body-reuse-before-consume', [1, 2, 3, 4, 1]), |
| 214 | ('incomplete', [1, 2, 3, 4, 5, 1])]: |
| 215 | run('ring-' + name, 'ring', values, 3, b'') |
| 216 | print('PASS ring single-producer/consumer order, 40 launches, 16 slots, wrap and byte visibility checked') |
| 217 | caught('header-before-body', 'ring-header-before-body') |
| 218 | print('PASS ring write-index/doorbell/consumer/reuse/incomplete-order refusals') |
| 219 | print(f'ORACLE PASS subprocess_cases={len(events)}') |
| 220 | |
| 221 | |
| 222 | if __name__ == '__main__': |
| 223 | try: |
| 224 | exercise(sys.argv[1], sys.argv[2]) |
| 225 | except (AssertionError, ValueError, OSError, subprocess.SubprocessError) as exc: |
| 226 | print('ORACLE FAIL', repr(exc), file=sys.stderr) |
| 227 | sys.exit(1) |