From 336ad50663eaf7f378f6f9b3c129d49df756e09f Mon Sep 17 00:00:00 2001 From: Tejas Ramesh Date: Wed, 27 Mar 2024 18:27:42 -0400 Subject: [PATCH 1/3] Updated Analyzer --- triton_viz/analysis.py | 38 ++++++++++++++++++++++++++++++++++++++ triton_viz/interface.py | 9 +++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 triton_viz/analysis.py diff --git a/triton_viz/analysis.py b/triton_viz/analysis.py new file mode 100644 index 0000000..ac8cc7b --- /dev/null +++ b/triton_viz/analysis.py @@ -0,0 +1,38 @@ +from collections import defaultdict +import numpy as np +from .interpreter import record_builder +from .data import (Load,Store,Grid) + +def analyzer(): + launch_data = record_builder.launches[0] + op_type_counts = defaultdict(int) + grid_size = launch_data.grid + total_load_bytes_true = 0 + total_store_bytes_true = 0 + total_load_bytes_attempted = 0 + total_store_bytes_attempted = 0 + tensor_ptr_to_element_size = {tensor.ptr: tensor.element_size for tensor in launch_data.tensors} + for record in launch_data.records: + if isinstance(record,Grid): + pass + else: + op_type_counts[type(record).__name__] += 1 + if isinstance(record, Load): + element_size = tensor_ptr_to_element_size[record.ptr] + mask_true = np.count_nonzero(record.access_masks) + mask_false = np.count_nonzero(np.logical_not(record.access_masks)) + total_load_bytes_true += mask_true * element_size + total_load_bytes_attempted += (mask_true + mask_false) * element_size + elif isinstance(record, Store): + element_size = tensor_ptr_to_element_size[record.ptr] + mask_true = np.count_nonzero(record.access_masks) + mask_false = np.count_nonzero(np.logical_not(record.access_masks)) + total_store_bytes_true += mask_true * element_size + total_store_bytes_attempted += (mask_true + mask_false) * element_size + overall_load_ratio = total_load_bytes_true / total_load_bytes_attempted if total_load_bytes_attempted > 0 else 0 + overall_store_ratio = total_store_bytes_true / total_store_bytes_attempted if total_store_bytes_attempted > 0 else 0 + data = [["Grid Size", tuple(grid_size)]] + data += [[op_type, count] for op_type, count in op_type_counts.items()] + data.append(["Masked Load Ratio", overall_load_ratio]) + data.append(["Masked Store Ratio", overall_store_ratio]) + return data diff --git a/triton_viz/interface.py b/triton_viz/interface.py index 0552c0a..6d8f5c7 100644 --- a/triton_viz/interface.py +++ b/triton_viz/interface.py @@ -1,10 +1,13 @@ import gradio as gr import triton_viz import tempfile +from .analysis import analyzer +import pandas as pd def launch(share=True): cache = {} + analysis_data = analyzer() program_records, tt, failures = triton_viz.collect_grid() m = [0, 0, 0] size = [0, 0] @@ -35,8 +38,10 @@ def launch(share=True): s2 = gr.Slider(0, m[1] - 1, value=0, step=1, label="Program Id 1") s3 = gr.Slider(0, m[2] - 1, value=0, step=1, label="Program Id 2") b1 = gr.Button("Precompute") - gr.Markdown(f"## Program Ids: {tuple(m)}") - + # gr.Markdown(f"## Program Ids: {tuple(m)}") + df = pd.DataFrame(analysis_data, columns=["Metric", "Value"]) + gr.Markdown("## Analysis") + gr.Dataframe(df) if failures: gr.Markdown( show_label=False, From 7b5bcac8fe4b0d918784ce027dd7eba7a3f36d96 Mon Sep 17 00:00:00 2001 From: Tejas Ramesh Date: Wed, 27 Mar 2024 18:36:34 -0400 Subject: [PATCH 2/3] Updated comments --- triton_viz/analysis.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/triton_viz/analysis.py b/triton_viz/analysis.py index ac8cc7b..f60c3c9 100644 --- a/triton_viz/analysis.py +++ b/triton_viz/analysis.py @@ -1,8 +1,10 @@ from collections import defaultdict import numpy as np from .interpreter import record_builder -from .data import (Load,Store,Grid) +from .data import Load, Store, Grid + +# Function to compute metrics in the analysis shown during visualization. def analyzer(): launch_data = record_builder.launches[0] op_type_counts = defaultdict(int) @@ -11,15 +13,17 @@ def analyzer(): total_store_bytes_true = 0 total_load_bytes_attempted = 0 total_store_bytes_attempted = 0 - tensor_ptr_to_element_size = {tensor.ptr: tensor.element_size for tensor in launch_data.tensors} + tensor_ptr_to_element_size = { + tensor.ptr: tensor.element_size for tensor in launch_data.tensors + } for record in launch_data.records: - if isinstance(record,Grid): + if isinstance(record, Grid): pass else: op_type_counts[type(record).__name__] += 1 if isinstance(record, Load): element_size = tensor_ptr_to_element_size[record.ptr] - mask_true = np.count_nonzero(record.access_masks) + mask_true = np.count_nonzero(record.access_masks) mask_false = np.count_nonzero(np.logical_not(record.access_masks)) total_load_bytes_true += mask_true * element_size total_load_bytes_attempted += (mask_true + mask_false) * element_size @@ -29,8 +33,16 @@ def analyzer(): mask_false = np.count_nonzero(np.logical_not(record.access_masks)) total_store_bytes_true += mask_true * element_size total_store_bytes_attempted += (mask_true + mask_false) * element_size - overall_load_ratio = total_load_bytes_true / total_load_bytes_attempted if total_load_bytes_attempted > 0 else 0 - overall_store_ratio = total_store_bytes_true / total_store_bytes_attempted if total_store_bytes_attempted > 0 else 0 + overall_load_ratio = ( + total_load_bytes_true / total_load_bytes_attempted + if total_load_bytes_attempted > 0 + else 0 + ) + overall_store_ratio = ( + total_store_bytes_true / total_store_bytes_attempted + if total_store_bytes_attempted > 0 + else 0 + ) data = [["Grid Size", tuple(grid_size)]] data += [[op_type, count] for op_type, count in op_type_counts.items()] data.append(["Masked Load Ratio", overall_load_ratio]) From a5a24e0fe5773147d8ff2b604f1ee96b2600e383 Mon Sep 17 00:00:00 2001 From: Tejas Ramesh Date: Wed, 27 Mar 2024 21:06:19 -0400 Subject: [PATCH 3/3] Fixed change requests --- triton_viz/analysis.py | 50 ++++++++++++++++++++--------------------- triton_viz/interface.py | 4 ++-- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/triton_viz/analysis.py b/triton_viz/analysis.py index f60c3c9..79cbfb4 100644 --- a/triton_viz/analysis.py +++ b/triton_viz/analysis.py @@ -5,7 +5,7 @@ # Function to compute metrics in the analysis shown during visualization. -def analyzer(): +def analyze_records(): launch_data = record_builder.launches[0] op_type_counts = defaultdict(int) grid_size = launch_data.grid @@ -17,32 +17,30 @@ def analyzer(): tensor.ptr: tensor.element_size for tensor in launch_data.tensors } for record in launch_data.records: - if isinstance(record, Grid): - pass - else: + if not isinstance(record, Grid): op_type_counts[type(record).__name__] += 1 - if isinstance(record, Load): - element_size = tensor_ptr_to_element_size[record.ptr] - mask_true = np.count_nonzero(record.access_masks) - mask_false = np.count_nonzero(np.logical_not(record.access_masks)) - total_load_bytes_true += mask_true * element_size - total_load_bytes_attempted += (mask_true + mask_false) * element_size - elif isinstance(record, Store): - element_size = tensor_ptr_to_element_size[record.ptr] - mask_true = np.count_nonzero(record.access_masks) - mask_false = np.count_nonzero(np.logical_not(record.access_masks)) - total_store_bytes_true += mask_true * element_size - total_store_bytes_attempted += (mask_true + mask_false) * element_size - overall_load_ratio = ( - total_load_bytes_true / total_load_bytes_attempted - if total_load_bytes_attempted > 0 - else 0 - ) - overall_store_ratio = ( - total_store_bytes_true / total_store_bytes_attempted - if total_store_bytes_attempted > 0 - else 0 - ) + if isinstance(record, Load): + element_size = tensor_ptr_to_element_size[record.ptr] + mask_true = np.count_nonzero(record.access_masks) + mask_false = np.count_nonzero(np.logical_not(record.access_masks)) + total_load_bytes_true += mask_true * element_size + total_load_bytes_attempted += (mask_true + mask_false) * element_size + elif isinstance(record, Store): + element_size = tensor_ptr_to_element_size[record.ptr] + mask_true = np.count_nonzero(record.access_masks) + mask_false = np.count_nonzero(np.logical_not(record.access_masks)) + total_store_bytes_true += mask_true * element_size + total_store_bytes_attempted += (mask_true + mask_false) * element_size + overall_load_ratio = ( + total_load_bytes_true / total_load_bytes_attempted + if total_load_bytes_attempted > 0 + else 0 + ) + overall_store_ratio = ( + total_store_bytes_true / total_store_bytes_attempted + if total_store_bytes_attempted > 0 + else 0 + ) data = [["Grid Size", tuple(grid_size)]] data += [[op_type, count] for op_type, count in op_type_counts.items()] data.append(["Masked Load Ratio", overall_load_ratio]) diff --git a/triton_viz/interface.py b/triton_viz/interface.py index 6d8f5c7..327e28b 100644 --- a/triton_viz/interface.py +++ b/triton_viz/interface.py @@ -1,13 +1,13 @@ import gradio as gr import triton_viz import tempfile -from .analysis import analyzer +from .analysis import analyze_records import pandas as pd def launch(share=True): cache = {} - analysis_data = analyzer() + analysis_data = analyze_records() program_records, tt, failures = triton_viz.collect_grid() m = [0, 0, 0] size = [0, 0]