cui-llama.rn 0.2.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.
Files changed (76) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +330 -0
  3. package/android/build.gradle +107 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +4 -0
  6. package/android/src/main/CMakeLists.txt +69 -0
  7. package/android/src/main/java/com/rnllama/LlamaContext.java +353 -0
  8. package/android/src/main/java/com/rnllama/RNLlama.java +446 -0
  9. package/android/src/main/java/com/rnllama/RNLlamaPackage.java +48 -0
  10. package/android/src/main/jni.cpp +635 -0
  11. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +94 -0
  12. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +95 -0
  13. package/cpp/README.md +4 -0
  14. package/cpp/common.cpp +3237 -0
  15. package/cpp/common.h +467 -0
  16. package/cpp/ggml-aarch64.c +2193 -0
  17. package/cpp/ggml-aarch64.h +39 -0
  18. package/cpp/ggml-alloc.c +1041 -0
  19. package/cpp/ggml-alloc.h +76 -0
  20. package/cpp/ggml-backend-impl.h +153 -0
  21. package/cpp/ggml-backend.c +2225 -0
  22. package/cpp/ggml-backend.h +236 -0
  23. package/cpp/ggml-common.h +1829 -0
  24. package/cpp/ggml-impl.h +655 -0
  25. package/cpp/ggml-metal.h +65 -0
  26. package/cpp/ggml-metal.m +3273 -0
  27. package/cpp/ggml-quants.c +15022 -0
  28. package/cpp/ggml-quants.h +132 -0
  29. package/cpp/ggml.c +22034 -0
  30. package/cpp/ggml.h +2444 -0
  31. package/cpp/grammar-parser.cpp +536 -0
  32. package/cpp/grammar-parser.h +29 -0
  33. package/cpp/json-schema-to-grammar.cpp +1045 -0
  34. package/cpp/json-schema-to-grammar.h +8 -0
  35. package/cpp/json.hpp +24766 -0
  36. package/cpp/llama.cpp +21789 -0
  37. package/cpp/llama.h +1201 -0
  38. package/cpp/log.h +737 -0
  39. package/cpp/rn-llama.hpp +630 -0
  40. package/cpp/sampling.cpp +460 -0
  41. package/cpp/sampling.h +160 -0
  42. package/cpp/sgemm.cpp +1027 -0
  43. package/cpp/sgemm.h +14 -0
  44. package/cpp/unicode-data.cpp +7032 -0
  45. package/cpp/unicode-data.h +20 -0
  46. package/cpp/unicode.cpp +812 -0
  47. package/cpp/unicode.h +64 -0
  48. package/ios/RNLlama.h +11 -0
  49. package/ios/RNLlama.mm +302 -0
  50. package/ios/RNLlama.xcodeproj/project.pbxproj +278 -0
  51. package/ios/RNLlamaContext.h +39 -0
  52. package/ios/RNLlamaContext.mm +426 -0
  53. package/jest/mock.js +169 -0
  54. package/lib/commonjs/NativeRNLlama.js +10 -0
  55. package/lib/commonjs/NativeRNLlama.js.map +1 -0
  56. package/lib/commonjs/grammar.js +574 -0
  57. package/lib/commonjs/grammar.js.map +1 -0
  58. package/lib/commonjs/index.js +151 -0
  59. package/lib/commonjs/index.js.map +1 -0
  60. package/lib/module/NativeRNLlama.js +3 -0
  61. package/lib/module/NativeRNLlama.js.map +1 -0
  62. package/lib/module/grammar.js +566 -0
  63. package/lib/module/grammar.js.map +1 -0
  64. package/lib/module/index.js +129 -0
  65. package/lib/module/index.js.map +1 -0
  66. package/lib/typescript/NativeRNLlama.d.ts +107 -0
  67. package/lib/typescript/NativeRNLlama.d.ts.map +1 -0
  68. package/lib/typescript/grammar.d.ts +38 -0
  69. package/lib/typescript/grammar.d.ts.map +1 -0
  70. package/lib/typescript/index.d.ts +46 -0
  71. package/lib/typescript/index.d.ts.map +1 -0
  72. package/llama-rn.podspec +56 -0
  73. package/package.json +230 -0
  74. package/src/NativeRNLlama.ts +132 -0
  75. package/src/grammar.ts +849 -0
  76. package/src/index.ts +182 -0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Jhen-Jie Hong
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,330 @@
1
+ # cui-llama.rn
2
+
3
+ This is a fork of llama.rn meant for ChatterUI
4
+
5
+ Original repo README.md below.
6
+
7
+ # llama.rn
8
+
9
+ [![Actions Status](https://github.com/mybigday/llama.rn/workflows/CI/badge.svg)](https://github.com/mybigday/llama.rn/actions)
10
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
11
+ [![npm](https://img.shields.io/npm/v/llama.rn.svg)](https://www.npmjs.com/package/llama.rn/)
12
+
13
+ React Native binding of [llama.cpp](https://github.com/ggerganov/llama.cpp).
14
+
15
+ [llama.cpp](https://github.com/ggerganov/llama.cpp): Inference of [LLaMA](https://arxiv.org/abs/2302.13971) model in pure C/C++
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ npm install llama.rn
21
+ ```
22
+
23
+ #### iOS
24
+
25
+ Please re-run `npx pod-install` again.
26
+
27
+ #### Android
28
+
29
+ Add proguard rule if it's enabled in project (android/app/proguard-rules.pro):
30
+
31
+ ```proguard
32
+ # llama.rn
33
+ -keep class com.rnllama.** { *; }
34
+ ```
35
+
36
+ ## Obtain the model
37
+
38
+ You can search HuggingFace for available models (Keyword: [`GGUF`](https://huggingface.co/search/full-text?q=GGUF&type=model)).
39
+
40
+ For create a GGUF model manually, for example in Llama 2:
41
+
42
+ Download the Llama 2 model
43
+
44
+ 1. Request access from [here](https://ai.meta.com/llama)
45
+ 2. Download the model from HuggingFace [here](https://huggingface.co/meta-llama/Llama-2-7b-chat) (`Llama-2-7b-chat`)
46
+
47
+ Convert the model to ggml format
48
+
49
+ ```bash
50
+ # Start with submodule in this repo (or you can clone the repo https://github.com/ggerganov/llama.cpp.git)
51
+ yarn && yarn bootstrap
52
+ cd llama.cpp
53
+
54
+ # install Python dependencies
55
+ python3 -m pip install -r requirements.txt
56
+
57
+ # Move the Llama model weights to the models folder
58
+ mv <path to Llama-2-7b-chat> ./models/7B
59
+
60
+ # convert the 7B model to ggml FP16 format
61
+ python3 convert.py models/7B/ --outtype f16
62
+
63
+ # Build the quantize tool
64
+ make quantize
65
+
66
+ # quantize the model to 2-bits (using q2_k method)
67
+ ./quantize ./models/7B/ggml-model-f16.gguf ./models/7B/ggml-model-q2_k.gguf q2_k
68
+
69
+ # quantize the model to 4-bits (using q4_0 method)
70
+ ./quantize ./models/7B/ggml-model-f16.gguf ./models/7B/ggml-model-q4_0.gguf q4_0
71
+ ```
72
+
73
+ ## Usage
74
+
75
+ ```js
76
+ import { initLlama } from 'llama.rn'
77
+
78
+ // Initial a Llama context with the model (may take a while)
79
+ const context = await initLlama({
80
+ model: 'file://<path to gguf model>',
81
+ use_mlock: true,
82
+ n_ctx: 2048,
83
+ n_gpu_layers: 1, // > 0: enable Metal on iOS
84
+ // embedding: true, // use embedding
85
+ })
86
+
87
+ // Do completion
88
+ const { text, timings } = await context.completion(
89
+ {
90
+ prompt:
91
+ 'This is a conversation between user and llama, a friendly chatbot. respond in simple markdown.\n\nUser: Hello!\nLlama:',
92
+ n_predict: 100,
93
+ stop: ['</s>', 'Llama:', 'User:'],
94
+ // n_threads: 4,
95
+ },
96
+ (data) => {
97
+ // This is a partial completion callback
98
+ const { token } = data
99
+ },
100
+ )
101
+ console.log('Result:', text)
102
+ console.log('Timings:', timings)
103
+ ```
104
+
105
+ The binding’s deisgn inspired by [server.cpp](https://github.com/ggerganov/llama.cpp/tree/master/examples/server) example in llama.cpp, so you can map its API to LlamaContext:
106
+
107
+ - `/completion`: `context.completion(params, partialCompletionCallback)`
108
+ - `/tokenize`: `context.tokenize(content)`
109
+ - `/detokenize`: `context.detokenize(tokens)`
110
+ - `/embedding`: `context.embedding(content)`
111
+ - Other methods
112
+ - `context.loadSession(path)`
113
+ - `context.saveSession(path)`
114
+ - `context.stopCompletion()`
115
+ - `context.release()`
116
+
117
+ Please visit the [Documentation](docs/API) for more details.
118
+
119
+ You can also visit the [example](example) to see how to use it.
120
+
121
+ Run the example:
122
+
123
+ ```bash
124
+ yarn && yarn bootstrap
125
+
126
+ # iOS
127
+ yarn example ios
128
+ # Use device
129
+ yarn example ios --device "<device name>"
130
+ # With release mode
131
+ yarn example ios --mode Release
132
+
133
+ # Android
134
+ yarn example android
135
+ # With release mode
136
+ yarn example android --mode release
137
+ ```
138
+
139
+ This example used [react-native-document-picker](https://github.com/rnmods/react-native-document-picker) for select model.
140
+
141
+ - iOS: You can move the model to iOS Simulator, or iCloud for real device.
142
+ - Android: Selected file will be copied or downloaded to cache directory so it may be slow.
143
+
144
+ ## Grammar Sampling
145
+
146
+ GBNF (GGML BNF) is a format for defining [formal grammars](https://en.wikipedia.org/wiki/Formal_grammar) to constrain model outputs in `llama.cpp`. For example, you can use it to force the model to generate valid JSON, or speak only in emojis.
147
+
148
+ You can see [GBNF Guide](https://github.com/ggerganov/llama.cpp/tree/master/grammars) for more details.
149
+
150
+ `llama.rn` provided a built-in function to convert JSON Schema to GBNF:
151
+
152
+ ```js
153
+ import { initLlama, convertJsonSchemaToGrammar } from 'llama.rn'
154
+
155
+ const schema = {
156
+ /* JSON Schema, see below */
157
+ }
158
+
159
+ const context = await initLlama({
160
+ model: 'file://<path to gguf model>',
161
+ use_mlock: true,
162
+ n_ctx: 2048,
163
+ n_gpu_layers: 1, // > 0: enable Metal on iOS
164
+ // embedding: true, // use embedding
165
+ grammar: convertJsonSchemaToGrammar({
166
+ schema,
167
+ propOrder: { function: 0, arguments: 1 },
168
+ }),
169
+ })
170
+
171
+ const { text } = await context.completion({
172
+ prompt: 'Schedule a birthday party on Aug 14th 2023 at 8pm.',
173
+ })
174
+ console.log('Result:', text)
175
+ // Example output:
176
+ // {"function": "create_event","arguments":{"date": "Aug 14th 2023", "time": "8pm", "title": "Birthday Party"}}
177
+ ```
178
+
179
+ <details>
180
+ <summary>JSON Schema example (Define function get_current_weather / create_event / image_search)</summary>
181
+
182
+ ```json5
183
+ {
184
+ oneOf: [
185
+ {
186
+ type: 'object',
187
+ name: 'get_current_weather',
188
+ description: 'Get the current weather in a given location',
189
+ properties: {
190
+ function: {
191
+ const: 'get_current_weather',
192
+ },
193
+ arguments: {
194
+ type: 'object',
195
+ properties: {
196
+ location: {
197
+ type: 'string',
198
+ description: 'The city and state, e.g. San Francisco, CA',
199
+ },
200
+ unit: {
201
+ type: 'string',
202
+ enum: ['celsius', 'fahrenheit'],
203
+ },
204
+ },
205
+ required: ['location'],
206
+ },
207
+ },
208
+ },
209
+ {
210
+ type: 'object',
211
+ name: 'create_event',
212
+ description: 'Create a calendar event',
213
+ properties: {
214
+ function: {
215
+ const: 'create_event',
216
+ },
217
+ arguments: {
218
+ type: 'object',
219
+ properties: {
220
+ title: {
221
+ type: 'string',
222
+ description: 'The title of the event',
223
+ },
224
+ date: {
225
+ type: 'string',
226
+ description: 'The date of the event',
227
+ },
228
+ time: {
229
+ type: 'string',
230
+ description: 'The time of the event',
231
+ },
232
+ },
233
+ required: ['title', 'date', 'time'],
234
+ },
235
+ },
236
+ },
237
+ {
238
+ type: 'object',
239
+ name: 'image_search',
240
+ description: 'Search for an image',
241
+ properties: {
242
+ function: {
243
+ const: 'image_search',
244
+ },
245
+ arguments: {
246
+ type: 'object',
247
+ properties: {
248
+ query: {
249
+ type: 'string',
250
+ description: 'The search query',
251
+ },
252
+ },
253
+ required: ['query'],
254
+ },
255
+ },
256
+ },
257
+ ],
258
+ }
259
+ ```
260
+
261
+ </details>
262
+
263
+ <details>
264
+ <summary>Converted GBNF looks like</summary>
265
+
266
+ ```bnf
267
+ space ::= " "?
268
+ 0-function ::= "\"get_current_weather\""
269
+ string ::= "\"" (
270
+ [^"\\] |
271
+ "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
272
+ )* "\"" space
273
+ 0-arguments-unit ::= "\"celsius\"" | "\"fahrenheit\""
274
+ 0-arguments ::= "{" space "\"location\"" space ":" space string "," space "\"unit\"" space ":" space 0-arguments-unit "}" space
275
+ 0 ::= "{" space "\"function\"" space ":" space 0-function "," space "\"arguments\"" space ":" space 0-arguments "}" space
276
+ 1-function ::= "\"create_event\""
277
+ 1-arguments ::= "{" space "\"date\"" space ":" space string "," space "\"time\"" space ":" space string "," space "\"title\"" space ":" space string "}" space
278
+ 1 ::= "{" space "\"function\"" space ":" space 1-function "," space "\"arguments\"" space ":" space 1-arguments "}" space
279
+ 2-function ::= "\"image_search\""
280
+ 2-arguments ::= "{" space "\"query\"" space ":" space string "}" space
281
+ 2 ::= "{" space "\"function\"" space ":" space 2-function "," space "\"arguments\"" space ":" space 2-arguments "}" space
282
+ root ::= 0 | 1 | 2
283
+ ```
284
+
285
+ </details>
286
+
287
+ ## Mock `llama.rn`
288
+
289
+ We have provided a mock version of `llama.rn` for testing purpose you can use on Jest:
290
+
291
+ ```js
292
+ jest.mock('llama.rn', () => require('llama.rn/jest/mock'))
293
+ ```
294
+
295
+ ## NOTE
296
+
297
+ iOS:
298
+
299
+ - The [Extended Virtual Addressing](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_kernel_extended-virtual-addressing) capability is recommended to enable on iOS project.
300
+ - Metal:
301
+ - We have tested to know some devices is not able to use Metal ('params.n_gpu_layers > 0') due to llama.cpp used SIMD-scoped operation, you can check if your device is supported in [Metal feature set tables](https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf), Apple7 GPU will be the minimum requirement.
302
+ - It's also not supported in iOS simulator due to [this limitation](https://developer.apple.com/documentation/metal/developing_metal_apps_that_run_in_simulator#3241609), we used constant buffers more than 14.
303
+
304
+ Android:
305
+
306
+ - Currently only supported arm64-v8a / x86_64 platform, this means you can't initialize a context on another platforms. The 64-bit platform are recommended because it can allocate more memory for the model.
307
+ - No integrated any GPU backend yet.
308
+
309
+ ## Contributing
310
+
311
+ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
312
+
313
+ ## License
314
+
315
+ MIT
316
+
317
+ ---
318
+
319
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
320
+
321
+ ---
322
+
323
+ <p align="center">
324
+ <a href="https://bricks.tools">
325
+ <img width="90px" src="https://avatars.githubusercontent.com/u/17320237?s=200&v=4">
326
+ </a>
327
+ <p align="center">
328
+ Built and maintained by <a href="https://bricks.tools">BRICKS</a>.
329
+ </p>
330
+ </p>
@@ -0,0 +1,107 @@
1
+ buildscript {
2
+ repositories {
3
+ google()
4
+ mavenCentral()
5
+ }
6
+
7
+ dependencies {
8
+ classpath "com.android.tools.build:gradle:7.2.1"
9
+ }
10
+ }
11
+
12
+ def isNewArchitectureEnabled() {
13
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
14
+ }
15
+
16
+ apply plugin: "com.android.library"
17
+
18
+
19
+ def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
20
+
21
+ if (isNewArchitectureEnabled()) {
22
+ apply plugin: "com.facebook.react"
23
+ }
24
+
25
+ def getExtOrDefault(name) {
26
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["RNLlama_" + name]
27
+ }
28
+
29
+ def getExtOrIntegerDefault(name) {
30
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["RNLlama_" + name]).toInteger()
31
+ }
32
+
33
+ def reactNativeArchitectures() {
34
+ def value = project.getProperties().get("reactNativeArchitectures")
35
+ def archs = value ? value.split(",") : ["x86_64", "arm64-v8a"]
36
+ return archs.findAll { it != "armeabi-v7a" && it != "x86" } // Not building for 32-bit architectures
37
+ }
38
+
39
+ android {
40
+ ndkVersion getExtOrDefault("ndkVersion")
41
+ def ndkVersionMajor = ndkVersion.split("\\.")[0].toInteger()
42
+ if (ndkVersionMajor < 24) {
43
+ ndkVersion = project.properties["RNLlama_ndkversion"]
44
+ }
45
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
46
+
47
+ defaultConfig {
48
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
49
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
50
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
51
+ externalNativeBuild {
52
+ cmake {
53
+ abiFilters (*reactNativeArchitectures())
54
+ }
55
+ }
56
+ }
57
+ externalNativeBuild {
58
+ cmake {
59
+ path = file('src/main/CMakeLists.txt')
60
+ }
61
+ }
62
+ buildTypes {
63
+ release {
64
+ minifyEnabled false
65
+ }
66
+ }
67
+
68
+ lintOptions {
69
+ disable "GradleCompatible"
70
+ }
71
+
72
+ compileOptions {
73
+ sourceCompatibility JavaVersion.VERSION_1_8
74
+ targetCompatibility JavaVersion.VERSION_1_8
75
+ }
76
+
77
+ sourceSets {
78
+ main {
79
+ if (isNewArchitectureEnabled()) {
80
+ java.srcDirs += ['src/newarch']
81
+ } else {
82
+ java.srcDirs += ['src/oldarch']
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ repositories {
89
+ mavenCentral()
90
+ google()
91
+ }
92
+
93
+
94
+ dependencies {
95
+ // For < 0.71, this will be from the local maven repo
96
+ // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
97
+ //noinspection GradleDynamicVersion
98
+ implementation "com.facebook.react:react-native:+"
99
+ }
100
+
101
+ if (isNewArchitectureEnabled()) {
102
+ react {
103
+ jsRootDir = file("../src/")
104
+ libraryName = "RNLlama"
105
+ codegenJavaPackageName = "com.rnllama"
106
+ }
107
+ }
@@ -0,0 +1,5 @@
1
+ RNLlama_kotlinVersion=1.7.0
2
+ RNLlama_minSdkVersion=23
3
+ RNLlama_targetSdkVersion=31
4
+ RNLlama_compileSdkVersion=31
5
+ RNLlama_ndkversion=25.1.8937393
@@ -0,0 +1,4 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.rnllama">
3
+
4
+ </manifest>
@@ -0,0 +1,69 @@
1
+ cmake_minimum_required(VERSION 3.10)
2
+
3
+ project(llama.rn)
4
+
5
+ set(CMAKE_CXX_STANDARD 11)
6
+ set(RNLLAMA_LIB_DIR ${CMAKE_SOURCE_DIR}/../../../cpp)
7
+
8
+ include_directories(${RNLLAMA_LIB_DIR})
9
+
10
+ set(
11
+ SOURCE_FILES
12
+ ${RNLLAMA_LIB_DIR}/ggml-aarch64.c
13
+ ${RNLLAMA_LIB_DIR}/ggml-alloc.c
14
+ ${RNLLAMA_LIB_DIR}/ggml-backend.c
15
+ ${RNLLAMA_LIB_DIR}/ggml.c
16
+ ${RNLLAMA_LIB_DIR}/ggml-quants.c
17
+ ${RNLLAMA_LIB_DIR}/common.cpp
18
+ ${RNLLAMA_LIB_DIR}/grammar-parser.cpp
19
+ ${RNLLAMA_LIB_DIR}/json.hpp
20
+ ${RNLLAMA_LIB_DIR}/json-schema-to-grammar.cpp
21
+ ${RNLLAMA_LIB_DIR}/sampling.cpp
22
+ ${RNLLAMA_LIB_DIR}/unicode-data.cpp
23
+ ${RNLLAMA_LIB_DIR}/unicode.cpp
24
+ ${RNLLAMA_LIB_DIR}/llama.cpp
25
+ ${RNLLAMA_LIB_DIR}/sgemm.cpp
26
+ ${RNLLAMA_LIB_DIR}/rn-llama.hpp
27
+ ${CMAKE_SOURCE_DIR}/jni.cpp
28
+ )
29
+
30
+ find_library(LOG_LIB log)
31
+
32
+ function(build_library target_name)
33
+ add_library(
34
+ ${target_name}
35
+ SHARED
36
+ ${SOURCE_FILES}
37
+ )
38
+
39
+ target_link_libraries(${target_name} ${LOG_LIB} android)
40
+
41
+ target_compile_options(${target_name} PRIVATE -pthread)
42
+
43
+ if (${target_name} STREQUAL "rnllama_v8fp16_va")
44
+ target_compile_options(${target_name} PRIVATE -march=armv8.4-a+fp16+dotprod)
45
+ endif ()
46
+
47
+ if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
48
+ target_compile_options(${target_name} PRIVATE -DRNLLAMA_ANDROID_ENABLE_LOGGING)
49
+ endif ()
50
+
51
+ # NOTE: If you want to debug the native code, you can uncomment if and endif
52
+ # if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
53
+
54
+ target_compile_options(${target_name} PRIVATE -O3 -DNDEBUG)
55
+ target_compile_options(${target_name} PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
56
+ target_compile_options(${target_name} PRIVATE -ffunction-sections -fdata-sections)
57
+
58
+ target_link_options(${target_name} PRIVATE -Wl,--gc-sections)
59
+ target_link_options(${target_name} PRIVATE -Wl,--exclude-libs,ALL)
60
+ target_link_options(${target_name} PRIVATE -flto)
61
+
62
+ # endif ()
63
+ endfunction()
64
+
65
+ build_library("rnllama") # Default target
66
+
67
+ if (${ANDROID_ABI} STREQUAL "arm64-v8a")
68
+ build_library("rnllama_v8fp16_va")
69
+ endif ()