Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make onnx package to be optional. #653

Merged
merged 4 commits into from
Feb 15, 2024
Merged
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
8 changes: 2 additions & 6 deletions .pipelines/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ stages:
python -m pip install --upgrade pip
python -m pip install --upgrade setuptools
python -m pip install onnxruntime==$(ort.version)
python -m pip install -r requirements.txt
displayName: Install requirements.txt
displayName: Install requirements

- script: |
CPU_NUMBER=8 python -m pip install .
Expand Down Expand Up @@ -283,8 +282,7 @@ stages:
python -m pip install --upgrade setuptools
python -m pip install --upgrade wheel
python -m pip install onnxruntime==$(ort.version)
python -m pip install -r requirements.txt
displayName: Install requirements.txt
displayName: Install requirements

- script: |
python -c "import onnxruntime;print(onnxruntime.__version__)"
Expand Down Expand Up @@ -419,7 +417,6 @@ stages:
call activate pyenv
python -m pip install --upgrade pip
python -m pip install onnxruntime==$(ort.version)
python -m pip install -r requirements.txt
python -m pip install -r requirements-dev.txt
displayName: Install requirements{-dev}.txt and cmake python modules

Expand Down Expand Up @@ -653,7 +650,6 @@ stages:
python3 -m pip install --upgrade pip; \
python3 -m pip install --upgrade setuptools; \
python3 -m pip install onnxruntime-gpu==$(ORT_VERSION); \
python3 -m pip install -r requirements.txt; \
python3 -m pip install -v --config-settings "ortx-user-option=use-cuda" . ; \
python3 -m pip install $(TORCH_VERSION) ; \
python3 -m pip install -r requirements-dev.txt; \
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ python -m pip install git+https://github.com/microsoft/onnxruntime-extensions.gi

## Usage

## 1. Generate the pre-/post- processing ONNX model
With onnxruntime-extensions Python package, you can easily get the ONNX processing graph by converting them from Huggingface transformer data processing classes, check the following API for details.
## 1. Generation of Pre-/Post-Processing ONNX Model
The `onnxruntime-extensions` Python package provides a convenient way to generate the ONNX processing graph. This can be achieved by converting the Huggingface transformer data processing classes into the desired format. For more detailed information, please refer to the API below:

```python
help(onnxruntime_extensions.gen_processing_models)
```
### NOTE: These data processing model can be merged into other model [onnx.compose](https://onnx.ai/onnx/api/compose.html) if needed.
### NOTE:
The generation of model processing requires the **ONNX** package to be installed. The data processing models generated in this manner can be merged with other models using the [onnx.compose](https://onnx.ai/onnx/api/compose.html) if needed.

## 2. Using Extensions for ONNX Runtime inference

### Python
Expand Down
79 changes: 56 additions & 23 deletions onnxruntime_extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,70 @@

__author__ = "Microsoft"

__all__ = [
'gen_processing_models',
'ort_inference',
'get_library_path',
'Opdef', 'onnx_op', 'PyCustomOpDef', 'PyOp',
'enable_py_op',
'expand_onnx_inputs',
'hook_model_op',
'default_opset_domain',
'OrtPyFunction', 'PyOrtFunction',
'optimize_model',
'make_onnx_model',
'ONNXRuntimeError',
'hash_64',
'__version__',
]

from ._version import __version__
from ._ocos import get_library_path
from ._ocos import Opdef, PyCustomOpDef
from ._ocos import hash_64
from ._ocos import enable_py_op
from ._ocos import expand_onnx_inputs
from ._ocos import hook_model_op
from ._ocos import default_opset_domain
from ._cuops import * # noqa
from ._ortapi2 import OrtPyFunction as PyOrtFunction # backward compatibility
from ._ortapi2 import OrtPyFunction, ort_inference, optimize_model, make_onnx_model
from ._ortapi2 import ONNXRuntimeError, ONNXRuntimeException
from .cvt import gen_processing_models


_lib_only = False

try:
import onnx # noqa
import onnxruntime # noqa
except ImportError:
_lib_only = True
pass


_offline_api = [
"gen_processing_models",
"ort_inference",
"OrtPyFunction",
"PyOrtFunction",
"optimize_model",
"make_onnx_model",
"ONNXRuntimeError",
]

__all__ = [
"get_library_path",
"Opdef",
"onnx_op",
"PyCustomOpDef",
"PyOp",
"enable_py_op",
"expand_onnx_inputs",
"hook_model_op",
"default_opset_domain",
"hash_64",
"__version__",
]

# rename the implementation with a more formal name
onnx_op = Opdef.declare
PyOp = PyCustomOpDef


if _lib_only:

def _unimplemented(*args, **kwargs):
raise NotImplementedError("ONNX or ONNX Runtime is not installed")

gen_processing_models = _unimplemented
OrtPyFunction = _unimplemented
ort_inference = _unimplemented

else:
__all__ += _offline_api

from ._cuops import * # noqa
from ._ortapi2 import hook_model_op
from ._ortapi2 import expand_onnx_inputs
from ._ortapi2 import OrtPyFunction, ort_inference, optimize_model, make_onnx_model
from ._ortapi2 import OrtPyFunction as PyOrtFunction # backward compatibility
from ._ortapi2 import ONNXRuntimeError, ONNXRuntimeException # noqa
from .cvt import gen_processing_models
123 changes: 22 additions & 101 deletions onnxruntime_extensions/_ocos.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,48 @@
"""
import os
import sys
import copy
import glob
import onnx
from onnx import helper


def _search_cuda_dir():
paths = os.getenv('PATH', '').split(os.pathsep)
paths = os.getenv("PATH", "").split(os.pathsep)
for path in paths:
for filename in glob.glob(os.path.join(path, 'cudart64*.dll')):
for filename in glob.glob(os.path.join(path, "cudart64*.dll")):
return os.path.dirname(filename)

return None


if sys.platform == 'win32':
if sys.platform == "win32":
from . import _version # noqa: E402
if hasattr(_version, 'cuda'):

if hasattr(_version, "cuda"):
cuda_path = _search_cuda_dir()
if cuda_path is None:
raise RuntimeError(
"Cannot locate CUDA directory in the environment variable for GPU package")
raise RuntimeError("Cannot locate CUDA directory in the environment variable for GPU package")

os.add_dll_directory(cuda_path)


from ._extensions_pydll import ( # noqa
PyCustomOpDef, enable_py_op, add_custom_op, hash_64, default_opset_domain)
PyCustomOpDef,
enable_py_op,
add_custom_op,
hash_64,
default_opset_domain,
)


def get_library_path():
"""
The custom operator library binary path
:return: A string of this library path.
"""
mod = sys.modules['onnxruntime_extensions._extensions_pydll']
mod = sys.modules["onnxruntime_extensions._extensions_pydll"]
return mod.__file__


class Opdef:

_odlist = {}

def __init__(self, op_type, func):
Expand All @@ -57,14 +58,14 @@ def __init__(self, op_type, func):

@staticmethod
def declare(*args, **kwargs):
if len(args) > 0 and hasattr(args[0], '__call__'):
if len(args) > 0 and hasattr(args[0], "__call__"):
raise RuntimeError("Unexpected arguments {}.".format(args))
# return Opdef._create(args[0])
return lambda f: Opdef.create(f, *args, **kwargs)

@staticmethod
def create(func, *args, **kwargs):
name = kwargs.get('op_type', None)
name = kwargs.get("op_type", None)
op_type = name or func.__name__
opdef = Opdef(op_type, func)
od_id = id(opdef)
Expand All @@ -76,15 +77,15 @@ def create(func, *args, **kwargs):
opdef._nativedef.op_type = op_type
opdef._nativedef.obj_id = od_id

inputs = kwargs.get('inputs', None)
inputs = kwargs.get("inputs", None)
if inputs is None:
inputs = [PyCustomOpDef.dt_float]
opdef._nativedef.input_types = inputs
outputs = kwargs.get('outputs', None)
outputs = kwargs.get("outputs", None)
if outputs is None:
outputs = [PyCustomOpDef.dt_float]
opdef._nativedef.output_types = outputs
attrs = kwargs.get('attrs', None)
attrs = kwargs.get("attrs", None)
if attrs is None:
attrs = {}
elif isinstance(attrs, (list, tuple)):
Expand All @@ -106,16 +107,15 @@ def cast_attributes(self, attributes):
elif self._nativedef.attrs[k] == PyCustomOpDef.dt_string:
res[k] = v
else:
raise RuntimeError("Unsupported attribute type {}.".format(
self._nativedef.attrs[k]))
raise RuntimeError("Unsupported attribute type {}.".format(self._nativedef.attrs[k]))
return res


def _on_pyop_invocation(k_id, feed, attributes):
if k_id not in Opdef._odlist:
raise RuntimeError(
"Unable to find function id={}. "
"Did you decorate the operator with @onnx_op?.".format(k_id))
"Unable to find function id={}. " "Did you decorate the operator with @onnx_op?.".format(k_id)
)
op_ = Opdef._odlist[k_id]
rv = op_.body(*feed, **op_.cast_attributes(attributes))
if isinstance(rv, tuple):
Expand All @@ -127,86 +127,7 @@ def _on_pyop_invocation(k_id, feed, attributes):
res = tuple(res)
else:
res = (rv.shape, rv.flatten().tolist())
return (k_id, ) + res


def _ensure_opset_domain(model):
op_domain_name = default_opset_domain()
domain_missing = True
for oi_ in model.opset_import:
if oi_.domain == op_domain_name:
domain_missing = False

if domain_missing:
model.opset_import.extend(
[helper.make_operatorsetid(op_domain_name, 1)])

return model


def expand_onnx_inputs(model, target_input, extra_nodes, new_inputs):
"""
Replace the existing inputs of a model with the new inputs, plus some extra nodes
:param model: The ONNX model loaded as ModelProto
:param target_input: The input name to be replaced
:param extra_nodes: The extra nodes to be added
:param new_inputs: The new input (type: ValueInfoProto) sequence
:return: The ONNX model after modification
"""
graph = model.graph
new_inputs = [n for n in graph.input if n.name !=
target_input] + new_inputs
new_nodes = list(model.graph.node) + extra_nodes
new_graph = helper.make_graph(
new_nodes, graph.name, new_inputs, list(graph.output), list(graph.initializer))

new_model = copy.deepcopy(model)
new_model.graph.CopyFrom(new_graph)

return _ensure_opset_domain(new_model)


def hook_model_op(model, node_name, hook_func, input_types):
"""
Add a hook function node in the ONNX Model, which could be used for the model diagnosis.
:param model: The ONNX model loaded as ModelProto
:param node_name: The node name where the hook will be installed
:param hook_func: The hook function, callback on the model inference
:param input_types: The input types as a list
:return: The ONNX model with the hook installed
"""

# onnx.shape_inference is very unstable, useless.
# hkd_model = shape_inference.infer_shapes(model)
hkd_model = model

n_idx = 0
hnode, nnode = (None, None)
nodes = list(hkd_model.graph.node)
brkpt_name = node_name + '_hkd'
optype_name = "op_{}_{}".format(hook_func.__name__, node_name)
for n_ in nodes:
if n_.name == node_name:
input_names = list(n_.input)
brk_output_name = [i_ + '_hkd' for i_ in input_names]
hnode = onnx.helper.make_node(
optype_name, n_.input, brk_output_name, name=brkpt_name, domain=default_opset_domain())
nnode = n_
del nnode.input[:]
nnode.input.extend(brk_output_name)
break
n_idx += 1

if hnode is None:
raise ValueError("{} is not an operator node name".format(node_name))

repacked = nodes[:n_idx] + [hnode, nnode] + nodes[n_idx+1:]
del hkd_model.graph.node[:]
hkd_model.graph.node.extend(repacked)

Opdef.create(hook_func, op_type=optype_name,
inputs=input_types, outputs=input_types)
return _ensure_opset_domain(hkd_model)
return (k_id,) + res


PyCustomOpDef.install_hooker(_on_pyop_invocation)
Loading
Loading