Skip to content

Commit 29f3168

Browse files
authored
fix(klaud): hold a candidate for LONG_HOLD_HOURS after two consecutive failures (#5054)
1 parent 7f818d9 commit 29f3168

2 files changed

Lines changed: 79 additions & 15 deletions

File tree

‎inferencex-e2e/infx/klaud/__main__.py‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
INFRA_COOLDOWN_DAYS = 7
6767
LONG_HOLD_HOURS = 72
6868
LONG_HOLD_OUTCOMES = frozenset({"already-updated", "duplicate", "route-mismatch"})
69+
REPEAT_HOLD_OUTCOMES = frozenset({"failed", "incompatible", "inconclusive"})
6970
PAUSED_WEIGHTS = frozenset(ASSET_REASONS)
7071
UNDATED_IMAGE_HOLD_DAYS = 7
7172
DATED_TAG = re.compile(r"(?<!\d)20\d\d(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])(?!\d)")
@@ -371,19 +372,24 @@ def offered_images(
371372
return offered
372373

373374

374-
def hold(label: str | None, phase: str | None, cooldown_hours: int) -> timedelta:
375+
def hold(
376+
label: str | None, phase: str | None, previous: str | None, cooldown_hours: int
377+
) -> timedelta:
375378
"""Return how long outcome ``label``, recorded in ``phase``, keeps its candidate out of plans.
376379
377-
Infrastructure outcomes (INFRA_OUTCOMES) hold for INFRA_COOLDOWN_DAYS. Outcomes a retry soon
378-
would repeat (LONG_HOLD_OUTCOMES, infrastructure faults in INFRA_REASONS, incompatible at
379-
resolve) hold for LONG_HOLD_HOURS, others for ``cooldown_hours``.
380+
``previous`` is the label of the candidate's session before it. Infrastructure outcomes
381+
(INFRA_OUTCOMES) hold for INFRA_COOLDOWN_DAYS. Outcomes a retry soon would repeat
382+
(LONG_HOLD_OUTCOMES, infrastructure faults in INFRA_REASONS, incompatible at resolve, and a
383+
REPEAT_HOLD_OUTCOMES outcome following another) hold for LONG_HOLD_HOURS, others for
384+
``cooldown_hours``.
380385
"""
381386
if label in INFRA_OUTCOMES:
382387
return timedelta(days=INFRA_COOLDOWN_DAYS)
383388
if (
384389
label in LONG_HOLD_OUTCOMES
385390
or label in INFRA_REASONS
386391
or (label == "incompatible" and phase == "resolve")
392+
or (label in REPEAT_HOLD_OUTCOMES and previous in REPEAT_HOLD_OUTCOMES)
387393
):
388394
return timedelta(hours=max(cooldown_hours, LONG_HOLD_HOURS))
389395
return timedelta(hours=cooldown_hours)
@@ -392,11 +398,13 @@ def hold(label: str | None, phase: str | None, cooldown_hours: int) -> timedelta
392398
def recent_candidate_outcomes(repository: str, cooldown_hours: int) -> History:
393399
"""Return the recent outcomes that still hold candidates, families, weights, images or clusters.
394400
395-
Each candidate's newest session artifact counts and holds the candidate for ``hold``. While
396-
held, missing weights and route mismatches (PAUSED_WEIGHTS) pause their (model, precision,
397-
hardware) key, and a cluster time limit (LIMIT_REASONS) holds the branch prefixes of the
398-
candidate's curve and of its keys whatever its image. An already-updated outcome holds its
399-
image from its newest time, and INFRA_BACKOFF_OUTCOMES infrastructure failures within
401+
Each candidate's newest session artifact counts and holds the candidate for ``hold``, given
402+
the finished outcome of its previous attempt (the newest finished session of an earlier
403+
planner run) when the newest one is in REPEAT_HOLD_OUTCOMES. While held, missing weights and
404+
route mismatches (PAUSED_WEIGHTS) pause their (model, precision, hardware) key, and a cluster
405+
time limit (LIMIT_REASONS) holds the branch prefixes of the candidate's curve and of its keys
406+
whatever its image. An already-updated outcome holds its image from its newest time, and
407+
INFRA_BACKOFF_OUTCOMES infrastructure failures within
400408
INFRA_BACKOFF_HOURS back off a target cluster. A wave's baseline deferral for a
401409
BASELINE_HOLD_REASONS reason holds the candidate for LONG_HOLD_HOURS unless a session of it
402410
started after that wave. Only scheduled auto-sweeps and Klaud Cold's resume runs hold
@@ -442,7 +450,7 @@ def artifacts(run_id: int) -> list[dict]:
442450
listings = dict(zip(ordered, pool.map(artifacts, ordered), strict=True))
443451
except (OSError, ValueError, KeyError, TypeError, subprocess.SubprocessError) as error:
444452
raise ReadError("candidate-history-unavailable") from error
445-
latest: dict[str, tuple[dict, int]] = {}
453+
sessions: dict[str, dict[int, tuple[dict, int]]] = {}
446454
for run_id in ordered:
447455
for artifact in listings[run_id]:
448456
try:
@@ -455,37 +463,60 @@ def artifacts(run_id: int) -> list[dict]:
455463
or utc(artifact["created_at"]) < window
456464
):
457465
continue
458-
if match[1] not in latest or artifact["id"] > latest[match[1]][0]["id"]:
459-
latest[match[1]] = (artifact, run_id)
466+
sessions.setdefault(match[1], {})[artifact["id"]] = (artifact, run_id)
460467
except (AttributeError, KeyError, TypeError, ValueError):
461468
continue
469+
ranked = {
470+
candidate: [found[artifact_id] for artifact_id in sorted(found, reverse=True)]
471+
for candidate, found in sessions.items()
472+
}
473+
latest = {candidate: found[0] for candidate, found in ranked.items()}
462474
deferring = [
463475
(created, run_id)
464476
for created, run_id in dated
465477
if plans[run_id] == run_id and now - created < timedelta(hours=LONG_HOLD_HOURS)
466478
]
467-
outcomes = pool.map(lambda pair: recorded_outcome(repository, pair[0]), latest.values())
479+
pending = pool.map(lambda pair: recorded_outcome(repository, pair[0]), latest.values())
468480
records = dict(
469481
zip(
470482
[run_id for _, run_id in deferring],
471483
pool.map(lambda pair: plan_record(repository, listings[pair[1]]), deferring),
472484
strict=True,
473485
)
474486
)
487+
outcomes = dict(zip(latest, pending, strict=True))
488+
489+
def previous_outcome(candidate: str) -> str | None:
490+
attempts = {plans[ranked[candidate][0][1]]}
491+
for artifact, run_id in ranked[candidate][1:]:
492+
if plans[run_id] in attempts:
493+
continue
494+
attempts.add(plans[run_id])
495+
label = recorded_outcome(repository, artifact)[0]
496+
if label not in {None, "no-outcome"}:
497+
return label
498+
return None
499+
500+
repeats = [
501+
candidate
502+
for candidate, (label, _) in outcomes.items()
503+
if label in REPEAT_HOLD_OUTCOMES and len(ranked[candidate]) > 1
504+
]
505+
previous = dict(zip(repeats, pool.map(previous_outcome, repeats), strict=True))
475506
result: dict[str, str | None] = {}
476507
held_images: dict[str, datetime] = {}
477508
held_families: set[str] = set()
478509
planned_labels: dict[int, dict[str, str]] = {}
479510
for (candidate, (artifact, run_id)), (label, phase) in zip(
480-
latest.items(), outcomes, strict=True
511+
latest.items(), outcomes.values(), strict=True
481512
):
482513
created = utc(artifact["created_at"])
483514
if label == "already-updated":
484515
image = candidate.split("-", 1)[1]
485516
held_images[image] = max(held_images.get(image, created), created)
486517
if label in INFRA_REASONS and now - created < timedelta(hours=INFRA_BACKOFF_HOURS):
487518
planned_labels.setdefault(plans[run_id], {})[candidate] = label
488-
if now - created >= hold(label, phase, cooldown_hours):
519+
if now - created >= hold(label, phase, previous.get(candidate), cooldown_hours):
489520
continue
490521
result[candidate] = label
491522
if label in LIMIT_REASONS:

‎inferencex-e2e/infx/tests/klaud/test_klaud_planner.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,39 @@ def test_outcomes_hold_their_candidate_and_weights_until_their_hold_ends(
167167
assert history.paused_weights == ({("dsr1", "fp8", "b200")} if paused else set())
168168

169169

170+
@pytest.mark.parametrize(("earlier", "hours", "held"), [
171+
pytest.param(None, 23 + 59 / 60, True, id="single-failure-held"),
172+
pytest.param(None, 24, False, id="single-failure-cools-down"),
173+
pytest.param(outcome("failed"), 24, True, id="second-failure-held"),
174+
pytest.param(outcome("failed"), 72, False, id="second-failure-released"),
175+
pytest.param(outcome("inconclusive"), 24, True, id="failure-after-inconclusive-held"),
176+
pytest.param(outcome("failed", "final-sweep", "infra-node-fault"), 24, False,
177+
id="failure-after-infra-fault-cools-down"),
178+
]) # fmt: skip
179+
def test_a_repeated_failure_holds_its_candidate_longer(monkeypatch, earlier, hours, held):
180+
candidate = candidate_id("family")
181+
runs, artifacts, records = [], {}, {}
182+
183+
def attempt(plan, hours, record):
184+
"""A parked first session in planner run ``plan``, then a finishing resume session."""
185+
resume = f"Klaud Cold resume {plan} {candidate} session 2"
186+
runs.extend([workflow_run(plan, "schedule", hours=hours + 4),
187+
workflow_run(plan + 1, "workflow_dispatch", title=resume,
188+
actor="Klaud-Cold", hours=hours)]) # fmt: skip
189+
artifacts[plan] = [session(plan * 10, candidate, hours + 4)]
190+
artifacts[plan + 1] = [session(plan * 10 + 10, candidate, hours)]
191+
records[plan * 10], records[plan * 10 + 10] = None, record
192+
193+
if earlier:
194+
attempt(1, hours + 24, earlier)
195+
attempt(3, hours, outcome("failed"))
196+
serve_history(monkeypatch, runs, artifacts, records)
197+
198+
history = klaud.recent_candidate_outcomes("example/project", 24)
199+
200+
assert (candidate in history.recent) is held
201+
202+
170203
@pytest.mark.parametrize(("reason", "hours", "held"), [
171204
pytest.param("baseline-point-mismatch", 71 + 59 / 60, True, id="mismatch-held"),
172205
pytest.param("baseline-point-mismatch", 72, False, id="mismatch-released"),

0 commit comments

Comments
 (0)