|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | | -import importlib |
4 | | -import importlib.util |
5 | | - |
6 | 3 | from pathlib import Path |
7 | 4 |
|
8 | 5 | from mlir import ir, passmanager |
9 | | -from torch_mlir import fx |
| 6 | +from lighthouse.ingress import torch as torch_ingress |
| 7 | + |
10 | 8 |
|
11 | 9 | kernels_as_pytorch_folder = Path(__file__).parent / "KernelBench" / "KernelBench" |
12 | 10 | kernels_as_pytorch_level1 = kernels_as_pytorch_folder / "level1" |
|
121 | 119 | ) |
122 | 120 | continue |
123 | 121 |
|
124 | | - module_name = kernel_pytorch_file.stem |
| 122 | + kernel_name = kernel_pytorch_file.stem |
125 | 123 |
|
126 | | - kernel_as_mlir_path = mlir_level / (module_name + ".mlir") |
| 124 | + kernel_as_mlir_path = mlir_level / (kernel_name + ".mlir") |
127 | 125 | if kernel_as_mlir_path.exists(): |
128 | 126 | print( |
129 | 127 | f"Already in cache: {kernel_pytorch_file.parent.name}/{kernel_pytorch_file.name}" |
|
132 | 130 | print( |
133 | 131 | f"Processing: {kernel_pytorch_file.parent.name}/{kernel_pytorch_file.name}" |
134 | 132 | ) |
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 |
138 | 135 | ) |
139 | 136 |
|
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" |
170 | 138 | try: |
171 | | - pm.run(m.operation) # cleanup |
| 139 | + pm.run(mlir_kernel.operation) # cleanup |
172 | 140 | 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}") |
174 | 142 | raise e |
175 | 143 |
|
176 | 144 | with kernel_as_mlir_path.open("w") as f: |
177 | 145 | print("// Torch-MLIR output:", file=f) |
178 | 146 | print(before_clean_up, file=f) |
179 | 147 | print("// MLIR output after clean-up:", file=f) |
180 | | - print(m, file=f) |
| 148 | + print(mlir_kernel, file=f) |
0 commit comments