Skip to content

Commit c4f94d2

Browse files
committed
feat: faster building from source
1 parent 1676d41 commit c4f94d2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/bindings/utils/compileLLamaCpp.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import {fileURLToPath} from "url";
33
import process from "process";
4+
import os from "os";
45
import fs from "fs-extra";
56
import chalk from "chalk";
67
import which from "which";
@@ -17,7 +18,7 @@ import {getModuleVersion} from "../../utils/getModuleVersion.js";
1718
import {ensureLlamaCppRepoIsCloned, isLlamaCppRepoCloned} from "./cloneLlamaCppRepo.js";
1819
import {getBuildFolderNameForBuildOptions} from "./getBuildFolderNameForBuildOptions.js";
1920
import {setLastBuildInfo} from "./lastBuildInfo.js";
20-
import {getPlatform} from "./getPlatform.js";
21+
import {BinaryPlatform, getPlatform} from "./getPlatform.js";
2122
import {logDistroInstallInstruction} from "./logDistroInstallInstruction.js";
2223
import {testCmakeBinary} from "./testCmakeBinary.js";
2324
import {getCudaNvccPaths} from "./detectAvailableComputeLayers.js";
@@ -45,6 +46,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
4546
ciMode = false
4647
} = compileOptions;
4748

49+
const platform = getPlatform();
4850
const buildFolderName = await getBuildFolderNameForBuildOptions(buildOptions);
4951
const finalBuildFolderName = includeBuildOptionsInBinaryFolderName
5052
? buildFolderName.withCustomCmakeOptions
@@ -123,6 +125,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
123125
"--arch=" + buildOptions.arch,
124126
"--out", path.relative(llamaDirectory, outDirectory),
125127
"--runtime-version=" + runtimeVersion,
128+
"--parallel=" + getParallelBuildThreadsToUse(platform),
126129
...cmakePathArgs,
127130
...(
128131
[...cmakeCustomOptions].map(([key, value]) => "--CD" + key + "=" + value)
@@ -174,7 +177,6 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
174177
}
175178
});
176179
} catch (err) {
177-
const platform = getPlatform();
178180
if (platform === "linux" && await which("make", {nothrow: true}) == null) {
179181
console.info("\n" +
180182
getConsoleLogPrefix(true) +
@@ -456,3 +458,15 @@ async function getToolchainFileForArch(targetArch: string) {
456458

457459
return null;
458460
}
461+
462+
function getParallelBuildThreadsToUse(platform: BinaryPlatform) {
463+
const cpuCount = os.cpus().length;
464+
465+
if (cpuCount <= 4)
466+
return cpuCount;
467+
468+
if (platform === "mac" && process.arch === "arm64")
469+
return cpuCount - 1;
470+
471+
return cpuCount - 2;
472+
}

src/bindings/utils/getLinuxDistroInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function getOsReleaseInfo() {
2929
if (!(await fs.pathExists(osReleasePath)))
3030
continue;
3131

32-
const osReleaseFile = await fs.readFile(osReleasePath, "utf-8");
32+
const osReleaseFile = await fs.readFile(osReleasePath, "utf8");
3333

3434
const res = new Map<string, string>();
3535
for (const line of osReleaseFile.split("\n")) {

0 commit comments

Comments
 (0)