Skip to content

Commit 4e6772f

Browse files
committed
Add retry for Build stage
Signed-off-by: Yuanjing Xue <197832395+yuanjingx87@users.noreply.github.com>
1 parent f188d62 commit 4e6772f

1 file changed

Lines changed: 66 additions & 6 deletions

File tree

‎jenkins/Build.groovy‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ BUILD_MEMORY_REQUEST = "48Gi"
195195
BUILD_MEMORY_LIMIT = "96Gi"
196196
BUILD_JOBS = "8"
197197

198+
// When the wheel build dies from memory exhaustion, retry with halved -j.
199+
BUILD_OOM_MAX_RETRIES = 2
200+
BUILD_OOM_MIN_JOBS = 2
201+
BUILD_WHEEL_LOG = "build_wheel.log"
202+
// The allocator giving up: memory exhaustion on its own.
203+
BUILD_OOM_ALLOC_LOG_PATTERN = "std::bad_alloc|[Oo]ut of memory|Cannot allocate memory"
204+
// A compiler/linker was SIGKILLed. Abort, timeout or eviction look the same,
205+
// so this only counts as OOM when the cgroup also recorded an oom_kill.
206+
BUILD_OOM_KILL_LOG_PATTERN = "signal terminated program|terminated with signal 9|died due to signal 9|exit status 137|^Killed\$"
207+
198208
TESTER_CORES = "12"
199209
TESTER_MEMORY = "96Gi"
200210

@@ -468,6 +478,34 @@ def prepareLLMBuild(pipeline, config, versionOverride)
468478

469479
}
470480

481+
// Number of OOM kills recorded by this container's cgroup (v2, then v1), or -1 if unreadable.
482+
def cgroupOomKills()
483+
{
484+
def out = sh(
485+
script: """for f in /sys/fs/cgroup/memory.events /sys/fs/cgroup/memory/memory.oom_control; do
486+
v=\$(awk '\$1=="oom_kill"{print \$2}' \$f 2>/dev/null)
487+
if [ -n "\$v" ]; then echo \$v; exit 0; fi
488+
done
489+
echo -1""",
490+
returnStdout: true).trim()
491+
return out as long
492+
}
493+
494+
// True when the last wheel build log shows memory exhaustion. oomKillsBefore is
495+
// cgroupOomKills() taken before that build attempt.
496+
def isBuildOom(long oomKillsBefore)
497+
{
498+
def logFile = "${WORKSPACE}/${BUILD_WHEEL_LOG}"
499+
if (sh(script: "grep -qE '${BUILD_OOM_ALLOC_LOG_PATTERN}' ${logFile}", returnStatus: true) == 0) {
500+
return true
501+
}
502+
if (sh(script: "grep -qE '${BUILD_OOM_KILL_LOG_PATTERN}' ${logFile}", returnStatus: true) != 0) {
503+
return false
504+
}
505+
// Without a readable counter we cannot rule OOM out, so trust the log.
506+
return oomKillsBefore < 0 || cgroupOomKills() > oomKillsBefore
507+
}
508+
471509
def runLLMBuild(
472510
pipeline, buildFlags, tarName, is_linux_x86_64, versionOverride, typeCheck=false, artifacts=null)
473511
{
@@ -511,17 +549,39 @@ def runLLMBuild(
511549
trtllm_utils.replaceWithAlternativeTRT(env.alternativeTRT, "cp312")
512550
}
513551

514-
def buildJobs = buildFlags[BUILD_JOBS_FOR_CONFIG] ?: BUILD_JOBS
552+
int buildJobs = (buildFlags[BUILD_JOBS_FOR_CONFIG] ?: BUILD_JOBS) as int
515553

516554
withEnv([
517555
"TRTLLM_BUILD_SOURCE_COMMIT=${env.gitlabCommit}",
518556
"TRTLLM_VERSION_OVERRIDE=${versionOverride}",
519557
]) {
520-
// cmake will download third-party sources, so we add retry
521-
trtllm_utils.llmExecStepWithRetry(
522-
pipeline,
523-
script: "cd ${LLM_ROOT} && python3 scripts/build_wheel.py --version-override \"\${TRTLLM_VERSION_OVERRIDE}\" --clean --use_ccache -G Ninja -j ${buildJobs} -a '${buildFlags[WHEEL_ARCHS]}' ${buildFlags[WHEEL_EXTRA_ARGS]}"
524-
)
558+
int oomRetries = 0
559+
while (true) {
560+
long oomKillsBefore = cgroupOomKills()
561+
try {
562+
// cmake will download third-party sources, so we add retry.
563+
// The shebang + pipefail keeps the build's exit code through tee,
564+
// and the log lets us tell an OOM from other failures.
565+
trtllm_utils.llmExecStepWithRetry(
566+
pipeline,
567+
script: """#!/bin/bash
568+
set -o pipefail
569+
cd ${LLM_ROOT} && python3 scripts/build_wheel.py --version-override \"\${TRTLLM_VERSION_OVERRIDE}\" --clean --use_ccache -G Ninja -j ${buildJobs} -a '${buildFlags[WHEEL_ARCHS]}' ${buildFlags[WHEEL_EXTRA_ARGS]} 2>&1 | tee ${WORKSPACE}/${BUILD_WHEEL_LOG}"""
570+
)
571+
break
572+
} catch (Exception e) {
573+
if (e.toString().contains("FlowInterruptedException") ||
574+
oomRetries >= BUILD_OOM_MAX_RETRIES ||
575+
buildJobs <= BUILD_OOM_MIN_JOBS ||
576+
!isBuildOom(oomKillsBefore)) {
577+
throw e
578+
}
579+
oomRetries++
580+
int newJobs = Math.max(BUILD_OOM_MIN_JOBS, (int) (buildJobs / 2))
581+
echo "Wheel build ran out of memory; retrying with -j ${newJobs} (was ${buildJobs}), OOM retry ${oomRetries}/${BUILD_OOM_MAX_RETRIES}."
582+
buildJobs = newJobs
583+
}
584+
}
525585
}
526586

527587
// Type-check with the compiled bindings that build_wheel.py just produced in

0 commit comments

Comments
 (0)