@@ -31,7 +31,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
3131 includeBuildOptionsInBinaryFolderName ?: boolean ,
3232 ensureLlamaCppRepoIsCloned ?: boolean ,
3333 downloadCmakeIfNeeded ?: boolean ,
34- ignoreWorkarounds ?: ( "cudaArchitecture" ) [ ] ,
34+ ignoreWorkarounds ?: ( "cudaArchitecture" | "reduceParallelBuildThreads" | "singleBuildThread" ) [ ] ,
3535 envVars ?: typeof process . env ,
3636 ciMode ?: boolean
3737} ) : Promise < void > {
@@ -54,6 +54,12 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
5454
5555 const outDirectory = path . join ( llamaLocalBuildBinsDirectory , finalBuildFolderName ) ;
5656
57+ let parallelBuildThreads = getParallelBuildThreadsToUse ( platform ) ;
58+ if ( ignoreWorkarounds . includes ( "singleBuildThread" ) )
59+ parallelBuildThreads = 1 ;
60+ else if ( ignoreWorkarounds . includes ( "reduceParallelBuildThreads" ) )
61+ parallelBuildThreads = reduceParallelBuildThreads ( parallelBuildThreads ) ;
62+
5763 await fs . mkdirp ( llamaLocalBuildBinsDirectory ) ;
5864 try {
5965 await withLockfile ( {
@@ -128,7 +134,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
128134 "--arch=" + buildOptions . arch ,
129135 "--out" , path . relative ( llamaDirectory , outDirectory ) ,
130136 "--runtime-version=" + runtimeVersion ,
131- "--parallel=" + getParallelBuildThreadsToUse ( platform ) ,
137+ "--parallel=" + parallelBuildThreads ,
132138 ...cmakePathArgs ,
133139 ...(
134140 [ ...cmakeCustomOptions ] . map ( ( [ key , value ] ) => "--CD" + key + "=" + value )
@@ -242,6 +248,38 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
242248 console . error ( getConsoleLogPrefix ( true , false ) , err ) ;
243249 }
244250 }
251+ } else if (
252+ ( ! ignoreWorkarounds . includes ( "reduceParallelBuildThreads" ) || ! ignoreWorkarounds . includes ( "singleBuildThread" ) ) &&
253+ ( platform === "win" || platform === "linux" ) &&
254+ err instanceof SpawnError &&
255+ reduceParallelBuildThreads ( parallelBuildThreads ) !== parallelBuildThreads &&
256+ err . combinedStd . toLowerCase ( ) . includes ( "compiler is out of heap space" . toLowerCase ( ) )
257+ ) {
258+ if ( buildOptions . progressLogs ) {
259+ if ( ignoreWorkarounds . includes ( "reduceParallelBuildThreads" ) )
260+ console . info (
261+ getConsoleLogPrefix ( true ) + "Trying to compile again with a single build thread"
262+ ) ;
263+ else
264+ console . info (
265+ getConsoleLogPrefix ( true ) + "Trying to compile again with reduced parallel build threads"
266+ ) ;
267+ }
268+
269+ try {
270+ return await compileLlamaCpp ( buildOptions , {
271+ ...compileOptions ,
272+ ignoreWorkarounds : [
273+ ...ignoreWorkarounds ,
274+ ignoreWorkarounds . includes ( "reduceParallelBuildThreads" )
275+ ? "singleBuildThread"
276+ : "reduceParallelBuildThreads"
277+ ]
278+ } ) ;
279+ } catch ( err ) {
280+ if ( buildOptions . progressLogs )
281+ console . error ( getConsoleLogPrefix ( true , false ) , err ) ;
282+ }
245283 }
246284
247285 console . info ( "\n" +
@@ -475,3 +513,7 @@ function getParallelBuildThreadsToUse(platform: BinaryPlatform) {
475513
476514 return cpuCount - 2 ;
477515}
516+
517+ function reduceParallelBuildThreads ( originalParallelBuildThreads : number ) {
518+ return Math . max ( 1 , Math . round ( originalParallelBuildThreads / 2 ) ) ;
519+ }
0 commit comments