75 lines
3.6 KiB
Python
75 lines
3.6 KiB
Python
"""Bound unproven sibling routes while awaiting actual validation results."""
|
|
from collections import Counter
|
|
from typing import Any
|
|
|
|
|
|
def profile_key(row: dict[str, Any]) -> tuple[str, ...] | None:
|
|
profile = row.get("preflightMetadata") or row.get("modelProfile") or {}
|
|
model_type = str(profile.get("modelType") or "").strip()
|
|
if not model_type:
|
|
return None
|
|
return (str(row.get("targetGpu") or row.get("gpuType") or ""),
|
|
str(row.get("framework") or ""), str(row.get("taskType") or ""),
|
|
model_type, str(profile.get("quantizationSignature") or profile.get("quantizationMethod") or "none"))
|
|
|
|
|
|
def profile_budget(candidate, stats, *, limit=2):
|
|
"""Expand useful public-evidence routes without requiring local exploration.
|
|
|
|
Broad evidence earns modest concurrency, not unlimited profile trust.
|
|
Specific negative evidence prevents that expansion.
|
|
"""
|
|
key = profile_key(candidate)
|
|
evidence = (stats.get("encodingCombinationStats") or {}).get("|".join(key or ())) or {}
|
|
successes = int(evidence.get("successCount") or 0)
|
|
failures = max(0, int(evidence.get("failureCount") or 0) - int(evidence.get("platformFailureCount") or 0))
|
|
if successes >= 3 and successes / max(1, successes + failures) >= 0.5:
|
|
return None, "proven_encoding"
|
|
profile = candidate.get("preflightMetadata") or {}
|
|
family = "|".join((*key[:4], str(profile.get("quantizationMethod") or "none"))) if key else ""
|
|
broad = (stats.get("profileCombinationStats") or {}).get(family) or {}
|
|
negative = failures > successes or int(broad.get("failureCount") or 0) > int(broad.get("successCount") or 0)
|
|
public_strong = (candidate.get("frameworkEvidenceQualified")
|
|
and candidate.get("frameworkOfficialConfigValid")
|
|
and not candidate.get("frameworkCircuitOpen")
|
|
and float(candidate.get("frameworkMarketWilsonLowerBound") or 0) >= 0.60
|
|
and int(candidate.get("frameworkMarketSamples") or 0) >= 300)
|
|
if public_strong and not negative:
|
|
return max(limit, 4), "strong_public_evidence"
|
|
return limit, "limited_profile_evidence"
|
|
|
|
|
|
def filter_profile_batch(candidates, pending, stats, *, limit=2, max_selected=None):
|
|
"""Reservations include the whole proposed batch; deferred rows are retryable.
|
|
|
|
Public evidence can expand the cap modestly; exact-encoding successes can
|
|
lift it. Model alternatives are considered only until a route is admitted.
|
|
Reservations are made only for the requested batch, not the whole pool.
|
|
"""
|
|
counts = Counter(profile_key(row) for row in pending if row.get("outcome") == "pending")
|
|
selected, deferred = [], []
|
|
selected_models = set()
|
|
for candidate in candidates:
|
|
if max_selected is not None and len(selected) >= max_selected:
|
|
break
|
|
model = candidate.get("repoId") or candidate.get("modelAddress")
|
|
if model and model in selected_models:
|
|
continue
|
|
key = profile_key(candidate)
|
|
budget, reason = profile_budget(candidate, stats, limit=limit)
|
|
candidate["profileAdmissionBudget"] = budget
|
|
candidate["profileAdmissionReason"] = reason
|
|
# Count legacy records with the same method as well, conservatively.
|
|
legacy = 0
|
|
if key and ":" in key[-1]:
|
|
legacy = counts.get((*key[:-1], key[-1].split(":", 1)[0]), 0)
|
|
if key and budget is not None and counts[key] + legacy >= budget:
|
|
deferred.append(candidate)
|
|
continue
|
|
selected.append(candidate)
|
|
if model:
|
|
selected_models.add(model)
|
|
if key:
|
|
counts[key] += 1
|
|
return selected, deferred
|