-
Notifications
You must be signed in to change notification settings - Fork 629
[Feature] add lower-trace support for debugging #2470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
erhsh
wants to merge
1
commit into
tile-ai:main
Choose a base branch
from
erhsh:main_lower-trace.v3.merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,12 @@ | ||
| from .plot_layout import plot_layout # noqa: F401 | ||
| from .Analyzer import * | ||
|
|
||
|
|
||
| def __getattr__(name): | ||
| if name == "lower_trace": | ||
| import importlib | ||
|
|
||
| mod = importlib.import_module(".lower_trace", __name__) | ||
| globals()["lower_trace"] = mod | ||
| return mod | ||
| raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| """IR Lower Trace — zero-intrusion debug tool for visualizing compilation passes. | ||
|
|
||
| Generates a self-contained HTML page and/or terminal diff showing all passes in the | ||
| compilation pipeline with side-by-side IR diff for each pass. | ||
|
|
||
| Usage:: | ||
|
|
||
| TL_LOWER_TRACE=1 python my_kernel.py # HTML report | ||
| TL_LOWER_TRACE=terminal python my_kernel.py # terminal diff only | ||
| TL_LOWER_TRACE=both python my_kernel.py # both terminal and HTML | ||
|
|
||
| Programmatic API:: | ||
|
|
||
| from tilelang.tools.lower_trace import lower_trace | ||
|
|
||
| lower_trace(func, my_pass, mode="terminal") | ||
| lower_trace(func, [pass_a, pass_b], mode="both", html_path="diff.html") | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from .core import ( | ||
| enable, | ||
| disable, | ||
| reset, | ||
| LowerRecord, | ||
| STATUS_COMPLETED, | ||
| STATUS_FAILED, | ||
| STATUS_SKIPPED, | ||
| STATUS_CODEGEN, | ||
| _get_pass_display_name, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "STATUS_CODEGEN", | ||
| "STATUS_COMPLETED", | ||
| "STATUS_FAILED", | ||
| "STATUS_SKIPPED", | ||
| "LowerRecord", | ||
| "disable", | ||
| "enable", | ||
| "lower_trace", | ||
| "reset", | ||
| ] | ||
|
|
||
|
|
||
| def lower_trace( | ||
| func_or_mod, | ||
| passes, | ||
| *, | ||
| mode: str = "terminal", | ||
| context: int = 3, | ||
| html_path: str = "lower_trace_report.html", | ||
| ) -> list[dict]: | ||
| """Compare IR before and after each pass in a chain. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| func_or_mod : PrimFunc or IRModule | ||
| The starting IR. | ||
| passes : Pass or list[Pass] or list[tuple[str, Pass]] | ||
| A single pass, a list of passes, or a list of (name, pass) pairs. | ||
| mode : {"terminal", "html", "both"} | ||
| Output mode. | ||
| context : int | ||
| Number of context lines in the unified diff (default 3). | ||
| html_path : str | ||
| Output path for HTML report (default ``lower_trace_report.html``). | ||
|
|
||
| Returns | ||
| ------- | ||
| list[dict] | ||
| One entry per pass step, each containing: | ||
| ``name``, ``before_script``, ``after_script``, ``diff_lines``, | ||
| ``insertions``, ``deletions``, ``changed``. | ||
| """ | ||
| from tilelang import tvm | ||
| from .diff import unified_diff | ||
|
|
||
| mode = str(mode).strip().lower() | ||
| if mode not in ("terminal", "html", "both"): | ||
| raise ValueError(f"mode must be one of 'terminal', 'html', 'both', got {mode!r}") | ||
|
|
||
| if isinstance(func_or_mod, tvm.IRModule): | ||
| mod = func_or_mod | ||
| else: | ||
| mod = tvm.IRModule({"main": func_or_mod}) | ||
|
|
||
| # A bare ``(name, pass)`` tuple is a single named pass, not an iterable of | ||
| # passes; wrap it so the loop below treats it as one entry. A non-list/tuple | ||
| # value (a bare Pass) is likewise wrapped. A list/tuple of passes (or of | ||
| # named pairs) is iterated as-is. | ||
| if (isinstance(passes, tuple) and len(passes) == 2 and isinstance(passes[0], str)) or not isinstance(passes, (list, tuple)): | ||
| passes = [passes] | ||
|
|
||
| named_passes: list[tuple[str, object]] = [] | ||
| for p in passes: | ||
| if isinstance(p, (list, tuple)) and len(p) == 2: | ||
| named_passes.append((str(p[0]), p[1])) | ||
| else: | ||
| named_passes.append((_get_pass_display_name(p), p)) | ||
|
|
||
| results: list[dict] = [] | ||
|
|
||
| try: | ||
| for step_idx, (name, p) in enumerate(named_passes, 1): | ||
| before_script = mod.script() | ||
| try: | ||
| mod = p(mod) | ||
| except Exception as e: | ||
| results.append( | ||
| { | ||
| "name": name, | ||
| "before_script": before_script, | ||
| "after_script": "", | ||
| "diff_lines": [], | ||
| "insertions": 0, | ||
| "deletions": 0, | ||
| "changed": False, | ||
| "error": str(e), | ||
| } | ||
| ) | ||
| raise | ||
|
|
||
| after_script = mod.script() | ||
|
|
||
| diff_text = unified_diff( | ||
| before_script, | ||
| after_script, | ||
| before_label=f"step {step_idx} before", | ||
| after_label=f"step {step_idx} after", | ||
| context=context, | ||
| color=False, | ||
| ) | ||
| diff_lines = diff_text.splitlines() if diff_text else [] | ||
|
|
||
| insertions = sum(1 for d in diff_lines if d.startswith("+") and not d.startswith("+++")) | ||
| deletions = sum(1 for d in diff_lines if d.startswith("-") and not d.startswith("---")) | ||
| changed = insertions > 0 or deletions > 0 | ||
|
|
||
| step_result = { | ||
| "name": name, | ||
| "before_script": before_script, | ||
| "after_script": after_script, | ||
| "diff_lines": diff_lines, | ||
| "insertions": insertions, | ||
| "deletions": deletions, | ||
| "changed": changed, | ||
| } | ||
| results.append(step_result) | ||
|
|
||
| if mode in ("terminal", "both"): | ||
| header = f"\n{'=' * 60}\n Pass {step_idx}: {name}\n{'=' * 60}\n" | ||
| print(header) | ||
| if changed: | ||
| colored = unified_diff( | ||
| before_script, | ||
| after_script, | ||
| before_label=f"step {step_idx} before", | ||
| after_label=f"step {step_idx} after", | ||
| context=context, | ||
| color=True, | ||
| ) | ||
| print(colored, end="") | ||
| print(f"\n >>> +{insertions} insertion(s), -{deletions} deletion(s)") | ||
| else: | ||
| print(" (no changes)") | ||
| finally: | ||
| if mode in ("html", "both"): | ||
| from .html import generate_html | ||
|
|
||
| records = [] | ||
| for i, r in enumerate(results): | ||
| failed = "error" in r | ||
| records.append( | ||
| LowerRecord( | ||
| phase="lower_trace", | ||
| name=r["name"], | ||
| index=i, | ||
| before_text=r["before_script"], | ||
| after_text=r["after_script"], | ||
| changed=r["changed"], | ||
| add_lines=r["insertions"], | ||
| del_lines=r["deletions"], | ||
| status=STATUS_FAILED if failed else STATUS_COMPLETED, | ||
| error_msg=r.get("error", ""), | ||
| ) | ||
| ) | ||
| generate_html(records, html_path) | ||
| print(f"\nHTML report written to: {html_path}") | ||
|
erhsh marked this conversation as resolved.
|
||
|
|
||
| return results | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.