|
| 1 | + |
| 2 | +Difficulty to implement an an Array API for ONNX |
| 3 | +================================================ |
| 4 | + |
| 5 | +Implementing the full array API is not always easy with:epkg:`onnx`. |
| 6 | +Python is not strongly typed and many different types can be used |
| 7 | +to represent a value. Argument *axis* can be an integer or a tuple |
| 8 | +(see `min from Array API |
| 9 | +<https://data-apis.org/array-api/2022.12/API_specification/ |
| 10 | +generated/array_api.min.html>` |
| 11 | +for example). On the other side, `ReduceMin from ONNX |
| 12 | +<https://onnx.ai/onnx/operators/onnx__ReduceMin.html>`_ |
| 13 | +is considered as a tensor. |
| 14 | + |
| 15 | +Performance |
| 16 | ++++++++++++ |
| 17 | + |
| 18 | +The Array API must work in eager mode and for every operation, |
| 19 | +it generates an ONNX graph and executes it with a specific |
| 20 | +backend. It can be:epkg:`numpy`,:epkg:`onnxruntime` or any other |
| 21 | +backend. The generation of every graph takes a significant amount of time. |
| 22 | +It must be avoided. These graphs are cached. But a graph can be reused |
| 23 | +only if the inputs - by ONNX semantic - change. If a parameter change, |
| 24 | +a new graph must be cached. Method:meth:`JitEager.make_key` |
| 25 | +generates a unique key based on the input it receives, |
| 26 | +the signature of the function to call. If the key is the same, |
| 27 | +a cached onnx can be reused on the second call. |
| 28 | + |
| 29 | +However, eager mode - use a small single onnx graph for every operation - |
| 30 | +is not the most efficient one. At the same time, the design must allow |
| 31 | +to merge every needed operation into a bigger graph. |
| 32 | +Bigger graphs can be more easily optimized by the backend. |
| 33 | + |
| 34 | +Input vs parameter |
| 35 | +++++++++++++++++++ |
| 36 | + |
| 37 | +An input is a tensor or array, a parameter is any other type. |
| 38 | +Following onnx semantic, an input is variable, a parameter is frozen |
| 39 | +cannot be changed. It is a constant. A good design would be |
| 40 | +to considered any named input (`**kwargs`) a parameter and |
| 41 | +any input (`*args`) a tensor. But the Array API does not follow that |
| 42 | +design. Function `astype |
| 43 | +<https://data-apis.org/array-api/2022.12/API_specification/ |
| 44 | +generated/array_api.astype.html>_` |
| 45 | +takes two inputs. Operator `Cast |
| 46 | +<https://onnx.ai/onnx/operators/onnx__Cast.html>_` |
| 47 | +takes one input and a frozen parameter `to`. |
| 48 | +And python allows `astype(x, dtype)` as well as `astype(x, dtype=dtype)` |
| 49 | +unless the signature enforces one call over another type. |
| 50 | +There may be ambiguities from time to time. |
| 51 | +Beside, from onnx point of view, argument dtype should be named. |
| 52 | + |
| 53 | +Tensor type |
| 54 | ++++++++++++ |
| 55 | + |
| 56 | +An:class:`EagerTensor` must be used to represent any tensor. |
| 57 | +This class defines the backend to use as well. |
| 58 | +`EagerNumpyTensor` for:epkg:`numpy`, `EagerOrtTensor` |
| 59 | +for:epkg:`onnxruntime`. Since the Array API is new, |
| 60 | +existing packages do not fully support the API if they support it |
| 61 | +(:epkg:`scikit-learn`). Some numpy array may still be used. |
| 62 | + |
| 63 | +Inplace |
| 64 | ++++++++ |
| 65 | + |
| 66 | +ONNX has no notion of inplace computation. Therefore something |
| 67 | +like `coefs[:, 1] = 1` is not valid unless some code is written |
| 68 | +to create another tensor. The current design supports some of these |
| 69 | +by storing every call to `__setitem__`. The user sees `coefs` |
| 70 | +but the framework sees that `coefs` holds a reference to another |
| 71 | +tensor. That's the one the framework needs to use. However, since |
| 72 | +`__setitem__` is used for efficiency, it becomes less than efficient |
| 73 | +with this design and should be avoided. This assumption may be true |
| 74 | +when the backend is relying on CPU but not on GPU. |
| 75 | +A function such as `empty |
| 76 | +<https://data-apis.org/array-api/2022.12/API_specification/ |
| 77 | +generated/array_api.astype.html>`_ should be avoided as it |
| 78 | +has to be followed by calls to `__setitem__`. |
| 79 | + |
| 80 | +Eager or compilation |
| 81 | +++++++++++++++++++++ |
| 82 | + |
| 83 | +Eager mode is what the Array API implies. |
| 84 | +Every function is converted into an ONNX graph based |
| 85 | +on its inputs without any knownledge of how these inputs |
| 86 | +were obtained. This graph is then executed before going |
| 87 | +to the next call of a function from the API. |
| 88 | +The conversion of a machine learned model |
| 89 | +into ONNX implies the gathering of all these operations |
| 90 | +into a graph. It means using a mode that records all the function |
| 91 | +calls to compile every tiny onnx graph into a unique graph. |
| 92 | + |
| 93 | +Iterators and Reduction |
| 94 | ++++++++++++++++++++++++ |
| 95 | + |
| 96 | +An efficient implementation of function |
| 97 | +:func:`numpy.any` or:func:`numpy.all` returns |
| 98 | +as soon as the result is known.:func:`numpy.all` is |
| 99 | +false whenever the first false condition is met. |
| 100 | +Same goes for:func:`numpy.any` which is true |
| 101 | +whenever the first true condition is met. |
| 102 | +There is no such operator in ONNX (<= 20) because |
| 103 | +it is unlikely to appear in a machine learned model. |
| 104 | +However, it is highly used when two results are |
| 105 | +compared in unit tests. The ONNX implementation is |
| 106 | +not efficient due to that reason but it only impacts |
| 107 | +the unit tests. |
| 108 | + |
| 109 | +Types |
| 110 | ++++++ |
| 111 | + |
| 112 | +:epkg:`onnx` supports more types than:epkg:`numpy` does. |
| 113 | +It is not always easy to deal with bfloat16 or float8 types. |