Skip to content

Commit 212a821

Browse files
jakmroMateusz Sluszniak
andauthored
refactor: Change model constants to preset objects (#477)
## Description Change model constants to preset objects ### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update (improves or adds clarity to existing documentation) ### Tested on - [x] iOS - [ ] Android ### 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 --------- Co-authored-by: Mateusz Sluszniak <sluszmat@amazon.com>
1 parent cff01b8 commit 212a821

69 files changed

Lines changed: 1900 additions & 1428 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.cspell-wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ QINT
5353
FNUZ
5454
wordlist
5555
jitpack
56+
coreml
57+
mobilenetv

apps/computer-vision/app/classification/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ export default function ClassificationScreen() {
1313
);
1414
const [imageUri, setImageUri] = useState('');
1515

16-
const model = useClassification({
17-
modelSource: EFFICIENTNET_V2_S,
18-
});
16+
const model = useClassification({ model: EFFICIENTNET_V2_S });
1917
const { setGlobalGenerating } = useContext(GeneratingContext);
2018
useEffect(() => {
2119
setGlobalGenerating(model.isGenerating);

apps/computer-vision/app/image_segmentation/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ const numberToColor: number[][] = [
6161
];
6262

6363
export default function ImageSegmentationScreen() {
64-
const model = useImageSegmentation({
65-
modelSource: DEEPLAB_V3_RESNET50,
66-
});
64+
const model = useImageSegmentation({ model: DEEPLAB_V3_RESNET50 });
6765
const { setGlobalGenerating } = useContext(GeneratingContext);
6866
useEffect(() => {
6967
setGlobalGenerating(model.isGenerating);

apps/computer-vision/app/object_detection/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ export default function ObjectDetectionScreen() {
2020
height: number;
2121
}>();
2222

23-
const ssdLite = useObjectDetection({
24-
modelSource: SSDLITE_320_MOBILENET_V3_LARGE,
25-
});
23+
const ssdLite = useObjectDetection({ model: SSDLITE_320_MOBILENET_V3_LARGE });
2624
const { setGlobalGenerating } = useContext(GeneratingContext);
2725
useEffect(() => {
2826
setGlobalGenerating(ssdLite.isGenerating);

apps/computer-vision/app/ocr/index.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import Spinner from 'react-native-loading-spinner-overlay';
22
import { BottomBar } from '../../components/BottomBar';
33
import { getImage } from '../../utils';
4-
import {
5-
DETECTOR_CRAFT_800,
6-
RECOGNIZER_EN_CRNN_128,
7-
RECOGNIZER_EN_CRNN_256,
8-
RECOGNIZER_EN_CRNN_512,
9-
useOCR,
10-
} from 'react-native-executorch';
4+
import { useOCR, OCR_ENGLISH } from 'react-native-executorch';
115
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
126
import ImageWithBboxes2 from '../../components/ImageWithOCRBboxes';
137
import React, { useContext, useEffect, useState } from 'react';
@@ -22,15 +16,7 @@ export default function OCRScreen() {
2216
height: number;
2317
}>();
2418

25-
const model = useOCR({
26-
detectorSource: DETECTOR_CRAFT_800,
27-
recognizerSources: {
28-
recognizerLarge: RECOGNIZER_EN_CRNN_512,
29-
recognizerMedium: RECOGNIZER_EN_CRNN_256,
30-
recognizerSmall: RECOGNIZER_EN_CRNN_128,
31-
},
32-
language: 'en',
33-
});
19+
const model = useOCR({ model: OCR_ENGLISH });
3420
const { setGlobalGenerating } = useContext(GeneratingContext);
3521
useEffect(() => {
3622
setGlobalGenerating(model.isGenerating);

apps/computer-vision/app/ocr_vertical/index.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import Spinner from 'react-native-loading-spinner-overlay';
22
import { BottomBar } from '../../components/BottomBar';
33
import { getImage } from '../../utils';
4-
import {
5-
DETECTOR_CRAFT_1280,
6-
DETECTOR_CRAFT_320,
7-
RECOGNIZER_EN_CRNN_512,
8-
RECOGNIZER_EN_CRNN_64,
9-
useVerticalOCR,
10-
} from 'react-native-executorch';
4+
import { useVerticalOCR, VERTICAL_OCR_ENGLISH } from 'react-native-executorch';
115
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
126
import ImageWithBboxes2 from '../../components/ImageWithOCRBboxes';
137
import React, { useContext, useEffect, useState } from 'react';
@@ -22,15 +16,7 @@ export default function VerticalOCRScree() {
2216
height: number;
2317
}>();
2418
const model = useVerticalOCR({
25-
detectorSources: {
26-
detectorLarge: DETECTOR_CRAFT_1280,
27-
detectorNarrow: DETECTOR_CRAFT_320,
28-
},
29-
recognizerSources: {
30-
recognizerLarge: RECOGNIZER_EN_CRNN_512,
31-
recognizerSmall: RECOGNIZER_EN_CRNN_64,
32-
},
33-
language: 'en',
19+
model: VERTICAL_OCR_ENGLISH,
3420
independentCharacters: true,
3521
});
3622
const { setGlobalGenerating } = useContext(GeneratingContext);

apps/computer-vision/app/style_transfer/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import { GeneratingContext } from '../../context';
1111
import ScreenWrapper from '../../ScreenWrapper';
1212

1313
export default function StyleTransferScreen() {
14-
const model = useStyleTransfer({
15-
modelSource: STYLE_TRANSFER_CANDY,
16-
});
14+
const model = useStyleTransfer({ model: STYLE_TRANSFER_CANDY });
1715
const { setGlobalGenerating } = useContext(GeneratingContext);
1816
useEffect(() => {
1917
setGlobalGenerating(model.isGenerating);

apps/llm/app/llm/index.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ import {
1212
} from 'react-native';
1313
import SendIcon from '../../assets/icons/send_icon.svg';
1414
import Spinner from 'react-native-loading-spinner-overlay';
15-
import {
16-
LLAMA3_2_1B_QLORA,
17-
LLAMA3_2_TOKENIZER,
18-
LLAMA3_2_TOKENIZER_CONFIG,
19-
useLLM,
20-
} from 'react-native-executorch';
15+
import { useLLM, LLAMA3_2_1B_QLORA } from 'react-native-executorch';
2116
import PauseIcon from '../../assets/icons/pause_icon.svg';
2217
import ColorPalette from '../../colors';
2318
import Messages from '../../components/Messages';
@@ -35,11 +30,7 @@ function LLMScreen() {
3530
const textInputRef = useRef<TextInput>(null);
3631
const { setGlobalGenerating } = useContext(GeneratingContext);
3732

38-
const llm = useLLM({
39-
modelSource: LLAMA3_2_1B_QLORA,
40-
tokenizerSource: LLAMA3_2_TOKENIZER,
41-
tokenizerConfigSource: LLAMA3_2_TOKENIZER_CONFIG,
42-
});
33+
const llm = useLLM({ model: LLAMA3_2_1B_QLORA });
4334

4435
useEffect(() => {
4536
if (llm.error) {

apps/llm/app/llm_structured_output/index.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import {
1313
import SendIcon from '../../assets/icons/send_icon.svg';
1414
import Spinner from 'react-native-loading-spinner-overlay';
1515
import {
16-
QWEN3_TOKENIZER,
17-
QWEN3_TOKENIZER_CONFIG,
1816
useLLM,
1917
fixAndValidateStructuredOutput,
2018
getStructuredOutputPrompt,
@@ -75,12 +73,7 @@ function LLMScreen() {
7573
const textInputRef = useRef<TextInput>(null);
7674
const { setGlobalGenerating } = useContext(GeneratingContext);
7775

78-
const llm = useLLM({
79-
// try out 4B model it this one struggles with following structured output
80-
modelSource: QWEN3_1_7B_QUANTIZED,
81-
tokenizerSource: QWEN3_TOKENIZER,
82-
tokenizerConfigSource: QWEN3_TOKENIZER_CONFIG,
83-
});
76+
const llm = useLLM({ model: QWEN3_1_7B_QUANTIZED }); // try out 4B model if 1.7B struggles with following structured output
8477

8578
useEffect(() => {
8679
setGlobalGenerating(llm.isGenerating);
@@ -89,10 +82,6 @@ function LLMScreen() {
8982
const { configure } = llm;
9083
useEffect(() => {
9184
const formattingInstructions = getStructuredOutputPrompt(responseSchema);
92-
// const formattingInstructionsWithZod = getStructuredOutputPrompt(
93-
// responseSchemaWithZod
94-
// );
95-
9685
const prompt = `Your goal is to parse user's messages and return them in JSON format. Don't respond to user. Simply return JSON with user's question parsed. \n${formattingInstructions}\n /no_think`;
9786

9887
configure({

apps/llm/app/llm_tool_calling/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import SWMIcon from '../../assets/icons/swm_icon.svg';
1414
import SendIcon from '../../assets/icons/send_icon.svg';
1515
import Spinner from 'react-native-loading-spinner-overlay';
1616
import {
17-
HAMMER2_1_1_5B,
18-
HAMMER2_1_TOKENIZER,
19-
HAMMER2_1_TOKENIZER_CONFIG,
2017
useLLM,
2118
DEFAULT_SYSTEM_PROMPT,
19+
HAMMER2_1_1_5B_QUANTIZED,
2220
} from 'react-native-executorch';
2321
import PauseIcon from '../../assets/icons/pause_icon.svg';
2422
import ColorPalette from '../../colors';
@@ -41,11 +39,7 @@ function LLMToolCallingScreen() {
4139
const textInputRef = useRef<TextInput>(null);
4240
const { setGlobalGenerating } = useContext(GeneratingContext);
4341

44-
const llm = useLLM({
45-
modelSource: HAMMER2_1_1_5B,
46-
tokenizerSource: HAMMER2_1_TOKENIZER,
47-
tokenizerConfigSource: HAMMER2_1_TOKENIZER_CONFIG,
48-
});
42+
const llm = useLLM({ model: HAMMER2_1_1_5B_QUANTIZED });
4943

5044
useEffect(() => {
5145
setGlobalGenerating(llm.isGenerating);

0 commit comments

Comments
 (0)