Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Runtime/python/OMPyCompile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore directories for build or install with -e
dist/
src/OMPyCompile/__pycache__/
52 changes: 52 additions & 0 deletions src/Runtime/python/OMPyCompile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!--- SPDX-License-Identifier: Apache-2.0 -->
# OMPyCompile
This package provides a python driver to compile a ONNX model with onnx-mlir.
The compilation can be done with onnx-mlir compiler at local directory or in
a container.
To use
```
import numpy as np
import OMPyInfer

# Initialize the inference session
# The onnx model simply performs tensor add on two 3x4x5xf32 tensors
# It is compiled into test_add.so with zDLC
sess = OMPyInfer.InferenceSession("./test_add.so")

# Prepare the inputs
a = np.arange(3 * 4 * 5, dtype=np.float32).reshape((3, 4, 5))
b = a + 4

# Run inference
r = sess.run([a, b])

# Print output
print(r)
```

## Compile onnx model to shared library
TBD


## Pre-requisites for OMPyInfer
These pre-requisities are currently provided as part of the OMPyInfer package for Python versions 3.9 until 3.13.
Prebuilt libraries for Linux on Z is provided.
Follow these instructions (TBD) to build the libraries for your own system.

## Install
Currently, only local installation is supported.
Suppose you have onnx-mlir cloned on your machine. Install OMPyInfer with the following command:
```
python onnx-mlir/src/Runtime/python/OMPyInfer
```


## Verify
```
cd OMPyInfer/tests
python helloworld.py
```

## VERSIONS
Version 1.0.0 supports the model copied with onnx-mlir before 29bde823f, 2026-02-04.

3 changes: 3 additions & 0 deletions src/Runtime/python/OMPyCompile/VERSION_NUMBER
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!--- SPDX-License-Identifier: Apache-2.0 -->

1.0.0
38 changes: 38 additions & 0 deletions src/Runtime/python/OMPyCompile/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-License-Identifier: Apache-2.0

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "OMPyCompile"
version = "1.0.0"
authors = [
{ name="Tong Chen", email="chentong@us.ibm.com" },
{ name="Sunny Anand", email="sunny.anand79@ibm.com" },
]
description = "Python driver to run inference on onnx model compiled with zdlc"
readme = "README.md"
requires-python = ">=3.9"
license = "Apache-2.0"
classifiers = [
"Programming Language :: Python :: 3",
"Operating System :: POSIX :: Linux",
]

dependencies = [
"docker"
]

[project.urls]
Homepage = "https://github.com/onnx/onnx-mlir"
Issues = "https://github.com/onnx/onnx-mlir/issues"

[tool.hatch.build.targets.wheel]
packages = ["src/OMPyCompile"]

[tool.hatch.build]
include = [
"src/OMPyCompile/libs/*"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which libraries do we need?

]


5 changes: 5 additions & 0 deletions src/Runtime/python/OMPyCompile/src/OMPyCompile/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0

from .onnxmlirdockercompile import compile

__all__ = ["compile"]
4 changes: 4 additions & 0 deletions src/Runtime/python/OMPyCompile/src/OMPyCompile/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .cli import main

if __name__ == "__main__":
raise SystemExit(main())
70 changes: 70 additions & 0 deletions src/Runtime/python/OMPyCompile/src/OMPyCompile/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import argparse
from .onnxmlirdockercompile import compile


def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="python -m OMPyCompile",
description="Command-line interface for package OMPyCompile.",
)
parser.add_argument(
"model",
type=str,
help="model file",
)
parser.add_argument(
"--debug",
action="store_true",
default=False,
help="Enable debug output (default: False)",
)
parser.add_argument(
"--compile_tag",
type=str,
default="NONE",
help="Tag to pass to the compiler via --tag= (default: NONE)",
)
parser.add_argument(
"--compile_options",
type=str,
default="",
help="Additional options to pass to the onnx-mlir compiler (default: '')",
)
parser.add_argument(
"--compiler_image_name",
type=str,
default="ghcr.io/onnxmlir/onnx-mlir-dev",
help="Container image name to use for compilation",
)
parser.add_argument(
"--container_engine",
type=str,
default=None,
choices=["docker", "podman", None],
help=(
"Container engine to use: 'docker', 'podman', or None to auto-detect "
"(default: None)"
),
)
parser.add_argument(
"--compiler_path",
type=str,
default=None,
help=(
"Path to the onnx-mlir compiler binary. Required when using a custom "
"compiler_image_name that is not in the built-in image dictionary."
),
)
return parser


def main(argv=None) -> int:
parser = build_parser()
args = parser.parse_args(argv)

kwargs = vars(args)
model = kwargs.pop("model")
compiled_model = compile(model, **kwargs)
print(f"Compiled to {compiled_model}")

return 0
Loading
Loading