Add deeper infeasible probe diagnostics

This commit is contained in:
2026-04-05 01:44:38 +08:00
parent 0aa607a4f1
commit 84c5d6bd80
5 changed files with 249 additions and 3 deletions

View File

@@ -36,7 +36,7 @@ def binary_search_max_feasible(
cur_low = low
cur_high = high
for _ in range(max_probes):
if cur_high - cur_low <= tolerance:
if cur_high - cur_low <= tolerance and best_payload is not None:
break
threshold = round((cur_low + cur_high) / 2.0, 12)
probe = cache.get(threshold)

View File

@@ -4,6 +4,7 @@ import json
import math
import os
import signal
import statistics
import subprocess
import threading
import time
@@ -30,6 +31,55 @@ class ProbePayload:
outcomes: list[dict[str, Any]]
early_stopped: bool = False
early_stop_reason: str = ""
latency_summary: dict[str, Any] | None = None
def _percentile(values: list[float], p: float) -> float | None:
if not values:
return None
ordered = sorted(values)
idx = min(len(ordered) - 1, max(0, math.ceil((p / 100.0) * len(ordered)) - 1))
return float(ordered[idx])
def _metric_summary(values: list[float]) -> dict[str, Any]:
return {
"count": len(values),
"mean": float(statistics.fmean(values)) if values else None,
"p50": _percentile(values, 50.0),
"p90": _percentile(values, 90.0),
"p95": _percentile(values, 95.0),
"p99": _percentile(values, 99.0),
}
def _reason_counts(evaluations: list[Any]) -> dict[str, int]:
counts: dict[str, int] = {}
for evaluation in evaluations:
for reason in evaluation.reasons:
counts[reason] = counts.get(reason, 0) + 1
return counts
def _latency_summary(
*,
outcomes: list[RequestOutcome],
evaluations: list[Any],
study: Any,
) -> dict[str, Any]:
ttft_values = [float(item.ttft_ms) for item in outcomes if item.ttft_ms is not None]
tpot_values = [float(item.tpot_ms) for item in outcomes if item.tpot_ms is not None]
return {
"observed_request_count": len(outcomes),
"ttft_ms": _metric_summary(ttft_values),
"tpot_ms": _metric_summary(tpot_values),
"failed_reason_counts": _reason_counts(evaluations),
"slo": {
"target_pass_rate": study.slo.target_pass_rate,
"ttft_rule": study.slo.ttft_rule.__dict__ if study.slo.ttft_rule is not None else None,
"tpot_rule": study.slo.tpot_rule.__dict__ if study.slo.tpot_rule is not None else None,
},
}
def _trial_spec_from_json(path: Path) -> TrialSpec:
payload = json.loads(path.read_text(encoding="utf-8"))
@@ -299,6 +349,11 @@ def run_trial(trial_spec_path: Path) -> dict[str, Any]:
feasible=bool(summary["feasible"]),
early_stopped=early_stopped,
early_stop_reason=early_stop_reason,
latency_summary=_latency_summary(
outcomes=outcomes,
evaluations=evaluations,
study=study,
),
outcomes=[
{
"request_id": outcome.request_id,
@@ -321,6 +376,7 @@ def run_trial(trial_spec_path: Path) -> dict[str, Any]:
"feasible": payload.feasible,
"early_stopped": payload.early_stopped,
"early_stop_reason": payload.early_stop_reason,
"latency_summary": payload.latency_summary,
}
probe_history.append(probe_record)
StudyStore.write_json(Path(trial.probe_log_path), probe_history)
@@ -356,11 +412,23 @@ def run_trial(trial_spec_path: Path) -> dict[str, Any]:
"request_rate": probe.payload.request_rate,
"early_stopped": probe.payload.early_stopped,
"early_stop_reason": probe.payload.early_stop_reason,
"latency_summary": probe.payload.latency_summary,
},
}
for probe in search.probes
],
}
if best is None and search.probes:
last_probe = search.probes[-1]
result["all_infeasible_diagnostics"] = {
"threshold": last_probe.threshold,
"request_count": last_probe.payload.request_count,
"request_rate": last_probe.payload.request_rate,
"pass_rate": last_probe.payload.pass_rate,
"early_stopped": last_probe.payload.early_stopped,
"early_stop_reason": last_probe.payload.early_stop_reason,
"latency_summary": last_probe.payload.latency_summary,
}
StudyStore.write_json(Path(trial.result_path), result)
return result
except Exception as exc: # noqa: BLE001