forked from rustformers/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.
Note that llama.json fails the tests, so it's likely it doesn't support it. I may investigate further, though.
- Loading branch information
1 parent
b2238c2
commit 2e35b46
Showing
17 changed files
with
221 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
"input": "Rustformers is", | ||
"output": 15 | ||
} | ||
}, | ||
{ | ||
"Delete": {} | ||
} | ||
] | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
"input": "Rustformers is", | ||
"output": 257 | ||
} | ||
}, | ||
{ | ||
"Delete": {} | ||
} | ||
] | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
"input": "Rustformers is", | ||
"output": 247 | ||
} | ||
}, | ||
{ | ||
"Delete": {} | ||
} | ||
] | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
"input": "Rustformers is", | ||
"output": 260 | ||
} | ||
}, | ||
{ | ||
"Delete": {} | ||
} | ||
] | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
"input": "Rustformers is", | ||
"output": 247 | ||
} | ||
}, | ||
{ | ||
"Delete": {} | ||
} | ||
] | ||
} |
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,100 @@ | ||
//! 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}; | ||
|
||
/// Error tolerance for the float comparisons. | ||
const TOLERANCE: f32 = 1e-7; | ||
|
||
/// 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."); | ||
}; | ||
|
||
// Delete, then re-add. Verify logits are the same. | ||
if let Err(err) = session.delete_tokens(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 + TOLERANCE || original < redone - TOLERANCE { | ||
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 (no expected output)!"); | ||
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, |x| { | ||
always_continue(x) | ||
}) | ||
} | ||
|
||
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), | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.