Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Windows and CUDA bindings #254

Merged
merged 1 commit into from
Jun 30, 2024
Merged
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
14 changes: 5 additions & 9 deletions src/bindings/getLlama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,32 +486,28 @@ async function loadExistingLlamaBinary({
}

if (canUsePrebuiltBinaries) {
const prebuiltBinPath = await getPrebuiltBinaryPath(
const prebuiltBinDetails = await getPrebuiltBinaryPath(
buildOptions,
existingPrebuiltBinaryMustMatchBuildOptions
? buildFolderName.withCustomCmakeOptions
: buildFolderName.withoutCustomCmakeOptions
);

if (prebuiltBinPath != null) {
if (prebuiltBinDetails != null) {
try {
const buildMetadata = await getPrebuiltBinaryBuildMetadata(
existingPrebuiltBinaryMustMatchBuildOptions
? buildFolderName.withCustomCmakeOptions
: buildFolderName.withoutCustomCmakeOptions
);
const buildMetadata = await getPrebuiltBinaryBuildMetadata(prebuiltBinDetails.folderPath, prebuiltBinDetails.folderName);
const shouldTestBinaryBeforeLoading = getShouldTestBinaryBeforeLoading({
isPrebuiltBinary: true,
platform,
platformInfo,
buildMetadata
});
const binaryCompatible = shouldTestBinaryBeforeLoading
? await testBindingBinary(prebuiltBinPath)
? await testBindingBinary(prebuiltBinDetails.binaryPath)
: true;

if (binaryCompatible) {
const binding = loadBindingModule(prebuiltBinPath);
const binding = loadBindingModule(prebuiltBinDetails.binaryPath);

return await Llama._create({
bindings: binding,
Expand Down
46 changes: 39 additions & 7 deletions src/bindings/utils/compileLLamaCpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,20 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions

for (const binFilesDirPath of binFilesDirPaths) {
if (await fs.pathExists(binFilesDirPath)) {
const files = await fs.readdir(binFilesDirPath);
const itemNames = await fs.readdir(binFilesDirPath);

await Promise.all(
files.map((fileName) => (
fs.copy(path.join(binFilesDirPath, fileName), path.join(compiledResultDirPath, fileName), {
itemNames.map((itemName) => (
fs.copy(path.join(binFilesDirPath, itemName), path.join(compiledResultDirPath, itemName), {
overwrite: false
})
))
);
}
}

await applyResultDirFixes(compiledResultDirPath, path.join(outDirectory, "_temp"));

await fs.writeFile(path.join(compiledResultDirPath, buildMetadataFileName), JSON.stringify({
buildOptions: convertBuildOptionsToBuildOptionsJSON(buildOptions)
} satisfies BuildMetadataFile), "utf8");
Expand Down Expand Up @@ -282,7 +284,11 @@ export async function getPrebuiltBinaryPath(buildOptions: BuildOptions, folderNa
const binaryPath = await resolvePrebuiltBinaryPath(localPrebuiltBinaryDirectoryPath);

if (binaryPath != null)
return binaryPath;
return {
binaryPath,
folderName,
folderPath: localPrebuiltBinaryDirectoryPath
};

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

if (binaryPathFromPackage != null)
return binaryPathFromPackage;
return {
binaryPath: binaryPathFromPackage,
folderName,
folderPath: packagePrebuiltBinaryDirectoryPath
};

return null;
}

export async function getPrebuiltBinaryBuildMetadata(folderName: string) {
const buildMetadataFilePath = path.join(llamaPrebuiltBinsDirectory, folderName, buildMetadataFileName);
export async function getPrebuiltBinaryBuildMetadata(folderPath: string, folderName: string) {
const buildMetadataFilePath = path.join(folderPath, buildMetadataFileName);

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

async function applyResultDirFixes(resultDirPath: string, tempDirPath: string) {
const releaseDirPath = path.join(resultDirPath, "Release");

if (await fs.pathExists(releaseDirPath)) {
await fs.remove(tempDirPath);
await fs.ensureDir(tempDirPath);
await fs.move(releaseDirPath, tempDirPath);

const itemNames = await fs.readdir(tempDirPath);

await Promise.all(
itemNames.map((itemName) => (
fs.move(path.join(tempDirPath, itemName), path.join(resultDirPath, itemName), {
overwrite: true
})
))
);

await fs.remove(tempDirPath);
}
}

async function resolvePrebuiltBinaryPath(prebuiltBinaryDirectoryPath: string) {
const binaryPath = path.join(prebuiltBinaryDirectoryPath, "llama-addon.node");
const buildMetadataFilePath = path.join(prebuiltBinaryDirectoryPath, buildMetadataFileName);
Expand Down
Loading