Skip to content
Open
Show file tree
Hide file tree
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
21 changes: 9 additions & 12 deletions llama/addon/AddonModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,14 @@ AddonModel::AddonModel(const Napi::CallbackInfo& info) :
model_params.vocab_only = options.Get("vocabOnly").As<Napi::Boolean>().Value();
}

if (options.Has("useMmap")) {
model_params.use_mmap = options.Get("useMmap").As<Napi::Boolean>().Value();
}

if (options.Has("useDirectIo")) {
model_params.use_direct_io = options.Get("useDirectIo").As<Napi::Boolean>().Value();
}

if (options.Has("useMlock")) {
model_params.use_mlock = options.Get("useMlock").As<Napi::Boolean>().Value();
if (options.Has("useMlock") && options.Get("useMlock").As<Napi::Boolean>().Value()) {
model_params.load_mode = LLAMA_LOAD_MODE_MLOCK;
} else if (options.Has("useDirectIo") && options.Get("useDirectIo").As<Napi::Boolean>().Value()) {
model_params.load_mode = LLAMA_LOAD_MODE_DIRECT_IO;
} else if (options.Has("useMmap") && options.Get("useMmap").As<Napi::Boolean>().Value()) {
model_params.load_mode = LLAMA_LOAD_MODE_MMAP;
} else {
model_params.load_mode = LLAMA_LOAD_MODE_NONE;
}

if (options.Has("checkTensors")) {
Expand Down Expand Up @@ -440,8 +438,7 @@ AddonModel::AddonModel(const Napi::CallbackInfo& info) :
}

if (model_params.no_alloc) {
model_params.use_mlock = false;
model_params.use_mmap = false;
model_params.load_mode = LLAMA_LOAD_MODE_NONE;
}
}

Expand Down
56 changes: 56 additions & 0 deletions llama/addon/addon.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <atomic>
#include <cctype>
#include <cstdlib>
#include <mutex>
#include <string_view>

#include "AddonContext.h"
#include "AddonGgufMetadata.h"
Expand All @@ -22,6 +24,20 @@ std::mutex backendMutex;
bool backendInitialized = false;
bool backendDisposed = false;

static bool compareWithUpperString(std::string_view source, std::string_view target) {
if (source.size() != target.size()) {
return false;
}

for (std::size_t i = 0; i < source.size(); i++) {
if (static_cast<unsigned char>(source[i]) != std::toupper(static_cast<unsigned char>(target[i]))) {
return false;
}
}

return true;
}

Napi::Value systemInfo(const Napi::CallbackInfo& info) {
return Napi::String::From(info.Env(), llama_print_system_info());
}
Expand Down Expand Up @@ -94,6 +110,45 @@ Napi::Value addonGetGgmlGraphOverheadCustom(const Napi::CallbackInfo& info) {
return Napi::Number::New(info.Env(), graphOverhead);
}

Napi::Value addonGetGgmlType(const Napi::CallbackInfo& info) {
if (info.Length() < 1) {
return info.Env().Undefined();
}

const auto typeParam = info[0];
if (typeParam.IsNumber()) {
const auto typeParamValue = typeParam.As<Napi::Number>().Int32Value();
if (typeParamValue < 0 || typeParamValue >= GGML_TYPE_COUNT) {
return info.Env().Undefined();
}

if (ggml_type_size(static_cast<ggml_type>(typeParamValue)) == 0) {
return info.Env().Undefined();
}

return Napi::Number::New(info.Env(), typeParamValue);
} else if (typeParam.IsString()) {
const auto typeParamValue = typeParam.As<Napi::String>().Utf8Value();

for (int i = 0; i < GGML_TYPE_COUNT; i++) {
if (ggml_type_size(static_cast<ggml_type>(i)) == 0) {
continue;
}

const auto typeName = ggml_type_name(static_cast<ggml_type>(i));
if (typeName == nullptr) {
continue;
}

if (compareWithUpperString(typeParamValue, typeName)) {
return Napi::Number::New(info.Env(), i);
}
}
}

return info.Env().Undefined();
}

Napi::Value addonGetConsts(const Napi::CallbackInfo& info) {
Napi::Object consts = Napi::Object::New(info.Env());
consts.Set("ggmlMaxDims", Napi::Number::New(info.Env(), GGML_MAX_DIMS));
Expand Down Expand Up @@ -301,6 +356,7 @@ Napi::Object registerCallback(Napi::Env env, Napi::Object exports) {
Napi::PropertyDescriptor::Function("getBlockSizeForGgmlType", addonGetBlockSizeForGgmlType),
Napi::PropertyDescriptor::Function("getTypeSizeForGgmlType", addonGetTypeSizeForGgmlType),
Napi::PropertyDescriptor::Function("getGgmlGraphOverheadCustom", addonGetGgmlGraphOverheadCustom),
Napi::PropertyDescriptor::Function("getGgmlType", addonGetGgmlType),
Napi::PropertyDescriptor::Function("getConsts", addonGetConsts),
Napi::PropertyDescriptor::Function("setLogger", setLogger),
Napi::PropertyDescriptor::Function("setLoggerLogLevel", setLoggerLogLevel),
Expand Down
1 change: 1 addition & 0 deletions src/bindings/AddonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type BindingModule = {
getBlockSizeForGgmlType(ggmlType: number): number | undefined,
getTypeSizeForGgmlType(ggmlType: number): number | undefined,
getGgmlGraphOverheadCustom(size: number, grads: boolean): number,
getGgmlType(ggmlType: string | number): number | undefined,
getConsts(): {
ggmlMaxDims: number,
ggmlTypeF16Size: number,
Expand Down
2 changes: 1 addition & 1 deletion src/chatWrappers/QwenChatWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class QwenChatWrapper extends ChatWrapper {

/** @internal */
public static override _checkModelCompatibility(options: ChatWrapperCheckModelCompatibilityParams): boolean {
const architecture = options.fileInfo?.metadata.general.architecture;
const architecture = options.architecture;
return (
architecture == null ||
architecture === GgufArchitectureType.qwen2 ||
Expand Down
Loading
Loading