react-native-litert-lm 0.2.1 → 0.2.2
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 +103 -6
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +47 -0
- package/cpp/HybridLiteRTLM.cpp +79 -0
- package/cpp/HybridLiteRTLM.hpp +12 -0
- package/cpp/cpp-adapter.cpp +10 -2
- package/lib/hooks.d.ts +25 -0
- package/lib/hooks.js +21 -4
- package/lib/index.d.ts +3 -1
- package/lib/index.js +4 -1
- package/lib/memoryTracker.d.ts +128 -0
- package/lib/memoryTracker.js +155 -0
- package/lib/modelFactory.d.ts +14 -1
- package/lib/modelFactory.js +70 -8
- package/lib/specs/LiteRTLM.nitro.d.ts +19 -0
- package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +21 -18
- package/nitrogen/generated/android/LiteRTLMOnLoad.hpp +13 -4
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +9 -0
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +1 -0
- package/nitrogen/generated/android/c++/JMemoryUsage.hpp +69 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +4 -0
- 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 +3 -3
- package/src/hooks.ts +48 -5
- package/src/index.ts +10 -0
- package/src/memoryTracker.ts +268 -0
- package/src/modelFactory.ts +79 -8
- package/src/specs/LiteRTLM.nitro.ts +21 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Memory tracking utilities for LiteRT-LM using real native memory metrics.
|
|
4
|
+
*
|
|
5
|
+
* Records real memory usage from OS-level APIs via `getMemoryUsage()`,
|
|
6
|
+
* and stores snapshots in a native-backed ArrayBuffer allocated via
|
|
7
|
+
* `NitroModules.createNativeArrayBuffer()` (v0.34+) for zero-copy interop.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { createMemoryTracker } from 'react-native-litert-lm';
|
|
12
|
+
*
|
|
13
|
+
* const tracker = createMemoryTracker(100);
|
|
14
|
+
*
|
|
15
|
+
* // Record a real snapshot (typically called internally after inference)
|
|
16
|
+
* tracker.record({
|
|
17
|
+
* timestamp: Date.now(),
|
|
18
|
+
* nativeHeapBytes: usage.nativeHeapBytes,
|
|
19
|
+
* residentBytes: usage.residentBytes,
|
|
20
|
+
* availableMemoryBytes: usage.availableMemoryBytes,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* console.log(`Peak RSS: ${tracker.getPeakMemory()} bytes`);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.createMemoryTracker = createMemoryTracker;
|
|
28
|
+
exports.createNativeBuffer = createNativeBuffer;
|
|
29
|
+
const react_native_nitro_modules_1 = require("react-native-nitro-modules");
|
|
30
|
+
/** Number of Float64 fields per snapshot */
|
|
31
|
+
const FIELDS_PER_SNAPSHOT = 4;
|
|
32
|
+
/** Bytes per Float64 value */
|
|
33
|
+
const BYTES_PER_FIELD = Float64Array.BYTES_PER_ELEMENT; // 8
|
|
34
|
+
/**
|
|
35
|
+
* Create a new memory tracker backed by a native ArrayBuffer.
|
|
36
|
+
*
|
|
37
|
+
* @param maxSnapshots Maximum number of snapshots to store (default: 256)
|
|
38
|
+
* @returns A MemoryTracker instance
|
|
39
|
+
*/
|
|
40
|
+
function createMemoryTracker(maxSnapshots = 256) {
|
|
41
|
+
const bufferSize = maxSnapshots * FIELDS_PER_SNAPSHOT * BYTES_PER_FIELD;
|
|
42
|
+
// Use NitroModules.createNativeArrayBuffer for native-backed allocation.
|
|
43
|
+
const nativeBuffer = react_native_nitro_modules_1.NitroModules.createNativeArrayBuffer(bufferSize);
|
|
44
|
+
const view = new Float64Array(nativeBuffer);
|
|
45
|
+
let currentIndex = 0;
|
|
46
|
+
return {
|
|
47
|
+
record(snapshot) {
|
|
48
|
+
if (currentIndex >= maxSnapshots) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const offset = currentIndex * FIELDS_PER_SNAPSHOT;
|
|
52
|
+
view[offset] = snapshot.timestamp;
|
|
53
|
+
view[offset + 1] = snapshot.nativeHeapBytes;
|
|
54
|
+
view[offset + 2] = snapshot.residentBytes;
|
|
55
|
+
view[offset + 3] = snapshot.availableMemoryBytes;
|
|
56
|
+
currentIndex++;
|
|
57
|
+
return true;
|
|
58
|
+
},
|
|
59
|
+
getSnapshots() {
|
|
60
|
+
const snapshots = [];
|
|
61
|
+
for (let i = 0; i < currentIndex; i++) {
|
|
62
|
+
const offset = i * FIELDS_PER_SNAPSHOT;
|
|
63
|
+
snapshots.push({
|
|
64
|
+
timestamp: view[offset],
|
|
65
|
+
nativeHeapBytes: view[offset + 1],
|
|
66
|
+
residentBytes: view[offset + 2],
|
|
67
|
+
availableMemoryBytes: view[offset + 3],
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return snapshots;
|
|
71
|
+
},
|
|
72
|
+
getSnapshotCount() {
|
|
73
|
+
return currentIndex;
|
|
74
|
+
},
|
|
75
|
+
getCapacity() {
|
|
76
|
+
return maxSnapshots;
|
|
77
|
+
},
|
|
78
|
+
getPeakMemory() {
|
|
79
|
+
let peak = 0;
|
|
80
|
+
for (let i = 0; i < currentIndex; i++) {
|
|
81
|
+
const rss = view[i * FIELDS_PER_SNAPSHOT + 2];
|
|
82
|
+
if (rss > peak) {
|
|
83
|
+
peak = rss;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return peak;
|
|
87
|
+
},
|
|
88
|
+
getLatestSnapshot() {
|
|
89
|
+
if (currentIndex === 0)
|
|
90
|
+
return undefined;
|
|
91
|
+
const offset = (currentIndex - 1) * FIELDS_PER_SNAPSHOT;
|
|
92
|
+
return {
|
|
93
|
+
timestamp: view[offset],
|
|
94
|
+
nativeHeapBytes: view[offset + 1],
|
|
95
|
+
residentBytes: view[offset + 2],
|
|
96
|
+
availableMemoryBytes: view[offset + 3],
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
getNativeBuffer() {
|
|
100
|
+
return nativeBuffer;
|
|
101
|
+
},
|
|
102
|
+
getView() {
|
|
103
|
+
return view;
|
|
104
|
+
},
|
|
105
|
+
reset() {
|
|
106
|
+
view.fill(0);
|
|
107
|
+
currentIndex = 0;
|
|
108
|
+
},
|
|
109
|
+
getSummary() {
|
|
110
|
+
let peakRss = 0;
|
|
111
|
+
let peakHeap = 0;
|
|
112
|
+
let sumRss = 0;
|
|
113
|
+
let firstRss = 0;
|
|
114
|
+
let lastRss = 0;
|
|
115
|
+
let lastHeap = 0;
|
|
116
|
+
for (let i = 0; i < currentIndex; i++) {
|
|
117
|
+
const offset = i * FIELDS_PER_SNAPSHOT;
|
|
118
|
+
const heap = view[offset + 1];
|
|
119
|
+
const rss = view[offset + 2];
|
|
120
|
+
if (rss > peakRss)
|
|
121
|
+
peakRss = rss;
|
|
122
|
+
if (heap > peakHeap)
|
|
123
|
+
peakHeap = heap;
|
|
124
|
+
sumRss += rss;
|
|
125
|
+
if (i === 0)
|
|
126
|
+
firstRss = rss;
|
|
127
|
+
if (i === currentIndex - 1) {
|
|
128
|
+
lastRss = rss;
|
|
129
|
+
lastHeap = heap;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
snapshotCount: currentIndex,
|
|
134
|
+
peakResidentBytes: peakRss,
|
|
135
|
+
averageResidentBytes: currentIndex > 0 ? sumRss / currentIndex : 0,
|
|
136
|
+
currentResidentBytes: lastRss,
|
|
137
|
+
peakNativeHeapBytes: peakHeap,
|
|
138
|
+
currentNativeHeapBytes: lastHeap,
|
|
139
|
+
residentDeltaBytes: lastRss - firstRss,
|
|
140
|
+
trackerBufferSizeBytes: bufferSize,
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Create a native ArrayBuffer for efficient data transfer.
|
|
147
|
+
*
|
|
148
|
+
* A convenience wrapper around `NitroModules.createNativeArrayBuffer()`.
|
|
149
|
+
*
|
|
150
|
+
* @param size Size in bytes
|
|
151
|
+
* @returns A native-backed ArrayBuffer
|
|
152
|
+
*/
|
|
153
|
+
function createNativeBuffer(size) {
|
|
154
|
+
return react_native_nitro_modules_1.NitroModules.createNativeArrayBuffer(size);
|
|
155
|
+
}
|
package/lib/modelFactory.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { LiteRTLM } from "./specs/LiteRTLM.nitro";
|
|
2
|
+
import { MemoryTracker } from "./memoryTracker";
|
|
2
3
|
/**
|
|
3
4
|
* Creates a new LiteRT-LM inference engine instance.
|
|
5
|
+
*
|
|
6
|
+
* Optionally creates a native-backed memory tracker using
|
|
7
|
+
* `NitroModules.createNativeArrayBuffer()` (v0.34+) for efficient
|
|
8
|
+
* zero-copy memory usage tracking.
|
|
9
|
+
*
|
|
10
|
+
* @param options.enableMemoryTracking Enable automatic memory tracking (default: false)
|
|
11
|
+
* @param options.maxMemorySnapshots Maximum number of memory snapshots to store (default: 256)
|
|
4
12
|
*/
|
|
5
|
-
export declare function createLLM(
|
|
13
|
+
export declare function createLLM(options?: {
|
|
14
|
+
enableMemoryTracking?: boolean;
|
|
15
|
+
maxMemorySnapshots?: number;
|
|
16
|
+
}): LiteRTLM & {
|
|
17
|
+
memoryTracker?: MemoryTracker;
|
|
18
|
+
};
|
package/lib/modelFactory.js
CHANGED
|
@@ -2,13 +2,45 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createLLM = createLLM;
|
|
4
4
|
const react_native_nitro_modules_1 = require("react-native-nitro-modules");
|
|
5
|
+
const memoryTracker_1 = require("./memoryTracker");
|
|
5
6
|
/**
|
|
6
7
|
* Creates a new LiteRT-LM inference engine instance.
|
|
8
|
+
*
|
|
9
|
+
* Optionally creates a native-backed memory tracker using
|
|
10
|
+
* `NitroModules.createNativeArrayBuffer()` (v0.34+) for efficient
|
|
11
|
+
* zero-copy memory usage tracking.
|
|
12
|
+
*
|
|
13
|
+
* @param options.enableMemoryTracking Enable automatic memory tracking (default: false)
|
|
14
|
+
* @param options.maxMemorySnapshots Maximum number of memory snapshots to store (default: 256)
|
|
7
15
|
*/
|
|
8
|
-
function createLLM() {
|
|
16
|
+
function createLLM(options) {
|
|
9
17
|
const native = react_native_nitro_modules_1.NitroModules.createHybridObject("LiteRTLM");
|
|
18
|
+
const enableTracking = options?.enableMemoryTracking ?? false;
|
|
19
|
+
const tracker = enableTracking
|
|
20
|
+
? (0, memoryTracker_1.createMemoryTracker)(options?.maxMemorySnapshots ?? 256)
|
|
21
|
+
: undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Record a real memory snapshot using OS-level APIs via getMemoryUsage().
|
|
24
|
+
*/
|
|
25
|
+
const recordMemorySnapshot = () => {
|
|
26
|
+
if (!tracker)
|
|
27
|
+
return;
|
|
28
|
+
try {
|
|
29
|
+
const usage = native.getMemoryUsage();
|
|
30
|
+
tracker.record({
|
|
31
|
+
timestamp: Date.now(),
|
|
32
|
+
nativeHeapBytes: usage.nativeHeapBytes,
|
|
33
|
+
residentBytes: usage.residentBytes,
|
|
34
|
+
availableMemoryBytes: usage.availableMemoryBytes,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Ignore errors during memory tracking - it's non-critical
|
|
39
|
+
}
|
|
40
|
+
};
|
|
10
41
|
return {
|
|
11
42
|
...native,
|
|
43
|
+
memoryTracker: tracker,
|
|
12
44
|
loadModel: async (pathOrUrl, config) => {
|
|
13
45
|
let modelPath = pathOrUrl;
|
|
14
46
|
// Check if it's a URL
|
|
@@ -24,17 +56,47 @@ function createLLM() {
|
|
|
24
56
|
});
|
|
25
57
|
console.log(`Model downloaded to: ${modelPath}`);
|
|
26
58
|
}
|
|
27
|
-
|
|
59
|
+
const result = await native.loadModel(modelPath, config);
|
|
60
|
+
// Record initial memory snapshot after model load
|
|
61
|
+
if (tracker) {
|
|
62
|
+
tracker.reset();
|
|
63
|
+
recordMemorySnapshot();
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
},
|
|
67
|
+
sendMessage: async (...args) => {
|
|
68
|
+
const result = await native.sendMessage(...args);
|
|
69
|
+
recordMemorySnapshot();
|
|
70
|
+
return result;
|
|
71
|
+
},
|
|
72
|
+
sendMessageAsync: (...args) => {
|
|
73
|
+
const [message, onToken] = args;
|
|
74
|
+
native.sendMessageAsync(message, (token, done) => {
|
|
75
|
+
onToken(token, done);
|
|
76
|
+
if (done) {
|
|
77
|
+
recordMemorySnapshot();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
sendMessageWithImage: async (...args) => {
|
|
82
|
+
const result = await native.sendMessageWithImage(...args);
|
|
83
|
+
recordMemorySnapshot();
|
|
84
|
+
return result;
|
|
85
|
+
},
|
|
86
|
+
sendMessageWithAudio: async (...args) => {
|
|
87
|
+
const result = await native.sendMessageWithAudio(...args);
|
|
88
|
+
recordMemorySnapshot();
|
|
89
|
+
return result;
|
|
28
90
|
},
|
|
29
|
-
// Bind valid methods to native instance
|
|
30
|
-
sendMessage: native.sendMessage.bind(native),
|
|
31
|
-
sendMessageAsync: native.sendMessageAsync.bind(native),
|
|
32
|
-
sendMessageWithImage: native.sendMessageWithImage.bind(native),
|
|
33
|
-
sendMessageWithAudio: native.sendMessageWithAudio.bind(native),
|
|
34
91
|
getHistory: native.getHistory.bind(native),
|
|
35
|
-
resetConversation:
|
|
92
|
+
resetConversation: () => {
|
|
93
|
+
native.resetConversation();
|
|
94
|
+
// KV cache is cleared on reset, record the drop
|
|
95
|
+
recordMemorySnapshot();
|
|
96
|
+
},
|
|
36
97
|
isReady: native.isReady.bind(native),
|
|
37
98
|
getStats: native.getStats.bind(native),
|
|
99
|
+
getMemoryUsage: native.getMemoryUsage.bind(native),
|
|
38
100
|
close: native.close.bind(native),
|
|
39
101
|
downloadModel: native.downloadModel.bind(native),
|
|
40
102
|
deleteModel: native.deleteModel.bind(native),
|
|
@@ -88,6 +88,20 @@ export interface GenerationStats {
|
|
|
88
88
|
/** Tokens per second */
|
|
89
89
|
tokensPerSecond: number;
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Real memory usage statistics from the native runtime.
|
|
93
|
+
* Measured from OS-level APIs, not estimated.
|
|
94
|
+
*/
|
|
95
|
+
export interface MemoryUsage {
|
|
96
|
+
/** Native heap allocated bytes (Debug.getNativeHeapAllocatedSize on Android, malloc_size on iOS) */
|
|
97
|
+
nativeHeapBytes: number;
|
|
98
|
+
/** Total process resident set size (RSS) in bytes */
|
|
99
|
+
residentBytes: number;
|
|
100
|
+
/** Available system memory in bytes */
|
|
101
|
+
availableMemoryBytes: number;
|
|
102
|
+
/** Whether the system considers memory low */
|
|
103
|
+
isLowMemory: boolean;
|
|
104
|
+
}
|
|
91
105
|
/**
|
|
92
106
|
* LiteRT-LM: High-performance LLM inference engine.
|
|
93
107
|
* Supports Gemma 3n, Phi-4, Qwen, and other .litertlm models.
|
|
@@ -175,6 +189,11 @@ export interface LiteRTLM extends HybridObject<{
|
|
|
175
189
|
* Get the last generation statistics.
|
|
176
190
|
*/
|
|
177
191
|
getStats(): GenerationStats;
|
|
192
|
+
/**
|
|
193
|
+
* Get real memory usage from the native runtime.
|
|
194
|
+
* Uses OS-level APIs to report actual memory consumption.
|
|
195
|
+
*/
|
|
196
|
+
getMemoryUsage(): MemoryUsage;
|
|
178
197
|
/**
|
|
179
198
|
* Release all native resources.
|
|
180
199
|
* Call this when done with the LLM instance.
|
|
@@ -23,26 +23,29 @@
|
|
|
23
23
|
namespace margelo::nitro::litertlm {
|
|
24
24
|
|
|
25
25
|
int initialize(JavaVM* vm) {
|
|
26
|
+
return facebook::jni::initialize(vm, []() {
|
|
27
|
+
::margelo::nitro::litertlm::registerAllNatives();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
void registerAllNatives() {
|
|
26
32
|
using namespace margelo::nitro;
|
|
27
33
|
using namespace margelo::nitro::litertlm;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
);
|
|
45
|
-
});
|
|
34
|
+
|
|
35
|
+
// Register native JNI methods
|
|
36
|
+
margelo::nitro::litertlm::JHybridLiteRTLMSpec::registerNatives();
|
|
37
|
+
margelo::nitro::litertlm::JFunc_void_double_cxx::registerNatives();
|
|
38
|
+
margelo::nitro::litertlm::JFunc_void_std__string_bool_cxx::registerNatives();
|
|
39
|
+
|
|
40
|
+
// Register Nitro Hybrid Objects
|
|
41
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
42
|
+
"LiteRTLM",
|
|
43
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
44
|
+
static DefaultConstructableObject<JHybridLiteRTLMSpec::javaobject> object("com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM");
|
|
45
|
+
auto instance = object.create();
|
|
46
|
+
return instance->cthis()->shared();
|
|
47
|
+
}
|
|
48
|
+
);
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
} // namespace margelo::nitro::litertlm
|
|
@@ -6,20 +6,29 @@
|
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#include <jni.h>
|
|
9
|
+
#include <functional>
|
|
9
10
|
#include <NitroModules/NitroDefines.hpp>
|
|
10
11
|
|
|
11
12
|
namespace margelo::nitro::litertlm {
|
|
12
13
|
|
|
14
|
+
[[deprecated("Use registerNatives() instead.")]]
|
|
15
|
+
int initialize(JavaVM* vm);
|
|
16
|
+
|
|
13
17
|
/**
|
|
14
|
-
*
|
|
15
|
-
* Call this in your `JNI_OnLoad` function (probably inside `cpp-adapter.cpp`)
|
|
18
|
+
* Register the native (C++) part of LiteRTLM, and autolinks all Hybrid Objects.
|
|
19
|
+
* Call this in your `JNI_OnLoad` function (probably inside `cpp-adapter.cpp`),
|
|
20
|
+
* inside a `facebook::jni::initialize(vm, ...)` call.
|
|
16
21
|
* Example:
|
|
17
22
|
* ```cpp (cpp-adapter.cpp)
|
|
18
23
|
* JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
|
|
19
|
-
* return
|
|
24
|
+
* return facebook::jni::initialize(vm, []() {
|
|
25
|
+
* // register all LiteRTLM HybridObjects
|
|
26
|
+
* margelo::nitro::litertlm::registerNatives();
|
|
27
|
+
* // any other custom registrations go here.
|
|
28
|
+
* });
|
|
20
29
|
* }
|
|
21
30
|
* ```
|
|
22
31
|
*/
|
|
23
|
-
|
|
32
|
+
void registerAllNatives();
|
|
24
33
|
|
|
25
34
|
} // namespace margelo::nitro::litertlm
|
|
@@ -13,6 +13,8 @@ namespace margelo::nitro::litertlm { struct Message; }
|
|
|
13
13
|
namespace margelo::nitro::litertlm { enum class Role; }
|
|
14
14
|
// Forward declaration of `GenerationStats` to properly resolve imports.
|
|
15
15
|
namespace margelo::nitro::litertlm { struct GenerationStats; }
|
|
16
|
+
// Forward declaration of `MemoryUsage` to properly resolve imports.
|
|
17
|
+
namespace margelo::nitro::litertlm { struct MemoryUsage; }
|
|
16
18
|
// Forward declaration of `LLMConfig` to properly resolve imports.
|
|
17
19
|
namespace margelo::nitro::litertlm { struct LLMConfig; }
|
|
18
20
|
// Forward declaration of `Backend` to properly resolve imports.
|
|
@@ -29,6 +31,8 @@ namespace margelo::nitro::litertlm { enum class Backend; }
|
|
|
29
31
|
#include "JRole.hpp"
|
|
30
32
|
#include "GenerationStats.hpp"
|
|
31
33
|
#include "JGenerationStats.hpp"
|
|
34
|
+
#include "MemoryUsage.hpp"
|
|
35
|
+
#include "JMemoryUsage.hpp"
|
|
32
36
|
#include "LLMConfig.hpp"
|
|
33
37
|
#include <optional>
|
|
34
38
|
#include "JLLMConfig.hpp"
|
|
@@ -204,6 +208,11 @@ namespace margelo::nitro::litertlm {
|
|
|
204
208
|
auto __result = method(_javaPart);
|
|
205
209
|
return __result->toCpp();
|
|
206
210
|
}
|
|
211
|
+
MemoryUsage JHybridLiteRTLMSpec::getMemoryUsage() {
|
|
212
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JMemoryUsage>()>("getMemoryUsage");
|
|
213
|
+
auto __result = method(_javaPart);
|
|
214
|
+
return __result->toCpp();
|
|
215
|
+
}
|
|
207
216
|
void JHybridLiteRTLMSpec::close() {
|
|
208
217
|
static const auto method = javaClassStatic()->getMethod<void()>("close");
|
|
209
218
|
method(_javaPart);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JMemoryUsage.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include "MemoryUsage.hpp"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
namespace margelo::nitro::litertlm {
|
|
16
|
+
|
|
17
|
+
using namespace facebook;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The C++ JNI bridge between the C++ struct "MemoryUsage" and the the Kotlin data class "MemoryUsage".
|
|
21
|
+
*/
|
|
22
|
+
struct JMemoryUsage final: public jni::JavaClass<JMemoryUsage> {
|
|
23
|
+
public:
|
|
24
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/MemoryUsage;";
|
|
25
|
+
|
|
26
|
+
public:
|
|
27
|
+
/**
|
|
28
|
+
* Convert this Java/Kotlin-based struct to the C++ struct MemoryUsage by copying all values to C++.
|
|
29
|
+
*/
|
|
30
|
+
[[maybe_unused]]
|
|
31
|
+
[[nodiscard]]
|
|
32
|
+
MemoryUsage toCpp() const {
|
|
33
|
+
static const auto clazz = javaClassStatic();
|
|
34
|
+
static const auto fieldNativeHeapBytes = clazz->getField<double>("nativeHeapBytes");
|
|
35
|
+
double nativeHeapBytes = this->getFieldValue(fieldNativeHeapBytes);
|
|
36
|
+
static const auto fieldResidentBytes = clazz->getField<double>("residentBytes");
|
|
37
|
+
double residentBytes = this->getFieldValue(fieldResidentBytes);
|
|
38
|
+
static const auto fieldAvailableMemoryBytes = clazz->getField<double>("availableMemoryBytes");
|
|
39
|
+
double availableMemoryBytes = this->getFieldValue(fieldAvailableMemoryBytes);
|
|
40
|
+
static const auto fieldIsLowMemory = clazz->getField<jboolean>("isLowMemory");
|
|
41
|
+
jboolean isLowMemory = this->getFieldValue(fieldIsLowMemory);
|
|
42
|
+
return MemoryUsage(
|
|
43
|
+
nativeHeapBytes,
|
|
44
|
+
residentBytes,
|
|
45
|
+
availableMemoryBytes,
|
|
46
|
+
static_cast<bool>(isLowMemory)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public:
|
|
51
|
+
/**
|
|
52
|
+
* Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
|
|
53
|
+
*/
|
|
54
|
+
[[maybe_unused]]
|
|
55
|
+
static jni::local_ref<JMemoryUsage::javaobject> fromCpp(const MemoryUsage& value) {
|
|
56
|
+
using JSignature = JMemoryUsage(double, double, double, jboolean);
|
|
57
|
+
static const auto clazz = javaClassStatic();
|
|
58
|
+
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
59
|
+
return create(
|
|
60
|
+
clazz,
|
|
61
|
+
value.nativeHeapBytes,
|
|
62
|
+
value.residentBytes,
|
|
63
|
+
value.availableMemoryBytes,
|
|
64
|
+
value.isLowMemory
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
} // namespace margelo::nitro::litertlm
|
package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MemoryUsage.kt
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// MemoryUsage.kt
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
package com.margelo.nitro.dev.litert.litertlm
|
|
9
|
+
|
|
10
|
+
import androidx.annotation.Keep
|
|
11
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Represents the JavaScript object/struct "MemoryUsage".
|
|
16
|
+
*/
|
|
17
|
+
@DoNotStrip
|
|
18
|
+
@Keep
|
|
19
|
+
data class MemoryUsage(
|
|
20
|
+
@DoNotStrip
|
|
21
|
+
@Keep
|
|
22
|
+
val nativeHeapBytes: Double,
|
|
23
|
+
@DoNotStrip
|
|
24
|
+
@Keep
|
|
25
|
+
val residentBytes: Double,
|
|
26
|
+
@DoNotStrip
|
|
27
|
+
@Keep
|
|
28
|
+
val availableMemoryBytes: Double,
|
|
29
|
+
@DoNotStrip
|
|
30
|
+
@Keep
|
|
31
|
+
val isLowMemory: Boolean
|
|
32
|
+
) {
|
|
33
|
+
/* primary constructor */
|
|
34
|
+
|
|
35
|
+
companion object {
|
|
36
|
+
/**
|
|
37
|
+
* Constructor called from C++
|
|
38
|
+
*/
|
|
39
|
+
@DoNotStrip
|
|
40
|
+
@Keep
|
|
41
|
+
@Suppress("unused")
|
|
42
|
+
@JvmStatic
|
|
43
|
+
private fun fromCpp(nativeHeapBytes: Double, residentBytes: Double, availableMemoryBytes: Double, isLowMemory: Boolean): MemoryUsage {
|
|
44
|
+
return MemoryUsage(nativeHeapBytes, residentBytes, availableMemoryBytes, isLowMemory)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -25,6 +25,7 @@ namespace margelo::nitro::litertlm {
|
|
|
25
25
|
prototype.registerHybridMethod("resetConversation", &HybridLiteRTLMSpec::resetConversation);
|
|
26
26
|
prototype.registerHybridMethod("isReady", &HybridLiteRTLMSpec::isReady);
|
|
27
27
|
prototype.registerHybridMethod("getStats", &HybridLiteRTLMSpec::getStats);
|
|
28
|
+
prototype.registerHybridMethod("getMemoryUsage", &HybridLiteRTLMSpec::getMemoryUsage);
|
|
28
29
|
prototype.registerHybridMethod("close", &HybridLiteRTLMSpec::close);
|
|
29
30
|
});
|
|
30
31
|
}
|
|
@@ -19,6 +19,8 @@ namespace margelo::nitro::litertlm { struct LLMConfig; }
|
|
|
19
19
|
namespace margelo::nitro::litertlm { struct Message; }
|
|
20
20
|
// Forward declaration of `GenerationStats` to properly resolve imports.
|
|
21
21
|
namespace margelo::nitro::litertlm { struct GenerationStats; }
|
|
22
|
+
// Forward declaration of `MemoryUsage` to properly resolve imports.
|
|
23
|
+
namespace margelo::nitro::litertlm { struct MemoryUsage; }
|
|
22
24
|
|
|
23
25
|
#include <NitroModules/Promise.hpp>
|
|
24
26
|
#include <string>
|
|
@@ -28,6 +30,7 @@ namespace margelo::nitro::litertlm { struct GenerationStats; }
|
|
|
28
30
|
#include "Message.hpp"
|
|
29
31
|
#include <vector>
|
|
30
32
|
#include "GenerationStats.hpp"
|
|
33
|
+
#include "MemoryUsage.hpp"
|
|
31
34
|
|
|
32
35
|
namespace margelo::nitro::litertlm {
|
|
33
36
|
|
|
@@ -71,6 +74,7 @@ namespace margelo::nitro::litertlm {
|
|
|
71
74
|
virtual void resetConversation() = 0;
|
|
72
75
|
virtual bool isReady() = 0;
|
|
73
76
|
virtual GenerationStats getStats() = 0;
|
|
77
|
+
virtual MemoryUsage getMemoryUsage() = 0;
|
|
74
78
|
virtual void close() = 0;
|
|
75
79
|
|
|
76
80
|
protected:
|