Skip to content

[RNE Rewrite] refactor!: add better model schema contract and validation logic - #1327

Merged
barhanc merged 31 commits into
rne-rewritefrom
@bh/schema-refactor
Aug 3, 2026
Merged

[RNE Rewrite] refactor!: add better model schema contract and validation logic#1327
barhanc merged 31 commits into
rne-rewritefrom
@bh/schema-refactor

Conversation

@barhanc

@barhanc barhanc commented Jul 23, 2026

Copy link
Copy Markdown
Member

Description

Intoduces a new powerful and extensible mechanism for validating model contracts both during the runtime execution (mitigating e.g. internal error crashes when two input tensors should have the same dimension during runtime) and pipeline construction (matching model contract against a specified interface).

  • Introduces consistent, clean, simple and extensible contract for custom schema specification as an optional constant companion method get_model_schema returning string-encoded JSON. No need for constructing auxiliary tensors for specifying dynamic dimensions, multiple companions for different kinds, etc.
  • Introduces distinction between symbolic dims and concrete dims which represent dimension domains the model can require (includes constant, enumerated and range dims).
  • Adds extensible mechanism of specifying runtime constraints (such as dynamic dim equality, linearity) which is distinct from dim domain constraints.
  • Adds ergonomic helper functions which make the model spec specification in pipeline implementation concise yet clear.

Introduces a breaking change?

  • Yes
  • No

Type of change

  • 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 (Simulator only, re-exported MLX and CoreML haven't been tested)
  • Android

Testing instructions

Models that use the companion method to get the JSON schema have not yet been updated on the SWM HF as they wait for the review of this PR. For testing you can use the models at https://huggingface.co/bhanc/scratch/tree/main/ptes.

  • Build all example apps on both Android and iOS and check that all models still work

Screenshots

Related issues

Closes #1323

Checklist

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional notes

  • The updated export scripts are on the internal gitlab on the @bh/schema-workflow branch.
  • @msluszniak I haven't been able to re-export the LFM2.5 embeddings from the @nk/universal-exporter branch. I would be grateful if you could take a look.
  • @benITo47 Please take a look if you can find some use-case which this mechanism cannot express.

@barhanc barhanc self-assigned this Jul 23, 2026
@barhanc barhanc changed the title @bh/schema refactor [RNE Rewrie] refactor: add better model schema contract and validation logic Jul 23, 2026
@barhanc barhanc linked an issue Jul 23, 2026 that may be closed by this pull request
@barhanc barhanc changed the title [RNE Rewrie] refactor: add better model schema contract and validation logic [RNE Rewrite] refactor: add better model schema contract and validation logic Jul 24, 2026
@barhanc
barhanc force-pushed the @bh/schema-refactor branch 2 times, most recently from ef56a3f to cacfa78 Compare July 28, 2026 23:49
@barhanc
barhanc marked this pull request as ready for review July 29, 2026 00:41
@barhanc
barhanc requested review from benITo47 and msluszniak July 29, 2026 00:41
@barhanc barhanc added the feature PRs that implement a new feature label Jul 29, 2026
@barhanc barhanc changed the title [RNE Rewrite] refactor: add better model schema contract and validation logic [RNE Rewrite] refactor!: add better model schema contract and validation logic Jul 29, 2026
@msluszniak

Copy link
Copy Markdown
Member

@barhanc I updated the export script so know it should work correctly (at least I can export them my myself from Norbert's branch). The problem was in the newest version of transformers. Please give it a try.

Comment thread .agents/skills/model-schema-validation/SKILL.md
Comment thread packages/react-native-executorch/cpp/core/model.cpp
Comment thread packages/react-native-executorch/cpp/core/schema.cpp
Comment thread packages/react-native-executorch/cpp/core/schema.cpp
Comment thread packages/react-native-executorch/cpp/core/schema.cpp
Comment thread packages/react-native-executorch/cpp/core/schema.cpp Outdated
Comment thread packages/react-native-executorch/cpp/core/schema.cpp
Comment thread packages/react-native-executorch/cpp/core/tensor_helpers.cpp Outdated
Comment thread packages/react-native-executorch/cpp/core/tensor_helpers.cpp Outdated
Comment thread packages/react-native-executorch/src/core/schema.ts
Comment thread packages/react-native-executorch/cpp/core/schema.cpp Outdated
Comment thread packages/react-native-executorch/src/core/schema.ts Outdated
Comment thread packages/react-native-executorch/cpp/core/schema.cpp
Comment thread packages/react-native-executorch/cpp/core/model.cpp Outdated
@msluszniak

msluszniak commented Jul 30, 2026

Copy link
Copy Markdown
Member

For text embeddinga, I got two errors:
Screenshot_20260730_123847_nlp
Screenshot_20260730_123834_nlp

The rest of the text embedding models worked correctly.

@msluszniak

msluszniak commented Jul 30, 2026

Copy link
Copy Markdown
Member

Regarding vad and whisper, I only had problems with coreml variants of whisper. For all that I was able to run on Iphone SE, I get the following error:
image

barhanc added a commit that referenced this pull request Jul 30, 2026
…ns (#1339)

## Description

This PR improves the quality of native C++ error messages thrown to
JavaScript, making it easier to diagnose issues without needing to read
source code. This is a follow-up to error messages improvements
introduced in #1327.

### Introduces a breaking change?

- [ ] Yes
- [x] No

### Type of change

- [ ] Bug fix (change which fixes an issue)
- [ ] New feature (change which adds functionality)
- [ ] Documentation update (improves or adds clarity to existing
documentation)
- [x] Other (chores, tests, code style improvements etc.)

### Tested on

- [ ] iOS
- [ ] Android

### Testing instructions

N/A

### Screenshots

<!-- Add screenshots here, if applicable -->

### Related issues

<!-- Link related issues here using #issue-number -->

### Checklist

- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have updated the documentation accordingly
- [ ] My changes generate no new warnings

### Additional notes

<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->
@barhanc
barhanc force-pushed the @bh/schema-refactor branch from 42cf543 to 3f0a772 Compare July 30, 2026 20:56
@barhanc

barhanc commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

The models should be fixed now. I've updated the .pte files on the https://huggingface.co/bhanc/scratch/tree/main repo for now. After you test that everything is fine I will upload them to the SWM HF under v0.10.0 tag.

@barhanc
barhanc requested a review from msluszniak July 31, 2026 10:25

@msluszniak msluszniak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now everything worked correctly, @benITo47 are you fine with these changes?

@benITo47

benITo47 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@msluszniak

From what I've been reading, changes seem sane. One real gap for OCR: enumerated shapes are a per-dimension cross product. tensor_helpers.cpp fromJs validates each dim against its own EnumDim.choices independently, and dims.enum only exposes per-dim arrays. There's no whole-shape domain, and runtimeConstraints (eq/linear) are dim-relations, so they can't carve out a sparse set either.

For example, our CoreML detectors ship sets like {640², 960², 1280², 1280×640} for PaddleOCR. To describe that in the new schema I can only give H:{640,960,1280}, W:{640,960,1280} - whose cross product also admits 640×1280, which the PTE never compiled. fromJs green-lights it, then CoreML rejects it at execute.

While resizing to match input, im picking which shape to use (snap up or snap down), in that process I can actively select that illegal shape.

To make the per-dim schema match the compiled model I'd have to either compile the full cross product (more ANE specializations, more coreML overhead, worse performance) or drop to square-only (lose the portrait bucket). The workaround is to hardcode the whole-shape list in the task and snap to it - but then the model doesn't self-describe its contract, code breaks when hardcoded list drifts from the export, and inspectModel shows a wrong over-permissive shape set. So at minimum this should be expressible in schema.ts - a whole-shape / tuple-enum domain (or constraint kind), whatever fits the model best.

barhanc added 22 commits August 3, 2026 16:32
…pers

- Re-export schema utilities under a namespaced 'schema' export in index.ts
- Re-export types flatly using 'export type *' from ./core/schema
- Group constraint helpers under 'constr' object and inline DimRef object literals in task pipelines
- Remove redundant static dimension constraint from Whisper task pipeline
- Expand schema.h file-level docblock to detail the two-phase validation
  model (load-time validateSpec + runtime validateRuntimeConstraints),
  explain the two spec source paths (companion method vs MethodMeta
  fallback), and document output-dimension skipping behaviour pre-execution
- Improve individual function docstrings for parseModelSpecJson, validateSpec,
  and validateRuntimeConstraints to reflect actual behaviour more precisely
- Fix whisperSpeechToText decode method spec: replace named symbol strings
  for static constant dimensions with literal integers (1) to match schema
  semantics
@barhanc
barhanc force-pushed the @bh/schema-refactor branch from 4146707 to bfcbf24 Compare August 3, 2026 14:33
@barhanc

barhanc commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

@msluszniak I've rebased, updated the models on SWM HF and tested them again (the ones I could on a simulator). If there is a green light I will merge this PR.

@msluszniak

Copy link
Copy Markdown
Member

There is a green light indeed, today I tested both coreml and mlx variants of uploaded files.

@barhanc
barhanc merged commit b38a61f into rne-rewrite Aug 3, 2026
3 checks passed
@barhanc
barhanc deleted the @bh/schema-refactor branch August 3, 2026 16:43
msluszniak added a commit that referenced this pull request Aug 4, 2026
#1327 replaced `validateModelSchema` with `validateSpec` and dropped the
`get_dynamic_dims_forward` companion in favour of `get_model_schema`.

- Declare `forward` as two spec variants: `dynamic`, whose sequence dim binds
  to the exported range, and `static`, whose sequence dim binds to the single
  exported constant. The window size falls out of whichever variant matched,
  replacing both the `inputTensorMeta` shape read and the `getMethodNames()`
  probe for the old companion method.
- Declare the sequence length shared by both inputs and the logits as an
  equality runtime constraint, so a length mismatch is rejected before
  `execute` instead of surfacing as an internal backend error.
- Snap the length buckets onto the exported range's grid (and start them at
  its lower bound) rather than assuming a step of 1 from zero.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature PRs that implement a new feature refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RNE Rewrite] Proper way to handle dynamic input shapes

3 participants