KBench / agent /tools /dump_ir.sh
ZMC2019's picture
Add agent/: the kernel-optimization skill and four subagents
1ab6c33 verified
Raw
History Blame Contribute Delete
2.05 kB
#!/bin/bash
# Extract what the compilers already generated, so you can start from it instead of from scratch.
# ./dump_ir.sh triton python3 my_triton.py # PTX / TTGIR / LLIR / cubin from Triton
# ./dump_ir.sh inductor python3 my_model.py # the Triton that torch.compile generated
# ./dump_ir.sh sass my_kernel.cubin|.so # what actually landed on the machine
set -uo pipefail
MODE=${1:-triton}; shift
case "$MODE" in
triton)
D=${TRITON_DUMP_DIR:-/tmp/triton_dump}; rm -rf "$D"; mkdir -p "$D"
TRITON_KERNEL_DUMP=1 TRITON_DUMP_DIR="$D" TRITON_ALWAYS_COMPILE=1 "$@" || exit $?
echo "== artifacts in $D =="
find "$D" \( -name "*.ptx" -o -name "*.ttgir" -o -name "*.llir" -o -name "*.cubin" \) | head -24
echo
echo "Read the .ttgir for the layouts Triton chose and the .ptx for what it emitted."
echo "Then: ./dump_ir.sh sass <that .cubin> -- the SASS that actually ran."
;;
inductor)
TORCH_LOGS=output_code TORCHINDUCTOR_MAX_AUTOTUNE=1 "$@" 2>&1 | tee /tmp/inductor_out.log
echo "== generated Triton kernels =="; grep -n "def triton_" /tmp/inductor_out.log | head -20
echo "Inductor's autotuned choice is a strong starting point -- read it before writing your own."
;;
sass)
F=${1:-}
[ -n "$F" ] && [ -f "$F" ] || { echo "usage: dump_ir.sh sass <file.cubin|.so> (no such file: '${F}')" >&2; exit 1; }
cuobjdump -sass "$F" > /tmp/kernel.sass || { echo "cuobjdump failed on $F" >&2; exit 1; }
[ -s /tmp/kernel.sass ] || { echo "no SASS in $F (not a cubin / no device code?)" >&2; exit 1; }
head -40 /tmp/kernel.sass; echo "..."
echo "== register spills (LDL/STL) =="; grep -cE "\bLDL\b|\bSTL\b" /tmp/kernel.sass || true
echo "== tensor-core instructions =="; grep -coE "HMMA|IMMA|QGMMA|wgmma" /tmp/kernel.sass || true
echo "full listing: /tmp/kernel.sass"
echo "Zero MMA instructions where you expected tensor cores means the MMA never issued."
;;
*) echo "usage: dump_ir.sh {triton|inductor|sass} ..." >&2; exit 1 ;;
esac