-
Notifications
You must be signed in to change notification settings - Fork 170
[onert] Export dedicated OneRT data types to Python #16210
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
6f78932 to
c36e868
Compare
|
While reviewing this PR I noticed a couple of pre-existing issues (not introduced by these changes):
np.array([1, 2, 3], dtype=onert.qint8)will not work as expected. We likely need a bidirectional converter and clear usage rules so that dtype handling is both intuitive and reliable. These are not regressions introduced by this PR, but they directly affect how usable the Python API works. If possible, could you also consider addressing them when binding tensorinfo? |
|
One additional request: after the changes in this PR, could you please check that the existing python examples still run correctly? Currently our CI does not include python API validation. |
I've been thinking about extending numpy with OneRT quantized types, so then numpy will be able to convert between floats, integers and quantized types. I've never done that before though, so I will have to investigate the pros and cons of such approach. Anyway, I think that then the API will be clear and simple because it would be possible to use all numpy operations (hopefully) on quantized tensors. |
|
I also looked into this and found NumPy’s ongoing work on a new dtype system relevant here: https://numpy.org/neps/roadmap.html#extensibility
All three are accepted or under discussion, but full implementation is still ongoing. Until this new dtype system is complete, introducing something like Given this situation, I would suggest some practical options for now:
|
ragmani
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Maybe we should wait with that PR (at least until the next week)? I will also look at the possibility to extend numpy data types, because I've seen somewhere (some time ago) that it is possible but maybe not very easy to do. I will investigate how complicated it would be right now (if possible at all). I will submit my investigation result here as well. |
|
That sounds like a good idea. I’ll look forward to your investigation. |
|
@arkq Please resolve conflicts |
runtime/onert/api/python/src/bindings/nnfw_tensorinfo_bindings.cc
Outdated
Show resolved
Hide resolved
21b0b60 to
1ed7bd5
Compare
|
@ragmani, I've researched a little bit the possibility of adding custom tl;dr; your previous post is correct, |
1ed7bd5 to
cb78e0d
Compare
|
@arkq |
|
Is this PR ready for review, now? |
Yes, it's ready |
|
@arkq I looked the code to find what is the change in user's side. However, I cannot find the changes in example codes. The changes in |
NNFW types and numpy data types do not map one to one because NNFW has quantized types represented as uint8 or int16. Because of that it should be more efficient to export custom data type object which will map these two types. Additionally, for convenience, dedicated types are exported in the top-level onert Python module, so one can use them as follows: > import numpy as np, onert > np.array([2, 42, 42], dtype=onert.float32) ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy <[email protected]>
|
@glistening, there are no changes in examples because all examples use dtype from tensorinfo when creating numpy arrays. Anyway, I've just grepped the code and I've found that I should update dtypes in the |
cb78e0d to
acec594
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request refactors the OneRT Python API's type system by introducing dedicated data type objects that properly map between NNFW tensor types and numpy dtypes. Previously, types were represented as strings, which didn't adequately handle quantized types (e.g., qint8, quint8) that share underlying numpy dtypes (int8, uint8) with non-quantized types.
Key Changes:
- Replaced string-based type conversion with a new
datatypeC++ class that encapsulates NNFW_TYPE and its corresponding numpy dtype - Exposed convenient top-level type objects (onert.float32, onert.int32, onert.qint8, etc.) for easy usage with numpy
- Updated the DataLoader default parameter to use onert.float32 instead of np.float32 for consistency
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| runtime/onert/api/python/include/nnfw_api_wrapper.h | Added datatype struct to encapsulate NNFW_TYPE with numpy dtype; removed old string-based conversion function declarations; updated tensorinfo to use datatype instead of string |
| runtime/onert/api/python/src/wrapper/nnfw_api_wrapper.cc | Implemented datatype constructor with switch-case mapping for all NNFW tensor types; removed getType/getStringType functions; updated all type conversions to use datatype class; simplified getLayout control flow |
| runtime/onert/api/python/src/bindings/nnfw_tensorinfo_bindings.cc | Added pybind11 bindings for datatype class with operator overloading and property exposure; created dtypes submodule to export all type constants |
| runtime/onert/api/python/onert/init.py | Updated imports to expose dtype and tensorinfo from native bindings; added wildcard import from dtypes submodule to expose type constants at top level |
| runtime/onert/api/python/onert/experimental/train/dataloader.py | Changed default dtype parameter from np.float32 to onert.float32 for consistency with the new type system |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
My understanding is:
|
Yes, that's correct. Beside the simple test in the PR description, I've tested this PR also with: python runtime/onert/sample/minimal-python/minimal.py nnpackage/examples/v1.3.0/two_tflites/mv1.0.tfliteThe $ runtime/onert/sample/minimal-python/experimental/src/train_step_with_dataset.py \
-m nnpackage/examples/v1.3.0/two_tflites/mv1.0.tflite \
--input=dataset.npy --label=labels.npy --data_length=1 --batch_size=16
Load data
== training parameter ==
- learning_rate = 0.01
- batch_size = 16
- loss_info = {loss = MeanSquaredError, reduction = sum over batch size}
- optimizer = SGD
- num_of_trainable_ops = -1
========================
Step 1/1 - Train time: 289.558 ms/step - Train Loss: 3.4727
===================================
Average Loss: 3.4727
CategoricalAccuracy: 0.0000
Average Time: 289.5583 ms/step
===================================
nnpackage mv1.0.tflite trains successfully. |
NNFW types and numpy data types do not map one to one because NNFW has quantized types represented as uint8 or int16. Because of that it should be more efficient to export custom data type object which will map these two types.
Additionally, for convenience, dedicated types are exported in the top-level onert Python module, so one can use them as follows:
ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy [email protected]