Skip to content

Commit da00a17

Browse files
authored
fix: Windows and CUDA bindings (#254)
1 parent 1fbbf72 commit da00a17

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/bindings/getLlama.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,32 +486,28 @@ async function loadExistingLlamaBinary({
486486
}
487487

488488
if (canUsePrebuiltBinaries) {
489-
const prebuiltBinPath = await getPrebuiltBinaryPath(
489+
const prebuiltBinDetails = await getPrebuiltBinaryPath(
490490
buildOptions,
491491
existingPrebuiltBinaryMustMatchBuildOptions
492492
? buildFolderName.withCustomCmakeOptions
493493
: buildFolderName.withoutCustomCmakeOptions
494494
);
495495

496-
if (prebuiltBinPath != null) {
496+
if (prebuiltBinDetails != null) {
497497
try {
498-
const buildMetadata = await getPrebuiltBinaryBuildMetadata(
499-
existingPrebuiltBinaryMustMatchBuildOptions
500-
? buildFolderName.withCustomCmakeOptions
501-
: buildFolderName.withoutCustomCmakeOptions
502-
);
498+
const buildMetadata = await getPrebuiltBinaryBuildMetadata(prebuiltBinDetails.folderPath, prebuiltBinDetails.folderName);
503499
const shouldTestBinaryBeforeLoading = getShouldTestBinaryBeforeLoading({
504500
isPrebuiltBinary: true,
505501
platform,
506502
platformInfo,
507503
buildMetadata
508504
});
509505
const binaryCompatible = shouldTestBinaryBeforeLoading
510-
? await testBindingBinary(prebuiltBinPath)
506+
? await testBindingBinary(prebuiltBinDetails.binaryPath)
511507
: true;
512508

513509
if (binaryCompatible) {
514-
const binding = loadBindingModule(prebuiltBinPath);
510+
const binding = loadBindingModule(prebuiltBinDetails.binaryPath);
515511

516512
return await Llama._create({
517513
bindings: binding,

src/bindings/utils/compileLLamaCpp.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,20 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
141141

142142
for (const binFilesDirPath of binFilesDirPaths) {
143143
if (await fs.pathExists(binFilesDirPath)) {
144-
const files = await fs.readdir(binFilesDirPath);
144+
const itemNames = await fs.readdir(binFilesDirPath);
145145

146146
await Promise.all(
147-
files.map((fileName) => (
148-
fs.copy(path.join(binFilesDirPath, fileName), path.join(compiledResultDirPath, fileName), {
147+
itemNames.map((itemName) => (
148+
fs.copy(path.join(binFilesDirPath, itemName), path.join(compiledResultDirPath, itemName), {
149149
overwrite: false
150150
})
151151
))
152152
);
153153
}
154154
}
155155

156+
await applyResultDirFixes(compiledResultDirPath, path.join(outDirectory, "_temp"));
157+
156158
await fs.writeFile(path.join(compiledResultDirPath, buildMetadataFileName), JSON.stringify({
157159
buildOptions: convertBuildOptionsToBuildOptionsJSON(buildOptions)
158160
} satisfies BuildMetadataFile), "utf8");
@@ -282,7 +284,11 @@ export async function getPrebuiltBinaryPath(buildOptions: BuildOptions, folderNa
282284
const binaryPath = await resolvePrebuiltBinaryPath(localPrebuiltBinaryDirectoryPath);
283285

284286
if (binaryPath != null)
285-
return binaryPath;
287+
return {
288+
binaryPath,
289+
folderName,
290+
folderPath: localPrebuiltBinaryDirectoryPath
291+
};
286292

287293
const packagePrebuiltBinariesDirectoryPath = await getPrebuiltBinariesPackageDirectoryForBuildOptions(buildOptions);
288294
if (packagePrebuiltBinariesDirectoryPath == null)
@@ -292,13 +298,17 @@ export async function getPrebuiltBinaryPath(buildOptions: BuildOptions, folderNa
292298
const binaryPathFromPackage = await resolvePrebuiltBinaryPath(packagePrebuiltBinaryDirectoryPath);
293299

294300
if (binaryPathFromPackage != null)
295-
return binaryPathFromPackage;
301+
return {
302+
binaryPath: binaryPathFromPackage,
303+
folderName,
304+
folderPath: packagePrebuiltBinaryDirectoryPath
305+
};
296306

297307
return null;
298308
}
299309

300-
export async function getPrebuiltBinaryBuildMetadata(folderName: string) {
301-
const buildMetadataFilePath = path.join(llamaPrebuiltBinsDirectory, folderName, buildMetadataFileName);
310+
export async function getPrebuiltBinaryBuildMetadata(folderPath: string, folderName: string) {
311+
const buildMetadataFilePath = path.join(folderPath, buildMetadataFileName);
302312

303313
if (!(await fs.pathExists(buildMetadataFilePath)))
304314
throw new Error(`Could not find build metadata file for prebuilt build "${folderName}"`);
@@ -307,6 +317,28 @@ export async function getPrebuiltBinaryBuildMetadata(folderName: string) {
307317
return buildMetadata;
308318
}
309319

320+
async function applyResultDirFixes(resultDirPath: string, tempDirPath: string) {
321+
const releaseDirPath = path.join(resultDirPath, "Release");
322+
323+
if (await fs.pathExists(releaseDirPath)) {
324+
await fs.remove(tempDirPath);
325+
await fs.ensureDir(tempDirPath);
326+
await fs.move(releaseDirPath, tempDirPath);
327+
328+
const itemNames = await fs.readdir(tempDirPath);
329+
330+
await Promise.all(
331+
itemNames.map((itemName) => (
332+
fs.move(path.join(tempDirPath, itemName), path.join(resultDirPath, itemName), {
333+
overwrite: true
334+
})
335+
))
336+
);
337+
338+
await fs.remove(tempDirPath);
339+
}
340+
}
341+
310342
async function resolvePrebuiltBinaryPath(prebuiltBinaryDirectoryPath: string) {
311343
const binaryPath = path.join(prebuiltBinaryDirectoryPath, "llama-addon.node");
312344
const buildMetadataFilePath = path.join(prebuiltBinaryDirectoryPath, buildMetadataFileName);

0 commit comments

Comments
 (0)