Skip to content

Commit db82bb9

Browse files
authored
Include torch-mlir-opt in Python wheels (#3964)
This adds the `torch-mlir-opt` tool to the Python wheels, which allows to use the commandline tool via a pip installed package instead of having to compile the torch-mlir project yourself. The executable is still installed to the deault location and copied over via the `setup.py` to be included in the Python wheel. This could be refactored and handled within CMake in a follow-up.
1 parent 33337fc commit db82bb9

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

python/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ declare_mlir_python_sources(TorchMLIRPythonSources.Tools
5353
ADD_TO_PARENT TorchMLIRPythonSources
5454
SOURCES
5555
tools/import_onnx/__main__.py
56+
tools/opt/__main__.py
5657
)
5758

5859
declare_mlir_python_sources(TorchMLIRSiteInitialize
@@ -123,3 +124,5 @@ add_mlir_python_modules(TorchMLIRPythonModules
123124
COMMON_CAPI_LINK_LIBS
124125
TorchMLIRAggregateCAPI
125126
)
127+
128+
add_dependencies(TorchMLIRPythonModules torch-mlir-opt)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
# Also available under a BSD-style license. See LICENSE.
5+
6+
"""Torch-MLIR modular optimizer driver
7+
8+
Typically, when installed from a wheel, this can be invoked as:
9+
10+
torch-mlir-opt [options] <input file>
11+
12+
To see available passes, dialects, and options, run:
13+
14+
torch-mlir-opt --help
15+
"""
16+
import os
17+
import platform
18+
import subprocess
19+
import sys
20+
21+
from typing import Optional
22+
23+
24+
def _get_builtin_tool(exe_name: str) -> Optional[str]:
25+
if platform.system() == "Windows":
26+
exe_name = exe_name + ".exe"
27+
this_path = os.path.dirname(__file__)
28+
tool_path = os.path.join(this_path, "..", "..", "_mlir_libs", exe_name)
29+
return tool_path
30+
31+
32+
def main(args=None):
33+
if args is None:
34+
args = sys.argv[1:]
35+
exe = _get_builtin_tool("torch-mlir-opt")
36+
return subprocess.call(args=[exe] + args)
37+
38+
39+
if __name__ == "__main__":
40+
sys.exit(main())

setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ def run(self):
198198

199199
shutil.copytree(python_package_dir, target_dir, symlinks=False)
200200

201+
torch_mlir_opt_src = os.path.join(cmake_build_dir, "bin", "torch-mlir-opt")
202+
torch_mlir_opt_dst = os.path.join(
203+
target_dir, "torch_mlir", "_mlir_libs", "torch-mlir-opt"
204+
)
205+
shutil.copy2(torch_mlir_opt_src, torch_mlir_opt_dst, follow_symlinks=False)
206+
201207

202208
class CMakeExtension(Extension):
203209
def __init__(self, name, sourcedir=""):
@@ -267,6 +273,7 @@ def build_extension(self, ext):
267273
entry_points={
268274
"console_scripts": [
269275
"torch-mlir-import-onnx = torch_mlir.tools.import_onnx:_cli_main",
276+
"torch-mlir-opt = torch_mlir.tools.opt.__main__:main",
270277
],
271278
},
272279
zip_safe=False,

0 commit comments

Comments
 (0)