Skip to content

Commit 06afc92

Browse files
committed
fix: future-proof llama.cpp backend loading
1 parent 68eef2e commit 06afc92

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

llama/addon/addon.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,17 @@ class AddonBackendUnloadWorker : public Napi::AsyncWorker {
152152
};
153153

154154
Napi::Value addonLoadBackends(const Napi::CallbackInfo& info) {
155-
ggml_backend_load_all();
155+
const bool forceLoadLibraries = info.Length() == 0
156+
? false
157+
: info[0].IsBoolean()
158+
? info[0].As<Napi::Boolean>().Value()
159+
: false;
160+
161+
ggml_backend_reg_count();
162+
163+
if (forceLoadLibraries) {
164+
ggml_backend_load_all();
165+
}
156166

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

src/bindings/AddonTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export type BindingModule = {
7676
free: number
7777
},
7878
init(): Promise<void>,
79-
loadBackends(): void,
79+
loadBackends(forceLoadLibraries?: boolean): void,
8080
dispose(): Promise<void>
8181
};
8282

src/bindings/Llama.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ export class Llama {
127127
}
128128

129129
this._bindings.loadBackends();
130+
const loadedGpu = bindings.getGpuType();
131+
if (loadedGpu == null || (loadedGpu === false && gpu !== false))
132+
this._bindings.loadBackends(true);
130133

131134
this._onExit = this._onExit.bind(this);
132135

src/bindings/utils/testBindingBinary.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ if (process.env.TEST_BINDING_CP === "true" && (process.parentPort != null || pro
196196
if (message.type === "start") {
197197
try {
198198
const binding: BindingModule = require(message.bindingBinaryPath);
199+
199200
binding.loadBackends();
201+
const loadedGpu = binding.getGpuType();
202+
if (loadedGpu == null || (loadedGpu === false && message.gpu !== false))
203+
binding.loadBackends(true);
204+
200205
await binding.init();
201206
binding.getGpuVramInfo();
202207
binding.getGpuDeviceInfo();

0 commit comments

Comments
 (0)