-
Notifications
You must be signed in to change notification settings - Fork 3
feat(inference): add benchmark performance guardrails #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TobiBu
merged 4 commits into
feat/posterior-predictive-outputs
from
feat/perf-guardrails
Apr 21, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b5d2a3c
feat(inference): add benchmark performance guardrails
fd1ccc2
feat(inference): add synthetic science recipe workflow and output writer
ae8ee4d
Merge pull request #220 from AstroAI-Lab/feat/science-recipe-workflow
TobiBu 9bffb0e
Changes before error encountered
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Optional | ||
|
|
||
| from .benchmark import IFUCubeBenchmarkResult | ||
| from .vi_benchmark import VIBenchmarkResult | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class RuntimeThresholds: | ||
| """Thresholds for runtime regression checks.""" | ||
|
|
||
| max_mean_runtime_s: Optional[float] = None | ||
| max_median_runtime_s: Optional[float] = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class OptimizationObjectiveThresholds: | ||
| """Thresholds for optimization loss quality checks.""" | ||
|
|
||
| max_final_loss: Optional[float] = None | ||
| max_best_loss: Optional[float] = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class VIObjectiveThresholds: | ||
| """Thresholds for variational inference objective quality checks.""" | ||
|
|
||
| max_final_objective: Optional[float] = None | ||
| max_best_objective: Optional[float] = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class PerformanceCheckResult: | ||
| """Outcome of a performance guardrail check.""" | ||
|
|
||
| passed: bool | ||
| message: str | ||
|
|
||
|
|
||
| def _check_runtime( | ||
| mean_runtime_s: float, | ||
| median_runtime_s: float, | ||
| runtime_thresholds: RuntimeThresholds, | ||
| ) -> list[str]: | ||
| errors: list[str] = [] | ||
| if runtime_thresholds.max_mean_runtime_s is not None: | ||
| if mean_runtime_s > runtime_thresholds.max_mean_runtime_s: | ||
| errors.append( | ||
| f"mean runtime {mean_runtime_s:.6f}s exceeds " | ||
| f"threshold {runtime_thresholds.max_mean_runtime_s:.6f}s" | ||
| ) | ||
|
|
||
| if runtime_thresholds.max_median_runtime_s is not None: | ||
| if median_runtime_s > runtime_thresholds.max_median_runtime_s: | ||
| errors.append( | ||
| f"median runtime {median_runtime_s:.6f}s exceeds " | ||
| f"threshold {runtime_thresholds.max_median_runtime_s:.6f}s" | ||
| ) | ||
|
|
||
| return errors | ||
|
|
||
|
|
||
| def check_ifu_optimization_guardrails( | ||
| result: IFUCubeBenchmarkResult, | ||
| runtime_thresholds: RuntimeThresholds, | ||
| objective_thresholds: OptimizationObjectiveThresholds, | ||
| ) -> PerformanceCheckResult: | ||
| """Check optimization benchmark result against runtime/objective thresholds. | ||
|
|
||
| Args: | ||
| result (IFUCubeBenchmarkResult): Optimization benchmark result. | ||
| runtime_thresholds (RuntimeThresholds): Runtime limits. | ||
| objective_thresholds (OptimizationObjectiveThresholds): Loss limits. | ||
|
|
||
| Returns: | ||
| PerformanceCheckResult: Pass/fail status and explanatory message. | ||
| """ | ||
| errors = _check_runtime( | ||
| result.mean_runtime_s, | ||
| result.median_runtime_s, | ||
| runtime_thresholds, | ||
| ) | ||
|
|
||
| if objective_thresholds.max_final_loss is not None: | ||
| if result.final_loss > objective_thresholds.max_final_loss: | ||
| errors.append( | ||
| f"final loss {result.final_loss:.6e} exceeds " | ||
| f"threshold {objective_thresholds.max_final_loss:.6e}" | ||
| ) | ||
|
|
||
| if objective_thresholds.max_best_loss is not None: | ||
| if result.best_loss > objective_thresholds.max_best_loss: | ||
| errors.append( | ||
| f"best loss {result.best_loss:.6e} exceeds " | ||
| f"threshold {objective_thresholds.max_best_loss:.6e}" | ||
| ) | ||
|
|
||
| if errors: | ||
| return PerformanceCheckResult( | ||
| passed=False, | ||
| message="; ".join(errors), | ||
| ) | ||
|
|
||
| return PerformanceCheckResult( | ||
| passed=True, | ||
| message="optimization benchmark satisfies configured thresholds", | ||
| ) | ||
|
|
||
|
|
||
| def check_vi_guardrails( | ||
| result: VIBenchmarkResult, | ||
| runtime_thresholds: RuntimeThresholds, | ||
| objective_thresholds: VIObjectiveThresholds, | ||
| ) -> PerformanceCheckResult: | ||
| """Check VI benchmark result against runtime/objective thresholds. | ||
|
|
||
| Args: | ||
| result (VIBenchmarkResult): VI benchmark result. | ||
| runtime_thresholds (RuntimeThresholds): Runtime limits. | ||
| objective_thresholds (VIObjectiveThresholds): Objective limits. | ||
|
|
||
| Returns: | ||
| PerformanceCheckResult: Pass/fail status and explanatory message. | ||
| """ | ||
| errors = _check_runtime( | ||
| result.mean_runtime_s, | ||
| result.median_runtime_s, | ||
| runtime_thresholds, | ||
| ) | ||
|
|
||
| if objective_thresholds.max_final_objective is not None: | ||
| if result.final_objective > objective_thresholds.max_final_objective: | ||
| errors.append( | ||
| f"final objective {result.final_objective:.6e} exceeds " | ||
| f"threshold {objective_thresholds.max_final_objective:.6e}" | ||
| ) | ||
|
|
||
| if objective_thresholds.max_best_objective is not None: | ||
| if result.best_objective > objective_thresholds.max_best_objective: | ||
| errors.append( | ||
| f"best objective {result.best_objective:.6e} exceeds " | ||
| f"threshold {objective_thresholds.max_best_objective:.6e}" | ||
| ) | ||
|
|
||
| if errors: | ||
| return PerformanceCheckResult( | ||
| passed=False, | ||
| message="; ".join(errors), | ||
| ) | ||
|
|
||
| return PerformanceCheckResult( | ||
| passed=True, | ||
| message="VI benchmark satisfies configured thresholds", | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ObjectiveThresholds mixes optimization loss thresholds (max_final_loss/max_best_loss) with VI objective thresholds (max_final_objective/max_best_objective), but each guardrail checker only evaluates a subset. As written, setting the “wrong” fields for a given checker will be silently ignored and can incorrectly report passed=True. Consider splitting this into two threshold dataclasses (optimization vs VI), or add explicit validation in check_ifu_optimization_guardrails/check_vi_guardrails to raise a ValueError when irrelevant threshold fields are set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot apply changes based on this feedback