From 49191ada77e63f4e3308e2be14cfb46d88228e5c Mon Sep 17 00:00:00 2001 From: nimlgen <138685161+nimlgen@users.noreply.github.com> Date: Tue, 4 Nov 2025 18:56:01 +0800 Subject: [PATCH] roc: install sqtt decoder (#13091) * roc: install? * msg * 0.1.4 --- autogen_stubs.sh | 12 +++++++----- .../install.py => install_sqtt_decoder.py} | 2 +- extra/sqtt/roc.py | 5 ++--- .../runtime/autogen}/rocprof.py | 15 ++++++++++++--- 4 files changed, 22 insertions(+), 12 deletions(-) rename extra/sqtt/{rocprof/install.py => install_sqtt_decoder.py} (92%) rename {extra/sqtt/rocprof => tinygrad/runtime/autogen}/rocprof.py (98%) diff --git a/autogen_stubs.sh b/autogen_stubs.sh index d4d745554d..04b0e0d6d6 100755 --- a/autogen_stubs.sh +++ b/autogen_stubs.sh @@ -432,11 +432,13 @@ generate_sqtt() { $ROCPROF_SRC/include/rocprof_trace_decoder.h \ $ROCPROF_SRC/include/trace_decoder_instrument.h \ $ROCPROF_SRC/include/trace_decoder_types.h \ - -o extra/sqtt/rocprof/rocprof.py - fixup extra/sqtt/rocprof/rocprof.py - sed -i '1s/^/# pylint: skip-file\n/' extra/sqtt/rocprof/rocprof.py - sed -i "s/import ctypes/import ctypes, ctypes.util/g" extra/sqtt/rocprof/rocprof.py - sed -i "s|FunctionFactoryStub()|ctypes.CDLL(ctypes.util.find_library('rocprof-trace-decoder'))|g" extra/sqtt/rocprof/rocprof.py + -o $BASE/rocprof.py + fixup $BASE/rocprof.py + sed -i '1s/^/# pylint: skip-file\n/' $BASE/rocprof.py + sed -i "s/import ctypes/import ctypes, ctypes.util/g" $BASE/rocprof.py + patch_dlopen $BASE/rocprof.py rocprof-trace-decoder "'/usr/local/lib/rocprof-trace-decoder.so'" "'/usr/local/lib/rocprof-trace-decoder.dylib'" + sed -i "s/def _try_dlopen_rocprof-trace-decoder():/def _try_dlopen_rocprof_trace_decoder():/g" $BASE/rocprof.py + sed -i "s|FunctionFactoryStub()|_try_dlopen_rocprof_trace_decoder()|g" $BASE/rocprof.py } generate_webgpu() { diff --git a/extra/sqtt/rocprof/install.py b/extra/sqtt/install_sqtt_decoder.py similarity index 92% rename from extra/sqtt/rocprof/install.py rename to extra/sqtt/install_sqtt_decoder.py index 5243180602..a07a917930 100755 --- a/extra/sqtt/rocprof/install.py +++ b/extra/sqtt/install_sqtt_decoder.py @@ -13,6 +13,6 @@ if __name__ == "__main__": os.chmod(fp, 0o755) os.system(f"sudo {fp} --prefix={fp.parent} --include-subdir") else: - lib = fetch("https://github.com/ROCm/rocprof-trace-decoder/raw/5420409ad0963b2d76450add067b9058493ccbd0/releases/linux_glibc_2_28_x86_64/librocprof-trace-decoder.so", name="librocprof-trace-decoder.so") + lib = fetch("https://github.com/ROCm/rocprof-trace-decoder/raw/43bf0fef74a83c3c25badfc5a09c0bd39ed8c6f9/releases/linux_glibc_2_28_x86_64/librocprof-trace-decoder.so", name="librocprof-trace-decoder.so") shutil.copy2(lib, DEST) print(f"Installed {lib.name} to", DEST) diff --git a/extra/sqtt/roc.py b/extra/sqtt/roc.py index 3bd636f5d0..011e8c73e4 100644 --- a/extra/sqtt/roc.py +++ b/extra/sqtt/roc.py @@ -1,9 +1,8 @@ import ctypes, pathlib, argparse, pickle, re, functools, dataclasses, itertools -from extra.sqtt.rocprof import rocprof from tinygrad.helpers import temp, unwrap, DEBUG from tinygrad.device import ProfileEvent, ProfileDeviceEvent, ProfileProgramEvent from tinygrad.runtime.ops_amd import ProfileSQTTEvent, ProfilePMCEvent -from tinygrad.runtime.autogen import llvm +from tinygrad.runtime.autogen import llvm, rocprof from tinygrad.runtime.support.elf import elf_loader # to pass NULL to callbacks @@ -122,7 +121,7 @@ def decode(profile:list[ProfileEvent]) -> _ROCParseCtx: try: rocprof.rocprof_trace_decoder_parse_data(copy_cb, trace_cb, isa_cb, None) - except Exception as e: print("Error in sqtt decoder:", e) + except AttributeError as e: raise RuntimeError("Failed to find rocprof-trace-decoder. Run ./extra/sqtt/install_sqtt_decoder.py to install") from e return ROCParseCtx if __name__ == "__main__": diff --git a/extra/sqtt/rocprof/rocprof.py b/tinygrad/runtime/autogen/rocprof.py similarity index 98% rename from extra/sqtt/rocprof/rocprof.py rename to tinygrad/runtime/autogen/rocprof.py index a90b86e055..00ecf75dc7 100644 --- a/extra/sqtt/rocprof/rocprof.py +++ b/tinygrad/runtime/autogen/rocprof.py @@ -8,11 +8,20 @@ # LONGDOUBLE_SIZE is: 16 # import ctypes, ctypes.util +PATHS_TO_TRY = [ + '/usr/local/lib/rocprof-trace-decoder.so', + '/usr/local/lib/rocprof-trace-decoder.dylib', +] +def _try_dlopen_rocprof_trace_decoder(): + library = ctypes.util.find_library("rocprof-trace-decoder") + if library: return ctypes.CDLL(library) + for candidate in PATHS_TO_TRY: + try: return ctypes.CDLL(candidate) + except OSError: pass + return None class AsDictMixin: - import sys - if sys.version_info >= (3, 14): _layout_ = 'ms' @classmethod def as_dict(cls, self): result = {} @@ -157,7 +166,7 @@ class FunctionFactoryStub: # You can either re-run clan2py with -l /path/to/library.so # Or manually fix this by comment the ctypes.CDLL loading _libraries = {} -_libraries['FIXME_STUB'] = ctypes.CDLL(ctypes.util.find_library('rocprof-trace-decoder')) # ctypes.CDLL('FIXME_STUB') +_libraries['FIXME_STUB'] = _try_dlopen_rocprof_trace_decoder() # ctypes.CDLL('FIXME_STUB')