Skip to content

Commit ed5f224

Browse files
committed
Move over to lighthouse.ingress.torch importer utility
1 parent 643d68e commit ed5f224

File tree

1 file changed

+10
-42
lines changed

1 file changed

+10
-42
lines changed

ingress/Torch-MLIR/convert-kernel-bench-to-mlir.py

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#!/usr/bin/env python3
22

3-
import importlib
4-
import importlib.util
5-
63
from pathlib import Path
74

85
from mlir import ir, passmanager
9-
from torch_mlir import fx
6+
from lighthouse.ingress import torch as torch_ingress
7+
108

119
kernels_as_pytorch_folder = Path(__file__).parent / "KernelBench" / "KernelBench"
1210
kernels_as_pytorch_level1 = kernels_as_pytorch_folder / "level1"
@@ -121,9 +119,9 @@
121119
)
122120
continue
123121

124-
module_name = kernel_pytorch_file.stem
122+
kernel_name = kernel_pytorch_file.stem
125123

126-
kernel_as_mlir_path = mlir_level / (module_name + ".mlir")
124+
kernel_as_mlir_path = mlir_level / (kernel_name + ".mlir")
127125
if kernel_as_mlir_path.exists():
128126
print(
129127
f"Already in cache: {kernel_pytorch_file.parent.name}/{kernel_pytorch_file.name}"
@@ -132,49 +130,19 @@
132130
print(
133131
f"Processing: {kernel_pytorch_file.parent.name}/{kernel_pytorch_file.name}"
134132
)
135-
136-
module_spec = importlib.util.spec_from_file_location(
137-
module_name, kernel_pytorch_file
133+
mlir_kernel = torch_ingress.import_from_file(
134+
kernel_pytorch_file, ir_context=ctx
138135
)
139136

140-
if module_spec is None or module_spec.loader is None:
141-
print(f"Error: Could not create module spec for {kernel_pytorch_file}")
142-
continue
143-
144-
module = importlib.util.module_from_spec(module_spec)
145-
# Execute the module to load its contents
146-
module_spec.loader.exec_module(module)
147-
148-
if not all(
149-
hasattr(module, a) for a in ("Model", "get_inputs", "get_init_inputs")
150-
):
151-
print(f"Error: module in file {kernel_pytorch_file} not a proper benchmark")
152-
continue
153-
154-
# TODO: check hasattr(module, "in_features") etc and adjust to sizes that are more tractable for torch-mlir
155-
156-
try:
157-
m = fx.export_and_import(
158-
module.Model(*module.get_init_inputs()),
159-
*module.get_inputs(),
160-
output_type=fx.OutputType.LINALG_ON_TENSORS,
161-
)
162-
except Exception as e:
163-
print(f"Error: got the following error converting {kernel_pytorch_file}")
164-
raise e
165-
166-
before_clean_up = "//" + str(m)[:-1].replace("\n", "\n//") + "\n"
167-
# Cross boundary from torch-mlir's mlir to environment's mlir
168-
m = ir.Module.parse(str(m), context=ctx)
169-
# Run clean-up, e.g. linalg-"specialization" passes to raise within Linalg.
137+
before_clean_up = "//" + str(mlir_kernel)[:-1].replace("\n", "\n//") + "\n"
170138
try:
171-
pm.run(m.operation) # cleanup
139+
pm.run(mlir_kernel.operation) # cleanup
172140
except Exception as e:
173-
print(f"Error: got the following error cleaning up {module_name}")
141+
print(f"Error: got the following error cleaning up {kernel_name}")
174142
raise e
175143

176144
with kernel_as_mlir_path.open("w") as f:
177145
print("// Torch-MLIR output:", file=f)
178146
print(before_clean_up, file=f)
179147
print("// MLIR output after clean-up:", file=f)
180-
print(m, file=f)
148+
print(mlir_kernel, file=f)

0 commit comments

Comments
 (0)