react-native-litert-lm 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +331 -150
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +140 -37
- package/app.plugin.js +33 -0
- package/cpp/HybridLiteRTLM.cpp +577 -378
- package/cpp/HybridLiteRTLM.hpp +66 -23
- package/cpp/IOSDownloadHelper.h +24 -0
- package/cpp/cpp-adapter.cpp +10 -2
- package/cpp/include/litert_lm_engine.h +502 -0
- package/ios/IOSDownloadHelper.mm +129 -0
- package/ios/LiteRTLMAutolinking.mm +30 -0
- package/lib/hooks.d.ts +33 -3
- package/lib/hooks.js +54 -23
- package/lib/index.d.ts +4 -1
- package/lib/index.js +6 -6
- package/lib/memoryTracker.d.ts +128 -0
- package/lib/memoryTracker.js +155 -0
- package/lib/modelFactory.d.ts +21 -2
- package/lib/modelFactory.js +78 -11
- package/lib/specs/LiteRTLM.nitro.d.ts +19 -0
- package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +28 -18
- package/nitrogen/generated/android/LiteRTLMOnLoad.hpp +13 -4
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +39 -36
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +20 -22
- package/nitrogen/generated/android/c++/JMemoryUsage.hpp +69 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +19 -18
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MemoryUsage.kt +47 -0
- package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.hpp +4 -0
- package/nitrogen/generated/shared/c++/MemoryUsage.hpp +95 -0
- package/package.json +12 -5
- package/react-native-litert-lm.podspec +20 -7
- package/scripts/build-ios-engine.sh +283 -0
- package/scripts/download-ios-frameworks.sh +72 -0
- package/scripts/postinstall.js +116 -0
- package/scripts/stubs/cxx_bridge_stubs.cc +224 -0
- package/scripts/stubs/gemma_model_constraint_provider.cc +46 -0
- package/scripts/stubs/llguidance_stubs.c +101 -0
- package/src/hooks.ts +107 -41
- package/src/index.ts +13 -6
- package/src/memoryTracker.ts +268 -0
- package/src/modelFactory.ts +107 -11
- package/src/specs/LiteRTLM.nitro.ts +21 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory tracking utilities for LiteRT-LM using real native memory metrics.
|
|
3
|
+
*
|
|
4
|
+
* Records real memory usage from OS-level APIs via `getMemoryUsage()`,
|
|
5
|
+
* and stores snapshots in a native-backed ArrayBuffer allocated via
|
|
6
|
+
* `NitroModules.createNativeArrayBuffer()` (v0.35+) for zero-copy interop.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { createMemoryTracker } from 'react-native-litert-lm';
|
|
11
|
+
*
|
|
12
|
+
* const tracker = createMemoryTracker(100);
|
|
13
|
+
*
|
|
14
|
+
* // Record a real snapshot (typically called internally after inference)
|
|
15
|
+
* tracker.record({
|
|
16
|
+
* timestamp: Date.now(),
|
|
17
|
+
* nativeHeapBytes: usage.nativeHeapBytes,
|
|
18
|
+
* residentBytes: usage.residentBytes,
|
|
19
|
+
* availableMemoryBytes: usage.availableMemoryBytes,
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* console.log(`Peak RSS: ${tracker.getPeakMemory()} bytes`);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { NitroModules } from "react-native-nitro-modules";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A single memory usage snapshot with real data from OS APIs.
|
|
30
|
+
*/
|
|
31
|
+
export interface MemorySnapshot {
|
|
32
|
+
/** Unix timestamp in milliseconds */
|
|
33
|
+
timestamp: number;
|
|
34
|
+
/** Native heap allocated bytes (Debug.getNativeHeapAllocatedSize on Android, task_info on iOS) */
|
|
35
|
+
nativeHeapBytes: number;
|
|
36
|
+
/** Process resident set size (RSS) in bytes */
|
|
37
|
+
residentBytes: number;
|
|
38
|
+
/** Available system memory in bytes */
|
|
39
|
+
availableMemoryBytes: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Number of Float64 fields per snapshot */
|
|
43
|
+
const FIELDS_PER_SNAPSHOT = 4;
|
|
44
|
+
/** Bytes per Float64 value */
|
|
45
|
+
const BYTES_PER_FIELD = Float64Array.BYTES_PER_ELEMENT; // 8
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Memory tracker that stores snapshots in a native-backed ArrayBuffer.
|
|
49
|
+
*
|
|
50
|
+
* Uses `NitroModules.createNativeArrayBuffer()` to allocate the backing
|
|
51
|
+
* buffer in native (C++) memory, ensuring zero-copy interop with native
|
|
52
|
+
* methods and keeping memory tracking data off the JS heap.
|
|
53
|
+
*/
|
|
54
|
+
export interface MemoryTracker {
|
|
55
|
+
/**
|
|
56
|
+
* Record a new memory snapshot.
|
|
57
|
+
* @param snapshot The memory usage data to record
|
|
58
|
+
* @returns true if recorded, false if buffer is full
|
|
59
|
+
*/
|
|
60
|
+
record(snapshot: MemorySnapshot): boolean;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get all recorded snapshots as structured objects.
|
|
64
|
+
*/
|
|
65
|
+
getSnapshots(): MemorySnapshot[];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get the number of recorded snapshots.
|
|
69
|
+
*/
|
|
70
|
+
getSnapshotCount(): number;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get the maximum number of snapshots this tracker can hold.
|
|
74
|
+
*/
|
|
75
|
+
getCapacity(): number;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get the peak resident set size across all snapshots.
|
|
79
|
+
*/
|
|
80
|
+
getPeakMemory(): number;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get the latest memory snapshot, or undefined if none recorded.
|
|
84
|
+
*/
|
|
85
|
+
getLatestSnapshot(): MemorySnapshot | undefined;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the underlying native ArrayBuffer.
|
|
89
|
+
* This buffer is allocated via `NitroModules.createNativeArrayBuffer()`
|
|
90
|
+
* and lives in native memory, enabling zero-copy transfer to native methods.
|
|
91
|
+
*/
|
|
92
|
+
getNativeBuffer(): ArrayBuffer;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get the Float64Array view over the native buffer.
|
|
96
|
+
*/
|
|
97
|
+
getView(): Float64Array;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Reset the tracker, clearing all recorded snapshots.
|
|
101
|
+
* The native buffer is preserved (not reallocated).
|
|
102
|
+
*/
|
|
103
|
+
reset(): void;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Get a summary of memory usage statistics.
|
|
107
|
+
*/
|
|
108
|
+
getSummary(): MemoryTrackerSummary;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Summary statistics from the memory tracker.
|
|
113
|
+
*/
|
|
114
|
+
export interface MemoryTrackerSummary {
|
|
115
|
+
/** Number of snapshots recorded */
|
|
116
|
+
snapshotCount: number;
|
|
117
|
+
/** Peak resident set size in bytes */
|
|
118
|
+
peakResidentBytes: number;
|
|
119
|
+
/** Average resident set size in bytes */
|
|
120
|
+
averageResidentBytes: number;
|
|
121
|
+
/** Latest resident set size in bytes */
|
|
122
|
+
currentResidentBytes: number;
|
|
123
|
+
/** Peak native heap allocated in bytes */
|
|
124
|
+
peakNativeHeapBytes: number;
|
|
125
|
+
/** Latest native heap allocated in bytes */
|
|
126
|
+
currentNativeHeapBytes: number;
|
|
127
|
+
/** RSS delta from first to last snapshot in bytes */
|
|
128
|
+
residentDeltaBytes: number;
|
|
129
|
+
/** Size of the native tracking buffer itself in bytes */
|
|
130
|
+
trackerBufferSizeBytes: number;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Create a new memory tracker backed by a native ArrayBuffer.
|
|
135
|
+
*
|
|
136
|
+
* @param maxSnapshots Maximum number of snapshots to store (default: 256)
|
|
137
|
+
* @returns A MemoryTracker instance
|
|
138
|
+
*/
|
|
139
|
+
export function createMemoryTracker(maxSnapshots: number = 256): MemoryTracker {
|
|
140
|
+
const bufferSize = maxSnapshots * FIELDS_PER_SNAPSHOT * BYTES_PER_FIELD;
|
|
141
|
+
|
|
142
|
+
// Use NitroModules.createNativeArrayBuffer for native-backed allocation.
|
|
143
|
+
const nativeBuffer = NitroModules.createNativeArrayBuffer(bufferSize);
|
|
144
|
+
const view = new Float64Array(nativeBuffer);
|
|
145
|
+
|
|
146
|
+
let currentIndex = 0;
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
record(snapshot: MemorySnapshot): boolean {
|
|
150
|
+
if (currentIndex >= maxSnapshots) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const offset = currentIndex * FIELDS_PER_SNAPSHOT;
|
|
155
|
+
view[offset] = snapshot.timestamp;
|
|
156
|
+
view[offset + 1] = snapshot.nativeHeapBytes;
|
|
157
|
+
view[offset + 2] = snapshot.residentBytes;
|
|
158
|
+
view[offset + 3] = snapshot.availableMemoryBytes;
|
|
159
|
+
currentIndex++;
|
|
160
|
+
|
|
161
|
+
return true;
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
getSnapshots(): MemorySnapshot[] {
|
|
165
|
+
const snapshots: MemorySnapshot[] = [];
|
|
166
|
+
for (let i = 0; i < currentIndex; i++) {
|
|
167
|
+
const offset = i * FIELDS_PER_SNAPSHOT;
|
|
168
|
+
snapshots.push({
|
|
169
|
+
timestamp: view[offset]!,
|
|
170
|
+
nativeHeapBytes: view[offset + 1]!,
|
|
171
|
+
residentBytes: view[offset + 2]!,
|
|
172
|
+
availableMemoryBytes: view[offset + 3]!,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return snapshots;
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
getSnapshotCount(): number {
|
|
179
|
+
return currentIndex;
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
getCapacity(): number {
|
|
183
|
+
return maxSnapshots;
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
getPeakMemory(): number {
|
|
187
|
+
let peak = 0;
|
|
188
|
+
for (let i = 0; i < currentIndex; i++) {
|
|
189
|
+
const rss = view[i * FIELDS_PER_SNAPSHOT + 2]!;
|
|
190
|
+
if (rss > peak) {
|
|
191
|
+
peak = rss;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return peak;
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
getLatestSnapshot(): MemorySnapshot | undefined {
|
|
198
|
+
if (currentIndex === 0) return undefined;
|
|
199
|
+
const offset = (currentIndex - 1) * FIELDS_PER_SNAPSHOT;
|
|
200
|
+
return {
|
|
201
|
+
timestamp: view[offset]!,
|
|
202
|
+
nativeHeapBytes: view[offset + 1]!,
|
|
203
|
+
residentBytes: view[offset + 2]!,
|
|
204
|
+
availableMemoryBytes: view[offset + 3]!,
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
getNativeBuffer(): ArrayBuffer {
|
|
209
|
+
return nativeBuffer;
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
getView(): Float64Array {
|
|
213
|
+
return view;
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
reset(): void {
|
|
217
|
+
view.fill(0);
|
|
218
|
+
currentIndex = 0;
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
getSummary(): MemoryTrackerSummary {
|
|
222
|
+
let peakRss = 0;
|
|
223
|
+
let peakHeap = 0;
|
|
224
|
+
let sumRss = 0;
|
|
225
|
+
let firstRss = 0;
|
|
226
|
+
let lastRss = 0;
|
|
227
|
+
let lastHeap = 0;
|
|
228
|
+
|
|
229
|
+
for (let i = 0; i < currentIndex; i++) {
|
|
230
|
+
const offset = i * FIELDS_PER_SNAPSHOT;
|
|
231
|
+
const heap = view[offset + 1]!;
|
|
232
|
+
const rss = view[offset + 2]!;
|
|
233
|
+
|
|
234
|
+
if (rss > peakRss) peakRss = rss;
|
|
235
|
+
if (heap > peakHeap) peakHeap = heap;
|
|
236
|
+
sumRss += rss;
|
|
237
|
+
if (i === 0) firstRss = rss;
|
|
238
|
+
if (i === currentIndex - 1) {
|
|
239
|
+
lastRss = rss;
|
|
240
|
+
lastHeap = heap;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
snapshotCount: currentIndex,
|
|
246
|
+
peakResidentBytes: peakRss,
|
|
247
|
+
averageResidentBytes: currentIndex > 0 ? sumRss / currentIndex : 0,
|
|
248
|
+
currentResidentBytes: lastRss,
|
|
249
|
+
peakNativeHeapBytes: peakHeap,
|
|
250
|
+
currentNativeHeapBytes: lastHeap,
|
|
251
|
+
residentDeltaBytes: lastRss - firstRss,
|
|
252
|
+
trackerBufferSizeBytes: bufferSize,
|
|
253
|
+
};
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Create a native ArrayBuffer for efficient data transfer.
|
|
260
|
+
*
|
|
261
|
+
* A convenience wrapper around `NitroModules.createNativeArrayBuffer()`.
|
|
262
|
+
*
|
|
263
|
+
* @param size Size in bytes
|
|
264
|
+
* @returns A native-backed ArrayBuffer
|
|
265
|
+
*/
|
|
266
|
+
export function createNativeBuffer(size: number): ArrayBuffer {
|
|
267
|
+
return NitroModules.createNativeArrayBuffer(size);
|
|
268
|
+
}
|
package/src/modelFactory.ts
CHANGED
|
@@ -1,19 +1,79 @@
|
|
|
1
1
|
import { NitroModules } from "react-native-nitro-modules";
|
|
2
2
|
import { LiteRTLM, LLMConfig } from "./specs/LiteRTLM.nitro";
|
|
3
|
+
import { createMemoryTracker, MemoryTracker } from "./memoryTracker";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extended LiteRT-LM instance with optional memory tracking and
|
|
7
|
+
* augmented loadModel that accepts a download progress callback.
|
|
8
|
+
*/
|
|
9
|
+
export type LiteRTLMInstance = Omit<LiteRTLM, "loadModel"> & {
|
|
10
|
+
memoryTracker?: MemoryTracker;
|
|
11
|
+
loadModel: (
|
|
12
|
+
pathOrUrl: string,
|
|
13
|
+
config?: LLMConfig,
|
|
14
|
+
onDownloadProgress?: (progress: number) => void,
|
|
15
|
+
) => Promise<void>;
|
|
16
|
+
};
|
|
3
17
|
|
|
4
18
|
/**
|
|
5
19
|
* Creates a new LiteRT-LM inference engine instance.
|
|
20
|
+
*
|
|
21
|
+
* Optionally creates a native-backed memory tracker using
|
|
22
|
+
* `NitroModules.createNativeArrayBuffer()` (v0.35+) for efficient
|
|
23
|
+
* zero-copy memory usage tracking.
|
|
24
|
+
*
|
|
25
|
+
* @param options.enableMemoryTracking Enable automatic memory tracking (default: false)
|
|
26
|
+
* @param options.maxMemorySnapshots Maximum number of memory snapshots to store (default: 256)
|
|
6
27
|
*/
|
|
7
|
-
export function createLLM(
|
|
28
|
+
export function createLLM(options?: {
|
|
29
|
+
enableMemoryTracking?: boolean;
|
|
30
|
+
maxMemorySnapshots?: number;
|
|
31
|
+
}): LiteRTLMInstance {
|
|
8
32
|
const native = NitroModules.createHybridObject<LiteRTLM>("LiteRTLM");
|
|
9
33
|
|
|
34
|
+
const enableTracking = options?.enableMemoryTracking ?? false;
|
|
35
|
+
const tracker = enableTracking
|
|
36
|
+
? createMemoryTracker(options?.maxMemorySnapshots ?? 256)
|
|
37
|
+
: undefined;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Record a real memory snapshot using OS-level APIs via getMemoryUsage().
|
|
41
|
+
*/
|
|
42
|
+
const recordMemorySnapshot = () => {
|
|
43
|
+
if (!tracker) return;
|
|
44
|
+
try {
|
|
45
|
+
const usage = native.getMemoryUsage();
|
|
46
|
+
tracker.record({
|
|
47
|
+
timestamp: Date.now(),
|
|
48
|
+
nativeHeapBytes: usage.nativeHeapBytes,
|
|
49
|
+
residentBytes: usage.residentBytes,
|
|
50
|
+
availableMemoryBytes: usage.availableMemoryBytes,
|
|
51
|
+
});
|
|
52
|
+
} catch {
|
|
53
|
+
// Ignore errors during memory tracking - it's non-critical
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
10
57
|
return {
|
|
11
58
|
...native,
|
|
12
|
-
|
|
59
|
+
memoryTracker: tracker,
|
|
60
|
+
loadModel: async (
|
|
61
|
+
pathOrUrl: string,
|
|
62
|
+
config?: LLMConfig,
|
|
63
|
+
onDownloadProgress?: (progress: number) => void,
|
|
64
|
+
) => {
|
|
13
65
|
let modelPath = pathOrUrl;
|
|
14
66
|
|
|
15
|
-
// Check if it's a URL
|
|
67
|
+
// Check if it's a URL — enforce HTTPS for model downloads
|
|
16
68
|
if (pathOrUrl.startsWith("http://") || pathOrUrl.startsWith("https://")) {
|
|
69
|
+
if (pathOrUrl.startsWith("http://")) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"Insecure HTTP URLs are not allowed for model downloads. " +
|
|
72
|
+
"Use HTTPS instead: " +
|
|
73
|
+
pathOrUrl.replace("http://", "https://"),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
17
77
|
// Extract filename from URL
|
|
18
78
|
const fileName = pathOrUrl.split("/").pop();
|
|
19
79
|
if (!fileName) {
|
|
@@ -25,23 +85,59 @@ export function createLLM(): LiteRTLM {
|
|
|
25
85
|
pathOrUrl,
|
|
26
86
|
fileName,
|
|
27
87
|
(progress) => {
|
|
28
|
-
|
|
88
|
+
onDownloadProgress?.(progress);
|
|
29
89
|
},
|
|
30
90
|
);
|
|
31
91
|
console.log(`Model downloaded to: ${modelPath}`);
|
|
32
92
|
}
|
|
33
93
|
|
|
34
|
-
|
|
94
|
+
const result = await native.loadModel(modelPath, config);
|
|
95
|
+
|
|
96
|
+
// Record initial memory snapshot after model load
|
|
97
|
+
if (tracker) {
|
|
98
|
+
tracker.reset();
|
|
99
|
+
recordMemorySnapshot();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return result;
|
|
103
|
+
},
|
|
104
|
+
sendMessage: async (...args: Parameters<typeof native.sendMessage>) => {
|
|
105
|
+
const result = await native.sendMessage(...args);
|
|
106
|
+
recordMemorySnapshot();
|
|
107
|
+
return result;
|
|
108
|
+
},
|
|
109
|
+
sendMessageAsync: (...args: Parameters<typeof native.sendMessageAsync>) => {
|
|
110
|
+
const [message, onToken] = args;
|
|
111
|
+
native.sendMessageAsync(message, (token, done) => {
|
|
112
|
+
onToken(token, done);
|
|
113
|
+
if (done) {
|
|
114
|
+
recordMemorySnapshot();
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
sendMessageWithImage: async (
|
|
119
|
+
...args: Parameters<typeof native.sendMessageWithImage>
|
|
120
|
+
) => {
|
|
121
|
+
const result = await native.sendMessageWithImage(...args);
|
|
122
|
+
recordMemorySnapshot();
|
|
123
|
+
return result;
|
|
124
|
+
},
|
|
125
|
+
sendMessageWithAudio: async (
|
|
126
|
+
...args: Parameters<typeof native.sendMessageWithAudio>
|
|
127
|
+
) => {
|
|
128
|
+
const result = await native.sendMessageWithAudio(...args);
|
|
129
|
+
recordMemorySnapshot();
|
|
130
|
+
return result;
|
|
35
131
|
},
|
|
36
|
-
// Bind valid methods to native instance
|
|
37
|
-
sendMessage: native.sendMessage.bind(native),
|
|
38
|
-
sendMessageAsync: native.sendMessageAsync.bind(native),
|
|
39
|
-
sendMessageWithImage: native.sendMessageWithImage.bind(native),
|
|
40
|
-
sendMessageWithAudio: native.sendMessageWithAudio.bind(native),
|
|
41
132
|
getHistory: native.getHistory.bind(native),
|
|
42
|
-
resetConversation:
|
|
133
|
+
resetConversation: () => {
|
|
134
|
+
native.resetConversation();
|
|
135
|
+
// KV cache is cleared on reset, record the drop
|
|
136
|
+
recordMemorySnapshot();
|
|
137
|
+
},
|
|
43
138
|
isReady: native.isReady.bind(native),
|
|
44
139
|
getStats: native.getStats.bind(native),
|
|
140
|
+
getMemoryUsage: native.getMemoryUsage.bind(native),
|
|
45
141
|
close: native.close.bind(native),
|
|
46
142
|
downloadModel: native.downloadModel.bind(native),
|
|
47
143
|
deleteModel: native.deleteModel.bind(native),
|
|
@@ -99,6 +99,21 @@ export interface GenerationStats {
|
|
|
99
99
|
tokensPerSecond: number;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Real memory usage statistics from the native runtime.
|
|
104
|
+
* Measured from OS-level APIs, not estimated.
|
|
105
|
+
*/
|
|
106
|
+
export interface MemoryUsage {
|
|
107
|
+
/** Native heap allocated bytes (Debug.getNativeHeapAllocatedSize on Android, malloc_size on iOS) */
|
|
108
|
+
nativeHeapBytes: number;
|
|
109
|
+
/** Total process resident set size (RSS) in bytes */
|
|
110
|
+
residentBytes: number;
|
|
111
|
+
/** Available system memory in bytes */
|
|
112
|
+
availableMemoryBytes: number;
|
|
113
|
+
/** Whether the system considers memory low */
|
|
114
|
+
isLowMemory: boolean;
|
|
115
|
+
}
|
|
116
|
+
|
|
102
117
|
/**
|
|
103
118
|
* LiteRT-LM: High-performance LLM inference engine.
|
|
104
119
|
* Supports Gemma 3n, Phi-4, Qwen, and other .litertlm models.
|
|
@@ -204,6 +219,12 @@ export interface LiteRTLM extends HybridObject<{
|
|
|
204
219
|
*/
|
|
205
220
|
getStats(): GenerationStats;
|
|
206
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Get real memory usage from the native runtime.
|
|
224
|
+
* Uses OS-level APIs to report actual memory consumption.
|
|
225
|
+
*/
|
|
226
|
+
getMemoryUsage(): MemoryUsage;
|
|
227
|
+
|
|
207
228
|
/**
|
|
208
229
|
* Release all native resources.
|
|
209
230
|
* Call this when done with the LLM instance.
|