Commit 26f88f6
authored
fix: add inference mutex to Text Embedding and Text-to-Image (#1060)
## Description
Adds thread-safety to Text Embeddings and Text-to-Image models mirroring
what was already done for models inheriting from VisionModel and VAD.
### 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
- [x] iOS
- [x] Android
### Testing instructions
Use the following app screen and try to trigger the race condition
before fix and verify that it doesn't occur after applying the fix. You
can use `adb logcat | grep -E "FATAL|SIGSEGV|backtrace"` to observe the
error on Android.
```ts
import React, { useState } from 'react';
import { Button, ScrollView, Text, View } from 'react-native';
import {
BK_SDM_TINY_VPRED_512,
TextToImageModule,
} from 'react-native-executorch';
import {
CLIP_VIT_BASE_PATCH32_TEXT,
TextEmbeddingsModule,
} from 'react-native-executorch';
import { FSMN_VAD, VADModule } from 'react-native-executorch';
const DELAY_MS = 50; // tune this so that forward() is running when delete() is called
const MODEL_VAD = {
name: 'VAD',
load: (onProgress: (p: number) => void) =>
VADModule.fromModelName(FSMN_VAD, onProgress),
input: () => new Float32Array(16000 * 300),
};
const MODEL_TEXT_EMBEDDINGS = {
name: 'TextEmbeddings',
load: (onProgress: (p: number) => void) =>
TextEmbeddingsModule.fromModelName(CLIP_VIT_BASE_PATCH32_TEXT, onProgress),
input: () => 'hello world',
};
const MODEL_TEXT_TO_IMAGE = {
name: 'TextToImage',
load: (onProgress: (p: number) => void) =>
TextToImageModule.fromModelName(BK_SDM_TINY_VPRED_512, onProgress),
input: () => 'a red apple',
};
const MODEL = MODEL_TEXT_EMBEDDINGS;
export default function RaceTest() {
const [lines, setLines] = useState<string[]>([]);
const [downloadProgress, setDownloadProgress] = useState<number | null>(null);
const log = (line: string) => setLines((prev) => [line, ...prev]);
const run = async () => {
setLines([]);
setDownloadProgress(null);
log(`model: ${MODEL.name}`);
log('loading');
const model = await MODEL.load((p) => setDownloadProgress(p));
setDownloadProgress(null);
log('running forward()');
const result = model.forward(MODEL.input());
log(`waiting ${DELAY_MS} ms`);
await new Promise((r) => setTimeout(r, DELAY_MS));
log('calling delete()');
model.delete();
try {
await result;
log('forward() completed successfully');
} catch (e: any) {
log('error: ' + (e?.message ?? String(e)));
}
};
return (
<View>
<Button title="Run Race Test" onPress={run} />
{downloadProgress !== null && (
<Text>downloading: {Math.round(downloadProgress * 100)}%</Text>
)}
<ScrollView>
{lines.map((l, i) => (
<Text key={i}>{l}</Text>
))}
</ScrollView>
</View>
);
}
```
### Screenshots
<!-- Add screenshots here, if applicable -->
### Related issues
#1055
### Checklist
- [x] 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
- [x] My changes generate no new warnings
### Additional notes
<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->1 parent cb032c6 commit 26f88f6
File tree
5 files changed
+16
-1
lines changed- packages/react-native-executorch/common/rnexecutorch
- host_objects
- models
- embeddings/text
- text_to_image
5 files changed
+16
-1
lines changedLines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
375 | 375 | | |
376 | 376 | | |
377 | 377 | | |
378 | | - | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
379 | 381 | | |
380 | 382 | | |
381 | 383 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
38 | 43 | | |
39 | 44 | | |
| 45 | + | |
40 | 46 | | |
41 | 47 | | |
42 | 48 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| |||
137 | 138 | | |
138 | 139 | | |
139 | 140 | | |
| 141 | + | |
140 | 142 | | |
141 | 143 | | |
142 | 144 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| 53 | + | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
| |||
0 commit comments