Skip to content

Commit ff1644d

Browse files
authored
fix: bugs (#80)
* feat: adapt to the latest `llama.cpp` interface * feat: print helpful information to help resolve a clone issue when it happens * feat: print helpful information to help resolve build issues related to CUDA * feat: make portable cmake on Windows more stable * feat: update `CMakeLists.txt` to match `llama.cpp` better * fix: do not download redundant node headers * fix: improve cmake custom options handling * fix: do not set `CMAKE_GENERATOR_TOOLSET` for CUDA * fix: do not fetch information from GitHub when using a local git bundle * fix: GBNF JSON schema string const formatting * docs: document a solution to a compilation problem on macOS * docs: document more CUDA build error solutions * docs: explain about ES modules in the getting started guide * chore: update `.commitlintrc.json` * chore: remove redundant dependency
1 parent 1cba701 commit ff1644d

File tree

19 files changed

+207
-107
lines changed

19 files changed

+207
-107
lines changed

.commitlintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": ["@commitlint/config-conventional"]
2+
"extends": ["@commitlint/config-conventional"],
3+
"rules": {
4+
"subject-case": [0, "never"]
5+
}
36
}

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ jobs:
231231
- name: Install modules
232232
run: npm ci
233233

234+
- name: Build binary
235+
run: node ./dist/cli/cli.js build
236+
234237
- name: Run standalone tests
235238
run: npm run test:standalone
236239

docs/guide/CUDA.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,39 @@ To build `node-llama-cpp` with any of these options, set an environment variable
2323
### Fix the `Failed to detect a default CUDA architecture` build error
2424
To fix this issue you have to set the `CUDACXX` environment variable to the path of the `nvcc` compiler.
2525

26-
For example, if you installed CUDA Toolkit 12.2 on Windows, you have to run the following command:
27-
```bash
28-
set CUDACXX=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\bin\nvcc.exe
26+
For example, if you have installed CUDA Toolkit 12.2, you have to run a command like this:
27+
::: code-group
28+
```bash [Linux]
29+
export CUDACXX=/usr/local/cuda-12.2/bin/nvcc
2930
```
3031

31-
On Linux, it would be something like this:
32-
```bash
33-
export CUDACXX=/usr/local/cuda-12.2/bin/nvcc
32+
```bash [Windows]
33+
set CUDACXX=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\bin\nvcc.exe
3434
```
35+
:::
3536

3637
Then run the build command again to check whether setting the `CUDACXX` environment variable fixed the issue.
3738

39+
### Fix the `The CUDA compiler identification is unknown` build error
40+
The solution to this error is the same as [the solution to the `Failed to detect a default CUDA architecture` error](#fix-the-failed-to-detect-a-default-cuda-architecture-build-error).
41+
42+
### Fix the `A single input file is required for a non-link phase when an outputfile is specified` build error
43+
To fix this issue you have to set the `CMAKE_GENERATOR_TOOLSET` cmake option to the CUDA home directory, usually already set as the `CUDA_PATH` environment variable.
44+
45+
To do this, set the `NODE_LLAMA_CPP_CMAKE_OPTION_CMAKE_GENERATOR_TOOLSET` environment variable to the path of your CUDA home directory:
46+
47+
::: code-group
48+
```bash [Linux]
49+
export NODE_LLAMA_CPP_CMAKE_OPTION_CMAKE_GENERATOR_TOOLSET=$CUDA_PATH
50+
```
51+
52+
```bash [Windows]
53+
set NODE_LLAMA_CPP_CMAKE_OPTION_CMAKE_GENERATOR_TOOLSET=%CUDA_PATH%
54+
```
55+
:::
56+
57+
Then run the build command again to check whether setting the `CMAKE_GENERATOR_TOOLSET` cmake option fixed the issue.
58+
3859
## Using `node-llama-cpp` with CUDA
3960
After you build `node-llama-cpp` with CUDA support, you can use it normally.
4061

docs/guide/building-from-source.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ If `cmake` is not installed on your machine, `node-llama-cpp` will automatically
2727

2828
If the build fails, make sure you have the required dependencies of `cmake` installed on your machine. More info is available [here](https://github.com/cmake-js/cmake-js#:~:text=projectRoot/build%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bstring%5D-,Requirements%3A,-CMake) (you don't have to install `cmake` or `cmake-js`, just the dependencies).
2929

30+
If the build fails on macOS with the error `"/usr/bin/cc" is not able to compile a simple test program`, try running `xcode-select --install` to install the Xcode command line tools.
31+
3032
:::
3133

3234
## `download` and `build` commands

docs/guide/cli/build.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const commandDoc = docs.build;
1010

1111
{{commandDoc.description}}
1212

13+
::: info
14+
If the build fails on macOS with the error `"/usr/bin/cc" is not able to compile a simple test program`, try running `xcode-select --install` to install the Xcode command line tools.
15+
:::
16+
1317
## Usage
1418
```shell-vue
1519
{{commandDoc.usage}}

docs/guide/cli/download.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ This is useful for building from source on machines that aren't connected to the
2020

2121
:::
2222

23+
::: info
24+
If the build fails on macOS with the error `"/usr/bin/cc" is not able to compile a simple test program`, try running `xcode-select --install` to install the Xcode command line tools.
25+
:::
26+
2327
## Usage
2428
```shell-vue
2529
{{commandDoc.usage}}

docs/guide/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ npm install --save node-llama-cpp
1414
> If binaries are not available for your platform, it'll fallback to download a release of `llama.cpp` and build it from source with `cmake`.
1515
> To disable this behavior, set the environment variable `NODE_LLAMA_CPP_SKIP_DOWNLOAD` to `true`.
1616
17+
## ESM usage
18+
`node-llama-cpp` is an [ES module](https://nodejs.org/api/esm.html#modules-ecmascript-modules), so can only use `import` to load it and cannot use `require`.
19+
20+
To make sure you can use it in your project, make sure your `package.json` file has `"type": "module"` in it.
21+
1722
## CUDA and Metal support
1823
**Metal:** Metal support is enabled by default on macOS. If you're using a Mac with an Intel chip, [you might want to disable it](./Metal.md).
1924

llama/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
cmake_minimum_required(VERSION 3.12)
1+
cmake_minimum_required(VERSION 3.13)
22

3-
project ("llama-addon")
3+
project("llama-addon" C CXX)
44

55
if (MSVC)
66
# add_compile_options(/EHsc)

llama/addon.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ class LLAMAContext : public Napi::ObjectWrap<LLAMAContext> {
208208
return Napi::String::New(info.Env(), ss.str());
209209
}
210210
Napi::Value TokenBos(const Napi::CallbackInfo& info) {
211-
return Napi::Number::From(info.Env(), llama_token_bos(ctx));
211+
return Napi::Number::From(info.Env(), llama_token_bos(model->model)); // TODO: move this to the model
212212
}
213213
Napi::Value TokenEos(const Napi::CallbackInfo& info) {
214-
return Napi::Number::From(info.Env(), llama_token_eos(ctx));
214+
return Napi::Number::From(info.Env(), llama_token_eos(model->model)); // TODO: move this to the model
215215
}
216216
Napi::Value TokenNl(const Napi::CallbackInfo& info) {
217-
return Napi::Number::From(info.Env(), llama_token_nl(ctx));
217+
return Napi::Number::From(info.Env(), llama_token_nl(model->model)); // TODO: move this to the model
218218
}
219219
Napi::Value GetContextSize(const Napi::CallbackInfo& info) {
220220
return Napi::Number::From(info.Env(), llama_n_ctx(ctx));
@@ -223,7 +223,7 @@ class LLAMAContext : public Napi::ObjectWrap<LLAMAContext> {
223223
int token = info[0].As<Napi::Number>().Int32Value();
224224
std::stringstream ss;
225225

226-
const char* str = llama_token_get_text(ctx, token);
226+
const char* str = llama_token_get_text(model->model, token); // TODO: move this to the model
227227
if (str == nullptr) {
228228
return info.Env().Undefined();
229229
}
@@ -336,18 +336,14 @@ class LLAMAContextEvalWorker : Napi::AsyncWorker, Napi::Promise::Deferred {
336336

337337
protected:
338338
void Execute() {
339-
llama_batch batch = llama_batch_init(tokens.size(), 0);
339+
llama_batch batch = llama_batch_init(tokens.size(), 0, 1);
340340

341-
batch.n_tokens = tokens.size();
342-
343-
for (int32_t i = 0; i < batch.n_tokens; i++) {
344-
batch.token[i] = tokens[i];
345-
batch.pos[i] = ctx->n_cur;
346-
batch.seq_id[i] = 0;
347-
batch.logits[i] = false;
341+
for (size_t i = 0; i < tokens.size(); i++) {
342+
llama_batch_add(batch, tokens[i], ctx->n_cur, { 0 }, false);
348343

349344
ctx->n_cur++;
350345
}
346+
GGML_ASSERT(batch.n_tokens == (int) tokens.size());
351347

352348
batch.logits[batch.n_tokens - 1] = true;
353349

@@ -381,14 +377,11 @@ class LLAMAContextEvalWorker : Napi::AsyncWorker, Napi::Promise::Deferred {
381377

382378
llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
383379

384-
auto eos_token = llama_token_eos(ctx->ctx);
380+
auto eos_token = llama_token_eos(ctx->model->model);
385381

386382
if (use_repeat_penalty && !repeat_penalty_tokens.empty()) {
387-
llama_sample_repetition_penalty(
388-
ctx->ctx, &candidates_p, repeat_penalty_tokens.data(), repeat_penalty_tokens.size(), repeat_penalty
389-
);
390-
llama_sample_frequency_and_presence_penalties(
391-
ctx->ctx, &candidates_p, repeat_penalty_tokens.data(), repeat_penalty_tokens.size(),
383+
llama_sample_repetition_penalties(
384+
ctx->ctx, &candidates_p, repeat_penalty_tokens.data(), repeat_penalty_tokens.size(), repeat_penalty,
392385
repeat_penalty_frequency_penalty, repeat_penalty_presence_penalty
393386
);
394387
}

llama/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"binary": {
3+
"napi_versions": [7]
4+
}
5+
}

0 commit comments

Comments
 (0)