Skip to content

Commit c080ff0

Browse files
committed
fix: adapt to breaking llama.cpp changes
1 parent b7dec7c commit c080ff0

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

llama/addon/addon.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Napi::Object registerCallback(Napi::Env env, Napi::Object exports) {
215215
AddonContext::init(exports);
216216
AddonSampler::init(exports);
217217

218+
ggml_backend_load_all();
218219
llama_log_set(addonLlamaCppLogCallback, nullptr);
219220

220221
exports.AddFinalizer(addonFreeLlamaBackend, static_cast<int*>(nullptr));

llama/addon/globals/getGpuInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,14 @@ Napi::Value getGpuType(const Napi::CallbackInfo& info) {
103103
}
104104
}
105105

106+
for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
107+
ggml_backend_dev_t device = ggml_backend_dev_get(i);
108+
const auto deviceName = std::string(ggml_backend_dev_name(device));
109+
110+
if (deviceName == "CPU") {
111+
return Napi::Boolean::New(info.Env(), false);
112+
}
113+
}
114+
106115
return info.Env().Undefined();
107116
}

src/bindings/AddonTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export type BindingModule = {
6969
getGpuDeviceInfo(): {
7070
deviceNames: string[]
7171
},
72-
getGpuType(): "cuda" | "vulkan" | "metal" | undefined,
72+
getGpuType(): "cuda" | "vulkan" | "metal" | false | undefined,
7373
getSwapInfo(): {
7474
total: number,
7575
maxSize: number,

src/bindings/getLlama.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ async function loadExistingLlamaBinary({
586586
buildMetadata
587587
});
588588
const binaryCompatible = shouldTestBinaryBeforeLoading
589-
? await testBindingBinary(localBuildBinPath)
589+
? await testBindingBinary(localBuildBinPath, buildOptions.gpu)
590590
: true;
591591

592592
if (binaryCompatible) {
@@ -643,7 +643,7 @@ async function loadExistingLlamaBinary({
643643
buildMetadata
644644
});
645645
const binaryCompatible = shouldTestBinaryBeforeLoading
646-
? await testBindingBinary(prebuiltBinDetails.binaryPath)
646+
? await testBindingBinary(prebuiltBinDetails.binaryPath, buildOptions.gpu)
647647
: true;
648648

649649
if (binaryCompatible) {

src/bindings/utils/testBindingBinary.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import {createRequire} from "module";
44
import path from "path";
55
import {getConsoleLogPrefix} from "../../utils/getConsoleLogPrefix.js";
66
import {runningInElectron} from "../../utils/runtime.js";
7+
import {BuildGpu} from "../types.js";
78
import type {BindingModule} from "../AddonTypes.js";
89

910
const require = createRequire(import.meta.url);
1011
const __filename = fileURLToPath(import.meta.url);
1112
const detectedFileName = path.basename(__filename);
1213
const expectedFileName = "testBindingBinary";
1314

14-
export async function testBindingBinary(bindingBinaryPath: string, testTimeout: number = 1000 * 60 * 5): Promise<boolean> {
15+
export async function testBindingBinary(bindingBinaryPath: string, gpu: BuildGpu, testTimeout: number = 1000 * 60 * 5): Promise<boolean> {
1516
if (!detectedFileName.startsWith(expectedFileName)) {
1617
console.warn(
1718
getConsoleLogPrefix() +
@@ -163,10 +164,14 @@ export async function testBindingBinary(bindingBinaryPath: string, testTimeout:
163164
onMessage(message: ChildToParentMessage) {
164165
if (message.type === "ready") {
165166
forkSucceeded = true;
166-
subProcess!.sendMessage({type: "start", bindingBinaryPath} satisfies ParentToChildMessage);
167+
subProcess!.sendMessage({
168+
type: "start",
169+
bindingBinaryPath,
170+
gpu
171+
});
167172
} else if (message.type === "done") {
168173
testPassed = true;
169-
subProcess!.sendMessage({type: "exit"} satisfies ParentToChildMessage);
174+
subProcess!.sendMessage({type: "exit"});
170175
}
171176
},
172177
onExit(code: number) {
@@ -194,6 +199,12 @@ if (process.env.TEST_BINDING_CP === "true" && (process.parentPort != null || pro
194199
await binding.init();
195200
binding.getGpuVramInfo();
196201
binding.getGpuDeviceInfo();
202+
203+
const gpuType = binding.getGpuType();
204+
void (gpuType as BuildGpu satisfies typeof gpuType);
205+
if (gpuType !== message.gpu)
206+
throw new Error(`GPU type mismatch. Expected: ${message.gpu}, got: ${gpuType}`);
207+
197208
sendMessage({type: "done"});
198209
} catch (err) {
199210
console.error(err);
@@ -214,7 +225,8 @@ if (process.env.TEST_BINDING_CP === "true" && (process.parentPort != null || pro
214225

215226
type ParentToChildMessage = {
216227
type: "start",
217-
bindingBinaryPath: string
228+
bindingBinaryPath: string,
229+
gpu: BuildGpu
218230
} | {
219231
type: "exit"
220232
};

0 commit comments

Comments
 (0)