-
Notifications
You must be signed in to change notification settings - Fork 0
feat(model): statistical Pareto K gates refuse LLM numerical authority #67
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56950cf
feat(model): statistical Pareto K gates refuse LLM numerical authority
seonghobae 7ff6ae8
Merge remote-tracking branch 'origin/main' into integrate-pr67-main
seonghobae d735177
test(model-selection): complete pareto gate coverage
seonghobae 191f14b
Merge origin/main into PR #67
seonghobae b3854db
test(model-selection): validate repeated truth recovery
seonghobae 45b272d
fix(model-selection): validate llm candidate K
seonghobae afdb0a2
Merge remote-tracking branch 'origin/main' into agent/model-selection…
seonghobae 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
| [package] | ||
| name = "model_selection" | ||
| description = "Statistical and Pareto candidate-K gates that refuse LLM numerical authority." | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| authors.workspace = true | ||
| repository.workspace = true | ||
| homepage.workspace = true | ||
| readme.workspace = true | ||
| keywords.workspace = true | ||
| categories.workspace = true | ||
| publish = false | ||
|
|
||
| [lints] | ||
| workspace = true |
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,156 @@ | ||
| //! Candidate topic counts with statistical diagnostics. | ||
|
|
||
| use crate::ModelSelectionError; | ||
|
|
||
| /// One candidate `K` together with the diagnostics that may admit it. | ||
| #[derive(Clone, Copy, Debug, PartialEq)] | ||
| pub struct ModelCandidate { | ||
| candidate_k: u32, | ||
| held_out_log_likelihood: Option<f64>, | ||
| complexity: Option<f64>, | ||
| llm_vote_only: bool, | ||
| } | ||
|
|
||
| impl ModelCandidate { | ||
| /// Construct a statistically supported candidate. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`ModelSelectionError::NonPositiveCandidateK`] when `candidate_k` | ||
| /// is less than two, or [`ModelSelectionError::InvalidDiagnostic`] when a | ||
| /// diagnostic is non-finite. | ||
| pub fn statistical( | ||
| candidate_k: u32, | ||
| held_out_log_likelihood: f64, | ||
| complexity: f64, | ||
| ) -> Result<Self, ModelSelectionError> { | ||
| if candidate_k < 2 { | ||
| return Err(ModelSelectionError::NonPositiveCandidateK); | ||
| } | ||
| if !held_out_log_likelihood.is_finite() || !complexity.is_finite() || complexity < 0.0 { | ||
| return Err(ModelSelectionError::InvalidDiagnostic); | ||
| } | ||
| Ok(Self { | ||
| candidate_k, | ||
| held_out_log_likelihood: Some(held_out_log_likelihood), | ||
| complexity: Some(complexity), | ||
| llm_vote_only: false, | ||
| }) | ||
| } | ||
|
|
||
| /// Construct a candidate whose only support is an LLM vote. | ||
| /// | ||
| /// The vote may later recommend among statistically admissible candidates. | ||
| /// It cannot itself define the numerical optimum. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`ModelSelectionError::NonPositiveCandidateK`] when `candidate_k` | ||
| /// is less than two. | ||
| pub fn llm_vote_only(candidate_k: u32) -> Result<Self, ModelSelectionError> { | ||
| if candidate_k < 2 { | ||
| return Err(ModelSelectionError::NonPositiveCandidateK); | ||
| } | ||
| Ok(Self { | ||
| candidate_k, | ||
| held_out_log_likelihood: None, | ||
| complexity: None, | ||
| llm_vote_only: true, | ||
| }) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /// Return the candidate topic count. | ||
| #[must_use] | ||
| pub const fn candidate_k(self) -> u32 { | ||
| self.candidate_k | ||
| } | ||
|
|
||
| /// Return whether this candidate carries finite statistical diagnostics. | ||
| #[must_use] | ||
| pub const fn is_statistically_supported(self) -> bool { | ||
| match (self.held_out_log_likelihood, self.complexity) { | ||
| (Some(_), Some(_)) => !self.llm_vote_only, | ||
| _ => false, | ||
| } | ||
| } | ||
|
|
||
| /// Held-out log-likelihood when the candidate is statistically supported. | ||
| #[must_use] | ||
| pub const fn held_out_log_likelihood(self) -> Option<f64> { | ||
| self.held_out_log_likelihood | ||
| } | ||
|
|
||
| /// Complexity penalty (larger is worse) when statistically supported. | ||
| #[must_use] | ||
| pub const fn complexity(self) -> Option<f64> { | ||
| self.complexity | ||
| } | ||
|
|
||
| /// Return whether the candidate is an LLM vote without statistical support. | ||
| #[must_use] | ||
| pub const fn is_llm_vote_only(self) -> bool { | ||
| self.llm_vote_only | ||
| } | ||
|
|
||
| /// Return whether `self` Pareto-dominates `other` on likelihood and complexity. | ||
| #[must_use] | ||
| pub fn dominates(self, other: Self) -> bool { | ||
| let (Some(self_ll), Some(self_complexity), Some(other_ll), Some(other_complexity)) = ( | ||
| self.held_out_log_likelihood, | ||
| self.complexity, | ||
| other.held_out_log_likelihood, | ||
| other.complexity, | ||
| ) else { | ||
| return false; | ||
| }; | ||
| let no_worse = self_ll >= other_ll && self_complexity <= other_complexity; | ||
| let strictly_better = self_ll > other_ll || self_complexity < other_complexity; | ||
| no_worse && strictly_better | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::ModelCandidate; | ||
| use crate::ModelSelectionError; | ||
|
|
||
| #[test] | ||
| fn statistical_candidate_accessors_and_dominance_cover_branches() { | ||
| let better = ModelCandidate::statistical(4, -10.0, 5.0).expect("better"); | ||
| let worse = ModelCandidate::statistical(8, -20.0, 9.0).expect("worse"); | ||
| assert_eq!(better.candidate_k(), 4); | ||
| assert_eq!(better.held_out_log_likelihood(), Some(-10.0)); | ||
| assert_eq!(better.complexity(), Some(5.0)); | ||
| assert!(better.is_statistically_supported()); | ||
| assert!(!better.is_llm_vote_only()); | ||
| assert!(better.dominates(worse)); | ||
| assert!(!worse.dominates(better)); | ||
| assert!(!better.dominates(better)); | ||
|
|
||
| let llm = ModelCandidate::llm_vote_only(3).expect("valid llm candidate"); | ||
| assert!(llm.is_llm_vote_only()); | ||
| assert!(!llm.is_statistically_supported()); | ||
| assert!(!llm.dominates(better)); | ||
| assert!(!better.dominates(llm)); | ||
| assert_eq!( | ||
| ModelCandidate::statistical(0, -1.0, 1.0), | ||
| Err(ModelSelectionError::NonPositiveCandidateK) | ||
| ); | ||
| assert_eq!( | ||
| ModelCandidate::statistical(2, -1.0, -0.1), | ||
| Err(ModelSelectionError::InvalidDiagnostic) | ||
| ); | ||
| assert_eq!( | ||
| ModelCandidate::statistical(2, f64::NAN, 1.0), | ||
| Err(ModelSelectionError::InvalidDiagnostic) | ||
| ); | ||
| assert_eq!( | ||
| ModelCandidate::statistical(2, -1.0, f64::INFINITY), | ||
| Err(ModelSelectionError::InvalidDiagnostic) | ||
| ); | ||
| assert_eq!( | ||
| ModelCandidate::llm_vote_only(1), | ||
| Err(ModelSelectionError::NonPositiveCandidateK) | ||
| ); | ||
| } | ||
| } | ||
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,60 @@ | ||
| //! Fail-closed model-selection errors. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A fail-closed model-selection error. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| #[non_exhaustive] | ||
| pub enum ModelSelectionError { | ||
| /// Candidate `K` was less than two. | ||
| NonPositiveCandidateK, | ||
|
seonghobae marked this conversation as resolved.
|
||
| /// A diagnostic was non-finite or otherwise unusable. | ||
| InvalidDiagnostic, | ||
| /// No candidates were supplied. | ||
| EmptyCandidateSet, | ||
| /// An LLM vote was asked to define the numerical optimum. | ||
| LlmVoteIsNotStatisticalAuthority, | ||
| } | ||
|
|
||
| impl fmt::Display for ModelSelectionError { | ||
| fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let message = match self { | ||
| Self::NonPositiveCandidateK => "candidate k must be at least two", | ||
| Self::InvalidDiagnostic => "invalid model-selection diagnostic", | ||
| Self::EmptyCandidateSet => "empty model-selection candidate set", | ||
| Self::LlmVoteIsNotStatisticalAuthority => "llm vote is not statistical authority", | ||
| }; | ||
| formatter.write_str(message) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for ModelSelectionError {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::ModelSelectionError; | ||
|
|
||
| #[test] | ||
| fn error_messages_are_stable() { | ||
| for (error, message) in [ | ||
| ( | ||
| ModelSelectionError::NonPositiveCandidateK, | ||
| "candidate k must be at least two", | ||
| ), | ||
| ( | ||
| ModelSelectionError::InvalidDiagnostic, | ||
| "invalid model-selection diagnostic", | ||
| ), | ||
| ( | ||
| ModelSelectionError::EmptyCandidateSet, | ||
| "empty model-selection candidate set", | ||
| ), | ||
| ( | ||
| ModelSelectionError::LlmVoteIsNotStatisticalAuthority, | ||
| "llm vote is not statistical authority", | ||
| ), | ||
| ] { | ||
| assert_eq!(error.to_string(), message); | ||
| } | ||
| } | ||
| } | ||
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.
📝 Info: README prose still says eleven crates
README.md still reads "The eleven bounded crates" while the block below and the workspace now list 12 crates after
model_selectionwas added. Stale prose.(Refers to this code)
Was this helpful? React with 👍 or 👎 to provide feedback.