-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Frontend][MLIR] Fix crash when MLIR_DUMP_PATH is set to a directory #6549
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
peymanbr
wants to merge
1
commit into
triton-lang:main
Choose a base branch
from
peymanbr:fix_mlir_dump_path
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
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
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,58 @@ | ||
import subprocess | ||
import tempfile | ||
import pytest | ||
import textwrap | ||
from pathlib import Path | ||
import os | ||
|
||
|
||
@pytest.mark.forked | ||
def test_basic_mlir_dump(monkeypatch): | ||
dump_dir = Path(tempfile.mkdtemp()) | ||
monkeypatch.setenv("MLIR_ENABLE_DUMP", "1") | ||
monkeypatch.setenv("MLIR_DUMP_PATH", str(dump_dir)) | ||
|
||
kernel_code = textwrap.dedent(""" | ||
import triton | ||
import triton.language as tl | ||
import torch | ||
|
||
@triton.jit | ||
def dummy_kernel(x_ptr, y_ptr, n_elements: tl.constexpr): | ||
pid = tl.program_id(0) | ||
offs = pid * 128 + tl.arange(0, 128) | ||
mask = offs < n_elements | ||
x = tl.load(x_ptr + offs, mask=mask) | ||
tl.store(y_ptr + offs, x, mask=mask) | ||
|
||
if __name__ == "__main__": | ||
n = 1024 | ||
x = torch.arange(n, dtype=torch.float32, device="cuda") | ||
y = torch.empty_like(x) | ||
dummy_kernel[(n // 128,)](x, y, n_elements=n) | ||
""") | ||
|
||
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f: | ||
f.write(kernel_code) | ||
script_path = f.name | ||
|
||
repo_root = Path(__file__).resolve().parents[2] | ||
env = os.environ.copy() | ||
env["PYTHONPATH"] = str(repo_root) + ":" + env.get("PYTHONPATH", "") | ||
|
||
result = subprocess.run( | ||
["python3", script_path], | ||
env=env, | ||
capture_output=True, | ||
text=True, | ||
) | ||
|
||
# Diagnostic printing if failure | ||
if result.returncode != 0: | ||
print("STDOUT:\n", result.stdout) | ||
print("STDERR:\n", result.stderr) | ||
|
||
assert result.returncode == 0, "Triton kernel script failed" | ||
assert any(f.suffix == ".mlir" for f in dump_dir.iterdir()), "No MLIR dump generated" | ||
|
||
os.remove(script_path) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This obviously isn't true since you only check the env variable on the first call. The only meaningful change here is supporting
MLIR_DUMP_PATH
as a directory, but in that case why not useTRITON_DUMP_DIR
which is probably exactly what you're looking for.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback.
Correct. What I meant was that in
mlir_dumps_or_dbgs()
, the environment variable is checked on every call, but the dump stream is initialized only once. I will correct the commit message and also add a clarifying comment to this function in the updated commit.Correct.
Actually not. Based on the Tips for hacking section of the
README
and the contents of the dumped files,my understanding is that
MLIR_DUMP_PATH
andTRITON_DUMP_DIR
serve different purposes.MLIR_DUMP_PATH
is controlled byMLIR_ENABLE_DUMP
and is used to store MLIR-native IR dumps.TRITON_DUMP_DIR
is controlled byTRITON_KERNEL_DUMP
and is used to store Triton IR, LLVM IR, and backend binaries, such as .cubin and .ptx files.The purpose of this PR is to prevent crashes when
MLIR_DUMP_PATH
is set to a directory,and to make its behavior consistent with what the
README
describes.I'm currently learning the Triton code base, so please let me know if I've missed anything. I'd really appreciate it.