From 7ccb5b9e620c0db55f4ab07caa484642e2bb81f5 Mon Sep 17 00:00:00 2001 From: Kumar Saurabh Date: Thu, 28 Aug 2025 16:37:15 -0500 Subject: [PATCH 1/2] Adding compilation with rocprofv3 --- .gitignore | 3 +++ README.md | 6 ++++-- hip_tools.cpp | 23 +++++++++++++++-------- hip_tools.py | 29 +++++++++++++++++++++++++---- roctx_example.py | 17 ++++++++++------- 5 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..162c888 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.so +__pycache__ +*.pftrace diff --git a/README.md b/README.md index 6a7b648..5230124 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,11 @@ Example: Using roctx calls within a Python program Step 1: Compile the hip_code library ``` -module load rocm/6.0.0 +module load rocm make ``` +This will create `libHIPcode.so` which is used in the python code downstream. + Step 2: Run the script to make sure it works ``` @@ -16,7 +18,7 @@ python3 roctx_example.py Step 3: Get the roctx trace using rocprof ``` -rocprof --roctx-trace -d rocprof_output -o rocprof_output/results.csv python3 roctx_example.py + rocprofv3 --marker-trace --output-format pftrace -- python roctx_example.py ``` Step 4: Copy the **rocprof_output/results.json** file to your system and visualize in [Perfetto](https://ui.perfetto.dev/) \ No newline at end of file diff --git a/hip_tools.cpp b/hip_tools.cpp index f0dcc38..fa75ce4 100644 --- a/hip_tools.cpp +++ b/hip_tools.cpp @@ -1,7 +1,7 @@ #include #include -#include -#include + +#include #include #define CHECK(command) { \ @@ -31,16 +31,23 @@ int set_device( int proc_id ) { } -void start_roctracer(){ - roctracer_start(); +void start_roctracer(int tid){ + + roctxProfilerResume(tid); + //roctracer_start(); } -void stop_roctracer(){ - roctracer_stop(); +int get_roctx_tid(){ + auto tid = roctx_thread_id_t{}; + roctxGetThreadId(&tid); + return tid; +} + +void stop_roctracer(int tid){ + roctxProfilerPause(tid); } int roctxr_start( char *c){ - // std::cout << "Starting roctx marker: " << c << std::endl; int id = roctxRangeStart(c); return id; } @@ -58,4 +65,4 @@ void roctxr_pop(){ } -} \ No newline at end of file +} diff --git a/hip_tools.py b/hip_tools.py index 5d1071b..afa548e 100644 --- a/hip_tools.py +++ b/hip_tools.py @@ -1,8 +1,18 @@ encode = lambda s : s.encode('utf-8') +# Needed so that we do not call start when already corresponding id is started +roctx_ids_start = set() +roctx_ids_stop = set() + class HIP_tools: - def __init__(self, hip_lib_path): + + """ + @param {string} hip_lib_path: path to compiled libHIPcode.so + @param {bool} stop_on_it prevents starting of rocprof on initialization. Useful for profiling ML workloads + + """ + def __init__(self, hip_lib_path, stop_on_init = True): from ctypes import cdll, c_int, c_char_p @@ -12,7 +22,7 @@ def __init__(self, hip_lib_path): self._set_device = libhip.set_device self._set_device.argtypes = [ c_int ] self._set_device.resypes = c_int - + self._get_tid = libhip.get_roctx_tid self._start_roctracer = libhip.start_roctracer self._stop_roctracer = libhip.stop_roctracer self._roctxr_push = libhip.roctxr_push @@ -23,16 +33,27 @@ def __init__(self, hip_lib_path): self._roctxr_start.argtypes = [ c_char_p ] self._roctxr_start.resypes = c_int self._roctxr_stop.argtypes = [ c_int ] + if stop_on_init: + self.stop_roctracer() def set_device(self, device_id ): self._set_device(device_id) def start_roctracer(self): - self._start_roctracer() + tid = self._get_tid() + if(tid not in roctx_ids_start): + self._start_roctracer() + roctx_ids_start.add(tid) + roctx_ids_stop.discard(tid) def stop_roctracer(self): - self._stop_roctracer() + tid = self._get_tid() + if(tid not in roctx_ids_stop): + self._stop_roctracer() + roctx_ids_stop.add(tid) + roctx_ids_start.discard(tid) + def start_marker( self, marker_name ): marker_id = self._roctxr_start( encode(marker_name) ) diff --git a/roctx_example.py b/roctx_example.py index 5ca8341..6b93d64 100644 --- a/roctx_example.py +++ b/roctx_example.py @@ -11,7 +11,7 @@ hip_lib_path = f'{work_dir}/libHIPcode.so' # Initialize the roctracer tools -hip_tools = HIP_tools( hip_lib_path ) +hip_tools = HIP_tools( hip_lib_path,True ) if use_torch: if not torch.cuda.is_available(): @@ -25,7 +25,7 @@ print('Setting hip device 0') hip_tools.set_device(0) - +hip_tools.start_roctracer() # Do some fun stuff id_init = hip_tools.start_marker('init') @@ -33,18 +33,21 @@ A = np.random.rand( nx, ny ) B = np.random.rand( nx, ny ) -hip_tools.stop_marker(id_init) - -id_main = hip_tools.start_marker('main') n_iterations = 20 for i in range(n_iterations): - + + #Only profiling even number + if(i%2 == 0): + hip_tools.start_roctracer() + id_iter = hip_tools.start_marker(f'iter_{i}') print( f'iteration: {i}') C = np.matmul(A, B) hip_tools.stop_marker(id_iter) + if(i%2 == 0): + hip_tools.stop_roctracer() + print('Finished successfully') -hip_tools.stop_marker(id_main) \ No newline at end of file From 98eafa86eb08d8c76425dac57624fe88004a2ebc Mon Sep 17 00:00:00 2001 From: Kumar Saurabh Date: Thu, 28 Aug 2025 16:40:12 -0500 Subject: [PATCH 2/2] Minor --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5230124..08cc954 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,4 @@ Step 3: Get the roctx trace using rocprof rocprofv3 --marker-trace --output-format pftrace -- python roctx_example.py ``` -Step 4: Copy the **rocprof_output/results.json** file to your system and visualize in [Perfetto](https://ui.perfetto.dev/) \ No newline at end of file +Step 4: Copy the **pftrace** file to your system and visualize in [Perfetto](https://ui.perfetto.dev/) \ No newline at end of file