Skip to content

Commit 01253b5

Browse files
committed
Merge branch 'jni' (kherud#3 kherud#4)
2 parents 17341a3 + ef76e8e commit 01253b5

43 files changed

Lines changed: 3140 additions & 3515 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/release.yaml‎

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,93 @@
1-
name: Build shared libraries and publish to Maven Central
1+
name: Build JNI bindings
22
on:
33
workflow_dispatch:
44
release:
55
types: [created]
66
jobs:
7-
build-linux-x86-64:
8-
name: Build Linux x86-64
9-
runs-on: ubuntu-latest
7+
build:
8+
name: Build ${{ matrix.target.name }}-${{ matrix.target.arch }}
9+
runs-on: ${{ matrix.target.image }}
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
target:
14+
- {
15+
name: Linux,
16+
arch: x86_64,
17+
image: ubuntu-latest,
18+
cmake: "-DCMAKE_CXX_FLAGS='-march=x86-64'"
19+
}
20+
# - {
21+
# name: Linux,
22+
# arch: aarch64,
23+
# image: ubuntu-latest,
24+
# cmake: "-DCMAKE_CXX_FLAGS='-march=armv8-a'"
25+
# }
26+
- {
27+
name: Mac,
28+
arch: x86_64,
29+
image: macos-latest,
30+
cmake: "-DCMAKE_OSX_ARCHITECTURES=x86_64"
31+
}
32+
- {
33+
name: Mac,
34+
arch: aarch64,
35+
image: macos-latest,
36+
cmake: "-DCMAKE_OSX_ARCHITECTURES=arm64"
37+
}
38+
- {
39+
name: Windows,
40+
arch: x86_64,
41+
image: windows-latest,
42+
cmake: "-DCMAKE_GENERATOR_PLATFORM=x64"
43+
}
44+
# - {
45+
# name: Windows,
46+
# arch: aarch64,
47+
# image: windows-latest,
48+
# cmake: "-DCMAKE_GENERATOR_PLATFORM=ARM64"
49+
# }
1050
steps:
11-
- name: Checkout llama.cpp
51+
- name: Checkout Repository
1252
uses: actions/checkout@v4
1353
with:
14-
repository: ggerganov/llama.cpp
15-
- name: CMake
54+
submodules: recursive
55+
ref: "jni"
56+
- name: CMake (Windows)
57+
if: ${{ matrix.target.name == 'Windows' }}
58+
shell: cmd
59+
run: |
60+
cd scripts && .\build.bat ${{ matrix.target.cmake }} -DOS_NAME=${{ matrix.target.name }} -DOS_ARCH=${{ matrix.target.arch }}
61+
- name: CMake (Unix)
62+
if: ${{ matrix.target.name != 'Windows' }}
1663
shell: bash
1764
run: |
18-
mkdir -p build && cd build
19-
cmake .. -DBUILD_SHARED_LIBS=ON -DLLAMA_BUILD_TESTS=OFF || exit 1
20-
cmake --build . --config Release || exit 1
21-
- name: Upload linux-x86-64
65+
cd scripts && ./build.sh ${{ matrix.target.cmake }} -DOS_NAME=${{ matrix.target.name }} -DOS_ARCH=${{ matrix.target.arch }}
66+
- name: Upload Unix Artifact
67+
if: ${{ matrix.target.name == 'Windows' }}
68+
uses: actions/upload-artifact@v3
69+
with:
70+
name: artifacts
71+
path: ${{ github.workspace }}\src\main\resources\de\kherud\llama\
72+
- name: Upload Windows Artifact
73+
if: ${{ matrix.target.name != 'Windows' }}
2274
uses: actions/upload-artifact@v3
2375
with:
24-
name: linux-x86-64
25-
path: build/libllama.so
26-
# build-linux-arm64:
27-
# name: Build Linux arm64
28-
# runs-on: ubuntu-latest
29-
# container:
30-
# image: arm64v8/ubuntu:latest
31-
# options: --platform linux/arm64
32-
# steps:
33-
# - name: Install dependencies
34-
# run: |
35-
# apt-get update
36-
# apt-get install -y build-essential cmake
37-
# - name: Checkout llama.cpp
38-
# uses: actions/checkout@v4
39-
# with:
40-
# repository: ggerganov/llama.cpp
41-
# - name: CMake
42-
# shell: bash
43-
# run: |
44-
# mkdir -p build && cd build
45-
# cmake .. -DBUILD_SHARED_LIBS=ON -DLLAMA_BUILD_TESTS=OFF || exit 1
46-
# cmake --build . --config Release || exit 1
47-
# - name: Upload linux-arm64
48-
# uses: actions/upload-artifact@v3
49-
# with:
50-
# name: linux-arm64
51-
# path: build/libllama.so
52-
53-
76+
name: artifacts
77+
path: ${{ github.workspace }}/src/main/resources/de/kherud/llama/
5478
publish:
55-
needs: [build-linux-x86-64]
79+
needs: [build]
5680
runs-on: ubuntu-latest
5781
steps:
58-
- name: Checkout java-llama.cpp
82+
- name: Checkout Repository
5983
uses: actions/checkout@v4
60-
- name: Download libraries
84+
with:
85+
ref: "jni"
86+
- name: Download Artifacts
6187
uses: actions/download-artifact@v3
62-
with: # specifying no name downloads all artifacts to a directory of their name
63-
path: src/main/resources
88+
with:
89+
name: artifacts
90+
path: ${{ github.workspace }}/src/main/resources/de/kherud/llama/
6491
- name: Set up Maven Central Repository
6592
uses: actions/setup-java@v3
6693
with:

‎.gitignore‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
.idea
22
target
3+
build
34
.DS_Store
5+
.directory
6+
.vscode
47

58
# Compiled class file
69
*.class
@@ -27,10 +30,9 @@ target
2730
hs_err_pid*
2831
replay_pid*
2932

30-
llama.h
31-
llama.cpp
32-
main.cpp
33-
34-
*.so
35-
*.dylib
36-
*.metal
33+
src/main/cpp/de_kherud_llama_*.h
34+
src/main/resources/**/*.so
35+
src/main/resources/**/*.dylib
36+
src/main/resources/**/*.dll
37+
src/main/resources/**/*.metal
38+
src/test/resources/**/*.gbnf

‎.gitmodules‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/main/cpp/llama.cpp"]
2+
path = src/main/cpp/llama.cpp
3+
url = https://github.com/ggerganov/llama.cpp.git

‎CMakeLists.txt‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
project(jllama CXX)
4+
5+
set(CMAKE_CXX_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD_REQUIRED true)
7+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
8+
set(BUILD_SHARED_LIBS ON)
9+
10+
find_package(Java REQUIRED)
11+
find_program(JAVA_EXECUTABLE NAMES java)
12+
13+
# find "jni.h" include directory
14+
find_path(JNI_INCLUDE_DIR NAMES jni.h HINTS ENV JAVA_HOME PATH_SUFFIXES include)
15+
if(NOT JNI_INCLUDE_DIR)
16+
message(FATAL_ERROR "Could not find jni.h")
17+
endif()
18+
19+
# find "jni_md.h" include directory if not set
20+
file(GLOB_RECURSE JNI_MD_PATHS RELATIVE "${JNI_INCLUDE_DIR}" "${JNI_INCLUDE_DIR}/**/jni_md.h")
21+
if(NOT JNI_MD_PATHS)
22+
message(FATAL_ERROR "Could not find jni_md.h")
23+
endif()
24+
foreach(PATH IN LISTS JNI_MD_PATHS)
25+
get_filename_component(DIR ${PATH} DIRECTORY)
26+
list(APPEND JNI_MD_INCLUDE_DIRS "${JNI_INCLUDE_DIR}/${DIR}")
27+
endforeach()
28+
29+
# find which OS we build for if not set (make sure to run mvn compile first)
30+
if(NOT DEFINED OS_NAME)
31+
execute_process(
32+
COMMAND ${JAVA_EXECUTABLE} -cp ${CMAKE_SOURCE_DIR}/target/classes de.kherud.llama.OSInfo --os
33+
OUTPUT_VARIABLE OS_NAME
34+
OUTPUT_STRIP_TRAILING_WHITESPACE
35+
)
36+
endif()
37+
38+
# find which architecture we build for if not set (make sure to run mvn compile first)
39+
if(NOT DEFINED OS_ARCH)
40+
execute_process(
41+
COMMAND ${JAVA_EXECUTABLE} -cp ${CMAKE_SOURCE_DIR}/target/classes de.kherud.llama.OSInfo --arch
42+
OUTPUT_VARIABLE OS_ARCH
43+
OUTPUT_STRIP_TRAILING_WHITESPACE
44+
)
45+
endif()
46+
47+
include_directories(
48+
${JNI_INCLUDE_DIR}
49+
${JNI_MD_INCLUDE_DIRS}
50+
src/main/cpp
51+
src/main/cpp/llama.cpp
52+
src/main/cpp/llama.cpp/common
53+
)
54+
55+
add_subdirectory(
56+
src/main/cpp/llama.cpp
57+
)
58+
59+
add_library(jllama SHARED src/main/cpp/jllama.cpp)
60+
61+
target_link_libraries(jllama PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
62+
63+
if(OS_NAME STREQUAL "Windows")
64+
set_target_properties(jllama llama PROPERTIES
65+
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_SOURCE_DIR}/src/main/resources/de/kherud/llama/${OS_NAME}/${OS_ARCH}"
66+
)
67+
else()
68+
set_target_properties(jllama llama PROPERTIES
69+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/src/main/resources/de/kherud/llama/${OS_NAME}/${OS_ARCH}"
70+
)
71+
endif()
72+
73+
message(STATUS "Installing files to ${CMAKE_SOURCE_DIR}/src/main/resources/de/kherud/llama/${OS_NAME}/${OS_ARCH}")

0 commit comments

Comments
 (0)