@dr33m/react-native-litert-lm 0.5.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/LICENSE +21 -0
- package/README.md +627 -0
- package/android/CMakeLists.txt +32 -0
- package/android/build.gradle +104 -0
- package/android/src/main/AndroidManifest.xml +19 -0
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +888 -0
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridModelStore.kt +189 -0
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/LiteRTLMRegistry.kt +32 -0
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/MultimodalPartFactories.kt +15 -0
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/StreamingCallbackListener.kt +89 -0
- package/android/src/main/java/dev/litert/litertlm/LiteRTLMInitProvider.kt +57 -0
- package/android/src/main/java/dev/litert/litertlm/LiteRTLMPackage.kt +43 -0
- package/android/src/test/java/com/margelo/nitro/core/Promise.kt +46 -0
- package/android/src/test/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMTest.kt +122 -0
- package/app.plugin.js +58 -0
- package/cpp/cpp-adapter.cpp +21 -0
- package/cpp/include/README.md +32 -0
- package/cpp/include/litert_lm_engine.h +516 -0
- package/cpp/include/stb_image.h +7988 -0
- package/ios/HybridLiteRTLM+Execute.swift +287 -0
- package/ios/HybridLiteRTLM+Streaming.swift +153 -0
- package/ios/HybridLiteRTLM.swift +541 -0
- package/ios/HybridModelStore.swift +206 -0
- package/ios/MultimodalPart+Factories.swift +22 -0
- package/ios/Tests/HybridLiteRTLMTests.swift +187 -0
- package/lib/__mocks__/react-native-nitro-modules.d.ts +81 -0
- package/lib/__mocks__/react-native-nitro-modules.js +98 -0
- package/lib/__tests__/hooks.test.d.ts +1 -0
- package/lib/__tests__/hooks.test.js +175 -0
- package/lib/__tests__/inferenceRouting.test.d.ts +1 -0
- package/lib/__tests__/inferenceRouting.test.js +28 -0
- package/lib/__tests__/memoryTracker.test.d.ts +1 -0
- package/lib/__tests__/memoryTracker.test.js +74 -0
- package/lib/__tests__/modelFactory.test.d.ts +1 -0
- package/lib/__tests__/modelFactory.test.js +107 -0
- package/lib/__tests__/modelPath.test.d.ts +1 -0
- package/lib/__tests__/modelPath.test.js +12 -0
- package/lib/__tests__/modelRegistry.test.d.ts +1 -0
- package/lib/__tests__/modelRegistry.test.js +61 -0
- package/lib/hooks.d.ts +46 -0
- package/lib/hooks.js +159 -0
- package/lib/index.d.ts +142 -0
- package/lib/index.js +184 -0
- package/lib/inferenceRouting.d.ts +18 -0
- package/lib/inferenceRouting.js +63 -0
- package/lib/memoryTracker.d.ts +128 -0
- package/lib/memoryTracker.js +155 -0
- package/lib/modelFactory.d.ts +24 -0
- package/lib/modelFactory.js +128 -0
- package/lib/modelPath.d.ts +8 -0
- package/lib/modelPath.js +18 -0
- package/lib/modelRegistry.d.ts +55 -0
- package/lib/modelRegistry.js +82 -0
- package/lib/specs/LiteRTLM.nitro.d.ts +380 -0
- package/lib/specs/LiteRTLM.nitro.js +2 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/LiteRTLM+autolinking.cmake +83 -0
- package/nitrogen/generated/android/LiteRTLM+autolinking.gradle +27 -0
- package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +74 -0
- package/nitrogen/generated/android/LiteRTLMOnLoad.hpp +34 -0
- package/nitrogen/generated/android/c++/JBackend.hpp +61 -0
- package/nitrogen/generated/android/c++/JExecuteResult.hpp +86 -0
- package/nitrogen/generated/android/c++/JFunc_void_double.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string_bool.hpp +76 -0
- package/nitrogen/generated/android/c++/JGenerationStats.hpp +77 -0
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +371 -0
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +83 -0
- package/nitrogen/generated/android/c++/JHybridModelStoreSpec.cpp +101 -0
- package/nitrogen/generated/android/c++/JHybridModelStoreSpec.hpp +67 -0
- package/nitrogen/generated/android/c++/JLLMConfig.hpp +125 -0
- package/nitrogen/generated/android/c++/JMemoryUsage.hpp +69 -0
- package/nitrogen/generated/android/c++/JMessage.hpp +63 -0
- package/nitrogen/generated/android/c++/JModelFile.hpp +69 -0
- package/nitrogen/generated/android/c++/JMultimodalPart.hpp +78 -0
- package/nitrogen/generated/android/c++/JPartType.hpp +61 -0
- package/nitrogen/generated/android/c++/JRole.hpp +61 -0
- package/nitrogen/generated/android/c++/JToolCall.hpp +61 -0
- package/nitrogen/generated/android/c++/JToolDefinition.hpp +65 -0
- package/nitrogen/generated/android/c++/JToolResponse.hpp +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Backend.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ExecuteResult.kt +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Func_void_double.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Func_void_std__string_bool.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/GenerationStats.kt +76 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +165 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridModelStoreSpec.kt +76 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LLMConfig.kt +106 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LiteRTLMOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MemoryUsage.kt +66 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Message.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ModelFile.kt +66 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MultimodalPart.kt +71 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/PartType.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Role.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ToolCall.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ToolDefinition.kt +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ToolResponse.kt +56 -0
- package/nitrogen/generated/ios/LiteRTLM+autolinking.rb +62 -0
- package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.cpp +98 -0
- package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.hpp +585 -0
- package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Umbrella.hpp +93 -0
- package/nitrogen/generated/ios/LiteRTLMAutolinking.mm +41 -0
- package/nitrogen/generated/ios/LiteRTLMAutolinking.swift +38 -0
- package/nitrogen/generated/ios/c++/HybridLiteRTLMSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridLiteRTLMSpecSwift.hpp +279 -0
- package/nitrogen/generated/ios/c++/HybridModelStoreSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridModelStoreSpecSwift.hpp +117 -0
- package/nitrogen/generated/ios/swift/Backend.swift +44 -0
- package/nitrogen/generated/ios/swift/ExecuteResult.swift +45 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_ExecuteResult.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_double.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string_bool.swift +46 -0
- package/nitrogen/generated/ios/swift/GenerationStats.swift +54 -0
- package/nitrogen/generated/ios/swift/HybridLiteRTLMSpec.swift +75 -0
- package/nitrogen/generated/ios/swift/HybridLiteRTLMSpec_cxx.swift +516 -0
- package/nitrogen/generated/ios/swift/HybridModelStoreSpec.swift +59 -0
- package/nitrogen/generated/ios/swift/HybridModelStoreSpec_cxx.swift +203 -0
- package/nitrogen/generated/ios/swift/LLMConfig.swift +239 -0
- package/nitrogen/generated/ios/swift/MemoryUsage.swift +44 -0
- package/nitrogen/generated/ios/swift/Message.swift +34 -0
- package/nitrogen/generated/ios/swift/ModelFile.swift +44 -0
- package/nitrogen/generated/ios/swift/MultimodalPart.swift +101 -0
- package/nitrogen/generated/ios/swift/PartType.swift +44 -0
- package/nitrogen/generated/ios/swift/Role.swift +44 -0
- package/nitrogen/generated/ios/swift/ToolCall.swift +34 -0
- package/nitrogen/generated/ios/swift/ToolDefinition.swift +39 -0
- package/nitrogen/generated/ios/swift/ToolResponse.swift +34 -0
- package/nitrogen/generated/shared/c++/Backend.hpp +80 -0
- package/nitrogen/generated/shared/c++/ExecuteResult.hpp +94 -0
- package/nitrogen/generated/shared/c++/GenerationStats.hpp +103 -0
- package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp +41 -0
- package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.hpp +109 -0
- package/nitrogen/generated/shared/c++/HybridModelStoreSpec.cpp +25 -0
- package/nitrogen/generated/shared/c++/HybridModelStoreSpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/LLMConfig.hpp +134 -0
- package/nitrogen/generated/shared/c++/MemoryUsage.hpp +95 -0
- package/nitrogen/generated/shared/c++/Message.hpp +89 -0
- package/nitrogen/generated/shared/c++/ModelFile.hpp +95 -0
- package/nitrogen/generated/shared/c++/MultimodalPart.hpp +103 -0
- package/nitrogen/generated/shared/c++/PartType.hpp +80 -0
- package/nitrogen/generated/shared/c++/Role.hpp +80 -0
- package/nitrogen/generated/shared/c++/ToolCall.hpp +87 -0
- package/nitrogen/generated/shared/c++/ToolDefinition.hpp +91 -0
- package/nitrogen/generated/shared/c++/ToolResponse.hpp +87 -0
- package/package.json +115 -0
- package/react-native-litert-lm.podspec +62 -0
- package/react-native.config.js +16 -0
- package/scripts/download-ios-frameworks.sh +39 -0
- package/scripts/framework-source.js +46 -0
- package/scripts/postinstall.js +145 -0
- package/src/__mocks__/react-native-nitro-modules.ts +132 -0
- package/src/__tests__/hooks.test.ts +216 -0
- package/src/__tests__/inferenceRouting.test.ts +36 -0
- package/src/__tests__/memoryTracker.test.ts +87 -0
- package/src/__tests__/modelFactory.test.ts +153 -0
- package/src/__tests__/modelPath.test.ts +14 -0
- package/src/__tests__/modelRegistry.test.ts +73 -0
- package/src/hooks.ts +234 -0
- package/src/index.ts +214 -0
- package/src/inferenceRouting.ts +80 -0
- package/src/memoryTracker.ts +268 -0
- package/src/modelFactory.ts +163 -0
- package/src/modelPath.ts +16 -0
- package/src/modelRegistry.ts +104 -0
- package/src/specs/LiteRTLM.nitro.ts +451 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import type { HybridObject } from "react-native-nitro-modules";
|
|
2
|
+
/**
|
|
3
|
+
* Backend types for LLM inference.
|
|
4
|
+
* - 'cpu': CPU inference (slowest, always available)
|
|
5
|
+
* - 'gpu': GPU acceleration (fast, recommended for most devices)
|
|
6
|
+
* - 'npu': NPU/Neural Engine (fastest on supported hardware)
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* NPU acceleration requires compatible hardware (e.g., Qualcomm Hexagon, MediaTek APU).
|
|
10
|
+
* If NPU is unavailable, LiteRT-LM automatically falls back to GPU.
|
|
11
|
+
*/
|
|
12
|
+
export type Backend = "cpu" | "gpu" | "npu";
|
|
13
|
+
/**
|
|
14
|
+
* Message roles for conversation.
|
|
15
|
+
*/
|
|
16
|
+
export type Role = "user" | "model" | "system";
|
|
17
|
+
/**
|
|
18
|
+
* Definition for a function/tool that the model can request to execute.
|
|
19
|
+
*/
|
|
20
|
+
export interface ToolDefinition {
|
|
21
|
+
/** Name of the function/tool */
|
|
22
|
+
name: string;
|
|
23
|
+
/** Human-readable description of what the function/tool does */
|
|
24
|
+
description: string;
|
|
25
|
+
/** JSON schema defining parameter names and types (stringified) */
|
|
26
|
+
parametersJson: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A tool call requested by the model during inference.
|
|
30
|
+
*/
|
|
31
|
+
export interface ToolCall {
|
|
32
|
+
/** Name of the tool the model wants to call */
|
|
33
|
+
name: string;
|
|
34
|
+
/** JSON string of the arguments the model passed to the tool */
|
|
35
|
+
argumentsJson: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A tool response to send back to the model after executing a tool call.
|
|
39
|
+
*/
|
|
40
|
+
export interface ToolResponse {
|
|
41
|
+
/** Name of the tool that was executed */
|
|
42
|
+
name: string;
|
|
43
|
+
/** JSON string of the tool execution result */
|
|
44
|
+
responseJson: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Result from execute() containing both text and any tool calls.
|
|
48
|
+
*/
|
|
49
|
+
export interface ExecuteResult {
|
|
50
|
+
/** The model's text response */
|
|
51
|
+
text: string;
|
|
52
|
+
/** Tool calls requested by the model (empty array if none) */
|
|
53
|
+
toolCalls: ToolCall[];
|
|
54
|
+
/** The model's internal thinking/reasoning text (empty string if thinking is disabled) */
|
|
55
|
+
thinkingText: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The part type for a multimodal message content part.
|
|
59
|
+
*/
|
|
60
|
+
export type PartType = "text" | "image" | "audio";
|
|
61
|
+
/**
|
|
62
|
+
* A part of a unified multimodal message payload.
|
|
63
|
+
*/
|
|
64
|
+
export interface MultimodalPart {
|
|
65
|
+
/** The part type: 'text', 'image', or 'audio' */
|
|
66
|
+
type: PartType;
|
|
67
|
+
/** The plain text content, if type is 'text' */
|
|
68
|
+
text?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Local filesystem path to an image or audio file, if type is 'image' or 'audio'.
|
|
71
|
+
* Takes precedence over `imageBuffer`/`audioBuffer` if both are set.
|
|
72
|
+
*/
|
|
73
|
+
path?: string;
|
|
74
|
+
/** Raw image binary data, if type is 'image' (zero-copy ArrayBuffer mapping) */
|
|
75
|
+
imageBuffer?: ArrayBuffer;
|
|
76
|
+
/** Raw audio binary data, if type is 'audio' (zero-copy ArrayBuffer mapping) */
|
|
77
|
+
audioBuffer?: ArrayBuffer;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Configuration options for loading an LLM.
|
|
81
|
+
*/
|
|
82
|
+
export interface LLMConfig {
|
|
83
|
+
/**
|
|
84
|
+
* System prompt to set the model's behavior.
|
|
85
|
+
* This is prepended to the conversation to guide model responses.
|
|
86
|
+
* @example "You are a helpful coding assistant."
|
|
87
|
+
*/
|
|
88
|
+
systemPrompt?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Primary compute backend for text generation.
|
|
91
|
+
* - 'cpu': CPU inference (safe default, always available)
|
|
92
|
+
* - 'gpu': GPU acceleration (fast, Metal on iOS, GPU delegate on Android)
|
|
93
|
+
* - 'npu': NPU/Neural Engine (fastest on supported devices)
|
|
94
|
+
*
|
|
95
|
+
* If not specified, defaults to 'cpu'.
|
|
96
|
+
* If specified backend is unavailable, falls back automatically.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* Vision encoder is always set to GPU (required by Gemma models).
|
|
100
|
+
* Audio encoder is always set to CPU (optimal for audio processing).
|
|
101
|
+
*
|
|
102
|
+
* @default 'cpu'
|
|
103
|
+
*/
|
|
104
|
+
backend?: Backend;
|
|
105
|
+
/**
|
|
106
|
+
* Total engine KV-cache budget in tokens (prompt + history + output combined).
|
|
107
|
+
* Must be ≥ the total number of input tokens + desired output tokens.
|
|
108
|
+
* For compiled bundles, should not exceed the bundle's compiled cache_length.
|
|
109
|
+
* @default 4096
|
|
110
|
+
*/
|
|
111
|
+
maxContextTokens?: number;
|
|
112
|
+
/**
|
|
113
|
+
* Maximum number of tokens the model generates per response.
|
|
114
|
+
* For compiled bundles, must not exceed the bundle's compiled decode-chunk size.
|
|
115
|
+
*
|
|
116
|
+
* @remarks
|
|
117
|
+
* **iOS only** — the Android Kotlin SDK does not currently expose per-session
|
|
118
|
+
* output token control. On Android this value is accepted but has no effect;
|
|
119
|
+
* the engine uses its own default.
|
|
120
|
+
*
|
|
121
|
+
* @default 1024
|
|
122
|
+
*/
|
|
123
|
+
maxOutputTokens?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Maximum number of tokens to generate.
|
|
126
|
+
* @deprecated Use `maxContextTokens` and `maxOutputTokens` instead.
|
|
127
|
+
* When set alone (without the new fields), maps to both for backward compatibility.
|
|
128
|
+
* @default 1024
|
|
129
|
+
*/
|
|
130
|
+
maxTokens?: number;
|
|
131
|
+
/**
|
|
132
|
+
* Sampling temperature (0.0 = deterministic, 1.0 = creative).
|
|
133
|
+
* @default 0.7
|
|
134
|
+
*/
|
|
135
|
+
temperature?: number;
|
|
136
|
+
/**
|
|
137
|
+
* Top-K sampling (number of top tokens to consider).
|
|
138
|
+
* @default 40
|
|
139
|
+
*/
|
|
140
|
+
topK?: number;
|
|
141
|
+
/**
|
|
142
|
+
* Top-P (nucleus) sampling threshold.
|
|
143
|
+
* @default 0.95
|
|
144
|
+
*/
|
|
145
|
+
topP?: number;
|
|
146
|
+
/**
|
|
147
|
+
* Whether this is a multimodal model.
|
|
148
|
+
* When enabled, the engine handles image/audio tokens properly.
|
|
149
|
+
* If not specified, the system will fall back to filename sniffing.
|
|
150
|
+
*/
|
|
151
|
+
multimodal?: boolean;
|
|
152
|
+
/**
|
|
153
|
+
* List of tools/functions that the model can call.
|
|
154
|
+
*/
|
|
155
|
+
tools?: ToolDefinition[];
|
|
156
|
+
/**
|
|
157
|
+
* Whether to enable speculative decoding (multi-token prediction) if supported by the model.
|
|
158
|
+
* @default false
|
|
159
|
+
*/
|
|
160
|
+
enableSpeculativeDecoding?: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Whether to enable thinking/reasoning mode.
|
|
163
|
+
* When enabled, the model generates internal reasoning before the final response.
|
|
164
|
+
* The thinking text is available in ExecuteResult.thinkingText and streamed via onThinkingToken.
|
|
165
|
+
* @default false
|
|
166
|
+
*/
|
|
167
|
+
enableThinking?: boolean;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* A simple message in the conversation.
|
|
171
|
+
* For multimodal, use sendMessageWithImage/sendMessageWithAudio instead.
|
|
172
|
+
*/
|
|
173
|
+
export interface Message {
|
|
174
|
+
/** Role of the message sender */
|
|
175
|
+
role: Role;
|
|
176
|
+
/** Text content of the message */
|
|
177
|
+
content: string;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Generation statistics returned after completion.
|
|
181
|
+
*/
|
|
182
|
+
export interface GenerationStats {
|
|
183
|
+
/** Number of tokens in the prompt */
|
|
184
|
+
promptTokens: number;
|
|
185
|
+
/** Number of tokens generated */
|
|
186
|
+
completionTokens: number;
|
|
187
|
+
/** Total tokens (prompt + completion) */
|
|
188
|
+
totalTokens: number;
|
|
189
|
+
/** Time to first token in milliseconds */
|
|
190
|
+
timeToFirstToken: number;
|
|
191
|
+
/** Total generation time in milliseconds */
|
|
192
|
+
totalTime: number;
|
|
193
|
+
/** Tokens per second */
|
|
194
|
+
tokensPerSecond: number;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Real memory usage statistics from the native runtime.
|
|
198
|
+
* Measured from OS-level APIs, not estimated.
|
|
199
|
+
*/
|
|
200
|
+
export interface MemoryUsage {
|
|
201
|
+
/** Native heap allocated bytes (Debug.getNativeHeapAllocatedSize on Android, malloc_size on iOS) */
|
|
202
|
+
nativeHeapBytes: number;
|
|
203
|
+
/** Total process resident set size (RSS) in bytes */
|
|
204
|
+
residentBytes: number;
|
|
205
|
+
/** Available system memory in bytes */
|
|
206
|
+
availableMemoryBytes: number;
|
|
207
|
+
/** Whether the system considers memory low */
|
|
208
|
+
isLowMemory: boolean;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* LiteRT-LM: High-performance LLM inference engine.
|
|
212
|
+
* Supports Gemma 4, Gemma 3n, Phi-4, Qwen, and other .litertlm models.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const llm = createLLM();
|
|
217
|
+
* llm.loadModel('/path/to/gemma-4-E2B-it.litertlm', { backend: 'cpu' });
|
|
218
|
+
*
|
|
219
|
+
* // Blocking generation
|
|
220
|
+
* const response = llm.sendMessage('What is the capital of France?');
|
|
221
|
+
*
|
|
222
|
+
* // Streaming generation
|
|
223
|
+
* llm.sendMessageAsync('Tell me a story', (token, done) => {
|
|
224
|
+
* process.stdout.write(token);
|
|
225
|
+
* });
|
|
226
|
+
*
|
|
227
|
+
* llm.close();
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export interface LiteRTLM extends HybridObject<{
|
|
231
|
+
ios: "swift";
|
|
232
|
+
android: "kotlin";
|
|
233
|
+
}> {
|
|
234
|
+
/**
|
|
235
|
+
* Load a .litertlm model file.
|
|
236
|
+
* @param config Optional configuration for backend and sampling.
|
|
237
|
+
* @throws Error if the model cannot be loaded.
|
|
238
|
+
*/
|
|
239
|
+
loadModel(modelPath: string, config?: LLMConfig): Promise<void>;
|
|
240
|
+
/**
|
|
241
|
+
* Send a text message and get the complete response (blocking).
|
|
242
|
+
* @param message User message text.
|
|
243
|
+
* @returns ExecuteResult with text and any tool calls.
|
|
244
|
+
*/
|
|
245
|
+
sendMessage(message: string): Promise<ExecuteResult>;
|
|
246
|
+
/**
|
|
247
|
+
* Send a text message with an image (multimodal).
|
|
248
|
+
* @param message User message text.
|
|
249
|
+
* @param imagePath Absolute path to an image file.
|
|
250
|
+
* @returns ExecuteResult with text and any tool calls.
|
|
251
|
+
*/
|
|
252
|
+
sendMessageWithImage(message: string, imagePath: string): Promise<ExecuteResult>;
|
|
253
|
+
/**
|
|
254
|
+
* Send a text message with an image and get a streaming response.
|
|
255
|
+
* Tokens are delivered via callback as they are generated.
|
|
256
|
+
* @param message User message text.
|
|
257
|
+
* @param imagePath Absolute path to an image file.
|
|
258
|
+
* @param onToken Callback invoked for each token (token, isDone).
|
|
259
|
+
*/
|
|
260
|
+
sendMessageWithImageAsync(message: string, imagePath: string, onToken: (token: string, done: boolean) => void): Promise<void>;
|
|
261
|
+
/**
|
|
262
|
+
* Download a model file from a URL.
|
|
263
|
+
* @deprecated Use ModelRegistry instead.
|
|
264
|
+
*/
|
|
265
|
+
downloadModel(url: string, fileName: string, onProgress?: (progress: number) => void): Promise<string>;
|
|
266
|
+
/**
|
|
267
|
+
* Delete a downloaded model file.
|
|
268
|
+
* @deprecated Use ModelRegistry instead.
|
|
269
|
+
*/
|
|
270
|
+
deleteModel(fileName: string): Promise<void>;
|
|
271
|
+
/**
|
|
272
|
+
* Send a text message with audio (multimodal).
|
|
273
|
+
* @param message User message text.
|
|
274
|
+
* @param audioPath Absolute path to an audio file (WAV).
|
|
275
|
+
* @returns The model's response text.
|
|
276
|
+
*/
|
|
277
|
+
sendMessageWithAudio(message: string, audioPath: string): Promise<ExecuteResult>;
|
|
278
|
+
/**
|
|
279
|
+
* Send a text message with audio and get a streaming response.
|
|
280
|
+
* Tokens are delivered via callback as they are generated.
|
|
281
|
+
* @param message User message text.
|
|
282
|
+
* @param audioPath Absolute path to an audio file (WAV).
|
|
283
|
+
* @param onToken Callback invoked for each token (token, isDone).
|
|
284
|
+
*/
|
|
285
|
+
sendMessageWithAudioAsync(message: string, audioPath: string, onToken: (token: string, done: boolean) => void): Promise<void>;
|
|
286
|
+
/**
|
|
287
|
+
* Send a unified multimodal message containing text and/or zero-copy binary buffers.
|
|
288
|
+
* @param parts The message content parts (text, image, and/or audio).
|
|
289
|
+
* @returns The model's response text.
|
|
290
|
+
*/
|
|
291
|
+
sendMultimodalMessage(parts: MultimodalPart[]): Promise<ExecuteResult>;
|
|
292
|
+
/**
|
|
293
|
+
* Send a message with streaming response.
|
|
294
|
+
* Tokens are delivered via callback as they are generated.
|
|
295
|
+
* @param message User message text.
|
|
296
|
+
* @param onToken Callback invoked for each token (token, isDone).
|
|
297
|
+
*/
|
|
298
|
+
sendMessageAsync(message: string, onToken: (token: string, done: boolean) => void): Promise<void>;
|
|
299
|
+
/**
|
|
300
|
+
* Get the current conversation history.
|
|
301
|
+
* @returns Array of messages in the conversation.
|
|
302
|
+
*/
|
|
303
|
+
getHistory(): Message[];
|
|
304
|
+
/**
|
|
305
|
+
* Clear the conversation context and start fresh.
|
|
306
|
+
*/
|
|
307
|
+
resetConversation(): void;
|
|
308
|
+
/**
|
|
309
|
+
* Check if a model is loaded and ready for inference.
|
|
310
|
+
*/
|
|
311
|
+
isReady(): boolean;
|
|
312
|
+
/**
|
|
313
|
+
* Get the last generation statistics.
|
|
314
|
+
*/
|
|
315
|
+
getStats(): GenerationStats;
|
|
316
|
+
/**
|
|
317
|
+
* Count tokens in a text string. Returns -1 if unavailable.
|
|
318
|
+
*/
|
|
319
|
+
countTokens(text: string): number;
|
|
320
|
+
/**
|
|
321
|
+
* Get real memory usage from the native runtime.
|
|
322
|
+
* Uses OS-level APIs to report actual memory consumption.
|
|
323
|
+
*/
|
|
324
|
+
getMemoryUsage(): MemoryUsage;
|
|
325
|
+
/**
|
|
326
|
+
* Execute a unified multimodal inference request.
|
|
327
|
+
*
|
|
328
|
+
* A single deep entry point that replaces all modalality-specific send methods.
|
|
329
|
+
* Accepts text, image (path or buffer), and audio (path or buffer) parts.
|
|
330
|
+
* When onToken is provided, streams tokens incrementally; otherwise resolves
|
|
331
|
+
* with the complete response.
|
|
332
|
+
*
|
|
333
|
+
* @param parts Array of content parts (text, image, audio)
|
|
334
|
+
* @param onToken Optional streaming callback - invoked per token with (token, isDone)
|
|
335
|
+
* @returns Promise resolving to ExecuteResult with text and any tool calls
|
|
336
|
+
*/
|
|
337
|
+
execute(parts: MultimodalPart[], onToken?: (token: string, done: boolean) => void): Promise<ExecuteResult>;
|
|
338
|
+
/**
|
|
339
|
+
* Send tool execution results back to the model to continue generation.
|
|
340
|
+
* Call this after receiving tool calls from execute() and executing them.
|
|
341
|
+
*
|
|
342
|
+
* @param responses Array of tool responses with name and result JSON
|
|
343
|
+
* @param onToken Optional streaming callback
|
|
344
|
+
* @returns Promise resolving to ExecuteResult (may contain further tool calls)
|
|
345
|
+
*/
|
|
346
|
+
sendToolResponse(responses: ToolResponse[], onToken?: (token: string, done: boolean) => void): Promise<ExecuteResult>;
|
|
347
|
+
/**
|
|
348
|
+
* Get the backend the engine is actually using after initialization.
|
|
349
|
+
* If GPU was requested but unavailable, this returns 'cpu' after fallback.
|
|
350
|
+
* Returns the requested backend before loadModel is called.
|
|
351
|
+
*/
|
|
352
|
+
getActiveBackend(): Backend;
|
|
353
|
+
/**
|
|
354
|
+
* Cancel any in-progress inference.
|
|
355
|
+
* Partial output generated so far is preserved.
|
|
356
|
+
* No-op if nothing is generating.
|
|
357
|
+
*/
|
|
358
|
+
stopGeneration(): void;
|
|
359
|
+
/**
|
|
360
|
+
* Release all native resources.
|
|
361
|
+
* Call this when done with the LLM instance.
|
|
362
|
+
*/
|
|
363
|
+
close(): void;
|
|
364
|
+
}
|
|
365
|
+
export interface ModelFile {
|
|
366
|
+
fileName: string;
|
|
367
|
+
absolutePath: string;
|
|
368
|
+
sizeBytes: number;
|
|
369
|
+
lastModifiedMs: number;
|
|
370
|
+
}
|
|
371
|
+
export interface ModelStore extends HybridObject<{
|
|
372
|
+
ios: "swift";
|
|
373
|
+
android: "kotlin";
|
|
374
|
+
}> {
|
|
375
|
+
isCached(fileName: string): boolean;
|
|
376
|
+
getFilePath(fileName: string): string;
|
|
377
|
+
listCachedFiles(): ModelFile[];
|
|
378
|
+
deleteFile(fileName: string): void;
|
|
379
|
+
downloadFile(url: string, fileName: string, headersJson: string, onProgress: (progress: number) => void): Promise<string>;
|
|
380
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
** linguist-generated=true
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#
|
|
2
|
+
# LiteRTLM+autolinking.cmake
|
|
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
|
+
# This is a CMake file that adds all files generated by Nitrogen
|
|
9
|
+
# to the current CMake project.
|
|
10
|
+
#
|
|
11
|
+
# To use it, add this to your CMakeLists.txt:
|
|
12
|
+
# ```cmake
|
|
13
|
+
# include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/LiteRTLM+autolinking.cmake)
|
|
14
|
+
# ```
|
|
15
|
+
|
|
16
|
+
# Define a flag to check if we are building properly
|
|
17
|
+
add_definitions(-DBUILDING_LITERTLM_WITH_GENERATED_CMAKE_PROJECT)
|
|
18
|
+
|
|
19
|
+
# Enable Raw Props parsing in react-native (for Nitro Views)
|
|
20
|
+
add_definitions(-DRN_SERIALIZABLE_STATE)
|
|
21
|
+
|
|
22
|
+
# Add all headers that were generated by Nitrogen
|
|
23
|
+
include_directories(
|
|
24
|
+
"../nitrogen/generated/shared/c++"
|
|
25
|
+
"../nitrogen/generated/android/c++"
|
|
26
|
+
"../nitrogen/generated/android/"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
# Add all .cpp sources that were generated by Nitrogen
|
|
30
|
+
target_sources(
|
|
31
|
+
# CMake project name (Android C++ library name)
|
|
32
|
+
LiteRTLM PRIVATE
|
|
33
|
+
# Autolinking Setup
|
|
34
|
+
../nitrogen/generated/android/LiteRTLMOnLoad.cpp
|
|
35
|
+
# Shared Nitrogen C++ sources
|
|
36
|
+
../nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp
|
|
37
|
+
../nitrogen/generated/shared/c++/HybridModelStoreSpec.cpp
|
|
38
|
+
# Android-specific Nitrogen C++ sources
|
|
39
|
+
../nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp
|
|
40
|
+
../nitrogen/generated/android/c++/JHybridModelStoreSpec.cpp
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# From node_modules/react-native/ReactAndroid/cmake-utils/folly-flags.cmake
|
|
44
|
+
# Used in node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake
|
|
45
|
+
target_compile_definitions(
|
|
46
|
+
LiteRTLM PRIVATE
|
|
47
|
+
-DFOLLY_NO_CONFIG=1
|
|
48
|
+
-DFOLLY_HAVE_CLOCK_GETTIME=1
|
|
49
|
+
-DFOLLY_USE_LIBCPP=1
|
|
50
|
+
-DFOLLY_CFG_NO_COROUTINES=1
|
|
51
|
+
-DFOLLY_MOBILE=1
|
|
52
|
+
-DFOLLY_HAVE_RECVMMSG=1
|
|
53
|
+
-DFOLLY_HAVE_PTHREAD=1
|
|
54
|
+
# Once we target android-23 above, we can comment
|
|
55
|
+
# the following line. NDK uses GNU style stderror_r() after API 23.
|
|
56
|
+
-DFOLLY_HAVE_XSI_STRERROR_R=1
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Add all libraries required by the generated specs
|
|
60
|
+
find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C++
|
|
61
|
+
find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)
|
|
62
|
+
find_package(react-native-nitro-modules REQUIRED) # <-- Used to create all HybridObjects and use the Nitro core library
|
|
63
|
+
|
|
64
|
+
# Link all libraries together
|
|
65
|
+
target_link_libraries(
|
|
66
|
+
LiteRTLM
|
|
67
|
+
fbjni::fbjni # <-- Facebook C++ JNI helpers
|
|
68
|
+
ReactAndroid::jsi # <-- RN: JSI
|
|
69
|
+
react-native-nitro-modules::NitroModules # <-- NitroModules Core :)
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# Link react-native (different prefab between RN 0.75 and RN 0.76)
|
|
73
|
+
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
|
|
74
|
+
target_link_libraries(
|
|
75
|
+
LiteRTLM
|
|
76
|
+
ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
|
|
77
|
+
)
|
|
78
|
+
else()
|
|
79
|
+
target_link_libraries(
|
|
80
|
+
LiteRTLM
|
|
81
|
+
ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
|
|
82
|
+
)
|
|
83
|
+
endif()
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LiteRTLM+autolinking.gradle
|
|
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
|
+
/// This is a Gradle file that adds all files generated by Nitrogen
|
|
9
|
+
/// to the current Gradle project.
|
|
10
|
+
///
|
|
11
|
+
/// To use it, add this to your build.gradle:
|
|
12
|
+
/// ```gradle
|
|
13
|
+
/// apply from: '../nitrogen/generated/android/LiteRTLM+autolinking.gradle'
|
|
14
|
+
/// ```
|
|
15
|
+
|
|
16
|
+
logger.warn("[NitroModules] 🔥 LiteRTLM is boosted by nitro!")
|
|
17
|
+
|
|
18
|
+
android {
|
|
19
|
+
sourceSets {
|
|
20
|
+
main {
|
|
21
|
+
java.srcDirs += [
|
|
22
|
+
// Nitrogen files
|
|
23
|
+
"${project.projectDir}/../nitrogen/generated/android/kotlin"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LiteRTLMOnLoad.cpp
|
|
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
|
+
#ifndef BUILDING_LITERTLM_WITH_GENERATED_CMAKE_PROJECT
|
|
9
|
+
#error LiteRTLMOnLoad.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
#include "LiteRTLMOnLoad.hpp"
|
|
13
|
+
|
|
14
|
+
#include <jni.h>
|
|
15
|
+
#include <fbjni/fbjni.h>
|
|
16
|
+
#include <NitroModules/HybridObjectRegistry.hpp>
|
|
17
|
+
|
|
18
|
+
#include "JHybridLiteRTLMSpec.hpp"
|
|
19
|
+
#include "JFunc_void_std__string_bool.hpp"
|
|
20
|
+
#include "JFunc_void_double.hpp"
|
|
21
|
+
#include "JHybridModelStoreSpec.hpp"
|
|
22
|
+
#include <NitroModules/DefaultConstructableObject.hpp>
|
|
23
|
+
|
|
24
|
+
namespace margelo::nitro::litertlm {
|
|
25
|
+
|
|
26
|
+
int initialize(JavaVM* vm) {
|
|
27
|
+
return facebook::jni::initialize(vm, []() {
|
|
28
|
+
::margelo::nitro::litertlm::registerAllNatives();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
struct JHybridLiteRTLMSpecImpl: public jni::JavaClass<JHybridLiteRTLMSpecImpl, JHybridLiteRTLMSpec::JavaPart> {
|
|
33
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM;";
|
|
34
|
+
static std::shared_ptr<JHybridLiteRTLMSpec> create() {
|
|
35
|
+
static const auto constructorFn = javaClassStatic()->getConstructor<JHybridLiteRTLMSpecImpl::javaobject()>();
|
|
36
|
+
jni::local_ref<JHybridLiteRTLMSpec::JavaPart> javaPart = javaClassStatic()->newObject(constructorFn);
|
|
37
|
+
return javaPart->getJHybridLiteRTLMSpec();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
struct JHybridModelStoreSpecImpl: public jni::JavaClass<JHybridModelStoreSpecImpl, JHybridModelStoreSpec::JavaPart> {
|
|
41
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/HybridModelStore;";
|
|
42
|
+
static std::shared_ptr<JHybridModelStoreSpec> create() {
|
|
43
|
+
static const auto constructorFn = javaClassStatic()->getConstructor<JHybridModelStoreSpecImpl::javaobject()>();
|
|
44
|
+
jni::local_ref<JHybridModelStoreSpec::JavaPart> javaPart = javaClassStatic()->newObject(constructorFn);
|
|
45
|
+
return javaPart->getJHybridModelStoreSpec();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
void registerAllNatives() {
|
|
50
|
+
using namespace margelo::nitro;
|
|
51
|
+
using namespace margelo::nitro::litertlm;
|
|
52
|
+
|
|
53
|
+
// Register native JNI methods
|
|
54
|
+
margelo::nitro::litertlm::JHybridLiteRTLMSpec::CxxPart::registerNatives();
|
|
55
|
+
margelo::nitro::litertlm::JFunc_void_std__string_bool_cxx::registerNatives();
|
|
56
|
+
margelo::nitro::litertlm::JFunc_void_double_cxx::registerNatives();
|
|
57
|
+
margelo::nitro::litertlm::JHybridModelStoreSpec::CxxPart::registerNatives();
|
|
58
|
+
|
|
59
|
+
// Register Nitro Hybrid Objects
|
|
60
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
61
|
+
"LiteRTLM",
|
|
62
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
63
|
+
return JHybridLiteRTLMSpecImpl::create();
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
67
|
+
"ModelStore",
|
|
68
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
69
|
+
return JHybridModelStoreSpecImpl::create();
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LiteRTLMOnLoad.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
|
+
#include <jni.h>
|
|
9
|
+
#include <functional>
|
|
10
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
11
|
+
|
|
12
|
+
namespace margelo::nitro::litertlm {
|
|
13
|
+
|
|
14
|
+
[[deprecated("Use registerNatives() instead.")]]
|
|
15
|
+
int initialize(JavaVM* vm);
|
|
16
|
+
|
|
17
|
+
/**
|
|
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.
|
|
21
|
+
* Example:
|
|
22
|
+
* ```cpp (cpp-adapter.cpp)
|
|
23
|
+
* JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
|
|
24
|
+
* return facebook::jni::initialize(vm, []() {
|
|
25
|
+
* // register all LiteRTLM HybridObjects
|
|
26
|
+
* margelo::nitro::litertlm::registerNatives();
|
|
27
|
+
* // any other custom registrations go here.
|
|
28
|
+
* });
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
void registerAllNatives();
|
|
33
|
+
|
|
34
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JBackend.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 "Backend.hpp"
|
|
12
|
+
|
|
13
|
+
namespace margelo::nitro::litertlm {
|
|
14
|
+
|
|
15
|
+
using namespace facebook;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The C++ JNI bridge between the C++ enum "Backend" and the the Kotlin enum "Backend".
|
|
19
|
+
*/
|
|
20
|
+
struct JBackend final: public jni::JavaClass<JBackend> {
|
|
21
|
+
public:
|
|
22
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/Backend;";
|
|
23
|
+
|
|
24
|
+
public:
|
|
25
|
+
/**
|
|
26
|
+
* Convert this Java/Kotlin-based enum to the C++ enum Backend.
|
|
27
|
+
*/
|
|
28
|
+
[[maybe_unused]]
|
|
29
|
+
[[nodiscard]]
|
|
30
|
+
Backend toCpp() const {
|
|
31
|
+
static const auto clazz = javaClassStatic();
|
|
32
|
+
static const auto fieldOrdinal = clazz->getField<int>("value");
|
|
33
|
+
int ordinal = this->getFieldValue(fieldOrdinal);
|
|
34
|
+
return static_cast<Backend>(ordinal);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public:
|
|
38
|
+
/**
|
|
39
|
+
* Create a Java/Kotlin-based enum with the given C++ enum's value.
|
|
40
|
+
*/
|
|
41
|
+
[[maybe_unused]]
|
|
42
|
+
static jni::alias_ref<JBackend> fromCpp(Backend value) {
|
|
43
|
+
static const auto clazz = javaClassStatic();
|
|
44
|
+
switch (value) {
|
|
45
|
+
case Backend::CPU:
|
|
46
|
+
static const auto fieldCPU = clazz->getStaticField<JBackend>("CPU");
|
|
47
|
+
return clazz->getStaticFieldValue(fieldCPU);
|
|
48
|
+
case Backend::GPU:
|
|
49
|
+
static const auto fieldGPU = clazz->getStaticField<JBackend>("GPU");
|
|
50
|
+
return clazz->getStaticFieldValue(fieldGPU);
|
|
51
|
+
case Backend::NPU:
|
|
52
|
+
static const auto fieldNPU = clazz->getStaticField<JBackend>("NPU");
|
|
53
|
+
return clazz->getStaticFieldValue(fieldNPU);
|
|
54
|
+
default:
|
|
55
|
+
std::string stringValue = std::to_string(static_cast<int>(value));
|
|
56
|
+
throw std::invalid_argument("Invalid enum value (" + stringValue + "!");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
} // namespace margelo::nitro::litertlm
|