refactor!: add static factories for remaining models#937
Conversation
|
Code looks good - didn't run it though. |
|
@chmjkb is there anything to test other then running all the demo apps? |
Other than testing the demo apps I think we're good. Maybe checking if type errors occur when unsupported model names are passed would make sense. |
|
Could we start with a rebase, it will be easier to review it ;p |
|
Overall looks good, checked the apps - they work. fromCustomModel also produces expected results. Fix the docs I marked above, add more info about fromCustomModel to the docs and it's ready to ship! |
- Remove abstract load() from BaseModule (enables clean factory subclasses) - Remove no-op load() overrides from BaseLabeledModule and VisionLabeledModule - Add deps parameter to useModuleFactory, relax Config generic constraint - Update useObjectDetection and useSemanticSegmentation to pass explicit deps - Convert ClassificationModule: private constructor + static fromModelName() - Migrate useClassification from useModule to useModuleFactory Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…nd telemetry Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…to fromCustomModel Also fix the misaligned JSDoc on ObjectDetectionModule where the fromCustomConfig doc block was placed before forward() instead of before fromCustomConfig itself. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ddings, VAD, TextEmbeddings Each fromCustomModel delegates to fromModelName with modelName cast to the built-in name type, allowing custom model binaries without requiring a built-in preset name. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…eOCR hook
- Add OCRModelName = `ocr-${OCRLanguage}` type to types/ocr.ts
- Add modelName: OCRModelName to OCRProps.model
- Replace public constructor + load() with private constructor + fromModelName() + fromCustomModel()
- Rewrite useOCR to use useModuleFactory with OCRModule.fromModelName
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… controllers - VerticalOCRModule: private constructor, fromModelName, fromCustomModel - useOCR: custom hook using OCRController directly (not useModuleFactory) - useVerticalOCR: custom hook using VerticalOCRController directly Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add LLMModelName union type - Add modelName to LLMProps.model - LLMModule: private constructor, fromModelName, fromCustomModel - useLLM: add model.modelName to deps Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…tModelName type - Add SpeechToTextModelName union type - Add modelName to SpeechToTextModelConfig - SpeechToTextModule: private constructor, fromModelName, fromCustomModel - useSpeechToText: use factory, add model.modelName to deps Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…odelName type - Add TextToImageModelName union type - Add modelName to TextToImageProps.model - TextToImageModule: private constructor, fromModelName, fromCustomModel - useTextToImage: use factory, add model.modelName to deps Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ba35183 to
86b5021
Compare
| @@ -763,6 +797,7 @@ export const FCN_RESNET101_QUANTIZED = { | |||
| } as const; | |||
|
|
|||
| const SELFIE_SEGMENTATION_MODEL = `${URL_PREFIX}-selfie-segmentation/${NEXT_VERSION_TAG}/xnnpack/selfie-segmentation.pte`; | |||
There was a problem hiding this comment.
There's no xnnpack directory so this link is wrong
| const SELFIE_SEGMENTATION_MODEL = `${URL_PREFIX}-selfie-segmentation/${NEXT_VERSION_TAG}/xnnpack/selfie-segmentation.pte`; | |
| const SELFIE_SEGMENTATION_MODEL = `${URL_PREFIX}-selfie-segmentation/${NEXT_VERSION_TAG}/selfie-segmentation.pte`; |
There was a problem hiding this comment.
I think it shoudl be added. So instead of modifying the URL I'll modify the repo so it's aligned iwht other models
…Module.md Co-authored-by: Norbert Klockiewicz <Nklockiewicz12@gmail.com>
NorbertKlockiewicz
left a comment
There was a problem hiding this comment.
LGTM, I run few example apps and didn't encounter any errors.
msluszniak
left a comment
There was a problem hiding this comment.
I got an error when running text to image example:
ERROR [TypeError: this.nativeModule.generate is not a function (it is undefined)]
Code: TextToImageModule.ts
164 | seed?: number
165 | ): Promise<string> {
> 166 | const output = await this.nativeModule.generate(
| ^
167 | input,
168 | imageSize,
169 | numSteps,
Call Stack
forward (packages/react-native-executorch/src/modules/computer_vision/TextToImageModule.ts:166:52)
forward (packages/react-native-executorch/src/modules/computer_vision/TextToImageModule.ts:160:16)
generate (packages/react-native-executorch/src/hooks/computer_vision/useTextToImage.ts:99:42)
useTextToImage (packages/react-native-executorch/src/hooks/computer_vision/useTextToImage.ts:81:17)
runForward (apps/computer-vision/app/text_to_image/index.tsx:56:42)
TextToImageScreen (apps/computer-vision/app/text_to_image/index.tsx:50:19)
TouchableOpacity.props.onPress (apps/computer-vision/components/BottomBarWithTextInput.tsx:85:21)
Fixed in 27403e4 |

Description
This PR refactors the existing codebase to match what has been recently done with object detection & semantic segmentation. Namely, it changes the model initialization flow from
new Module() -> module.load()to a single, static factory, for example:fromModelName(modelName)All affected modules now follow the same two-method factory API:
Module.fromModelName(namedSources, onDownloadProgress?)— for built-in models. Requires amodelNamefield (typed literal union, e.g.LLMModelName,SpeechToTextModelName) that will be used for telemetry once that infrastructure is in place.Module.fromCustomModel(...sources, onDownloadProgress?)— for user-provided model binaries. Does not expose amodelNameparameter — users with custom models shouldn't need to know about or satisfy a type constraint that exists purely for internal tracking.Note: for now
fromCustomModelinternally delegates tofromModelNamewith a'custom'placeholder. The two methods will diverge meaningfully once telemetry is wired up.Modules migrated
ClassificationModulefromCustomModelStyleTransferModulefromCustomModelImageEmbeddingsModulefromCustomModelVADModulefromCustomModelTextEmbeddingsModulefromCustomModelOCRModulefromCustomModel, typedOCRModelNameVerticalOCRModulefromCustomModelLLMModulefromCustomModel, typedLLMModelNameSpeechToTextModulefromCustomModel, typedSpeechToTextModelNameTextToImageModulefromCustomModel, typedTextToImageModelNameObjectDetectionModulefromCustomConfig→fromCustomModelrenameSemanticSegmentationModulefromCustomConfig→fromCustomModelrenameTextToSpeechModuleis not included — handled separately.Hook updates
Hooks for modules that have a controller (
OCRModule,VerticalOCRModule,LLMModule) already used the controller directly and remain unchanged in structure. Hooks for the remaining migrated modules (useSpeechToText,useTextToImage) were updated to use the factory instead ofnew Module() + load().All hooks now include
modelNamein theiruseEffectdependency arrays, enabling correct reload behavior when switching between built-in models.Typed model name unions
Each module now has a corresponding typed union (e.g.
LLMModelName,SpeechToTextModelName,OCRModelName) derived from the existing model constants. This replaces the previous use of untypedstringand provides compile-time safety when passing model identifiers.Introduces a breaking change?
Type of change
Tested on
Testing instructions
fromCustomModelmethod.Screenshots
Related issues
Checklist
Additional notes