Skip to content

Commit

Permalink
refactor: handle max output length in StopCondition (#910)
Browse files Browse the repository at this point in the history
* refactor: handle max output length in StopCondition

* trim stop words

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
wsxiaoys and autofix-ci[bot] authored Nov 28, 2023
1 parent c049f23 commit 2b131ad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
24 changes: 17 additions & 7 deletions crates/llama-cpp-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,40 @@ impl LlamaTextGeneration {
#[async_trait]
impl TextGeneration for LlamaTextGeneration {
async fn generate(&self, prompt: &str, options: TextGenerationOptions) -> String {
let language = options.language;
let s = self.generate_stream(prompt, options).await;
helpers::stream_to_string(s).await
let text = helpers::stream_to_string(s).await;

let Some(language) = language else {
return text;
};

let Some(trimmed) = self.stop_condition_factory.trim_stop_words(language, &text) else {
return text;
};

trimmed
}

async fn generate_stream(
&self,
prompt: &str,
options: TextGenerationOptions,
) -> BoxStream<String> {
let stop_condition = self.stop_condition_factory.create(prompt, options.language);
let stop_condition = self.stop_condition_factory.create(
prompt,
options.max_decoding_length,
options.language,
);

let mut rx = self
.service
.add_request(prompt, options.max_input_length, stop_condition)
.await;

let s = stream! {
let mut length = 0;
while let Some(new_text) = rx.recv().await {
yield new_text;
length += 1;
if length >= options.max_decoding_length {
break;
}
}

rx.close();
Expand Down
7 changes: 3 additions & 4 deletions crates/llama-cpp-bindings/src/llama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,13 @@ impl LlamaServiceImpl {
if tx.is_closed() || text.is_empty() {
// Cancelled by client side or hit eos.
stopped = true;
} else if !stop_condition.should_stop(&text) {
} else {
stopped = stop_condition.should_stop(&text);

match tx.send(text).await {
Ok(_) => (),
Err(_) => stopped = true,
}
} else {
// Stoop words stopped
stopped = true;
}

if stopped {
Expand Down
36 changes: 31 additions & 5 deletions crates/tabby-inference/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ impl Default for StopConditionFactory {
}

impl StopConditionFactory {
pub fn create(&self, text: &str, language: Option<&'static Language>) -> StopCondition {
pub fn create(
&self,
text: &str,
max_decoding_length: usize,
language: Option<&'static Language>,
) -> StopCondition {
if let Some(language) = language {
StopCondition::new(self.get_re(language), text)
StopCondition::new(self.get_re(language), max_decoding_length, text)
} else {
StopCondition::new(None, text)
StopCondition::new(None, max_decoding_length, text)
}
}

Expand All @@ -45,6 +50,22 @@ impl StopConditionFactory {
re.map(|x| x.value().clone())
}
}

pub fn trim_stop_words(&self, language: &'static Language, text: &str) -> Option<String> {
let Some(re) = self.get_re(language) else {
return None;
};

let text = reverse(text);

let text = if let Some(m) = re.find_at(&text, 0) {
&text[m.end()..]
} else {
&text
};

Some(reverse(text))
}
}

fn create_stop_regex(stop_words: Vec<String>) -> Regex {
Expand All @@ -60,14 +81,18 @@ fn create_stop_regex(stop_words: Vec<String>) -> Regex {

pub struct StopCondition {
stop_re: Option<Regex>,
max_decoding_length: usize,
reversed_text: String,
num_decoded: usize,
}

impl StopCondition {
pub fn new(stop_re: Option<Regex>, text: &str) -> Self {
pub fn new(stop_re: Option<Regex>, max_decoding_length: usize, text: &str) -> Self {
Self {
stop_re,
max_decoding_length,
reversed_text: reverse(text),
num_decoded: 0,
}
}

Expand All @@ -82,7 +107,8 @@ impl StopCondition {
}
}

false
self.num_decoded += 1;
self.num_decoded >= self.max_decoding_length
}
}

Expand Down

0 comments on commit 2b131ad

Please sign in to comment.