-
-
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.
- Loading branch information
Showing
7 changed files
with
149 additions
and
162 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
use super::{TextGeneratorResult, TextGeneratorTrait}; | ||
use anyhow::Result; | ||
|
||
pub struct DummyTextGenerator { | ||
text: String, | ||
} | ||
|
||
impl DummyTextGenerator { | ||
pub fn new(text: String) -> Self { | ||
Self { text } | ||
} | ||
} | ||
|
||
impl TextGeneratorTrait for DummyTextGenerator { | ||
fn init(&mut self, prompt: String) -> Result<()> { | ||
self.text = prompt; | ||
Ok(()) | ||
} | ||
|
||
fn next(&mut self) -> Result<TextGeneratorResult> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests {} |
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,97 @@ | ||
use super::{ | ||
token_generator::{TokenGeneratorResult, TokenGeneratorTrait}, | ||
token_output_stream::TokenOutputStream, | ||
FinishReason, | ||
}; | ||
use anyhow::Result; | ||
mod dummy_text_generator; | ||
|
||
pub type TextProbability = (String, f32); | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum TextGeneratorResult { | ||
Token(TextProbability), | ||
Finish(FinishReason), | ||
} | ||
|
||
/// Trait for text generation functionality. | ||
pub trait TextGeneratorTrait { | ||
fn init(&mut self, prompt: String) -> Result<()>; | ||
fn next(&mut self) -> Result<TextGeneratorResult>; | ||
} | ||
|
||
pub struct TextGenerator { | ||
tokenizer: TokenOutputStream, | ||
token_generator: Box<dyn TokenGeneratorTrait>, | ||
} | ||
|
||
impl TextGenerator { | ||
pub fn new( | ||
tokenizer: TokenOutputStream, | ||
token_generator: Box<dyn TokenGeneratorTrait>, | ||
) -> Self { | ||
Self { | ||
tokenizer, | ||
token_generator, | ||
} | ||
} | ||
} | ||
|
||
impl TextGeneratorTrait for TextGenerator { | ||
fn init(&mut self, prompt: String) -> Result<()> { | ||
let prompt_tokens = self | ||
.tokenizer | ||
.tokenizer() | ||
.encode(prompt, true) | ||
.map_err(anyhow::Error::msg)?; | ||
self.token_generator | ||
.init(prompt_tokens.get_ids().to_vec())?; | ||
Ok(()) | ||
} | ||
|
||
fn next(&mut self) -> Result<TextGeneratorResult> { | ||
let token = self.token_generator.next()?; | ||
match token { | ||
TokenGeneratorResult::Token((token, probability)) => { | ||
let text = self.tokenizer.next_token(token)?; | ||
match text { | ||
Some(text) => Ok(TextGeneratorResult::Token((text, probability))), | ||
None => Ok(TextGeneratorResult::Token(("".to_string(), 1.0))), | ||
} | ||
} | ||
TokenGeneratorResult::Finish(reason) => Ok(TextGeneratorResult::Finish(reason)), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::llm::{ | ||
generate_parameter::GenerateParameter, token_generator::dummy::DummyTokenGenerator, | ||
}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_text_generator() { | ||
let mut text_generator = TextGenerator::new( | ||
TokenOutputStream::new(tokenizers::tokenizer::Tokenizer::new( | ||
tokenizers::models::bpe::BPE::default(), | ||
)), | ||
Box::new(DummyTokenGenerator::new(GenerateParameter { | ||
max_new_tokens: 10, | ||
})), | ||
); | ||
text_generator.init("Hello World".to_string()).unwrap(); | ||
for _ in 0..10 { | ||
assert!(match text_generator.next().unwrap() { | ||
TextGeneratorResult::Token((_, _)) => true, | ||
_ => false, | ||
}); | ||
} | ||
assert_eq!( | ||
text_generator.next().unwrap(), | ||
TextGeneratorResult::Finish(FinishReason::Length) | ||
); | ||
} | ||
} |
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.