@fugood/llama.node 0.4.4 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/darwin/arm64/llama-node.node +0 -0
- package/bin/darwin/x64/llama-node.node +0 -0
- package/bin/linux/arm64/llama-node.node +0 -0
- package/bin/linux/x64/llama-node.node +0 -0
- package/bin/linux-cuda/arm64/llama-node.node +0 -0
- package/bin/linux-cuda/x64/llama-node.node +0 -0
- package/bin/linux-vulkan/arm64/llama-node.node +0 -0
- package/bin/linux-vulkan/x64/llama-node.node +0 -0
- package/bin/win32/arm64/llama-node.node +0 -0
- package/bin/win32/arm64/node.lib +0 -0
- package/bin/win32/x64/llama-node.node +0 -0
- package/bin/win32/x64/node.lib +0 -0
- package/bin/win32-vulkan/arm64/llama-node.node +0 -0
- package/bin/win32-vulkan/arm64/node.lib +0 -0
- package/bin/win32-vulkan/x64/llama-node.node +0 -0
- package/bin/win32-vulkan/x64/node.lib +0 -0
- package/package.json +1 -1
- package/src/LlamaCompletionWorker.cpp +24 -0
- package/src/LlamaContext.cpp +82 -17
- package/src/SaveSessionWorker.cpp +6 -5
- package/src/addons.cc +21 -0
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/bin/win32/arm64/node.lib
CHANGED
|
Binary file
|
|
Binary file
|
package/bin/win32/x64/node.lib
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -266,6 +266,30 @@ llama_pos processImage(
|
|
|
266
266
|
|
|
267
267
|
llama_pos new_n_past = n_past;
|
|
268
268
|
|
|
269
|
+
// Adjust n_past to position of the text chunk
|
|
270
|
+
// TODO: Edit the text chunk to remove the tokens before n_past to speed up
|
|
271
|
+
// need to update the mtmd api
|
|
272
|
+
auto adjusted_n_past = -1;
|
|
273
|
+
for (size_t i = 0; i < chunk_pos.size(); i++) {
|
|
274
|
+
if (n_past < chunk_pos[i]) {
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
bool is_end = i + 1 == chunk_pos.size();
|
|
278
|
+
if (
|
|
279
|
+
chunk_pos[i] < n_past &&
|
|
280
|
+
(!is_end && chunk_pos[i + 1] > n_past)
|
|
281
|
+
// is_end & n_past < total_token_count:
|
|
282
|
+
// don't need to adjust and it will skip eval_chunk_single, let nextToken() to finish the job
|
|
283
|
+
) {
|
|
284
|
+
adjusted_n_past = chunk_pos[i];
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (adjusted_n_past != -1) {
|
|
288
|
+
n_past = adjusted_n_past;
|
|
289
|
+
new_n_past = n_past;
|
|
290
|
+
fprintf(stdout, "[DEBUG] Adjusted n_past to %d\n", n_past);
|
|
291
|
+
}
|
|
292
|
+
|
|
269
293
|
// Compare bitmap hashes, if they are not the same, backtrack n_past to the position of the first mismatch
|
|
270
294
|
auto mtmd_bitmap_past_hashes = sess->mtmd_bitmap_past_hashes_ptr();
|
|
271
295
|
if (mtmd_bitmap_past_hashes->size() > 0) {
|
package/src/LlamaContext.cpp
CHANGED
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
#include "SaveSessionWorker.h"
|
|
13
13
|
#include "TokenizeWorker.h"
|
|
14
14
|
|
|
15
|
+
#include <mutex>
|
|
16
|
+
#include <queue>
|
|
17
|
+
#include <atomic>
|
|
18
|
+
|
|
15
19
|
// Helper function for formatted strings (for console logs)
|
|
16
20
|
template<typename ... Args>
|
|
17
21
|
static std::string format_string(const std::string& format, Args ... args) {
|
|
@@ -383,17 +387,60 @@ bool validateModelChatTemplate(const struct llama_model * model, const bool use_
|
|
|
383
387
|
return common_chat_verify_template(tmpl, use_jinja);
|
|
384
388
|
}
|
|
385
389
|
|
|
386
|
-
|
|
390
|
+
// Store log messages for processing
|
|
391
|
+
struct LogMessage {
|
|
392
|
+
std::string level;
|
|
393
|
+
std::string text;
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
// Global variables for logging
|
|
397
|
+
static Napi::ThreadSafeFunction g_tsfn;
|
|
398
|
+
static std::atomic<bool> g_logging_enabled{false};
|
|
399
|
+
static std::mutex g_mutex;
|
|
400
|
+
static std::queue<LogMessage> g_message_queue;
|
|
401
|
+
|
|
402
|
+
// Forward declaration of the cleanup function
|
|
403
|
+
extern "C" void cleanup_logging();
|
|
387
404
|
|
|
388
405
|
// toggleNativeLog(enable: boolean, callback: (log: string) => void): void
|
|
389
406
|
void LlamaContext::ToggleNativeLog(const Napi::CallbackInfo &info) {
|
|
407
|
+
Napi::Env env = info.Env();
|
|
390
408
|
bool enable = info[0].ToBoolean().Value();
|
|
409
|
+
|
|
391
410
|
if (enable) {
|
|
392
|
-
|
|
411
|
+
if (!info[1].IsFunction()) {
|
|
412
|
+
Napi::TypeError::New(env, "Callback function required").ThrowAsJavaScriptException();
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// First clean up existing thread-safe function if any
|
|
417
|
+
if (g_logging_enabled) {
|
|
418
|
+
g_tsfn.Release();
|
|
419
|
+
g_logging_enabled = false;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Create thread-safe function that can be called from any thread
|
|
423
|
+
g_tsfn = Napi::ThreadSafeFunction::New(
|
|
424
|
+
env,
|
|
425
|
+
info[1].As<Napi::Function>(),
|
|
426
|
+
"LLAMA Logger",
|
|
427
|
+
0,
|
|
428
|
+
1,
|
|
429
|
+
[](Napi::Env) {
|
|
430
|
+
// Finalizer callback - nothing needed here
|
|
431
|
+
}
|
|
432
|
+
);
|
|
433
|
+
|
|
434
|
+
g_logging_enabled = true;
|
|
393
435
|
|
|
394
|
-
|
|
436
|
+
// Set up log callback
|
|
437
|
+
llama_log_set([](ggml_log_level level, const char* text, void* user_data) {
|
|
438
|
+
// First call the default logger
|
|
395
439
|
llama_log_callback_default(level, text, user_data);
|
|
396
440
|
|
|
441
|
+
if (!g_logging_enabled) return;
|
|
442
|
+
|
|
443
|
+
// Determine log level string
|
|
397
444
|
std::string level_str = "";
|
|
398
445
|
if (level == GGML_LOG_LEVEL_ERROR) {
|
|
399
446
|
level_str = "error";
|
|
@@ -402,24 +449,32 @@ void LlamaContext::ToggleNativeLog(const Napi::CallbackInfo &info) {
|
|
|
402
449
|
} else if (level == GGML_LOG_LEVEL_WARN) {
|
|
403
450
|
level_str = "warn";
|
|
404
451
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
Napi::String::New(env,
|
|
414
|
-
Napi::String::New(env, text)
|
|
452
|
+
|
|
453
|
+
// Create a heap-allocated copy of the data
|
|
454
|
+
auto* data = new LogMessage{level_str, text};
|
|
455
|
+
|
|
456
|
+
// Queue callback to be executed on the JavaScript thread
|
|
457
|
+
auto status = g_tsfn.BlockingCall(data, [](Napi::Env env, Napi::Function jsCallback, LogMessage* data) {
|
|
458
|
+
// This code runs on the JavaScript thread
|
|
459
|
+
jsCallback.Call({
|
|
460
|
+
Napi::String::New(env, data->level),
|
|
461
|
+
Napi::String::New(env, data->text)
|
|
415
462
|
});
|
|
416
|
-
|
|
417
|
-
|
|
463
|
+
delete data;
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
// If the call failed (e.g., runtime is shutting down), clean up the data
|
|
467
|
+
if (status != napi_ok) {
|
|
468
|
+
delete data;
|
|
418
469
|
}
|
|
419
470
|
}, nullptr);
|
|
420
471
|
} else {
|
|
421
|
-
|
|
422
|
-
|
|
472
|
+
// Disable logging
|
|
473
|
+
if (g_logging_enabled) {
|
|
474
|
+
g_logging_enabled = false;
|
|
475
|
+
g_tsfn.Release();
|
|
476
|
+
llama_log_set(llama_log_callback_default, nullptr);
|
|
477
|
+
}
|
|
423
478
|
}
|
|
424
479
|
}
|
|
425
480
|
|
|
@@ -1004,6 +1059,7 @@ Napi::Value LlamaContext::Release(const Napi::CallbackInfo &info) {
|
|
|
1004
1059
|
if (_wip != nullptr) {
|
|
1005
1060
|
_wip->SetStop();
|
|
1006
1061
|
}
|
|
1062
|
+
|
|
1007
1063
|
if (_sess == nullptr) {
|
|
1008
1064
|
auto promise = Napi::Promise::Deferred(env);
|
|
1009
1065
|
promise.Resolve(env.Undefined());
|
|
@@ -1020,6 +1076,15 @@ Napi::Value LlamaContext::Release(const Napi::CallbackInfo &info) {
|
|
|
1020
1076
|
return worker->Promise();
|
|
1021
1077
|
}
|
|
1022
1078
|
|
|
1079
|
+
// Cleanup function for the logging system
|
|
1080
|
+
// This is exposed externally for module cleanup
|
|
1081
|
+
extern "C" void cleanup_logging() {
|
|
1082
|
+
if (g_logging_enabled) {
|
|
1083
|
+
g_logging_enabled = false;
|
|
1084
|
+
g_tsfn.Release();
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1023
1088
|
LlamaContext::~LlamaContext() {
|
|
1024
1089
|
if (_mtmd_ctx != nullptr) {
|
|
1025
1090
|
mtmd_free(_mtmd_ctx);
|
|
@@ -9,15 +9,16 @@ SaveSessionWorker::SaveSessionWorker(const Napi::CallbackInfo &info,
|
|
|
9
9
|
void SaveSessionWorker::Execute() {
|
|
10
10
|
_sess->get_mutex().lock();
|
|
11
11
|
auto tokens = _sess->tokens_ptr();
|
|
12
|
+
auto tokens_to_save = std::vector<llama_token>(tokens->begin(), tokens->end());
|
|
12
13
|
|
|
13
14
|
// Find LLAMA_TOKEN_NULL in the tokens and resize the array to the index of the null token
|
|
14
|
-
auto null_token_iter = std::find(
|
|
15
|
-
if (null_token_iter !=
|
|
16
|
-
|
|
15
|
+
auto null_token_iter = std::find(tokens_to_save.begin(), tokens_to_save.end(), LLAMA_TOKEN_NULL);
|
|
16
|
+
if (null_token_iter != tokens_to_save.end()) {
|
|
17
|
+
tokens_to_save.resize(std::distance(tokens_to_save.begin(), null_token_iter));
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
if (!llama_state_save_file(_sess->context(), _path.c_str(),
|
|
20
|
-
|
|
20
|
+
if (!llama_state_save_file(_sess->context(), _path.c_str(), tokens_to_save.data(),
|
|
21
|
+
tokens_to_save.size())) {
|
|
21
22
|
SetError("Failed to save session");
|
|
22
23
|
}
|
|
23
24
|
_sess->get_mutex().unlock();
|
package/src/addons.cc
CHANGED
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
#include "LlamaContext.h"
|
|
2
2
|
#include <napi.h>
|
|
3
3
|
|
|
4
|
+
// Forward declaration of our cleanup function
|
|
5
|
+
extern "C" void cleanup_logging();
|
|
6
|
+
|
|
7
|
+
// Register cleanup function on module unload
|
|
8
|
+
static Napi::Value register_cleanup(const Napi::CallbackInfo& info) {
|
|
9
|
+
napi_add_env_cleanup_hook(info.Env(), [](void*) {
|
|
10
|
+
cleanup_logging();
|
|
11
|
+
}, nullptr);
|
|
12
|
+
|
|
13
|
+
return info.Env().Undefined();
|
|
14
|
+
}
|
|
15
|
+
|
|
4
16
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
5
17
|
LlamaContext::Init(env, exports);
|
|
18
|
+
|
|
19
|
+
// Register our cleanup handler for module unload
|
|
20
|
+
exports.Set("__registerCleanup", Napi::Function::New(env, register_cleanup));
|
|
21
|
+
|
|
22
|
+
// Also register cleanup directly on module init
|
|
23
|
+
napi_add_env_cleanup_hook(env, [](void*) {
|
|
24
|
+
cleanup_logging();
|
|
25
|
+
}, nullptr);
|
|
26
|
+
|
|
6
27
|
return exports;
|
|
7
28
|
}
|
|
8
29
|
|