#!/usr/bin/env bash # Pinned offline assembly/disassembly only; no installed driver/GPU execution. set -euo pipefail [[ $# == 0 ]] || { echo 'Usage: scripts/test-cuda-fixed-ptx.sh' >&2; exit 2; } repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" image='ubuntu@sha256:f610ab94648195aa356059f5b41d6085c9d4d903c072430cdd1af7bdb646106b' work="$(mktemp -d "${TMPDIR:-/tmp}/leleka-fixed-ptx.XXXXXX")" container='' cleanup() { local result=$? failed=0 set +e if [[ -n "$container" ]]; then docker rm -f "$container" >/dev/null || failed=1; fi rm -rf "$work" || failed=1 if [[ "$failed" == 1 ]]; then echo 'PTX owned cleanup failed' >&2; result=1; else echo 'PTX owned container/files removed; shared base retained'; fi exit "$result" } trap cleanup EXIT python3 - "$repo_root" "$work" <<'PY' import hashlib, json, pathlib, re, sys, tarfile, urllib.request root, work = map(pathlib.Path, sys.argv[1:]) lock = json.loads((root/'deploy/cuda-toolkit.lock.json').read_text()) component = next(row for row in lock['components'] if row['component'] == 'cuda_nvcc') expected = '48e35be3cfbf4b4fbc16828eaec8a7048ee789403049dc409f7b643d6259cf7a' assert component['version']=='13.0.88' and component['sha256']==expected assert component['relative_path']=='cuda_nvcc/linux-x86_64/cuda_nvcc-linux-x86_64-13.0.88-archive.tar.xz' assert int(component['size'])==26657716 manifest_url=lock['manifest_url'] with urllib.request.urlopen(manifest_url,timeout=60) as response: manifest=response.read(1_000_000) assert hashlib.sha256(manifest).hexdigest()==lock['manifest_sha256'] entry=json.loads(manifest)['cuda_nvdisasm'];row=entry['linux-x86_64'] expected_disasm='0541e0230f724a43d67288ef882c63351d0c302bf567591b7620d40f93c2c93c' assert entry['version']=='13.0.85' and row['sha256']==expected_disasm assert row['relative_path']=='cuda_nvdisasm/linux-x86_64/cuda_nvdisasm-linux-x86_64-13.0.85-archive.tar.xz' assert int(row['size'])==4129456 print('Official manifest URL/SHA256:',manifest_url,lock['manifest_sha256']) for name,part,size,digest in [('ptxas',component,26657716,expected),('nvdisasm',row,4129456,expected_disasm)]: url='https://developer.download.nvidia.com/compute/cuda/redist/'+part['relative_path'] with urllib.request.urlopen(url,timeout=60) as response:data=response.read(size+1) assert len(data)==size and hashlib.sha256(data).hexdigest()==digest archive=work/(name+'.tar.xz');archive.write_bytes(data) with tarfile.open(archive,'r:xz') as source: directory=pathlib.PurePosixPath(part['relative_path']).name.removesuffix('.tar.xz') member=source.getmember(directory+'/bin/'+name) assert member.isfile() and member.size<100_000_000 binary=source.extractfile(member).read() assert binary[:4]==b'\x7fELF' (work/name).write_bytes(binary);(work/name).chmod(0o755) print(name,'official archive URL/bytes/SHA256:',url,size,digest) print(name,'extracted executable SHA256:',hashlib.sha256(binary).hexdigest()) for name in ['vector_add','matvec']: ptx=(root/f'crates/cuda-driver/src/{name}.ptx').read_bytes() assert 0clean.index(required[-2]) ops=[line.split()[0] for line in statements if not line.startswith('.')] assert not any(re.match(r'(fma|mad)\..*f32|.*\.ftz',op) for op in ops) assert sum(op.startswith('st.global') for op in ops)==1 assert sum(op.startswith('ld.global') for op in ops)==2 assert not any(op.startswith(('atom','call','red','st.shared','ld.shared')) for op in ops) print('Executable PTX dataflow: widened row guard before access, +zero, u64 ascending counter, ' 'row*N+c, separate mul/add, single final store: PASS') PY echo "Source root=$repo_root commit=$(git -C "$repo_root" rev-parse HEAD) host=$(uname -sm)" shasum -a 256 "$repo_root/crates/cuda-driver/src/ffi.rs" "$repo_root/scripts/test-cuda-fixed-ptx.sh" docker pull --platform linux/amd64 "$image" >/dev/null docker image inspect "$image" --format 'Pinned assembly CPU base {{.Id}} architecture={{.Architecture}}' container="$(docker create --platform linux/amd64 --runtime runc --user 65532:65532 \ --cap-drop ALL --security-opt no-new-privileges --network none --read-only \ --memory 768m --cpus 2 --pids-limit 64 --tmpfs /tmp:rw,nosuid,exec,size=128m \ "$image" sleep 900)" configuration="$(docker inspect "$container" --format '{{.HostConfig.NetworkMode}}|{{.HostConfig.Runtime}}|{{.HostConfig.Privileged}}|{{.Config.User}}|{{len .HostConfig.Devices}}|{{len .HostConfig.DeviceRequests}}|{{len .HostConfig.Binds}}|{{.HostConfig.CapDrop}}|{{.HostConfig.SecurityOpt}}')" [[ "$configuration" == 'none|runc|false|65532:65532|0|0|0|[ALL]|[no-new-privileges]' ]] echo "PTX assembly container $container: $configuration" docker start "$container" >/dev/null for file in ptxas nvdisasm vector_add.ptx matvec.ptx; do docker exec -i "$container" sh -c "cat > /tmp/$file" < "$work/$file" done docker exec "$container" chmod 0755 /tmp/ptxas /tmp/nvdisasm docker exec "$container" bash -euc ' for device in /dev/nvidia* /dev/dri/*; do [[ ! -e "$device" ]] || exit 1; done [[ "$(awk '\''/^Cap(Eff|Prm|Bnd):/{print $2}'\'' /proc/self/status)" == $'\''0000000000000000\n0000000000000000\n0000000000000000'\'' ]] if ldconfig -p 2>/dev/null | grep -q "libcuda\.so"; then exit 1; fi uname -sm /tmp/ptxas --version /tmp/nvdisasm --version for name in vector_add matvec; do echo "argv: /tmp/ptxas -arch=sm_120 /tmp/$name.ptx -o /tmp/$name.cubin" /tmp/ptxas -arch=sm_120 "/tmp/$name.ptx" -o "/tmp/$name.cubin" sha256sum /tmp/ptxas /tmp/nvdisasm "/tmp/$name.ptx" "/tmp/$name.cubin" test -s "/tmp/$name.cubin" done echo "argv: /tmp/nvdisasm -c /tmp/matvec.cubin" /tmp/nvdisasm -c /tmp/matvec.cubin > /tmp/matvec.sass echo "argv: /tmp/nvdisasm -cfg /tmp/matvec.cubin" /tmp/nvdisasm -cfg /tmp/matvec.cubin > /tmp/matvec.cfg.dot sha256sum /tmp/matvec.sass /tmp/matvec.cfg.dot cat /tmp/matvec.sass /tmp/matvec.cfg.dot echo "Pinned PTX assembler acceptance: PASS (two images; no GPU execution)" ' docker exec "$container" cat /tmp/matvec.sass > "$work/matvec.sass" docker exec "$container" cat /tmp/matvec.cfg.dot > "$work/matvec.cfg.dot" python3 - "$work" <<'PY' import pathlib,re,sys root=pathlib.Path(sys.argv[1]);sass=(root/'matvec.sass').read_text() instructions=[] for line in sass.splitlines(): match=re.search(r'/\*[0-9a-fA-F]+\*/\s+((?:@!?P\d+\s+)?[A-Z][^;]*);',line) if match:instructions.append(match.group(1).strip()) assert instructions,'no emitted executable instructions' ops=[re.sub(r'^@!?P\d+\s+','',s).split()[0].split('.')[0] for s in instructions] assert 'FMUL' in ops and 'FADD' in ops and 'FFMA' not in ops and 'FMAD' not in ops assert 'LDG' in ops and 'STG' in ops and 'BRA' in ops and 'EXIT' in ops cfg=(root/'matvec.cfg.dot').read_text() assert '->' in cfg and 'FMUL' in cfg and 'FADD' in cfg # Pinned sm_120 machine dataflow, including the assembler's four-column # unrolling. Loads may be scheduled ahead; accumulator dependencies preserve # ascending addition order. Each check addresses executable operands/edges. assert instructions.index('IMAD.WIDE.U32 R2, R5, 0x80, R2') < instructions.index('ISETP.GE.U64.AND P0, PT, R2, UR4, PT') exit_guard=instructions.index('@P0 EXIT') assert exit_guard < min(i for i,op in enumerate(ops) if op in ['LDG','STG']) assert 'MOV.64 R4, RZ' in instructions and 'MOV R0, RZ' in instructions loop=sass.split('.L_x_1:',1)[1].split('.L_x_0:',1)[0] loop_instructions=[s for s in instructions if s in [ 'FMUL R15, R14, R15', 'FADD R15, R15, R0', 'FMUL R18, R18, R19', 'FMUL R20, R20, R21', 'FADD R15, R15, R18', 'FADD R15, R15, R20', 'FMUL R22, R22, R23', 'FADD R0, R15, R22']] assert loop_instructions==[ 'FMUL R15, R14, R15', 'FADD R15, R15, R0', 'FMUL R18, R18, R19', 'FMUL R20, R20, R21', 'FADD R15, R15, R18', 'FADD R15, R15, R20', 'FMUL R22, R22, R23', 'FADD R0, R15, R22'] for statement in ['IADD.64 R4, R4, 0x4','ISETP.GE.U64.AND P1, PT, R4, UR6, PT', 'LDG.E R15, desc[UR8][R12.64]', 'LDG.E R14, desc[UR8][R16.64]', 'LDG.E R19, desc[UR8][R12.64+0x4]', 'LDG.E R18, desc[UR8][R16.64+0x4]', 'LDG.E R21, desc[UR8][R12.64+0x8]', 'LDG.E R20, desc[UR8][R16.64+0x8]', 'LDG.E R23, desc[UR8][R12.64+0xc]', 'LDG.E R22, desc[UR8][R16.64+0xc]']: assert statement in loop assert '".L_x_1":exit0:e -> ".L_x_1":entry:n' in cfg assert 'FADD R0, R0, R13' in instructions and 'FADD R0, R0, R19' in instructions assert '@P0 FMUL R7, R8, R11' in instructions and '@P0 FADD R0, R0, R7' in instructions assert ops.count('STG')==1 # HFMA2 here materializes integer address high-half zero only; it is not an # f32 recurrence operation. Explicitly identify it instead of hiding it. assert [s for s in instructions if 'HFMA2' in s]==['HFMA2 R3, -RZ, RZ, 0, 0'] print('Emitted dataflow/CFG: row guard, positive-zero accumulator, u64 column induction, ' 'ascending four/two/one column additions and final single store: PASS') print('HFMA2 R3 zero materialization is address initialization; no FFMA/FMAd in binary32 recurrence') print('Actual emitted instruction counts:',len(instructions),{op:ops.count(op) for op in sorted(set(ops))}) print('Emitted separate FMUL/FADD, no FMA/MAD, guarded memory/control-flow artifact: PASS; ' 'inspect printed operands/loop edges for this pinned target only') PY