|
| 1 | +# Python Operator |
| 2 | + |
| 3 | +**Deprecation Note: This feature is deprecated and no longer supported, please refer to [onnxruntime_customops](https://github.com/microsoft/ort-customops) project for this function.** |
| 4 | + |
| 5 | +The Python Operator provides the capability to easily invoke any custom Python code within a single node of an ONNX graph using ONNX Runtime. This can be useful for quicker experimentation when a model requires operators that are not officially supported in ONNX and ONNX Runtime, particularly if there is already a Python implementation for the required functionality. This should be used with discretion in production scenarios, and all security or other risks should be considered beforehand. |
| 6 | + |
| 7 | +## Design Overview |
| 8 | +The feature can be found under [onnxruntime/core/language_interop_ops](../onnxruntime/core/language_interop_ops). |
| 9 | +Here is a chart of calling sequence: |
| 10 | +<pre> |
| 11 | +onnxruntime python capi script |
| 12 | + | | | |
| 13 | + | ------------------------------> | | |
| 14 | + | call with tensor(s) | ------------------------------> | |
| 15 | + | | call with numpy(s) | |
| 16 | + | | | compute |
| 17 | + | | <------------------------------ | |
| 18 | + | <------------------------------ | return numpys(s) | |
| 19 | + | return tensor(s) | | |
| 20 | +</pre> |
| 21 | + |
| 22 | +## How to Use |
| 23 | +### Step 1 |
| 24 | +Build onnxruntime with `--config Release --enable_language_interop_ops --build_wheel` and pip install the latest wheel file. |
| 25 | + |
| 26 | +### Step 2 |
| 27 | +Create an onnx model containing Python operator nodes: |
| 28 | +```python |
| 29 | +ad1_node = helper.make_node('Add', ['A','B'], ['S']) |
| 30 | +mul_node = helper.make_node('Mul', ['C','D'], ['P']) |
| 31 | +py1_node = helper.make_node(op_type = 'PyOp', #required, must be 'PyOp' |
| 32 | + inputs = ['S','P'], #required |
| 33 | + outputs = ['L','M','N'], #required |
| 34 | + domain = 'pyopmulti_1', #required, must be unique |
| 35 | + input_types = [TensorProto.FLOAT, TensorProto.FLOAT], #required |
| 36 | + output_types = [TensorProto.FLOAT, TensorProto.FLOAT, TensorProto.FLOAT], #required |
| 37 | + module = 'mymodule', #required |
| 38 | + class_name = 'Multi_1', #required |
| 39 | + compute = 'compute', #optional, 'compute' by default |
| 40 | + W1 = '5', W2 = '7', W3 = '9') #optional, must all be strings |
| 41 | +ad2_node = helper.make_node('Add', ['L','M'], ['H']) |
| 42 | +py2_node = helper.make_node('PyOp',['H','N','E'],['O','W'], domain = 'pyopmulti_2', |
| 43 | + input_types = [TensorProto.FLOAT, TensorProto.FLOAT, TensorProto.FLOAT], |
| 44 | + output_types = [TensorProto.FLOAT, TensorProto.FLOAT], |
| 45 | + module = 'mymodule', class_name = 'Multi_2') |
| 46 | +sub_node = helper.make_node('Sub', ['O','W'], ['F']) |
| 47 | +graph = helper.make_graph([ad1_node,mul_node,py1_node,ad2_node,py2_node,sub_node], 'multi_pyop_graph', [A,B,C,D,E], [F]) |
| 48 | +model = helper.make_model(graph, producer_name = 'pyop_model') |
| 49 | +onnx.save(model, './model.onnx') |
| 50 | +``` |
| 51 | +### Step 3 |
| 52 | +Implement mymodule.py: |
| 53 | +```python |
| 54 | +class Multi_1: |
| 55 | + def __init__(self, W1, W2, W3): |
| 56 | + self.W1 = int(W1) |
| 57 | + self.W2 = int(W2) |
| 58 | + self.W3 = int(W3) |
| 59 | + def compute(self, S, P): |
| 60 | + ret = S + P |
| 61 | + return ret + self.W1, ret + self.W2, ret + self.W3 |
| 62 | +class Multi_2: |
| 63 | + def compute(self, *kwargs): |
| 64 | + return sum(kwargs[0:-1]), sum(kwargs[1:]) |
| 65 | +``` |
| 66 | +### Step 4 |
| 67 | +Copy mymodule.py into Python sys.path, then run the model with onnxruntime python API. On Windows, please set PYTHONHOME beforehand. It should point to directory where the python is installed, such as C:\Python37 or C:\ProgramData\Anaconda3\envs\myconda1 if it is in conda. |
| 68 | + |
| 69 | +## Supported Data Types |
| 70 | +* TensorProto.BOOL |
| 71 | +* TensorProto.UINT8 |
| 72 | +* TensorProto.UINT16 |
| 73 | +* TensorProto.UINT32 |
| 74 | +* TensorProto.INT16 |
| 75 | +* TensorProto.INT32 |
| 76 | +* TensorProto.FLOAT |
| 77 | +* TensorProto.DOUBLE |
| 78 | + |
| 79 | +## Limitations |
| 80 | +* Inferencing and compiling environments must be installed with same version of python. |
| 81 | +* On Windows, `--config Debug` has known issues. Please build with `--config RelWithDebInfo` if debugging symbols are needed. |
| 82 | +* Due to Python C API restrictions, multi-threading is disabled so Python operators will run sequentially. |
| 83 | + |
| 84 | +## Test Coverage |
| 85 | +The operator has been tested on multiple platforms, with or without conda: |
| 86 | + |
| 87 | +Platform | Python 3.5 | Python 3.6 | Python 3.7 |
| 88 | +----------- | ------------| ----------- | ----------- |
| 89 | +Windows | (conda) passed | (conda) passed | passed |
| 90 | +Linux | (conda) passed | (conda) passed | passed |
| 91 | +Mac | (conda) passed | (conda) passed | (conda) passed |
| 92 | + |
| 93 | +## Example |
| 94 | +Developers could resort to PyOp during model conversion for missing operators: |
| 95 | +```python |
| 96 | +import os |
| 97 | +import numpy as np |
| 98 | +from onnx import * |
| 99 | +from skl2onnx import convert_sklearn |
| 100 | +from skl2onnx.common.data_types import FloatTensorType |
| 101 | +from skl2onnx.common.utils import check_input_and_output_numbers |
| 102 | + |
| 103 | +X = np.array([[1, 1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]],dtype=np.single) |
| 104 | +nmf = NMF(n_components=2, init='random', random_state=0) |
| 105 | +W = np.array(nmf.fit_transform(X), dtype=np.single) |
| 106 | + |
| 107 | +def calculate_sklearn_nmf_output_shapes(operator): |
| 108 | + check_input_and_output_numbers(operator, output_count_range=1, input_count_range=1) |
| 109 | + operator.outputs[0].type.shape = operator.inputs[0].type.shape |
| 110 | + |
| 111 | +def convert_nmf(scope, operator, container): |
| 112 | + ws = [str(w) for w in W.flatten()] |
| 113 | + attrs = {'W':'|'.join(ws)} |
| 114 | + container.add_node(op_type='PyOp', name='nmf', inputs=['X'], outputs=['variable'], |
| 115 | + op_version=10, op_domain='MyDomain', module='mymodule', class_name='MyNmf', |
| 116 | + input_types=[TensorProto.FLOAT], output_types=[TensorProto.FLOAT], **attrs) |
| 117 | + |
| 118 | +custom_shape_calculators = {type(nmf): calculate_sklearn_nmf_output_shapes} |
| 119 | +custom_conversion_functions = {type(nmf): convert_nmf} |
| 120 | +initial_types = [('X', FloatTensorType([6,2]))] |
| 121 | +onx = convert_sklearn(nmf, '', initial_types, '', None, custom_conversion_functions, custom_shape_calculators) |
| 122 | +with th open("model.onnx", "wb") as f: |
| 123 | + f.write(onx.SerializeToString()) |
| 124 | +``` |
| 125 | +mymodule.py: |
| 126 | +```python |
| 127 | +import numpy as np |
| 128 | +class MyNmf: |
| 129 | + def __init__(self,W): |
| 130 | + A = [] |
| 131 | + for w in W.split('|'): |
| 132 | + A.append(float(w)) |
| 133 | + self.__W = np.array(A,dtype=np.single).reshape(6,2) |
| 134 | + def compute(self,X): |
| 135 | + return self.__W |
| 136 | +``` |
0 commit comments