Skip to content

Commit 1043596

Browse files
authored
feat: add support for metal and cuda in the build command (#17)
1 parent c28d2de commit 1043596

File tree

12 files changed

+179
-105
lines changed

12 files changed

+179
-105
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
with:
1515
node-version: "20"
1616
- name: Install modules
17-
run: npm ci --ignore-scripts
17+
run: npm ci
1818
- name: Build
1919
run: npm run build
2020
- name: Generate docs
@@ -117,7 +117,7 @@ jobs:
117117
env:
118118
ARTIFACT_NAME: ${{ matrix.config.artifact }}
119119
run: |
120-
npm ci --ignore-scripts
120+
npm ci
121121
122122
npx zx -y <<'EOF'
123123
@@ -221,7 +221,7 @@ jobs:
221221
with:
222222
node-version: "20"
223223
- name: Install modules
224-
run: npm ci --ignore-scripts
224+
run: npm ci
225225
- uses: actions/download-artifact@v3
226226
with:
227227
path: artifacts
@@ -239,6 +239,8 @@ jobs:
239239
240240
echo "Built binaries:"
241241
ls llamaBins
242+
- name: Add "postinstall" script to package.json
243+
run: npm run addPostinstallScript
242244
- name: Release
243245
env:
244246
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
with:
1010
node-version: "18"
1111
- name: Install modules
12-
run: npm ci --ignore-scripts
12+
run: npm ci
1313
- name: ESLint
1414
run: npx eslint --ext .js --ext .ts .
1515
- name: TypeScript validity

llama/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
],
1919
"cflags": ["-fexceptions"],
2020
"cflags_cc": ["-fexceptions"],
21-
"defines": [ "GGML_USE_K_QUANTS", "NAPI_CPP_EXCEPTIONS" ],
21+
"defines": ["GGML_USE_K_QUANTS", "NAPI_CPP_EXCEPTIONS"],
2222
"msvs_settings": {
2323
"VCCLCompilerTool": { "AdditionalOptions": [ '/arch:AVX2', '/EHsc' ] }
2424
}

package-lock.json

Lines changed: 29 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
"node": ">=18.0.0"
4141
},
4242
"scripts": {
43-
"postinstall": "node ./dist/cli/cli.js postinstall",
4443
"prepare": "[ $CI = true ] || [ -d '.husky/_' ] || husky install",
4544
"prebuild": "rm -rf ./dist ./tsconfig.tsbuildinfo",
4645
"build": "tsc --build tsconfig.json --force",
46+
"addPostinstallScript": "npm pkg set scripts.postinstall=\"node ./dist/cli/cli.js postinstall\"",
4747
"generate-docs": "typedoc",
4848
"prewatch": "rm -rf ./dist ./tsconfig.tsbuildinfo",
4949
"watch": "tsc --build tsconfig.json --watch --force",
@@ -123,6 +123,7 @@
123123
"node-stream-zip": "^1.15.0",
124124
"octokit": "^3.1.0",
125125
"ora": "^7.0.1",
126+
"simple-git": "^3.19.1",
126127
"uuid": "^9.0.0",
127128
"yargs": "^17.7.2"
128129
}

src/cli/commands/BuildCommand.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import process from "process";
12
import {CommandModule} from "yargs";
23
import chalk from "chalk";
34
import {compileLlamaCpp} from "../../utils/compileLLamaCpp.js";
45
import withOra from "../../utils/withOra.js";
56
import {clearTempFolder} from "../../utils/clearTempFolder.js";
7+
import {defaultLlamaCppCudaSupport, defaultLlamaCppMetalSupport} from "../../config.js";
68

79
type BuildCommand = {
810
arch?: string,
9-
nodeTarget?: string
11+
nodeTarget?: string,
12+
metal: boolean,
13+
cuda: boolean
1014
};
1115

1216
export const BuildCommand: CommandModule<object, BuildCommand> = {
@@ -23,12 +27,30 @@ export const BuildCommand: CommandModule<object, BuildCommand> = {
2327
alias: "t",
2428
type: "string",
2529
description: "The Node.js version to compile llama.cpp for. Example: v18.0.0"
30+
})
31+
.option("metal", {
32+
type: "boolean",
33+
default: defaultLlamaCppMetalSupport,
34+
description: "Compile llama.cpp with Metal support. Can also be set via the NODE_LLAMA_CPP_METAL environment variable"
35+
})
36+
.option("cuda", {
37+
type: "boolean",
38+
default: defaultLlamaCppCudaSupport,
39+
description: "Compile llama.cpp with CUDA support. Can also be set via the NODE_LLAMA_CPP_CUDA environment variable"
2640
});
2741
},
2842
handler: BuildLlamaCppCommand
2943
};
3044

31-
export async function BuildLlamaCppCommand({arch, nodeTarget}: BuildCommand) {
45+
export async function BuildLlamaCppCommand({arch, nodeTarget, metal, cuda}: BuildCommand) {
46+
if (metal && process.platform === "darwin") {
47+
console.log(`${chalk.yellow("Metal:")} enabled`);
48+
}
49+
50+
if (cuda) {
51+
console.log(`${chalk.yellow("CUDA:")} enabled`);
52+
}
53+
3254
await withOra({
3355
loading: chalk.blue("Compiling llama.cpp"),
3456
success: chalk.blue("Compiled llama.cpp"),
@@ -37,7 +59,9 @@ export async function BuildLlamaCppCommand({arch, nodeTarget}: BuildCommand) {
3759
await compileLlamaCpp({
3860
arch: arch ? arch : undefined,
3961
nodeTarget: nodeTarget ? nodeTarget : undefined,
40-
setUsedBingFlag: true
62+
setUsedBingFlag: true,
63+
metal,
64+
cuda
4165
});
4266
});
4367

0 commit comments

Comments
 (0)