-
Notifications
You must be signed in to change notification settings - Fork 170
[onert] Refactor tensorinfo to be more pythonic and robust #16199
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
base: master
Are you sure you want to change the base?
Conversation
This commit removes unused and broken tensorinfo() function from the
common Python module.
>>> import onert
>>> onert.tensorinfo()
Traceback (most recent call last):
File ".../onert/common/basesession.py", line 189, in tensorinfo
return infer.nnfw_tensorinfo()
^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'onert.native.libnnfw_api_pybind.infer' has no attribute 'nnfw_tensorinfo'
Removing this function allows to properly use the native tensorinfo
structure from the onert top level module.
>>> import onert
>>> onert.tensorinfo()
<onert.native.libnnfw_api_pybind.tensorinfo object at 0x7601fe0916f0>
ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy <a.bokowy@samsung.com>
This commit refactors the tensorinfo structure to be easier to use on the Python side. Instead of exporting C dimension array which needs to be trimmed on the Python side, now it exports Python tuple with length equal to tensor rank. Also, the entire structure is designed to be immutable to improve the understanding of the API concept - altering the info structure will not change the tensor. The new tensorinfo structure consists of a data type and the tensor shape. The data type is a dedicated type exposed by the native Python library. It provides a direct mapping between numpy data type and the internal NNFW data type. Additionally, it allows to expose all internal data types (quantized types) which was not possible with previous approach - there is no 1:1 mapping between C++ types and NNFW types. ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy <a.bokowy@samsung.com>
|
This is still work in progress (hence a draft PR), because I'm investigating the possibility to improve the shape type which right now is a simple vector of integers. The problem with that approach is the magic value Anyway, what do you mean (in general) about such refactoring for the |
|
It might be better not to expose the C++ Regarding shape, rather than a custom dim type, a simpler and more natural approach on the Python side is to accept tuples or numpy arrays and normalize Example: def _normalize_shape(shape):
if isinstance(shape, np.ndarray):
shape = shape.tolist()
shp = tuple(-1 if d is None else int(d) for d in shape)
if len(shp) == 0 or any((d < -1 or d == 0) for d in shp):
raise ValueError("invalid shape")
return shp
class TensorInfo:
def __init__(self, dtype, shape):
self.dtype = dtype
self.shape = _normalize_shape(shape)
...# Usage
shape = sess.get_inputs_tensorinfo()[0].shapeThis way users always work with a clean Python |
And what about the data types? The numpy data types do not have quantized types. Should we expose these in a similar way pytorch does it - using our own types i.e. |
I actually hadn’t considered the data types enough, but you’re right. Since numpy doesn’t cover quantized types, we can’t rely on |
This commit refactors the
tensorinfostructure to be easier to use on the Python side. Instead of exporting C dimension array which needs to be trimmed on the Python side, now it exports Python tuple with length equal to tensor rank. Also, the entire structure is designed to be immutable to improve the understanding of the API concept - altering the info structure will not change the tensor.The new
tensorinfostructure consists of a data type and the tensor shape. The data type is a dedicated type exposed by the native Python library. It provides a direct mapping between numpy data type and the internal NNFW data type. Additionally, it allows to expose all internal data types (quantized types) which was not possible with previous approach - there is no 1:1 mapping between C++ types and NNFW types.New usage:
ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy a.bokowy@samsung.com