-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * Blocking cancellation
- Loading branch information
Showing
10 changed files
with
202 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 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,56 @@ | ||
use flume::{bounded, Receiver, SendError, Sender}; | ||
use std::convert::Infallible; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::sync::Mutex; | ||
use tracing::info; | ||
|
||
pub struct Canceller { | ||
cancelled: AtomicBool, | ||
rx: Mutex<Receiver<()>>, | ||
tx: Sender<()>, | ||
} | ||
|
||
impl Canceller { | ||
#[tracing::instrument(skip(self))] | ||
pub async fn cancel(&self) -> Result<(), SendError<()>> { | ||
info!("cancelling inference"); | ||
self.cancelled.store(true, Ordering::Release); | ||
self.tx.send_async(()).await | ||
} | ||
|
||
pub fn is_cancelled(&self) -> bool { | ||
self.cancelled.load(Ordering::Acquire) | ||
} | ||
|
||
pub fn reset(&self) { | ||
self.cancelled.store(false, Ordering::Release); | ||
} | ||
|
||
#[tracing::instrument(skip(self))] | ||
pub fn inference_feedback(&self) -> Result<llm::InferenceFeedback, Infallible> { | ||
// When a cancellation occurs, the sender will block until it is received at least | ||
// once. We want to check and see if that message has been sent, and if so we'll cancel. | ||
let cancelled = if let Ok(rx) = self.rx.try_lock() { | ||
rx.try_recv().is_ok() | ||
} else { | ||
false | ||
}; | ||
if cancelled || self.is_cancelled() { | ||
info!("sending halt"); | ||
Ok(llm::InferenceFeedback::Halt) | ||
} else { | ||
Ok(llm::InferenceFeedback::Continue) | ||
} | ||
} | ||
} | ||
|
||
impl Default for Canceller { | ||
fn default() -> Self { | ||
let (tx, rx) = bounded(0); | ||
Self { | ||
cancelled: Default::default(), | ||
rx: Mutex::new(rx), | ||
tx, | ||
} | ||
} | ||
} |
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.