Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make prebuilt libs #112

Merged
merged 19 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Run unit tests
run: yarn test --maxWorkers=2 --coverage

build-ios:
build-ios-from-source:
runs-on: macos-latest
steps:
- name: Checkout
Expand Down Expand Up @@ -69,7 +69,55 @@ jobs:
run: |
yarn build:ios

build-android:
build-ios-frameworks:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Cache build & pods
uses: actions/cache@v3
with:
path: |
example/ios/Pods
example/ios/build
key: ${{ runner.os }}-pods-${{ hashFiles('example/ios/Podfile.lock') }}
restore-keys: |
${{ runner.os }}-pods-

- name: Upgrade CocoaPods to version 1.15.2
run: |
gem uninstall cocoapods --ignore-dependencies
gem install cocoapods -v 1.15.2

- name: Install cocoapods
run: |
yarn example pods
env:
NO_FLIPPER: 1
RNLLAMA_BUILD_FROM_SOURCE: 0

- name: Build example for iOS
run: |
yarn build:ios

build-android-from-source:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Build example for Android
run: |
yarn build:android

build-android-libs:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -80,4 +128,5 @@ jobs:

- name: Build example for Android
run: |
sed -i 's/rnllamaBuildFromSource=true/rnllamaBuildFromSource=false/g' example/android/gradle.properties
yarn build:android
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ docs/API/.nojekyll

*.metallib
.xocde.env.local

build-arm64/
build-x86_64/
jniLibs/
build-ios/
build-tvos/

ios/rnllama.xcframework/**/*.framework
16 changes: 0 additions & 16 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ yarn bootstrap

While developing, you can run the [example app](/example/) to test your changes. Any changes you make in your library's JavaScript code will be reflected in the example app without a rebuild. If you change any native code, then you'll need to rebuild the example app.

To start the packager:

```sh
yarn example start
```

To run the example app on iOS:

```sh
yarn example pods
yarn example ios
```

For test better performance on completion, you can run the app in Release mode:
- iOS: `yarn example ios --mode Release`

Make sure your code passes TypeScript and ESLint. Run the following to verify:

```sh
Expand Down
27 changes: 4 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ npm install llama.rn

Please re-run `npx pod-install` again.

By default, `llama.rn` will use pre-built `rnllama.xcframework` for iOS. If you want to build from source, please set `RNLLAMA_BUILD_FROM_SOURCE` to `1` in your Podfile.

#### Android

Add proguard rule if it's enabled in project (android/app/proguard-rules.pro):
Expand All @@ -27,6 +29,8 @@ Add proguard rule if it's enabled in project (android/app/proguard-rules.pro):
-keep class com.rnllama.** { *; }
```

By default, `llama.rn` will use pre-built libraries for Android. If you want to build from source, please set `rnllamaBuildFromSource` to `true` in `android/gradle.properties`.

## Obtain the model

You can search HuggingFace for available models (Keyword: [`GGUF`](https://huggingface.co/search/full-text?q=GGUF&type=model)).
Expand Down Expand Up @@ -118,29 +122,6 @@ Please visit the [Documentation](docs/API) for more details.

You can also visit the [example](example) to see how to use it.

Run the example:

```bash
yarn && yarn bootstrap

# iOS
yarn example ios
# Use device
yarn example ios --device "<device name>"
# With release mode
yarn example ios --mode Release

# Android
yarn example android
# With release mode
yarn example android --mode release
```

This example used [react-native-document-picker](https://github.com/rnmods/react-native-document-picker) for select model.

- iOS: You can move the model to iOS Simulator, or iCloud for real device.
- Android: Selected file will be copied or downloaded to cache directory so it may be slow.

## Grammar Sampling

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.
Expand Down
15 changes: 12 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ android {
}
}
}
externalNativeBuild {
cmake {
path = file('src/main/CMakeLists.txt')
def rnllamaBuildFromSource = project.properties["rnllamaBuildFromSource"]
if (rnllamaBuildFromSource == "true") {
externalNativeBuild {
cmake {
path = file('src/main/CMakeLists.txt')
}
}
// Exclude jniLibs
sourceSets {
main {
jniLibs.srcDirs = []
}
}
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ set(
${RNLLAMA_LIB_DIR}/unicode.cpp
${RNLLAMA_LIB_DIR}/sgemm.cpp
${RNLLAMA_LIB_DIR}/common.cpp
${RNLLAMA_LIB_DIR}/rn-llama.hpp
${RNLLAMA_LIB_DIR}/amx/amx.cpp
${RNLLAMA_LIB_DIR}/amx/mmq.cpp
${RNLLAMA_LIB_DIR}/rn-llama.cpp
${CMAKE_SOURCE_DIR}/jni-utils.h
${CMAKE_SOURCE_DIR}/jni.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "llama.h"
#include "llama-impl.h"
#include "ggml.h"
#include "rn-llama.hpp"
#include "rn-llama.h"
#include "jni-utils.h"

#define UNUSED(x) (void)(x)
Expand Down
2 changes: 1 addition & 1 deletion cpp/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Note

- Only `rn-llama.hpp` is the specific file for this project, others are sync from [llama.cpp](https://github.com/ggerganov/llama.cpp).
- Only `rn-llama.h` and `rn-llama.cpp` are the specific files for this folder, others are sync from [llama.cpp](https://github.com/ggerganov/llama.cpp).
- We can update the native source by using the [bootstrap](../scripts/bootstrap.sh) script.
Loading
Loading