tritonparse 0.3.1.dev20251028071524__py3-none-any.whl → 0.3.1.dev20251030071508__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tritonparse might be problematic. Click here for more details.

tritonparse/cli.py CHANGED
@@ -68,12 +68,20 @@ def main():
68
68
  }
69
69
  unified_parse(**parse_args)
70
70
  elif args.func == "reproduce":
71
+ replacer = None
72
+ if args.use_fbcode:
73
+ from tritonparse.fb.reproducer.replacer import FBCodePlaceholderReplacer
74
+
75
+ replacer = FBCodePlaceholderReplacer()
76
+ print(f"Using FBCode placeholder replacer for template: {args.template}")
77
+
71
78
  reproduce(
72
79
  input_path=args.input,
73
80
  line_index=args.line - 1, # Convert 1-based line number to 0-based index
74
81
  out_dir=args.out_dir,
75
82
  template=args.template,
76
83
  kernel_import=args.kernel_import,
84
+ replacer=replacer,
77
85
  )
78
86
  else:
79
87
  raise RuntimeError(f"Unknown command: {args.func}")
@@ -46,20 +46,349 @@ def process_amd_gcn_bufferops(
46
46
  return process_amd_bufferop(ir_content, io_keys)
47
47
 
48
48
 
49
+ def find_loop_bounds(ir_content: str) -> list[tuple[int, int]]:
50
+ """
51
+ Find the bounds of all scf.for loops in the IR content.
52
+ These are the only candidates for Software Pipelining (SWP).
53
+
54
+ A loop starts with 'scf.for' and ends when its closing brace '}' is found.
55
+ Brace counts are tracked to determine when each loop closes.
56
+
57
+ Args:
58
+ ir_content: The IR content as a string.
59
+
60
+ Returns:
61
+ A list of tuples (start_line, end_line) for each scf.for loop found.
62
+ Line numbers are 0-indexed.
63
+ """
64
+ if not ir_content:
65
+ return []
66
+
67
+ loop_bounds: list[tuple[int, int]] = []
68
+ lines = ir_content.split("\n")
69
+
70
+ # Stack to track loop starts and their brace counts
71
+ # Each entry is (start_line, brace_count_at_start)
72
+ loop_stack: list[tuple[int, int]] = []
73
+ current_brace_count = 0
74
+
75
+ for line_idx, line in enumerate(lines):
76
+ # Check if this line starts a new scf.for loop
77
+ if "scf.for" in line:
78
+ loop_stack.append((line_idx, current_brace_count))
79
+
80
+ # Count braces on this line
81
+ for char in line:
82
+ if char == "{":
83
+ current_brace_count += 1
84
+ elif char == "}":
85
+ current_brace_count -= 1
86
+
87
+ # Check if we've closed any loops
88
+ while loop_stack and current_brace_count <= loop_stack[-1][1]:
89
+ start_line, _start_brace_count = loop_stack.pop()
90
+ # The loop ends at this line
91
+ loop_bounds.append((start_line, line_idx))
92
+
93
+ return loop_bounds
94
+
95
+
96
+ def find_inner_loop_bounds(ir_content: str) -> list[tuple[int, int]]:
97
+ """
98
+ Find the bounds of inner scf.for loops (loops without nested loops inside).
99
+
100
+ Inner loops are the primary candidates for Software Pipelining (SWP) as they
101
+ represent the innermost computation that can be optimized.
102
+
103
+ Args:
104
+ ir_content: The IR content as a string.
105
+
106
+ Returns:
107
+ A list of tuples (start_line, end_line) for each inner scf.for loop found.
108
+ Line numbers are 0-indexed.
109
+ """
110
+ all_loops = find_loop_bounds(ir_content)
111
+
112
+ if not all_loops:
113
+ return []
114
+
115
+ # Filter to keep only inner loops (loops that don't contain other loops)
116
+ inner_loops: list[tuple[int, int]] = []
117
+
118
+ for i, (start_i, end_i) in enumerate(all_loops):
119
+ # Check if any other loop is nested inside this loop
120
+ has_nested_loop = False
121
+ for j, (start_j, end_j) in enumerate(all_loops):
122
+ if i != j:
123
+ # Check if loop j is nested inside loop i
124
+ if start_i < start_j and end_j < end_i:
125
+ has_nested_loop = True
126
+ break
127
+
128
+ # If no nested loops found, this is an inner loop
129
+ if not has_nested_loop:
130
+ inner_loops.append((start_i, end_i))
131
+
132
+ return inner_loops
133
+
134
+
135
+ def find_loop_pipelining(
136
+ ttir_content: str,
137
+ ttgir_content: str,
138
+ ttir_loop_start: int,
139
+ ttir_loop_end: int,
140
+ loop_index: int,
141
+ ttir_to_ttgir_mapping: dict[str, dict],
142
+ ttgir_to_source_mapping: dict[str, dict],
143
+ python_source_content: str | None,
144
+ python_source_start_line: int,
145
+ ) -> dict[str, list[str]]:
146
+ """
147
+ Find pipelining information for a specific loop by identifying tt.load and tt.dot operations
148
+ in TTIR and mapping them to their corresponding operations in the original Python source code.
149
+
150
+ For each tt.load or tt.dot operation found in the TTIR loop, this function uses source
151
+ mappings to find the corresponding operations in TTGIR, then maps them back to the original
152
+ Python source code. Operations are categorized into three sections:
153
+ - prologue: Operations that appear before the loop body
154
+ - loop_body: Operations that appear within the loop body
155
+ - epilogue: Operations that appear after the loop body
156
+
157
+ Operations are merged together (both loads and dots) and sorted in program order
158
+ within each section.
159
+
160
+ Args:
161
+ ttir_content: The TTIR content as a string.
162
+ ttgir_content: The TTGIR content as a string.
163
+ ttir_loop_start: The starting line number of the loop in TTIR (0-indexed).
164
+ ttir_loop_end: The ending line number of the loop in TTIR (0-indexed).
165
+ ttir_to_ttgir_mapping: Source mapping from TTIR lines to TTGIR lines.
166
+ ttgir_to_source_mapping: Source mapping from TTGIR lines to original Python source.
167
+ python_source_content: The original Python source code content.
168
+
169
+ Returns:
170
+ A dictionary containing:
171
+ - "prologue": List of Python source line strings in program order
172
+ - "loop_body": List of Python source line strings in program order
173
+ - "epilogue": List of Python source line strings in program order
174
+ """
175
+ if not ttir_content or not ttgir_content:
176
+ return {
177
+ "prologue": [],
178
+ "loop_body": [],
179
+ "epilogue": [],
180
+ }
181
+
182
+ ttir_lines = ttir_content.split("\n")
183
+ ttgir_lines = ttgir_content.split("\n")
184
+ python_lines = python_source_content.split("\n") if python_source_content else []
185
+
186
+ def apply_trailing_space(op: str) -> str:
187
+ """
188
+ Add a trailing space to all ops to avoid false positives like
189
+ warp_group_dot and warp_group_dot_wait.
190
+ """
191
+ return op + " "
192
+
193
+ # Step 1: Find tt.load and tt.dot operations in TTIR loop
194
+ ttir_pipeline_lines: list[int] = []
195
+ pipeline_tt_ops = ["tt.load", "tt.dot"]
196
+ pipeline_tt_ops = [apply_trailing_space(op) for op in pipeline_tt_ops]
197
+ pipeline_ttgir_ops = [
198
+ "tt.load",
199
+ "tt.dot",
200
+ "async_copy_global_to_local",
201
+ "warp_group_dot",
202
+ ]
203
+ pipeline_ttgir_ops = [apply_trailing_space(op) for op in pipeline_ttgir_ops]
204
+ for line_idx in range(ttir_loop_start, min(ttir_loop_end + 1, len(ttir_lines))):
205
+ line = ttir_lines[line_idx]
206
+ for op in pipeline_tt_ops:
207
+ if op in line:
208
+ ttir_pipeline_lines.append(line_idx)
209
+ break
210
+
211
+ # Step 2: Find the corresponding loop in TTGIR using source mappings
212
+ # Map the TTIR loop bounds to TTGIR using source mappings
213
+ ttgir_inner_loops = find_inner_loop_bounds(ttgir_content)
214
+
215
+ if not ttgir_inner_loops:
216
+ # No loop found in TTGIR, return empty results
217
+ return {
218
+ "prologue": [],
219
+ "loop_body": [],
220
+ "epilogue": [],
221
+ }
222
+
223
+ # Use the first inner loop as the reference
224
+ # TODO: Implement more sophisticated mapping logic to match TTIR loops to TTGIR loops
225
+ ttgir_loop_start, ttgir_loop_end = ttgir_inner_loops[loop_index]
226
+
227
+ # Step 3: Map TTIR operations to TTGIR operations using source mappings
228
+ # and categorize them by their position relative to the TTGIR loop
229
+ # Store as (line_number, source_line) to maintain order before extracting just the source
230
+ prologue_ops: list[tuple[int, str]] = []
231
+ loop_body_ops: list[tuple[int, str]] = []
232
+ epilogue_ops: list[tuple[int, str]] = []
233
+
234
+ for ttir_line in ttir_pipeline_lines:
235
+ # Convert 0-indexed line to 1-indexed string key for mapping lookup
236
+ ttir_line_key = str(ttir_line + 1)
237
+
238
+ # Get the corresponding TTGIR lines from the source mapping
239
+ if ttir_line_key in ttir_to_ttgir_mapping:
240
+ ttgir_lines_list = ttir_to_ttgir_mapping[ttir_line_key].get(
241
+ "ttgir_lines", []
242
+ )
243
+
244
+ # For each mapped TTGIR line, categorize it
245
+ for ttgir_line in ttgir_lines_list:
246
+ # Convert back to 0-indexed
247
+ ttgir_line_idx = ttgir_line - 1
248
+
249
+ # Get the actual TTGIR line content to check if it's relevant
250
+ if ttgir_line_idx < len(ttgir_lines):
251
+ ttgir_source_line = ttgir_lines[ttgir_line_idx].strip()
252
+
253
+ # Only keep mappings to the "compute" op.
254
+ if any(op in ttgir_source_line for op in pipeline_ttgir_ops):
255
+ # Map TTGIR line back to Python source
256
+ ttgir_line_key = str(ttgir_line)
257
+ python_source_line = ttgir_source_line # Default to TTGIR line
258
+
259
+ if ttgir_line_key in ttgir_to_source_mapping:
260
+ source_info = ttgir_to_source_mapping[ttgir_line_key]
261
+ python_line_num = source_info.get("line")
262
+
263
+ if python_line_num and python_lines:
264
+ # Account for the offset: the Python source may not start at line 1
265
+ # python_line_num is the absolute line number in the original file
266
+ # python_source_start_line is where the extracted code starts
267
+ # So we need to subtract the offset to get the index in our python_lines array
268
+ python_line_idx = (
269
+ python_line_num - python_source_start_line
270
+ )
271
+ if 0 <= python_line_idx < len(python_lines):
272
+ python_source_line = python_lines[
273
+ python_line_idx
274
+ ].strip()
275
+
276
+ if ttgir_line_idx < ttgir_loop_start:
277
+ prologue_ops.append((ttgir_line_idx, python_source_line))
278
+ elif ttgir_loop_start <= ttgir_line_idx <= ttgir_loop_end:
279
+ loop_body_ops.append((ttgir_line_idx, python_source_line))
280
+ else:
281
+ epilogue_ops.append((ttgir_line_idx, python_source_line))
282
+
283
+ # Step 4: Sort each section by line number to maintain program order
284
+ prologue_ops.sort(key=lambda x: x[0])
285
+ loop_body_ops.sort(key=lambda x: x[0])
286
+ epilogue_ops.sort(key=lambda x: x[0])
287
+
288
+ # Extract just the source lines (without line numbers)
289
+ prologue_lines = [line for _, line in prologue_ops]
290
+ loop_body_lines = [line for _, line in loop_body_ops]
291
+ epilogue_lines = [line for _, line in epilogue_ops]
292
+
293
+ # Log the pipelining results
294
+ logger.info(
295
+ f"Loop pipelining results (TTIR lines {ttir_loop_start}-{ttir_loop_end}):"
296
+ )
297
+ logger.info(f" Prologue ({len(prologue_lines)} ops):")
298
+ for line in prologue_lines:
299
+ logger.info(f" {line}")
300
+ logger.info(f" Loop Body ({len(loop_body_lines)} ops):")
301
+ for line in loop_body_lines:
302
+ logger.info(f" {line}")
303
+ logger.info(f" Epilogue ({len(epilogue_lines)} ops):")
304
+ for line in epilogue_lines:
305
+ logger.info(f" {line}")
306
+
307
+ return {
308
+ "prologue": prologue_lines,
309
+ "loop_body": loop_body_lines,
310
+ "epilogue": epilogue_lines,
311
+ }
312
+
313
+
314
+ def generate_loop_schedule(
315
+ ttir_key: str,
316
+ ttgir_key: str,
317
+ file_content: dict[str, str],
318
+ file_path: dict[str, str],
319
+ source_mappings: dict[str, dict],
320
+ python_source_content: str | None,
321
+ python_source_start_line: int,
322
+ ) -> list[dict]:
323
+ """
324
+ Generate loop schedule information by finding inner scf.for loops in TTIR
325
+ and analyzing their pipelining potential using source mappings.
326
+
327
+ Only inner loops (loops without nested loops) are considered as they are
328
+ the primary candidates for Software Pipelining (SWP).
329
+
330
+ Args:
331
+ ttir_key: Key for the TTIR file.
332
+ ttgir_key: Key for the TTGIR file.
333
+ file_content: Dictionary mapping file keys to content.
334
+ file_path: Dictionary mapping file keys to file paths.
335
+ source_mappings: Dictionary containing source mappings between IR stages.
336
+ python_source_content: The original Python source code content.
337
+ python_source_start_line: The starting line number of the Python source in the original file.
338
+
339
+ Returns:
340
+ A list of dictionaries, each containing:
341
+ - "loop_bounds": Tuple of (start_line, end_line) for the loop in TTIR
342
+ - "pipelining": Dictionary with Python source lines for operations
343
+ """
344
+ ttir_content = load_ir_contents(ttir_key, file_content, file_path)
345
+ ttgir_content = load_ir_contents(ttgir_key, file_content, file_path)
346
+
347
+ # Get the TTIR to TTGIR mapping and TTGIR to source mapping
348
+ ttir_to_ttgir_mapping = source_mappings.get("ttir", {})
349
+ ttgir_to_source_mapping = source_mappings.get("ttgir", {})
350
+
351
+ # Find only inner loops (loops without nested loops inside)
352
+ inner_loop_bounds = find_inner_loop_bounds(ttir_content)
353
+ # TODO: Fix loop mapping with multiple loops.
354
+ inner_loop_bounds = inner_loop_bounds[:1]
355
+
356
+ # For each inner loop, find pipelining information
357
+ loop_schedules = []
358
+ for i, (loop_start, loop_end) in enumerate(inner_loop_bounds):
359
+ pipelining_info = find_loop_pipelining(
360
+ ttir_content,
361
+ ttgir_content,
362
+ loop_start,
363
+ loop_end,
364
+ i,
365
+ ttir_to_ttgir_mapping,
366
+ ttgir_to_source_mapping,
367
+ python_source_content,
368
+ python_source_start_line,
369
+ )
370
+ loop_schedules.append(pipelining_info)
371
+
372
+ return loop_schedules
373
+
374
+
49
375
  def _generate_ir_analysis(entry: str):
50
376
  payload = entry.setdefault("payload", {})
51
377
  file_content = payload.get("file_content", {})
52
378
  file_path = payload.get("file_path", {})
379
+ source_mappings = payload.get("source_mappings", {})
53
380
 
54
381
  # Find the IR file keys
382
+ ttir_key = next((k for k in file_content if k.endswith(".ttir")), None)
55
383
  ttgir_key = next((k for k in file_content if k.endswith(".ttgir")), None)
56
384
  amdgcn_key = next((k for k in file_content if k.endswith(".amdgcn")), None)
57
385
  # Skip if no IR files found
58
- if not (ttgir_key or amdgcn_key):
59
- logger.debug("No AMD IR found")
386
+ if not (ttir_key or ttgir_key or amdgcn_key):
387
+ logger.debug("No IR found")
60
388
  return {}
61
389
  ir_analysis = {}
62
- if amdgcn_key:
390
+ if amdgcn_key and ttgir_key:
391
+ # Add BufferOps information
63
392
  ttgir_bufferops_info = process_amd_ttgir_bufferops(
64
393
  ttgir_key, file_content, file_path
65
394
  )
@@ -74,4 +403,25 @@ def _generate_ir_analysis(entry: str):
74
403
  io_counts["amd_gcn_bufferops_count"] = gcn_bufferops_info
75
404
  if io_counts:
76
405
  ir_analysis["io_counts"] = io_counts
406
+ if ttir_key and ttgir_key:
407
+ # Get Python source content and start line if available
408
+ python_source_content = None
409
+ python_source_start_line = 1 # Default to 1 if not available
410
+ python_source_info = payload.get("python_source")
411
+ if python_source_info:
412
+ python_source_content = python_source_info.get("code")
413
+ python_source_start_line = python_source_info.get("start_line", 1)
414
+
415
+ # Add loop schedule information
416
+ loop_schedule = generate_loop_schedule(
417
+ ttir_key,
418
+ ttgir_key,
419
+ file_content,
420
+ file_path,
421
+ source_mappings,
422
+ python_source_content,
423
+ python_source_start_line,
424
+ )
425
+ if loop_schedule:
426
+ ir_analysis["loop_schedules"] = loop_schedule
77
427
  return ir_analysis
tritonparse/ir_parser.py CHANGED
@@ -44,6 +44,14 @@ ALIAS_WITH_NAME_PATTERN = re.compile(
44
44
  # Example: #loc20 = loc(#loc16)
45
45
  ALIAS_SIMPLE_PATTERN = re.compile(r"#loc(\d+)\s*=\s*loc\(\s*#loc(\d*)\s*\)")
46
46
 
47
+ # Callsite loc definitions in TTIR/TTGIR
48
+ # Example: #loc220 = loc(callsite(#loc57 at #loc190))
49
+ # Captures: loc_id, callee_loc_id, caller_loc_id
50
+ # Note: Uses (\d*) to match optional numbers (for bare #loc references)
51
+ CALLSITE_PATTERN = re.compile(
52
+ r"#loc(\d+)\s*=\s*loc\(\s*callsite\(\s*#loc(\d*)\s+at\s+#loc(\d*)\s*\)\s*\)"
53
+ )
54
+
47
55
 
48
56
  def extract_loc_definitions(ir_content: str) -> Dict[str, Dict[str, Any]]:
49
57
  """
@@ -141,6 +149,50 @@ def extract_loc_definitions(ir_content: str) -> Dict[str, Dict[str, Any]]:
141
149
  for alias_id, target_id in alias_map.items():
142
150
  if alias_id not in locations:
143
151
  resolve_alias(alias_id)
152
+
153
+ # Collect callsite definitions
154
+ callsite_defs = []
155
+ for i, line in enumerate(ir_content.split("\n"), start=1):
156
+ if m := CALLSITE_PATTERN.search(line):
157
+ loc_id, callee_id, caller_id = m.groups()
158
+ # Empty strings map to main loc key ""
159
+ callsite_defs.append((loc_id, callee_id or "", caller_id or "", i))
160
+
161
+ # Resolve callsite definitions
162
+ # A callsite inherits the location from its callee (the code being called)
163
+ # and stores a reference to its caller (the code doing the calling)
164
+ for loc_id, callee_id, caller_id, def_line in callsite_defs:
165
+ if loc_id not in locations: # Avoid overwriting existing definitions
166
+ if callee_id in locations:
167
+ # Inherit location info from callee
168
+ callee_info = locations[callee_id]
169
+ locations[loc_id] = {
170
+ "file": callee_info["file"],
171
+ "line": callee_info["line"],
172
+ "column": callee_info["column"],
173
+ "def_line": def_line,
174
+ "is_callsite": True,
175
+ "callsite_callee": callee_id,
176
+ "callsite_caller": caller_id,
177
+ }
178
+ else:
179
+ logger.warning(
180
+ f"Callsite #loc{loc_id} references undefined callee #loc{callee_id}"
181
+ )
182
+ # Note: We don't add this callsite to locations since callee is missing
183
+
184
+ # Verify caller references (warning only, don't block)
185
+ for loc_id, _callee_id, caller_id, _def_line in callsite_defs:
186
+ if loc_id in locations and caller_id and caller_id not in locations:
187
+ logger.warning(
188
+ f"Callsite #loc{loc_id} references undefined caller #loc{caller_id}"
189
+ )
190
+
191
+ # Attach definition line and alias metadata
192
+ for k, v in def_line_map.items():
193
+ if k in locations:
194
+ locations[k]["def_line"] = v
195
+ for alias_id, target_id in alias_map.items():
144
196
  if alias_id in locations:
145
197
  locations[alias_id]["alias_of"] = target_id
146
198
  if alias_id in alias_name_map:
@@ -46,3 +46,8 @@ def _add_reproducer_args(parser: argparse.ArgumentParser) -> None:
46
46
  "Defaults to 'default'."
47
47
  ),
48
48
  )
49
+ parser.add_argument(
50
+ "--use-fbcode",
51
+ action="store_true",
52
+ help=("Use fbcode to setup repro environment."),
53
+ )
@@ -23,7 +23,7 @@ def reproduce(
23
23
  template: str,
24
24
  replacer: Optional[PlaceholderReplacer] = None,
25
25
  kernel_import: KernelImportMode = KernelImportMode.DEFAULT,
26
- ) -> dict[str, Path]:
26
+ ) -> dict[str, str]:
27
27
  """
28
28
  Generate a reproducer script from NDJSON trace file.
29
29
 
@@ -45,7 +45,7 @@ def reproduce(
45
45
  f"Built context bundle for kernel: {context_bundle.kernel_info.function_name}"
46
46
  )
47
47
  out_py_path, temp_json_path = determine_output_paths(
48
- out_dir, context_bundle.kernel_info.function_name
48
+ out_dir, context_bundle.kernel_info.function_name, template
49
49
  )
50
50
  save_prettified_json(context_bundle.raw_launch_event, temp_json_path)
51
51
 
@@ -76,21 +76,39 @@ class DefaultPlaceholderReplacer(PlaceholderReplacer):
76
76
  - # {{KERNEL_INVOCATION_PLACEHOLDER}}: Replaced with kernel invocation code
77
77
  """
78
78
 
79
+ KERNEL_NAME_PLACEHOLDER = "{{KERNEL_NAME_PLACEHOLDER}}"
80
+ JSON_FILE_NAME_PLACEHOLDER = "{{JSON_FILE_NAME_PLACEHOLDER}}"
81
+ IR_OVERRIDE_SETUP_PLACEHOLDER = "# {{IR_OVERRIDE_SETUP_PLACEHOLDER}}"
82
+ KERNEL_SYSPATH_PLACEHOLDER = "# {{KERNEL_SYSPATH_PLACEHOLDER}}"
83
+ KERNEL_IMPORT_PLACEHOLDER = "# {{KERNEL_IMPORT_PLACEHOLDER}}"
84
+ UTILITY_FUNCTIONS_PLACEHOLDER = "# {{UTILITY_FUNCTIONS_PLACEHOLDER}}"
85
+ KERNEL_INVOCATION_PLACEHOLDER = "# {{KERNEL_INVOCATION_PLACEHOLDER}}"
86
+
79
87
  def __init__(self):
80
88
  super().__init__()
81
89
  # Register all default handlers
82
- self.register("{{JSON_FILE_NAME_PLACEHOLDER}}", self._replace_json_filename)
90
+ self.register(self.JSON_FILE_NAME_PLACEHOLDER, self._replace_json_filename)
83
91
  self.register(
84
- "# {{IR_OVERRIDE_SETUP_PLACEHOLDER}}", self._replace_ir_override_setup
92
+ self.IR_OVERRIDE_SETUP_PLACEHOLDER, self._replace_ir_override_setup
85
93
  )
86
- self.register("# {{KERNEL_SYSPATH_PLACEHOLDER}}", self._replace_kernel_syspath)
87
- self.register("# {{KERNEL_IMPORT_PLACEHOLDER}}", self._replace_kernel_import)
94
+ self.register(self.KERNEL_SYSPATH_PLACEHOLDER, self._replace_kernel_syspath)
95
+ self.register(self.KERNEL_IMPORT_PLACEHOLDER, self._replace_kernel_import)
88
96
  self.register(
89
- "# {{UTILITY_FUNCTIONS_PLACEHOLDER}}", self._replace_utility_functions
97
+ self.UTILITY_FUNCTIONS_PLACEHOLDER, self._replace_utility_functions
90
98
  )
91
99
  self.register(
92
- "# {{KERNEL_INVOCATION_PLACEHOLDER}}", self._replace_kernel_invocation
100
+ self.KERNEL_INVOCATION_PLACEHOLDER, self._replace_kernel_invocation
93
101
  )
102
+ self.register(self.KERNEL_NAME_PLACEHOLDER, self._replace_kernel_name)
103
+
104
+ def _replace_kernel_name(
105
+ self, code: str, context_bundle: ContextBundle, **kwargs
106
+ ) -> str:
107
+ """Replace the kernel name placeholder."""
108
+ kernel_name = context_bundle.kernel_info.function_name
109
+ if not kernel_name:
110
+ raise ValueError("Kernel function name is not available")
111
+ return code.replace(self.KERNEL_NAME_PLACEHOLDER, kernel_name)
94
112
 
95
113
  def _replace_json_filename(
96
114
  self, code: str, context_bundle: ContextBundle, **kwargs
@@ -99,7 +117,7 @@ class DefaultPlaceholderReplacer(PlaceholderReplacer):
99
117
  temp_json_path = kwargs.get("temp_json_path")
100
118
  if temp_json_path is None:
101
119
  raise ValueError("temp_json_path is required for JSON filename replacement")
102
- return code.replace("{{JSON_FILE_NAME_PLACEHOLDER}}", temp_json_path.name)
120
+ return code.replace(self.JSON_FILE_NAME_PLACEHOLDER, temp_json_path.name)
103
121
 
104
122
  def _replace_ir_override_setup(
105
123
  self, code: str, context_bundle: ContextBundle, **kwargs
@@ -108,7 +126,7 @@ class DefaultPlaceholderReplacer(PlaceholderReplacer):
108
126
  kernel_import = kwargs.get("kernel_import", KernelImportMode.DEFAULT)
109
127
 
110
128
  if kernel_import != KernelImportMode.OVERRIDE_TTIR:
111
- return code.replace("# {{IR_OVERRIDE_SETUP_PLACEHOLDER}}", "")
129
+ return code.replace(self.IR_OVERRIDE_SETUP_PLACEHOLDER, "")
112
130
 
113
131
  comp_json_filename = kwargs.get("comp_json_filename")
114
132
  if not comp_json_filename:
@@ -158,7 +176,7 @@ _original_autotune = triton.autotune
158
176
  triton.autotune = _patched_autotune
159
177
  '''
160
178
 
161
- return code.replace("# {{IR_OVERRIDE_SETUP_PLACEHOLDER}}", setup_code)
179
+ return code.replace(self.IR_OVERRIDE_SETUP_PLACEHOLDER, setup_code)
162
180
 
163
181
  def _replace_kernel_syspath(
164
182
  self, code: str, context_bundle: ContextBundle, **kwargs
@@ -168,15 +186,15 @@ triton.autotune = _patched_autotune
168
186
 
169
187
  if kernel_import == KernelImportMode.DEFAULT:
170
188
  sys_stmt, _ = _generate_import_statements(context_bundle.kernel_info)
171
- return code.replace("# {{KERNEL_SYSPATH_PLACEHOLDER}}", sys_stmt)
189
+ return code.replace(self.KERNEL_SYSPATH_PLACEHOLDER, sys_stmt)
172
190
  elif kernel_import == KernelImportMode.COPY:
173
191
  comment = (
174
192
  "# Kernel sys.path setup skipped - kernel source code embedded below"
175
193
  )
176
- return code.replace("# {{KERNEL_SYSPATH_PLACEHOLDER}}", comment)
194
+ return code.replace(self.KERNEL_SYSPATH_PLACEHOLDER, comment)
177
195
  elif kernel_import == KernelImportMode.OVERRIDE_TTIR:
178
196
  comment = "# Kernel sys.path setup skipped - using IR override mode"
179
- return code.replace("# {{KERNEL_SYSPATH_PLACEHOLDER}}", comment)
197
+ return code.replace(self.KERNEL_SYSPATH_PLACEHOLDER, comment)
180
198
  else:
181
199
  raise ValueError(f"Unknown kernel_import mode: {kernel_import}")
182
200
 
@@ -190,7 +208,7 @@ triton.autotune = _patched_autotune
190
208
  _, import_statement = _generate_import_statements(
191
209
  context_bundle.kernel_info
192
210
  )
193
- return code.replace("# {{KERNEL_IMPORT_PLACEHOLDER}}", import_statement)
211
+ return code.replace(self.KERNEL_IMPORT_PLACEHOLDER, import_statement)
194
212
  elif kernel_import == KernelImportMode.COPY:
195
213
  source_code = context_bundle.kernel_info.source_code
196
214
  func_name = context_bundle.kernel_info.function_name
@@ -216,10 +234,10 @@ triton.autotune = _patched_autotune
216
234
  embedded_code += "\n" + source_code
217
235
  embedded_code += f"\n\n# Use kernel function directly\nimported_kernel_function = {func_name}"
218
236
 
219
- return code.replace("# {{KERNEL_IMPORT_PLACEHOLDER}}", embedded_code)
237
+ return code.replace(self.KERNEL_IMPORT_PLACEHOLDER, embedded_code)
220
238
  elif kernel_import == KernelImportMode.OVERRIDE_TTIR:
221
239
  comment = "# Kernel import skipped - using IR override mode with TTIR"
222
- return code.replace("# {{KERNEL_IMPORT_PLACEHOLDER}}", comment)
240
+ return code.replace(self.KERNEL_IMPORT_PLACEHOLDER, comment)
223
241
  else:
224
242
  raise ValueError(f"Unknown kernel_import mode: {kernel_import}")
225
243
 
@@ -228,7 +246,7 @@ triton.autotune = _patched_autotune
228
246
  ) -> str:
229
247
  """Replace the utility functions placeholder with extracted functions."""
230
248
  utility_code = extract_utility_functions()
231
- return code.replace("# {{UTILITY_FUNCTIONS_PLACEHOLDER}}", utility_code)
249
+ return code.replace(self.UTILITY_FUNCTIONS_PLACEHOLDER, utility_code)
232
250
 
233
251
  def _replace_kernel_invocation(
234
252
  self, code: str, context_bundle: ContextBundle, **kwargs
@@ -237,4 +255,4 @@ triton.autotune = _patched_autotune
237
255
  source_code = context_bundle.kernel_info.source_code
238
256
  pos_args, kw_args = _parse_kernel_signature(source_code)
239
257
  invocation_snippet = _generate_invocation_snippet(pos_args, kw_args)
240
- return code.replace("# {{KERNEL_INVOCATION_PLACEHOLDER}}", invocation_snippet)
258
+ return code.replace(self.KERNEL_INVOCATION_PLACEHOLDER, invocation_snippet)
@@ -14,7 +14,7 @@ import torch
14
14
  # {{UTILITY_FUNCTIONS_PLACEHOLDER}}
15
15
 
16
16
 
17
- if __name__ == "__main__":
17
+ def launch_kernel():
18
18
  script_dir = Path(__file__).resolve().parent # noqa: F821
19
19
  json_file = script_dir / "{{JSON_FILE_NAME_PLACEHOLDER}}"
20
20
  grid, args_dict = create_args_from_json_file(str(json_file)) # noqa: F821
@@ -28,3 +28,7 @@ if __name__ == "__main__":
28
28
 
29
29
  torch.cuda.synchronize()
30
30
  print("Kernel execution finished.")
31
+
32
+
33
+ if __name__ == "__main__":
34
+ launch_kernel()
@@ -0,0 +1,103 @@
1
+ # (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2
+
3
+ from pathlib import Path
4
+ from typing import Any, Callable, Dict, Optional, Tuple
5
+
6
+ import torch
7
+ from tritonbench.utils.triton_op import (
8
+ BenchmarkOperator,
9
+ register_benchmark,
10
+ REGISTERED_X_VALS,
11
+ )
12
+
13
+
14
+ imported_kernel_function: Optional[Callable[[Tuple[int], Dict[str, Any]], None]] = None
15
+
16
+ # {{IR_OVERRIDE_SETUP_PLACEHOLDER}}
17
+
18
+ # {{KERNEL_SYSPATH_PLACEHOLDER}}
19
+
20
+ # {{KERNEL_IMPORT_PLACEHOLDER}}
21
+
22
+ # {{UTILITY_FUNCTIONS_PLACEHOLDER}}
23
+
24
+ assert imported_kernel_function is not None, "imported_kernel_function is missing"
25
+
26
+ KERNEL_NAME = "{{KERNEL_NAME_PLACEHOLDER}}"
27
+ REPRO_CONTEXT_FILE_NAME = "{{JSON_FILE_NAME_PLACEHOLDER}}"
28
+
29
+
30
+ def _get_launch_kernel_args() -> Tuple[Tuple[int], Dict[str, Any]]:
31
+ script_dir = Path(__file__).resolve().parent # noqa: F821
32
+ json_file = script_dir / REPRO_CONTEXT_FILE_NAME
33
+
34
+ grid, args_dict = create_args_from_json_file(json_file) # noqa: F821, F841
35
+
36
+ print("Recorded kernel arguments dictionary:")
37
+ for name, arg in args_dict.items():
38
+ if isinstance(arg, torch.Tensor):
39
+ print(
40
+ f" {name}: Tensor: {arg.shape} {arg.dtype} stride: {arg.stride()}, is_contiguous: {arg.is_contiguous()}"
41
+ )
42
+ else:
43
+ print(f" {name}: {arg}")
44
+ print(f"Grid: {grid}")
45
+
46
+ return tuple(grid), args_dict
47
+
48
+
49
+ grid, args_dict = _get_launch_kernel_args()
50
+
51
+
52
+ def _launch_kernel(grid: tuple[int], args_dict: dict[str, Any]):
53
+ try:
54
+ assert grid is not None
55
+ assert args_dict is not None
56
+
57
+ # {{KERNEL_INVOCATION_PLACEHOLDER}}
58
+
59
+ except Exception as e:
60
+ print(f"Error: {e}")
61
+ print("Failed to launch kernel!")
62
+
63
+
64
+ # HACK: @register_x_val doesn't allow us to pass `operator_name`` as a parameter
65
+ tensor_args = {k: v for k, v in args_dict.items() if isinstance(v, torch.Tensor)}
66
+ x_vals_label = ", ".join(tensor_args.keys())
67
+ REGISTERED_X_VALS[KERNEL_NAME] = x_vals_label
68
+
69
+
70
+ class Operator(BenchmarkOperator):
71
+ @register_benchmark(operator_name=KERNEL_NAME)
72
+ def run_kernel(self, grid, args_dict):
73
+ return lambda: _launch_kernel(grid, args_dict)
74
+
75
+ def get_input_iter(self):
76
+ yield {"grid": grid, "args_dict": args_dict}
77
+
78
+ def get_x_val(self, example_inputs):
79
+ tensors_shapes = [
80
+ tuple(v.shape)
81
+ for v in example_inputs["args_dict"].values()
82
+ if isinstance(v, torch.Tensor)
83
+ ]
84
+ return tuple(tensors_shapes)
85
+
86
+
87
+ if __name__ == "__main__":
88
+ print("do_benchmark...")
89
+
90
+ args = [
91
+ "--benchmark-name",
92
+ KERNEL_NAME,
93
+ ]
94
+
95
+ from tritonbench.utils.parser import get_parser
96
+
97
+ parser = get_parser(args)
98
+ tb_args, extra_args = parser.parse_known_args(args)
99
+ bench = Operator(tb_args, extra_args)
100
+ bench.run()
101
+
102
+ print(bench.output)
103
+ print("Benchmark completed successfully!")
@@ -327,7 +327,7 @@ def _create_arg_from_info(arg_info):
327
327
  return None
328
328
 
329
329
 
330
- def determine_output_paths(out_dir: str, kernel_name: str):
330
+ def determine_output_paths(out_dir: str, kernel_name: str, template: str):
331
331
  """
332
332
  Determine output file paths for reproducer script and context data.
333
333
 
@@ -342,7 +342,12 @@ def determine_output_paths(out_dir: str, kernel_name: str):
342
342
  output_directory = Path(out_dir) / kernel_name
343
343
  output_directory.mkdir(parents=True, exist_ok=True)
344
344
 
345
- out_py_path = output_directory / f"repro_{timestamp}.py"
345
+ filename_parts = ["repro"]
346
+ if template != "example":
347
+ filename_parts.append(template.replace(".", "_"))
348
+ filename_parts.append(timestamp)
349
+ filename = "_".join(filename_parts) + ".py"
350
+ out_py_path = output_directory / filename
346
351
  temp_json_path = output_directory / f"repro_context_{timestamp}.json"
347
352
 
348
353
  return out_py_path, temp_json_path
@@ -77,6 +77,11 @@ def generate_source_mappings(
77
77
  "column": info["column"],
78
78
  f"{ir_type}_line": ln,
79
79
  }
80
+ # Propagate callsite metadata if present
81
+ if info.get("is_callsite"):
82
+ entry["is_callsite"] = True
83
+ entry["callsite_callee"] = info["callsite_callee"]
84
+ entry["callsite_caller"] = info["callsite_caller"]
80
85
  # Propagate alias metadata if present
81
86
  if "alias_name" in info:
82
87
  entry["alias_name"] = info["alias_name"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tritonparse
3
- Version: 0.3.1.dev20251028071524
3
+ Version: 0.3.1.dev20251030071508
4
4
  Summary: TritonParse: A Compiler Tracer, Visualizer, and mini-Reproducer Generator for Triton Kernels
5
5
  Author-email: Yueming Hao <yhao@meta.com>
6
6
  License-Expression: BSD-3-Clause
@@ -1,31 +1,32 @@
1
1
  tritonparse/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  tritonparse/__main__.py,sha256=RXbkALBewcb1xlJBnsQl9IaBRUNln7U8NuRZKT8UdIk,117
3
- tritonparse/cli.py,sha256=Z3nz_rGYXF6NKmI3LsTomWqM51sJ8Tp-ybxRq7l20BI,2569
3
+ tritonparse/cli.py,sha256=JqBwzpxiFKb0TFdhovDXnz3gwkjeASYgIe311GRBy0o,2876
4
4
  tritonparse/common.py,sha256=MJo9bVCgSKkwXpEoUkUczPo_5jOYpJgXLq4UsWYqN3c,13924
5
5
  tritonparse/context_manager.py,sha256=OdMn11qbApYL2c9IlbUpcT27r04ZSa4DfvrY2mLA958,2243
6
6
  tritonparse/event_diff.py,sha256=USCjfjYr-7Ie-EfZgtCFMZMA1KRzFRDe7yDFy98zYI4,4962
7
7
  tritonparse/extract_source_mappings.py,sha256=Z6UxFj2cCE5NCWLQTYPKqUpLfbYhqP8xgCl5mvud9KI,1451
8
- tritonparse/ir_analysis.py,sha256=MoOXuHsUGZ705R4JnXmlsrBn9gJdLO1Dnf0L5AxcaBM,2551
9
- tritonparse/ir_parser.py,sha256=MH4RwoNZMBdWUxkFyEhemJ7Aa7-asoba66b06bGPNsk,13237
8
+ tritonparse/ir_analysis.py,sha256=DZz9H8DqW753UkYECnyt6ATC6J1yvLxOHVRHHAZbrVg,16627
9
+ tritonparse/ir_parser.py,sha256=JQ7hsevmhFGmtZ3CoXi4utcomAycBQTT-KFjSva2K8U,15565
10
10
  tritonparse/mapper.py,sha256=QBCUMHM9pu3x3ahFp0wyXJbmv9TFGVPdkcLULok1E-k,4205
11
11
  tritonparse/shared_vars.py,sha256=RifXq55KisHgspYAmGcaCWY6ZHX8iejFHvwIewvcWZE,707
12
12
  tritonparse/source_type.py,sha256=nmYEQS8rfkIN9BhNhQbkmEvKnvS-3zAxRGLY4TaZdi8,1676
13
13
  tritonparse/sourcemap_utils.py,sha256=uI02n5Sgnlx7Nc15QAX5N6_tZZMips0PyJuo1n3eouY,2654
14
14
  tritonparse/structured_logging.py,sha256=L1xkkCx8Jr9YQbM0Kgtf2g6L3aWMkYOEeFFEOSo8Lkk,60306
15
15
  tritonparse/tp_logger.py,sha256=vXzY7hMDmVnRBGBhIjFZe3nHZzG5NKKPONGUszJhGgU,242
16
- tritonparse/trace_processor.py,sha256=aQPqlnpTtWoGzHYv4BXWUH4nCeUQGSK3o-fj0LD9I0c,14147
16
+ tritonparse/trace_processor.py,sha256=AW4YDrPDayURtmePkFi5m5p6P7OTi1UlTPbbrPzujwY,14418
17
17
  tritonparse/utils.py,sha256=Jnlptcd79llSDev-_1XyyOnv2izUqv0PEL74A8GF2tc,4565
18
18
  tritonparse/reproducer/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
19
- tritonparse/reproducer/cli.py,sha256=wk0K8qJhvP9gty2EBMH3WEc3TSFcszvNq3JbfYu_sTw,1577
19
+ tritonparse/reproducer/cli.py,sha256=95AgH9QOlSFpkC5iR89XV9wubv_5vfD1MKl2IxpAIzs,1718
20
20
  tritonparse/reproducer/function_extractor.py,sha256=kQr10JKHy8EvAN7ic4Azjz6TYe-udBW2DVmbQ--c1pc,6643
21
- tritonparse/reproducer/orchestrator.py,sha256=GotBOJjrShN1oCFc_xTMXn8WWT1Jlfap5qcM21dKBpM,3259
22
- tritonparse/reproducer/placeholder_replacer.py,sha256=ARPZAa9A3Fyit_dIclOKe1JzFgUPBFdHvfy3z20x2E8,9607
21
+ tritonparse/reproducer/orchestrator.py,sha256=OO-eeT4iN-QcB6uXMfH-VoMmiYHJUtrQDQnfneWkuAM,3268
22
+ tritonparse/reproducer/placeholder_replacer.py,sha256=_ehcve5V8_TwemE0NftoO97gZpf4i-n626juAIrixOE,10515
23
23
  tritonparse/reproducer/types.py,sha256=86wql3NaGgpkOzx0gDFb5qexNjKExzhL0uIwGU7grrw,564
24
- tritonparse/reproducer/utils.py,sha256=yFS1Mg2IhRgW-1UNfqjWH5gRSqc8Wbn5Ykre8L-EWcU,16599
24
+ tritonparse/reproducer/utils.py,sha256=DsO7695AuGaFOp4sRSCmsljBeyKnQud9NOKntaUL_VE,16803
25
25
  tritonparse/reproducer/ingestion/ndjson.py,sha256=7amSwpbtG-od1-pW18Nm9AiaFc3Etd0-UETXwiYCmgw,7443
26
26
  tritonparse/reproducer/templates/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
- tritonparse/reproducer/templates/example.py,sha256=jR3c8_d7fAFJYaj1DuUuthnI4Xd-_606bWDRdUPMNyo,785
27
+ tritonparse/reproducer/templates/example.py,sha256=mTK_H4BfHntFdk9bybMEYSx8TyKXzQDwMxZok0Urw5s,828
28
28
  tritonparse/reproducer/templates/loader.py,sha256=x14KHXkovOIcXFKii3Jx4XjpEhXqUMqp575qAffi370,1975
29
+ tritonparse/reproducer/templates/tritonbench.py,sha256=vRQ9xvIF3pgPHN2nGVBay6ngXScVdicU3agCV3f9Ao0,2875
29
30
  tritonparse/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
31
  tritonparse/tools/decompress_bin_ndjson.py,sha256=Gn5foDIlxBN5D5wmcdrEmwvxo3_wRlH8ih2U2Ys3RdM,4199
31
32
  tritonparse/tools/disasm.py,sha256=c4HmNNoPPeXPQBQkPVcMaHwDHbHNZNxuqXn4UIIs1Z0,2434
@@ -33,9 +34,9 @@ tritonparse/tools/format_fix.py,sha256=ISalg_N_L7Xktag3mLr-G9T6Opxv793s1WG6A9wUt
33
34
  tritonparse/tools/load_tensor.py,sha256=7-LbpboKDNJFBLNhiKS3enoqRlVAb55OjPc70PwHXAw,2789
34
35
  tritonparse/tools/prettify_ndjson.py,sha256=kR8hmBCv-iJeuzpi2_6CZv9T4_edRQbBOSOPpMm6wrw,11117
35
36
  tritonparse/tools/readme.md,sha256=w6PWYfYnRgoPArLjxG9rVrpcLUkoVMGuRlbpF-o0IQM,110
36
- tritonparse-0.3.1.dev20251028071524.dist-info/licenses/LICENSE,sha256=4ZciugpyN7wcM4L-9pyDh_etvMUeIfBhDTyH1zeZlQM,1515
37
- tritonparse-0.3.1.dev20251028071524.dist-info/METADATA,sha256=6PkpYMi1Qjf4Lar46WHsMPBo6dQts6i6n3IcwmijYeg,8282
38
- tritonparse-0.3.1.dev20251028071524.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
39
- tritonparse-0.3.1.dev20251028071524.dist-info/entry_points.txt,sha256=wEXdaieDoRRCCdhEv2p_C68iytnaXU_2pwt5CqjfbWY,56
40
- tritonparse-0.3.1.dev20251028071524.dist-info/top_level.txt,sha256=ITcTKgp3vf_bXV9vixuQU9IrZa3L1EfDSZwvRzRaoJU,12
41
- tritonparse-0.3.1.dev20251028071524.dist-info/RECORD,,
37
+ tritonparse-0.3.1.dev20251030071508.dist-info/licenses/LICENSE,sha256=4ZciugpyN7wcM4L-9pyDh_etvMUeIfBhDTyH1zeZlQM,1515
38
+ tritonparse-0.3.1.dev20251030071508.dist-info/METADATA,sha256=GgewTGhlYm2vvn7S39qg5UiXAzJ07JzkETek6YJnwSw,8282
39
+ tritonparse-0.3.1.dev20251030071508.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
40
+ tritonparse-0.3.1.dev20251030071508.dist-info/entry_points.txt,sha256=wEXdaieDoRRCCdhEv2p_C68iytnaXU_2pwt5CqjfbWY,56
41
+ tritonparse-0.3.1.dev20251030071508.dist-info/top_level.txt,sha256=ITcTKgp3vf_bXV9vixuQU9IrZa3L1EfDSZwvRzRaoJU,12
42
+ tritonparse-0.3.1.dev20251030071508.dist-info/RECORD,,