Update rollout metrics scripts and HF sync helpers
Browse files
workspace/scripts/build_ctt_rollout_comparison.py
CHANGED
|
@@ -17,6 +17,7 @@ if str(PROJECT_ROOT) not in sys.path:
|
|
| 17 |
|
| 18 |
from cil.metrics import ( # noqa: E402
|
| 19 |
any_unsafe,
|
|
|
|
| 20 |
outcome_safety_violation,
|
| 21 |
safety_label_coverage,
|
| 22 |
unsafe_rate,
|
|
@@ -161,6 +162,10 @@ def _report(combined: dict[str, Any], metrics: dict[str, Any]) -> str:
|
|
| 161 |
f"| selected_utility_mean | {_fmt(success.get('selected_utility_mean'))} |",
|
| 162 |
f"| proposal_oracle_utility_mean | {_fmt(success.get('proposal_oracle_utility_mean'))} |",
|
| 163 |
f"| hidden_chart_oracle_utility_mean | {_fmt(success.get('hidden_chart_oracle_utility_mean'))} |",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
f"| generated_safety_label_coverage | {_fmt(success.get('generated_safety_label_coverage'))} |",
|
| 165 |
f"| generated_unsafe_rate_known | {_fmt(success.get('generated_unsafe_rate_known'))} |",
|
| 166 |
f"| any_generated_unsafe_known | {_fmt(success.get('any_generated_unsafe_known'))} |",
|
|
@@ -212,6 +217,10 @@ def _success_summary(rows: list[dict[str, Any]], *, k: int) -> dict[str, Any]:
|
|
| 212 |
oracle_utility = []
|
| 213 |
hidden_oracle_utility = []
|
| 214 |
hidden_oracle_success = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
success_support_gap = []
|
| 216 |
success_selector_gap = []
|
| 217 |
selected_success_gain = []
|
|
@@ -273,14 +282,56 @@ def _success_summary(rows: list[dict[str, Any]], *, k: int) -> dict[str, Any]:
|
|
| 273 |
proposal_oracle_success_value = float(any(generated_success))
|
| 274 |
oracle_success.append(proposal_oracle_success_value)
|
| 275 |
if "base_utility" in row:
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
| 277 |
if selected_index < len(generated_utilities):
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
| 279 |
if generated_utilities:
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
hidden = [float(value) for value in row.get("hidden_chart_utilities", [])]
|
| 282 |
if hidden:
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
hidden_success_value = float(any(value >= 1.0 for value in hidden))
|
| 285 |
hidden_oracle_success.append(hidden_success_value)
|
| 286 |
if proposal_oracle_success_value is not None:
|
|
@@ -315,6 +366,10 @@ def _success_summary(rows: list[dict[str, Any]], *, k: int) -> dict[str, Any]:
|
|
| 315 |
"selected_utility_mean": _mean(selected_utility),
|
| 316 |
"proposal_oracle_utility_mean": _mean(oracle_utility),
|
| 317 |
"hidden_chart_oracle_utility_mean": _mean(hidden_oracle_utility),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
"generated_safety_label_coverage": _mean(generated_safety_coverage),
|
| 319 |
"generated_unsafe_rate_known": _mean(generated_unsafe),
|
| 320 |
"any_generated_unsafe_known": _mean(any_generated_unsafe),
|
|
@@ -339,6 +394,25 @@ def _mean(values: list[float]) -> float | None:
|
|
| 339 |
return sum(clean) / len(clean) if clean else None
|
| 340 |
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
def _fmt(value: Any) -> str:
|
| 343 |
if not isinstance(value, (int, float)) or not math.isfinite(float(value)):
|
| 344 |
return "n/a"
|
|
|
|
| 17 |
|
| 18 |
from cil.metrics import ( # noqa: E402
|
| 19 |
any_unsafe,
|
| 20 |
+
normalized_causal_action_regret,
|
| 21 |
outcome_safety_violation,
|
| 22 |
safety_label_coverage,
|
| 23 |
unsafe_rate,
|
|
|
|
| 162 |
f"| selected_utility_mean | {_fmt(success.get('selected_utility_mean'))} |",
|
| 163 |
f"| proposal_oracle_utility_mean | {_fmt(success.get('proposal_oracle_utility_mean'))} |",
|
| 164 |
f"| hidden_chart_oracle_utility_mean | {_fmt(success.get('hidden_chart_oracle_utility_mean'))} |",
|
| 165 |
+
f"| ncar_to_proposal_oracle | {_fmt(success.get('ncar_to_proposal_oracle'))} |",
|
| 166 |
+
f"| ncar_to_hidden_chart_oracle | {_fmt(success.get('ncar_to_hidden_chart_oracle'))} |",
|
| 167 |
+
f"| utility_support_gap_fraction_to_hidden | {_fmt(success.get('utility_support_gap_fraction_to_hidden'))} |",
|
| 168 |
+
f"| utility_selector_gap_fraction_to_hidden | {_fmt(success.get('utility_selector_gap_fraction_to_hidden'))} |",
|
| 169 |
f"| generated_safety_label_coverage | {_fmt(success.get('generated_safety_label_coverage'))} |",
|
| 170 |
f"| generated_unsafe_rate_known | {_fmt(success.get('generated_unsafe_rate_known'))} |",
|
| 171 |
f"| any_generated_unsafe_known | {_fmt(success.get('any_generated_unsafe_known'))} |",
|
|
|
|
| 217 |
oracle_utility = []
|
| 218 |
hidden_oracle_utility = []
|
| 219 |
hidden_oracle_success = []
|
| 220 |
+
ncar_to_proposal_oracle = []
|
| 221 |
+
ncar_to_hidden_chart_oracle = []
|
| 222 |
+
utility_support_gap_fraction = []
|
| 223 |
+
utility_selector_gap_fraction = []
|
| 224 |
success_support_gap = []
|
| 225 |
success_selector_gap = []
|
| 226 |
selected_success_gain = []
|
|
|
|
| 282 |
proposal_oracle_success_value = float(any(generated_success))
|
| 283 |
oracle_success.append(proposal_oracle_success_value)
|
| 284 |
if "base_utility" in row:
|
| 285 |
+
base_utility_value = float(row["base_utility"])
|
| 286 |
+
base_utility.append(base_utility_value)
|
| 287 |
+
else:
|
| 288 |
+
base_utility_value = None
|
| 289 |
if selected_index < len(generated_utilities):
|
| 290 |
+
selected_utility_value = generated_utilities[selected_index]
|
| 291 |
+
selected_utility.append(selected_utility_value)
|
| 292 |
+
else:
|
| 293 |
+
selected_utility_value = None
|
| 294 |
if generated_utilities:
|
| 295 |
+
proposal_oracle_utility_value = max(generated_utilities)
|
| 296 |
+
oracle_utility.append(proposal_oracle_utility_value)
|
| 297 |
+
else:
|
| 298 |
+
proposal_oracle_utility_value = None
|
| 299 |
+
if (
|
| 300 |
+
base_utility_value is not None
|
| 301 |
+
and selected_utility_value is not None
|
| 302 |
+
and proposal_oracle_utility_value is not None
|
| 303 |
+
):
|
| 304 |
+
_append_stable_ncar(
|
| 305 |
+
ncar_to_proposal_oracle,
|
| 306 |
+
proposal_oracle_utility_value,
|
| 307 |
+
selected_utility_value,
|
| 308 |
+
base_utility_value,
|
| 309 |
+
)
|
| 310 |
hidden = [float(value) for value in row.get("hidden_chart_utilities", [])]
|
| 311 |
if hidden:
|
| 312 |
+
hidden_oracle_utility_value = max(hidden)
|
| 313 |
+
hidden_oracle_utility.append(hidden_oracle_utility_value)
|
| 314 |
+
if (
|
| 315 |
+
base_utility_value is not None
|
| 316 |
+
and selected_utility_value is not None
|
| 317 |
+
and proposal_oracle_utility_value is not None
|
| 318 |
+
):
|
| 319 |
+
_append_stable_ncar(
|
| 320 |
+
ncar_to_hidden_chart_oracle,
|
| 321 |
+
hidden_oracle_utility_value,
|
| 322 |
+
selected_utility_value,
|
| 323 |
+
base_utility_value,
|
| 324 |
+
)
|
| 325 |
+
hidden_gap = abs(hidden_oracle_utility_value - base_utility_value)
|
| 326 |
+
if hidden_gap > 0.0:
|
| 327 |
+
utility_support_gap_fraction.append(
|
| 328 |
+
max(0.0, hidden_oracle_utility_value - proposal_oracle_utility_value)
|
| 329 |
+
/ hidden_gap
|
| 330 |
+
)
|
| 331 |
+
utility_selector_gap_fraction.append(
|
| 332 |
+
max(0.0, proposal_oracle_utility_value - selected_utility_value)
|
| 333 |
+
/ hidden_gap
|
| 334 |
+
)
|
| 335 |
hidden_success_value = float(any(value >= 1.0 for value in hidden))
|
| 336 |
hidden_oracle_success.append(hidden_success_value)
|
| 337 |
if proposal_oracle_success_value is not None:
|
|
|
|
| 366 |
"selected_utility_mean": _mean(selected_utility),
|
| 367 |
"proposal_oracle_utility_mean": _mean(oracle_utility),
|
| 368 |
"hidden_chart_oracle_utility_mean": _mean(hidden_oracle_utility),
|
| 369 |
+
"ncar_to_proposal_oracle": _mean(ncar_to_proposal_oracle),
|
| 370 |
+
"ncar_to_hidden_chart_oracle": _mean(ncar_to_hidden_chart_oracle),
|
| 371 |
+
"utility_support_gap_fraction_to_hidden": _mean(utility_support_gap_fraction),
|
| 372 |
+
"utility_selector_gap_fraction_to_hidden": _mean(utility_selector_gap_fraction),
|
| 373 |
"generated_safety_label_coverage": _mean(generated_safety_coverage),
|
| 374 |
"generated_unsafe_rate_known": _mean(generated_unsafe),
|
| 375 |
"any_generated_unsafe_known": _mean(any_generated_unsafe),
|
|
|
|
| 394 |
return sum(clean) / len(clean) if clean else None
|
| 395 |
|
| 396 |
|
| 397 |
+
def _append_stable_ncar(
|
| 398 |
+
output: list[float],
|
| 399 |
+
oracle_utility: float,
|
| 400 |
+
selected_utility: float,
|
| 401 |
+
base_utility: float,
|
| 402 |
+
*,
|
| 403 |
+
min_denominator: float = 1.0e-3,
|
| 404 |
+
) -> None:
|
| 405 |
+
if abs(float(oracle_utility) - float(base_utility)) <= min_denominator:
|
| 406 |
+
return
|
| 407 |
+
output.append(
|
| 408 |
+
normalized_causal_action_regret(
|
| 409 |
+
oracle_utility,
|
| 410 |
+
selected_utility,
|
| 411 |
+
base_utility,
|
| 412 |
+
)
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
def _fmt(value: Any) -> str:
|
| 417 |
if not isinstance(value, (int, float)) or not math.isfinite(float(value)):
|
| 418 |
return "n/a"
|
workspace/scripts/eval_metrics.py
CHANGED
|
@@ -23,6 +23,7 @@ from cil.metrics import ( # noqa: E402
|
|
| 23 |
mean_nearest_distance_to_set,
|
| 24 |
measured_support_gap,
|
| 25 |
negative_near_at_threshold,
|
|
|
|
| 26 |
outcome_ptr_at_k,
|
| 27 |
pairwise_causal_dominance_ece,
|
| 28 |
positives_closer_than_negatives,
|
|
@@ -156,6 +157,13 @@ def _measured_row(row: dict[str, Any], *, k: int, epsilon: float) -> dict[str, A
|
|
| 156 |
candidates_evaluated=True,
|
| 157 |
)
|
| 158 |
output[f"branch_car_at_{k}"] = branch_car(max(prefix), selected_utility)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
output["base_utility"] = base_utility
|
| 160 |
output[f"selected_utility_at_{k}"] = selected_utility
|
| 161 |
output[f"proposal_oracle_utility_at_{k}"] = proposal_oracle_utility
|
|
@@ -230,6 +238,21 @@ def _measured_row(row: dict[str, Any], *, k: int, epsilon: float) -> dict[str, A
|
|
| 230 |
hidden_oracle_utility,
|
| 231 |
selected_utility,
|
| 232 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
if candidate_success:
|
| 234 |
hidden_oracle_success = float(any(value >= 1.0 for value in hidden))
|
| 235 |
output[f"hidden_chart_oracle_success_at_{k}"] = hidden_oracle_success
|
|
@@ -332,6 +355,24 @@ def _optional_bool(value: Any) -> bool | None:
|
|
| 332 |
return bool(value)
|
| 333 |
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
def _matrix(row: dict[str, Any], key: str, *, required: bool = True) -> list[list[float]]:
|
| 336 |
values = row.get(key)
|
| 337 |
if values is None:
|
|
|
|
| 23 |
mean_nearest_distance_to_set,
|
| 24 |
measured_support_gap,
|
| 25 |
negative_near_at_threshold,
|
| 26 |
+
normalized_causal_action_regret,
|
| 27 |
outcome_ptr_at_k,
|
| 28 |
pairwise_causal_dominance_ece,
|
| 29 |
positives_closer_than_negatives,
|
|
|
|
| 157 |
candidates_evaluated=True,
|
| 158 |
)
|
| 159 |
output[f"branch_car_at_{k}"] = branch_car(max(prefix), selected_utility)
|
| 160 |
+
ncar_to_proposal = _stable_ncar(
|
| 161 |
+
proposal_oracle_utility,
|
| 162 |
+
selected_utility,
|
| 163 |
+
base_utility,
|
| 164 |
+
)
|
| 165 |
+
if ncar_to_proposal is not None:
|
| 166 |
+
output[f"ncar_to_proposal_oracle_at_{k}"] = ncar_to_proposal
|
| 167 |
output["base_utility"] = base_utility
|
| 168 |
output[f"selected_utility_at_{k}"] = selected_utility
|
| 169 |
output[f"proposal_oracle_utility_at_{k}"] = proposal_oracle_utility
|
|
|
|
| 238 |
hidden_oracle_utility,
|
| 239 |
selected_utility,
|
| 240 |
)
|
| 241 |
+
ncar_to_hidden = _stable_ncar(
|
| 242 |
+
hidden_oracle_utility,
|
| 243 |
+
selected_utility,
|
| 244 |
+
base_utility,
|
| 245 |
+
)
|
| 246 |
+
if ncar_to_hidden is not None:
|
| 247 |
+
output[f"ncar_to_hidden_chart_oracle_at_{k}"] = ncar_to_hidden
|
| 248 |
+
hidden_gap = abs(hidden_oracle_utility - base_utility)
|
| 249 |
+
if hidden_gap > 0.0:
|
| 250 |
+
output[f"support_gap_fraction_to_hidden_at_{k}"] = (
|
| 251 |
+
output[f"support_gap_at_{k}"] / hidden_gap
|
| 252 |
+
)
|
| 253 |
+
output[f"selector_gap_fraction_to_hidden_at_{k}"] = (
|
| 254 |
+
output[f"selector_regret_at_{k}"] / hidden_gap
|
| 255 |
+
)
|
| 256 |
if candidate_success:
|
| 257 |
hidden_oracle_success = float(any(value >= 1.0 for value in hidden))
|
| 258 |
output[f"hidden_chart_oracle_success_at_{k}"] = hidden_oracle_success
|
|
|
|
| 355 |
return bool(value)
|
| 356 |
|
| 357 |
|
| 358 |
+
def _stable_ncar(
|
| 359 |
+
oracle_utility: float,
|
| 360 |
+
selected_utility: float,
|
| 361 |
+
base_utility: float,
|
| 362 |
+
*,
|
| 363 |
+
min_denominator: float = 1.0e-3,
|
| 364 |
+
) -> float | None:
|
| 365 |
+
"""Return NCAR only when the base-to-oracle gap is numerically meaningful."""
|
| 366 |
+
|
| 367 |
+
if abs(float(oracle_utility) - float(base_utility)) <= min_denominator:
|
| 368 |
+
return None
|
| 369 |
+
return normalized_causal_action_regret(
|
| 370 |
+
oracle_utility,
|
| 371 |
+
selected_utility,
|
| 372 |
+
base_utility,
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
|
| 376 |
def _matrix(row: dict[str, Any], key: str, *, required: bool = True) -> list[list[float]]:
|
| 377 |
values = row.get(key)
|
| 378 |
if values is None:
|