|
| 1 | +""" |
| 2 | +Example demonstrating how to load a PyTorch model to MLIR using Lighthouse |
| 3 | +without initializing the model class on the user's side. |
| 4 | +
|
| 5 | +The script uses 'lighthouse.ingress.torch.import_from_file' function that |
| 6 | +takes a path to a Python file containing the model definition (a Python class derived from 'nn.Module'), |
| 7 | +along with the names of functions to get model init arguments and sample inputs. The function |
| 8 | +imports the model class on its own, initializes it, and passes it to torch_mlir |
| 9 | +to get a MLIR module in the specified dialect. |
| 10 | +
|
| 11 | +The script uses the model from 'MLPModel/model.py' as an example. |
| 12 | +""" |
| 13 | + |
| 14 | +import os |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +# MLIR infrastructure imports (only needed if you want to manipulate the MLIR module) |
| 18 | +import mlir.dialects.func as func |
| 19 | +from mlir import ir |
| 20 | + |
| 21 | +# Lighthouse imports |
| 22 | +from lighthouse.ingress.torch import import_from_file |
| 23 | + |
| 24 | +# Step 1: Set up paths to locate the model definition file |
| 25 | +script_dir = Path(os.path.dirname(os.path.abspath(__file__))) |
| 26 | +model_path = script_dir / "MLPModel" / "model.py" |
| 27 | + |
| 28 | +ir_context = ir.Context() |
| 29 | + |
| 30 | +# Step 2: Convert PyTorch model to MLIR |
| 31 | +# Conversion step where Lighthouse: |
| 32 | +# - Loads the MLPModel class and instantiates it with arguments obtained from 'get_init_inputs()' |
| 33 | +# - Calls get_sample_inputs() to get sample input tensors for shape inference |
| 34 | +# - Converts PyTorch model to linalg-on-tensors dialect operations using torch_mlir |
| 35 | +mlir_module_ir: ir.Module = import_from_file( |
| 36 | + model_path, # Path to the Python file containing the model |
| 37 | + model_class_name="MLPModel", # Name of the PyTorch nn.Module class to convert |
| 38 | + init_args_fn_name="get_init_inputs", # Function that returns args for model.__init__() |
| 39 | + sample_args_fn_name="get_sample_inputs", # Function that returns sample inputs to pass to 'model(...)' |
| 40 | + dialect="linalg-on-tensors", # Target MLIR dialect (linalg ops on tensor types) |
| 41 | + ir_context=ir_context # MLIR context for the conversion |
| 42 | +) |
| 43 | + |
| 44 | +# The PyTorch model is now converted to MLIR at this point. You can now convert |
| 45 | +# the MLIR module to a text form (e.g. 'str(mlir_module_ir)') and save it to a file. |
| 46 | +# |
| 47 | +# The following optional MLIR-processing steps are to give you an idea of what can |
| 48 | +# also be done with the MLIR module. |
| 49 | + |
| 50 | +# Step 3: Extract the main function operation from the MLIR module and print its metadata |
| 51 | +func_op: func.FuncOp = mlir_module_ir.operation.regions[0].blocks[0].operations[0] |
| 52 | +print(f"entry-point name: {func_op.name}") |
| 53 | +print(f"entry-point type: {func_op.type}") |
| 54 | + |
| 55 | +# Step 4: output the imported MLIR module |
| 56 | +print("\n\nModule dump:") |
| 57 | +mlir_module_ir.dump() |
| 58 | + |
| 59 | +# You can alternatively write the MLIR module to a file: |
| 60 | +# with open("output.mlir", "w") as f: |
| 61 | +# f.write(str(mlir_module_ir)) |
0 commit comments