[RNE Rewrite] feat: add input validation and JSI conversion utilities#1296
Conversation
|
@msluszniak This is still a draft and there are still quite some rough edges to polish, but could you take a look if this approach is something you would agree on and if there are any other aspects I missed. I was thinking that we could also use this PR for #1268 since adding the validation/conversion helpers modifies quite a lot of core code so it would be good to review and streamline also the other aspects. |
msluszniak
left a comment
There was a problem hiding this comment.
I like this concept. I combines elegance of short macros with flexibility of manual code. Just very small nits, will dive into it later today / tomorrow. I like usage of std::format, we previously tried use it in old codebase, but because it required some version of iOS we eventually dropped it, but I don't think it will be a case now.
…ntime error handling
…operty for ArrayBuffer
… switch braces in model.cpp
… error message in model.cpp
|
The |
…ary asType template
…ness in TensorHostObject
…p and improve fromJs error reporting
…s::asType cleanups, and organize includes
…hape constraints syntax
… and model helpers
|
No more comments from my side, I'm checking demo apps on physical android and ios simulator. |
|
One comment not related to the changes but spotted in demo app testing, can we fix the overlapping android nav bar with buttons in classifications section? |
msluszniak
left a comment
There was a problem hiding this comment.
Two small comments which where strictly demo app related, so they might be addressed in a separate PR. 🚀
…ion screen (#1299) ## Description Follow-up PR to two problems raised in review of #1296 - #1296 (comment) - #1296 (comment) It - Fixes the classification example screen to not overlap with Android navigation buttons. - Leaves the default YOLO threshold as `0.25` since it is the official recommended default value (see: [ultralytics docs](https://docs.ultralytics.com/modes/predict#fixed-shape-vs-minimum-rectangle-rect:~:text=float-,0.25,-Sets%20the%20minimum)) ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [x] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) ### Tested on - [ ] iOS - [x] Android ### Testing instructions - [ ] Run the computer-vision app on Android and confirm that there is no overlap with navigation buttons. ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have updated the documentation accordingly - [x] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. -->
Rebase onto rne-rewrite adopted #1296's rewritten model.cpp, which delegates tensor dtype/shape checks to tensor::fromJs and already supports RangeDim [min, max, step] bounds. Re-implement variable-length forward inputs on top of it: parse get_dynamic_dims once per method into cached bounds, build a SymbolicShape of RangeDims, and pass it to fromJs. Statically shaped methods keep exact validation.
Rebase onto rne-rewrite adopted #1296's rewritten model.cpp, which delegates tensor dtype/shape checks to tensor::fromJs and already supports RangeDim [min, max, step] bounds. Re-implement variable-length forward inputs on top of it: parse get_dynamic_dims once per method into cached bounds, build a SymbolicShape of RangeDims, and pass it to fromJs. Statically shaped methods keep exact validation.


Description
Refactors the C++ layer by pulling two families of scattered, copy-pasted utilities out of individual translation units and into purpose-built, reusable modules:
cpp/core/conversions.{h,cpp}JSI -> C++ type coercionscpp/core/tensor_helpers.{h,cpp}: tensor acquisition, locking, and shape/dtype validationMotivation
The old code had two recurring problems that became impossible to ignore as the extension surface grew:
JSI coercion was inlined everywhere
Every file that needed to read a number, string, or array out of a
jsi::Valuehad its own copy of the same defensive checks, cast chains, and error-message construction. A bug or policy change (e.g. hownullis treated, or which format an error message takes) had to be hunted down and patched in every translation unit independently.Tensor validation was duplicated across all extension operations
Every operation in
box_ops,image_ops,operations, andtokenizerthat accepted aTensorargument re-implemented the same sequence: unwrap thejsi::Value, cast toTensorHostObject, acquire the appropriateshared/exclusive lock, and assert dtype + shape constraints. This led to subtle inconsistencies in error messages and locking strategies between operations.
Introduces a breaking change?
Type of change
Tested on
Testing instructions
Screenshots
Related issues
Closes #1270
Closes #1268
Checklist
Additional notes
Since this PR changes quite a lot of stuff in
core/it can also be used for #1268