Skip to content

Commit da4f968

Browse files
committed
docs: small-change
1 parent c45d442 commit da4f968

1 file changed

Lines changed: 164 additions & 25 deletions

File tree

README.md

Lines changed: 164 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,30 @@ console.log('Output:', outputData);
5050

5151
### Loading Models from Assets
5252

53-
Models can be loaded using `require()` for bundled assets:
53+
Models can be loaded using `require()` for bundled assets. The library automatically copies the model from your app bundle to the device's file system and caches it:
5454

5555
```typescript
56+
// Load from bundled asset
5657
const session = await ort.loadModel(require('./assets/model.onnx'));
5758
```
5859

59-
This automatically copies the model to the device's file system on first load.
60+
You can also load from a URL:
61+
62+
```typescript
63+
// Load from URL
64+
const session = await ort.loadModel({
65+
url: 'http://example.com/model.onnx'
66+
});
67+
```
68+
69+
Or from a file path:
70+
71+
```typescript
72+
// Load from file path
73+
const session = await ort.loadModel('/path/to/model.onnx');
74+
```
75+
76+
**Note:** When using `require()` or `{ url }`, the model is automatically copied to the app's files directory and cached. Subsequent loads will use the cached file for faster initialization.
6077

6178
### Hardware Acceleration
6279

@@ -131,27 +148,86 @@ const session = await ort.loadModel('model.onnx', {
131148

132149
### Loading from Buffer
133150

151+
For advanced use cases, you can load models directly from an ArrayBuffer:
152+
134153
```typescript
135154
import RNFS from 'react-native-fs';
136155

137-
// Load model file into buffer
156+
// Option 1: Load from file system
138157
const modelPath = 'path/to/model.onnx';
139-
const modelBuffer = await RNFS.readFile(modelPath, 'base64');
140-
const arrayBuffer = Uint8Array.from(atob(modelBuffer), c => c.charCodeAt(0)).buffer;
158+
const base64Data = await RNFS.readFile(modelPath, 'base64');
159+
const arrayBuffer = Uint8Array.from(atob(base64Data), c => c.charCodeAt(0)).buffer;
141160

142-
// Create session from buffer
143161
const session = await ort.loadModelFromBuffer(arrayBuffer, {
144162
executionProviders: ['nnapi']
145163
});
164+
165+
// Option 2: Load from network
166+
const response = await fetch('https://example.com/model.onnx');
167+
const arrayBuffer = await response.arrayBuffer();
168+
169+
const session = await ort.loadModelFromBuffer(arrayBuffer, {
170+
executionProviders: ['coreml']
171+
});
172+
```
173+
174+
**Use cases for `loadModelFromBuffer`:**
175+
- Loading models from encrypted storage
176+
- Downloading models from authenticated endpoints
177+
- Processing models before loading (e.g., decompression)
178+
- Dynamic model generation
179+
180+
### React Hooks
181+
182+
The library provides a convenient React hook for loading models:
183+
184+
```typescript
185+
import { useLoadModel } from 'react-native-nitro-onnxruntime';
186+
187+
function MyComponent() {
188+
const modelState = useLoadModel(require('./assets/model.onnx'), {
189+
executionProviders: ['nnapi']
190+
});
191+
192+
if (modelState.state === 'loading') {
193+
return <Text>Loading model...</Text>;
194+
}
195+
196+
if (modelState.state === 'error') {
197+
return <Text>Error: {modelState.error.message}</Text>;
198+
}
199+
200+
// modelState.state === 'loaded'
201+
const session = modelState.model;
202+
203+
// Use session for inference
204+
const runInference = async () => {
205+
const input = new Float32Array(224 * 224 * 3);
206+
const results = await session.run({
207+
[session.inputNames[0].name]: input.buffer
208+
});
209+
};
210+
211+
return <Button onPress={runInference} title="Run Inference" />;
212+
}
146213
```
147214

148215
### Memory Management
149216

217+
Sessions are automatically cleaned up by Nitro Modules when they go out of scope. However, you can manually dispose of a session to free memory immediately:
218+
150219
```typescript
151-
// Dispose of session when done
220+
// Optional: Dispose of session early to free memory immediately
152221
session.dispose();
153222
```
154223

224+
**When to call `dispose()`:**
225+
- Loading multiple models and want to free memory between loads
226+
- Memory-constrained environments
227+
- Want immediate cleanup instead of waiting for garbage collection
228+
229+
**Note:** You don't need to call `dispose()` in most cases - Nitro Modules will automatically clean up when the session is no longer referenced.
230+
155231
## API Reference
156232

157233
### `ort.getVersion()`
@@ -163,16 +239,31 @@ const version = ort.getVersion();
163239
console.log('ONNX Runtime version:', version);
164240
```
165241

166-
### `ort.loadModel(path, options?)`
242+
### `ort.loadModel(source, options?)`
167243

168-
Load an ONNX model from a file path or `require()` asset.
244+
Load an ONNX model from various sources.
169245

170246
**Parameters:**
171-
- `path`: `string` | `number` (from `require()`) - Path to the model file
247+
- `source`: `string` | `number` | `{ url: string }` - Model source:
248+
- `string`: File path on device
249+
- `number`: `require()` asset (automatically copied to files directory)
250+
- `{ url: string }`: URL to download from (automatically cached)
172251
- `options`: `SessionOptions` (optional) - Configuration options
173252

174253
**Returns:** `Promise<InferenceSession>`
175254

255+
**Example:**
256+
```typescript
257+
// From bundled asset
258+
const session1 = await ort.loadModel(require('./model.onnx'));
259+
260+
// From file path
261+
const session2 = await ort.loadModel('/data/user/model.onnx');
262+
263+
// From URL
264+
const session3 = await ort.loadModel({ url: 'https://example.com/model.onnx' });
265+
```
266+
176267
### `ort.loadModelFromBuffer(buffer, options?)`
177268

178269
Load an ONNX model from an ArrayBuffer.
@@ -183,6 +274,47 @@ Load an ONNX model from an ArrayBuffer.
183274

184275
**Returns:** `Promise<InferenceSession>`
185276

277+
### `copyFile(source)`
278+
279+
Manually copy a model file from a bundled asset or URL to the device's file system. This is useful if you want to copy the file before loading it.
280+
281+
**Parameters:**
282+
- `source`: `number` | `{ url: string }` - Model source to copy
283+
284+
**Returns:** `Promise<string>` - Path to the copied file
285+
286+
**Example:**
287+
```typescript
288+
// Copy bundled asset
289+
const modelPath = await copyFile(require('./model.onnx'));
290+
console.log('Model copied to:', modelPath);
291+
292+
// Now you can load it
293+
const session = await ort.loadModel(modelPath);
294+
295+
// Or copy from URL
296+
const urlPath = await copyFile({ url: 'https://example.com/model.onnx' });
297+
const session2 = await ort.loadModel(urlPath);
298+
```
299+
300+
**Note:** `loadModel()` calls this automatically when you pass a `require()` or `{ url }`, so you typically don't need to call this manually.
301+
302+
### `useLoadModel(source, options?)`
303+
304+
React hook for loading models with state management.
305+
306+
**Parameters:**
307+
- `source`: Same as `loadModel()`
308+
- `options`: `SessionOptions` (optional)
309+
310+
**Returns:** `OnnxRuntimePlugin`
311+
```typescript
312+
type OnnxRuntimePlugin =
313+
| { model: InferenceSession; state: 'loaded' }
314+
| { model: undefined; state: 'loading' }
315+
| { model: undefined; state: 'error'; error: Error };
316+
```
317+
186318
### `InferenceSession`
187319

188320
#### `session.inputNames`
@@ -211,7 +343,9 @@ Run inference with the given inputs.
211343

212344
#### `session.dispose()`
213345

214-
Free the session and release resources.
346+
Manually free the session and release resources immediately.
347+
348+
**Note:** This is optional - sessions are automatically cleaned up by Nitro Modules when they go out of scope. Only call this if you need immediate memory cleanup.
215349

216350
### `SessionOptions`
217351

@@ -267,25 +401,30 @@ type ProviderOptions = {
267401
4. **Reuse Sessions**: Create the session once and reuse it for multiple inferences
268402
5. **Use FP16**: Enable `useFP16` on NNAPI for faster inference with acceptable accuracy loss
269403

270-
## Troubleshooting
404+
## Exports
271405

272-
### Android Build Issues
406+
The library exports the following:
273407

274-
If you encounter duplicate library errors, ensure your `android/app/build.gradle` has:
408+
```typescript
409+
import ort, { useLoadModel, copyFile } from 'react-native-nitro-onnxruntime';
275410

276-
```gradle
277-
android {
278-
packaging {
279-
jniLibs {
280-
pickFirsts += ['**/libonnxruntime.so']
281-
}
282-
}
283-
}
284-
```
411+
// Using default export object
412+
const session = await ort.loadModel(require('./model.onnx'));
413+
const session2 = await ort.loadModelFromBuffer(arrayBuffer);
285414

286-
### Memory Issues
415+
// Or using destructured methods
416+
const { loadModel, loadModelFromBuffer } = ort;
417+
const session3 = await loadModel(require('./model.onnx'));
287418

288-
Always call `session.dispose()` when you're done with a session to free up memory.
419+
// Utility functions
420+
const modelPath = await copyFile(require('./model.onnx'));
421+
422+
// React hook
423+
function MyComponent() {
424+
const modelState = useLoadModel(require('./model.onnx'));
425+
// ...
426+
}
427+
```
289428

290429
## Example App
291430

0 commit comments

Comments
 (0)