Skip to content

Commit 558477f

Browse files
committed
Move to examples, parallelize, comment and clean up
1 parent d199529 commit 558477f

File tree

4 files changed

+86
-49
lines changed

4 files changed

+86
-49
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Lighthouse cache
2+
cache
3+
14
# Python caches
25
__pycache__/
36
*.py[cod]

.gitmodules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "ingress/KernelBench/KernelBench"]
2-
path = ingress/KernelBench/KernelBench
1+
[submodule "third_party/KernelBench"]
2+
path = third_party/KernelBench
33
url = https://github.com/ScalingIntelligence/KernelBench.git

ingress/KernelBench/convert-kernel-bench-to-mlir.py renamed to python/examples/ingress/convert-kernel-bench-to-mlir.py

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
1-
#!/usr/bin/env python3
1+
# RUN: %PYTHON %s
2+
3+
# Basic conversion of KernelBench PyTorch kernels to mlir kernels, relying on
4+
# torch-mlir for the conversion. As there are a number of kernels for which
5+
# conversion fails (or is prohibitively slow/gives prohibitively large IR),
6+
# there's an ingore list. Runs the conversion in parallel.
27

38
import sys
9+
10+
from concurrent.futures import ProcessPoolExecutor
11+
from dataclasses import dataclass
412
from pathlib import Path
513

614
from mlir import ir, passmanager
715
from lighthouse.ingress import torch as torch_ingress
816

17+
project_root = Path(__file__).parent.parent.parent.parent
18+
kernels_as_torch_folder = (
19+
project_root / "third_party" / "KernelBench" / "KernelBench"
20+
)
921

10-
kernels_as_pytorch_folder = Path(__file__).parent / "KernelBench" / "KernelBench"
11-
12-
if not (kernels_as_pytorch_folder.exists() and kernels_as_pytorch_folder.is_dir()):
22+
if not kernels_as_torch_folder.is_dir():
1323
print(
1424
"ERROR: KernelBench repo not found.\n"
15-
"NOTE: Pull in dependency with: git submodule update "
16-
+ str(kernels_as_pytorch_folder.parent.relative_to(Path.cwd()))
17-
+ "",
25+
"NOTE: Pull in dependency with: git submodule update --init "
26+
+ str(kernels_as_torch_folder.parent.relative_to(Path.cwd(), walk_up=True)),
1827
file=sys.stderr,
1928
)
2029
sys.exit(1)
2130

2231

23-
kernels_as_pytorch_level1 = kernels_as_pytorch_folder / "level1"
24-
kernels_as_pytorch_level2 = kernels_as_pytorch_folder / "level2"
32+
kernels_as_torch_level1 = kernels_as_torch_folder / "level1"
33+
kernels_as_torch_level2 = kernels_as_torch_folder / "level2"
2534

26-
kernels_as_mlir_folder = Path(__file__).parent / "cache"
35+
kernels_as_mlir_folder = project_root / "cache" / "ingress" / "KernelBench"
2736
kernels_as_mlir_level1 = kernels_as_mlir_folder / "level1"
2837
kernels_as_mlir_level1.mkdir(parents=True, exist_ok=True)
2938
kernels_as_mlir_level2 = kernels_as_mlir_folder / "level2"
3039
kernels_as_mlir_level2.mkdir(parents=True, exist_ok=True)
3140

41+
# The following kernels won't get converted:
3242
level1, level2 = Path("level1"), Path("level2")
3343
ignore_list = [
3444
level1 / "12_Matmul_with_diagonal_matrices_.py", # torch.operator "torch.aten.diag"
@@ -118,48 +128,72 @@
118128
pm = passmanager.PassManager(context=ctx)
119129
pm.add("linalg-specialize-generic-ops")
120130

121-
print("Output directory:", kernels_as_mlir_folder)
122-
exitcode = 0
123-
for pytorch_level, mlir_level in (
124-
(kernels_as_pytorch_level1, kernels_as_mlir_level1),
125-
(kernels_as_pytorch_level2, kernels_as_mlir_level2),
126-
):
127-
for kernel_pytorch_file in pytorch_level.iterdir():
128-
level_and_kernel = (
129-
Path(kernel_pytorch_file.parent.name) / kernel_pytorch_file.name
130-
)
131-
if level_and_kernel in ignore_list or not kernel_pytorch_file.is_file():
132-
print(
133-
f"Skipping: {kernel_pytorch_file.parent.name}/{kernel_pytorch_file.name}",
134-
file=sys.stderr,
135-
)
136-
continue
137131

138-
kernel_name = kernel_pytorch_file.stem
132+
@dataclass
133+
class KernelConversionTask:
134+
name: str
135+
torch_path: Path
136+
mlir_path: Path
137+
139138

140-
kernel_as_mlir_path = mlir_level / (kernel_name + ".mlir")
141-
if kernel_as_mlir_path.exists():
142-
print(
143-
f"Already in cache: {kernel_pytorch_file.parent.name}/{kernel_pytorch_file.name}"
139+
def tasks():
140+
for torch_level, mlir_level in (
141+
(kernels_as_torch_level1, kernels_as_mlir_level1),
142+
(kernels_as_torch_level2, kernels_as_mlir_level2),
143+
):
144+
for kernel_torch_file in torch_level.iterdir():
145+
level_and_kernel = (
146+
Path(kernel_torch_file.parent.name) / kernel_torch_file.name
144147
)
145-
continue
148+
kernel_torch_path = torch_level / kernel_torch_file
149+
if level_and_kernel in ignore_list or not kernel_torch_path.is_file():
150+
print(
151+
f"Skipping: {kernel_torch_file.parent.name}/{kernel_torch_file.name}",
152+
file=sys.stderr,
153+
)
154+
continue
155+
156+
kernel_name = kernel_torch_file.stem
157+
158+
kernel_mlir_path = mlir_level / (kernel_name + ".mlir")
159+
if kernel_mlir_path.exists():
160+
print(
161+
f"Already in cache: {kernel_torch_file.parent.name}/{kernel_torch_file.name}"
162+
)
163+
continue
164+
yield KernelConversionTask(
165+
kernel_name, torch_path=kernel_torch_path, mlir_path=kernel_mlir_path
166+
)
167+
168+
169+
def process_task(task: KernelConversionTask):
170+
print(f"Processing: {task.torch_path.parent.name}/{task.torch_path.name}")
171+
172+
try:
173+
mlir_kernel = torch_ingress.import_from_file(task.torch_path, ir_context=ctx)
174+
assert isinstance(mlir_kernel, ir.Module)
175+
except Exception as e:
146176
print(
147-
f"Processing: {kernel_pytorch_file.parent.name}/{kernel_pytorch_file.name}"
177+
f"ERROR: got the following error converting '{task.name}':\n",
178+
e,
179+
file=sys.stderr,
148180
)
149-
mlir_kernel = torch_ingress.import_from_file(
150-
kernel_pytorch_file, ir_context=ctx
181+
raise e
182+
183+
try:
184+
pm.run(mlir_kernel.operation) # cleanup
185+
except Exception as e:
186+
print(
187+
f"ERROR: got the following error cleaning up '{task.name}':\n",
188+
e,
189+
file=sys.stderr,
151190
)
152-
assert isinstance(mlir_kernel, ir.Module)
191+
raise e
153192

154-
try:
155-
pm.run(mlir_kernel.operation) # cleanup
156-
except Exception as e:
157-
print(
158-
f"ERROR: got the following error cleaning up '{kernel_name}'",
159-
file=sys.stderr,
160-
)
161-
raise e
193+
with task.mlir_path.open("w") as f:
194+
print("// MLIR output after conversion and clean-up:", file=f)
195+
print(mlir_kernel, file=f)
162196

163-
with kernel_as_mlir_path.open("w") as f:
164-
print("// MLIR output after conversion and clean-up:", file=f)
165-
print(mlir_kernel, file=f)
197+
198+
print("Output directory:", kernels_as_mlir_folder)
199+
ProcessPoolExecutor().map(process_task, tasks())

0 commit comments

Comments
 (0)