forked from TheSeamau5/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into feat/opencl-windows-ci
- Loading branch information
Showing
21 changed files
with
534 additions
and
189 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,30 @@ | ||
//! Tests that are run on every model, regardless of config. | ||
pub(super) fn can_send<M: llm::KnownModel + 'static>(model: M) -> anyhow::Result<M> { | ||
let model = std::thread::spawn(move || model) | ||
.join() | ||
.map_err(|e| anyhow::anyhow!("Failed to join thread: {e:?}")); | ||
|
||
log::info!("`can_send` test passed!"); | ||
|
||
model | ||
} | ||
|
||
pub(super) fn can_roundtrip_hyperparameters<M: llm::KnownModel + 'static>( | ||
model: &M, | ||
) -> anyhow::Result<()> { | ||
fn test_hyperparameters<M: llm::Hyperparameters>(hyperparameters: &M) -> anyhow::Result<()> { | ||
let mut data = vec![]; | ||
hyperparameters.write_ggml(&mut data)?; | ||
let new_hyperparameters = | ||
<M as llm::Hyperparameters>::read_ggml(&mut std::io::Cursor::new(data))?; | ||
|
||
assert_eq!(hyperparameters, &new_hyperparameters); | ||
|
||
log::info!("`can_roundtrip_hyperparameters` test passed!"); | ||
|
||
Ok(()) | ||
} | ||
|
||
test_hyperparameters(model.hyperparameters()) | ||
} |
This file contains 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,95 @@ | ||
//! Tests the model's token manipulation APIs: | ||
//! | ||
//! * [llm::InferenceSession::feed_prompt()] | ||
//! | ||
//! See [crate::TestCase::Tokens]. | ||
use std::convert::Infallible; | ||
|
||
use llm::{InferenceFeedback, InferenceSession, Model, OutputRequest}; | ||
use serde::Serialize; | ||
|
||
use crate::{TestCaseReport, TestCaseReportMeta}; | ||
|
||
/// Tests that models can delete tokens without changing the model's behavior. | ||
pub(crate) fn can_delete(model: &impl Model) -> TestCaseReport { | ||
let report = DeleteReport::default(); | ||
let mut session = model.start_session(Default::default()); | ||
let mut output = OutputRequest { | ||
all_logits: Some(vec![]), | ||
..Default::default() | ||
}; | ||
|
||
// Feed some tokens | ||
if let Err(err) = feed_prompt("The llama lived on the", &mut session, model, &mut output) { | ||
return report.failure(&err.to_string()); | ||
} | ||
|
||
// Add token and get the logits | ||
if let Err(err) = feed_prompt(" ", &mut session, model, &mut output) { | ||
return report.failure(&err.to_string()); | ||
} | ||
let Some(original_logits) = output.all_logits.clone() else { | ||
return report.failure("Model did not return logits."); | ||
}; | ||
|
||
// Rewind, then re-add. Verify logits are the same. | ||
if let Err(err) = session.rewind(model, 1) { | ||
return report.failure(&err.to_string()); | ||
} | ||
if let Err(err) = feed_prompt(" ", &mut session, model, &mut output) { | ||
return report.failure(&err.to_string()); | ||
} | ||
let Some(redone_logits) = output.all_logits.clone() else { | ||
return report.failure("Second run of model did not return logits."); | ||
}; | ||
|
||
// Compare the logits | ||
for (idx, (&original, redone)) in original_logits.iter().zip(redone_logits).enumerate() { | ||
if original > redone + f32::EPSILON || original < redone - f32::EPSILON { | ||
return report.failure(&format!( | ||
"Expected logits to be the same after delete, but differed at {idx}, \ | ||
expected {original}, but was {redone}." | ||
)); | ||
} | ||
} | ||
|
||
log::info!("`can_delete` test passed!"); | ||
report.success() | ||
} | ||
|
||
fn feed_prompt( | ||
prompt: &str, | ||
session: &mut InferenceSession, | ||
model: &impl Model, | ||
output: &mut OutputRequest, | ||
) -> Result<(), llm::InferenceError> { | ||
session.feed_prompt(model, &Default::default(), prompt, output, always_continue) | ||
} | ||
|
||
fn always_continue(_: &[u8]) -> Result<InferenceFeedback, Infallible> { | ||
Ok(InferenceFeedback::Continue) | ||
} | ||
|
||
#[derive(Serialize, Default)] | ||
pub struct DeleteReport { | ||
output: usize, | ||
} | ||
|
||
impl DeleteReport { | ||
fn failure(self, msg: &str) -> TestCaseReport { | ||
TestCaseReport { | ||
meta: TestCaseReportMeta::Error { | ||
error: msg.to_owned(), | ||
}, | ||
report: crate::TestCaseReportInner::Delete(self), | ||
} | ||
} | ||
|
||
fn success(self) -> TestCaseReport { | ||
TestCaseReport { | ||
meta: TestCaseReportMeta::Success, | ||
report: crate::TestCaseReportInner::Delete(self), | ||
} | ||
} | ||
} |
Oops, something went wrong.