Skip to content

Initial project setup for llamacpp bindings #267

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

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -29,5 +29,6 @@ jobs:
cache: "pnpm"

- run: pnpm install --frozen-lockfile
- run: pnpm build:setup
- run: pnpm build
- run: pnpm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ examples/*/build/**
packages/*/build/**
packages/*/coverage/**
packages/*/dist/**
packages/*/lib/**
tools/*/build/**
tools/*/coverage/**
tools/*/dist/**
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -37,5 +37,6 @@
"upsert",
"whispercpp"
],
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"cmake.sourceDirectory": "packages/@modelfusion-llamacpp-bindings"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
"private": true,
"scripts": {
"build": "turbo build",
"build:setup": "turbo setup",
"clean": "rimraf .turbo node_modules/.cache/turbo && pnpm --filter \"./packages/**\" clean && pnpm --filter \"./examples/**\" clean",
"lint": "turbo lint",
"setup": "husky install",
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/node_modules/node-addon-api",
"/usr/local/include/node"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
46 changes: 46 additions & 0 deletions packages/@modelfusion-llamacpp-bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.13)

project("llamacpp-bindings-native" C CXX)

if (MSVC)
# add_compile_options(/EHsc)
else()
add_compile_options(-fexceptions)
endif()

add_definitions(-DNAPI_VERSION=7)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

execute_process(COMMAND node -p "require('node-addon-api').include.slice(1,-1)"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)

include_directories(${NODE_ADDON_API_DIR} ${CMAKE_JS_INC})

if(EXISTS ${CMAKE_SOURCE_DIR}/lib/llamacpp)
add_subdirectory(${CMAKE_SOURCE_DIR}/lib/llamacpp)
include_directories(${CMAKE_SOURCE_DIR}/lib/llamacpp)
include_directories(${CMAKE_SOURCE_DIR}/lib/llamacpp/common)
else()
message(FATAL_ERROR "Directory 'lib/llamacpp' does not exist. Run ./update_submodules.sh")
endif()

file(GLOB SOURCE_FILES "src/llamacpp_bindings.cc")

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
target_link_libraries(${PROJECT_NAME} "llama")
target_link_libraries(${PROJECT_NAME} "common")

# copy ggml-metal.metal to release folder
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/lib/llamacpp/ggml-metal.metal
${CMAKE_CURRENT_BINARY_DIR}/Release/ggml-metal.metal)

if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
# Generate node.lib
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()
19 changes: 19 additions & 0 deletions packages/@modelfusion-llamacpp-bindings/bin/update_submodules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Define the repository URL
REPO_URL="https://github.com/ggerganov/llama.cpp.git"

# Define the directory where the repository will be cloned
DIR="lib/llamacpp"

# If the directory already exists, pull the latest changes
if [ -d "$DIR" ]; then
echo "Directory $DIR exists."
echo "Pulling latest changes..."
cd "$DIR" && git pull origin master
else
# If the directory does not exist, clone the repository
echo "Directory $DIR does not exist."
echo "Cloning repository..."
git clone "$REPO_URL" "$DIR"
fi
16 changes: 16 additions & 0 deletions packages/@modelfusion-llamacpp-bindings/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"targets": [
{
"target_name": "llamacpp-bindings-native",
"type": "none",
"actions": [
{
"action_name": "build",
"inputs": ["<(module_root_dir)/CMakeLists.txt"],
"outputs": ["<(module_root_dir)/build"],
"action": ["cmake-js"]
}
]
}
]
}
45 changes: 45 additions & 0 deletions packages/@modelfusion-llamacpp-bindings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"dependencies": {
"node-addon-api": "^7.0.0"
},
"scripts": {
"setup": "./bin/update_submodules.sh",
"build": "node-gyp rebuild && tsc",
"clean": "node-gyp clean && tsc --build --clean",
"pretest": "tsc",
"test": "vitest --run src"
},
"gypfile": true,
"name": "@modelfusion/llamacpp-bindings",
"version": "0.0.1",
"description": "llama.cpp bindings for ModelFusion",
"repository": {
"type": "git",
"url": "git+https://github.com/lgrammel/modelfusion.git"
},
"type": "module",
"engines": {
"node": ">=18"
},
"main": "./dist/index.js",
"keywords": [
"modelfusion",
"llamacpp",
"llama"
],
"author": "Nick Nance",
"license": "MIT",
"bugs": {
"url": "https://github.com/lgrammel/modelfusion/issues"
},
"homepage": "https://github.com/lgrammel/modelfusion#readme",
"devDependencies": {
"@types/node": "^20.11.3",
"cmake-js": "^7.3.0",
"typescript": "^5.3.3",
"modelfusion": "0.131.0"
},
"peerDependencies": {
"modelfusion": ">=0.131.0"
}
}
Loading