-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose the real-time internal state of the batcher through SSE #3065
Draft
mfuntowicz
wants to merge
5
commits into
main
Choose a base branch
from
proxy_sse_engine_state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−23
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb8f596
feat(metrics): exposes queue size as tokens along with individual req…
mfuntowicz 8de41f6
feat(metrics): exposes the engine state as an endpoint
mfuntowicz 1a9c5de
feat(metrics): update Cargo.lock
mfuntowicz 712199c
feat(metrics): dispatch internal engine state event from queuing/batc…
mfuntowicz f72547c
feat(metrics): remove ngrok mandatory feature for backendv3 crate
mfuntowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,14 +6,17 @@ use crate::client::{ | |||||||||||||||||||||||||||||||||||||||
use nohash_hasher::{BuildNoHashHasher, IntMap}; | ||||||||||||||||||||||||||||||||||||||||
use std::cmp::max; | ||||||||||||||||||||||||||||||||||||||||
use std::collections::VecDeque; | ||||||||||||||||||||||||||||||||||||||||
use text_generation_router::infer::InferError; | ||||||||||||||||||||||||||||||||||||||||
use std::sync::Arc; | ||||||||||||||||||||||||||||||||||||||||
use text_generation_router::infer::InferStreamResponse; | ||||||||||||||||||||||||||||||||||||||||
use text_generation_router::infer::{EngineState, InferError}; | ||||||||||||||||||||||||||||||||||||||||
use text_generation_router::validation::{ | ||||||||||||||||||||||||||||||||||||||||
Chunk, ChunksToString, ValidGenerateRequest, ValidGrammar, ValidParameters, | ||||||||||||||||||||||||||||||||||||||||
ValidStoppingParameters, | ||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||
use tokio::sync::{mpsc, oneshot}; | ||||||||||||||||||||||||||||||||||||||||
use tokio::sync::broadcast::Sender as BroadcastSender; | ||||||||||||||||||||||||||||||||||||||||
use tokio::sync::{mpsc, oneshot, RwLock}; | ||||||||||||||||||||||||||||||||||||||||
use tokio::time::Instant; | ||||||||||||||||||||||||||||||||||||||||
use tracing::log::warn; | ||||||||||||||||||||||||||||||||||||||||
use tracing::{info_span, instrument, Instrument, Span}; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// Queue entry | ||||||||||||||||||||||||||||||||||||||||
|
@@ -51,6 +54,8 @@ impl Queue { | |||||||||||||||||||||||||||||||||||||||
speculate: u32, | ||||||||||||||||||||||||||||||||||||||||
max_batch_total_tokens: u32, | ||||||||||||||||||||||||||||||||||||||||
support_chunking: bool, | ||||||||||||||||||||||||||||||||||||||||
engine_state: Arc<RwLock<EngineState>>, | ||||||||||||||||||||||||||||||||||||||||
queue_events: BroadcastSender<EngineState>, | ||||||||||||||||||||||||||||||||||||||||
) -> Self { | ||||||||||||||||||||||||||||||||||||||||
// Create channel | ||||||||||||||||||||||||||||||||||||||||
let (queue_sender, queue_receiver) = mpsc::unbounded_channel(); | ||||||||||||||||||||||||||||||||||||||||
|
@@ -64,7 +69,9 @@ impl Queue { | |||||||||||||||||||||||||||||||||||||||
speculate, | ||||||||||||||||||||||||||||||||||||||||
max_batch_total_tokens, | ||||||||||||||||||||||||||||||||||||||||
support_chunking, | ||||||||||||||||||||||||||||||||||||||||
engine_state, | ||||||||||||||||||||||||||||||||||||||||
queue_receiver, | ||||||||||||||||||||||||||||||||||||||||
queue_events, | ||||||||||||||||||||||||||||||||||||||||
)); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
Self { queue_sender } | ||||||||||||||||||||||||||||||||||||||||
|
@@ -113,7 +120,7 @@ impl Queue { | |||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// Background task responsible of the queue state | ||||||||||||||||||||||||||||||||||||||||
// Background task responsible for the queue state | ||||||||||||||||||||||||||||||||||||||||
#[allow(clippy::too_many_arguments)] | ||||||||||||||||||||||||||||||||||||||||
async fn queue_task( | ||||||||||||||||||||||||||||||||||||||||
requires_padding: bool, | ||||||||||||||||||||||||||||||||||||||||
|
@@ -123,7 +130,9 @@ async fn queue_task( | |||||||||||||||||||||||||||||||||||||||
speculate: u32, | ||||||||||||||||||||||||||||||||||||||||
max_batch_total_tokens: u32, | ||||||||||||||||||||||||||||||||||||||||
support_chunking: bool, | ||||||||||||||||||||||||||||||||||||||||
engine_state: Arc<RwLock<EngineState>>, | ||||||||||||||||||||||||||||||||||||||||
mut receiver: mpsc::UnboundedReceiver<QueueCommand>, | ||||||||||||||||||||||||||||||||||||||||
queue_events: BroadcastSender<EngineState>, | ||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||
let mut state = State::new( | ||||||||||||||||||||||||||||||||||||||||
requires_padding, | ||||||||||||||||||||||||||||||||||||||||
|
@@ -138,8 +147,29 @@ async fn queue_task( | |||||||||||||||||||||||||||||||||||||||
while let Some(cmd) = receiver.recv().await { | ||||||||||||||||||||||||||||||||||||||||
match cmd { | ||||||||||||||||||||||||||||||||||||||||
QueueCommand::Append(entry, span) => { | ||||||||||||||||||||||||||||||||||||||||
let entry_num_tokens = entry.request.input_length; | ||||||||||||||||||||||||||||||||||||||||
span.in_scope(|| state.append(*entry)); | ||||||||||||||||||||||||||||||||||||||||
metrics::gauge!("tgi_queue_size").increment(1.0); | ||||||||||||||||||||||||||||||||||||||||
metrics::gauge!("tgi_queue_size_tokens").increment(entry_num_tokens); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// Dispatch new state to the proxy | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
// Lock free operation (read) | ||||||||||||||||||||||||||||||||||||||||
let num_queued_tokens = engine_state.read().await.in_queue; | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
// Critical section, doing as little as possible here | ||||||||||||||||||||||||||||||||||||||||
let mut engine_state = engine_state.write().await; | ||||||||||||||||||||||||||||||||||||||||
engine_state.in_queue = num_queued_tokens + entry_num_tokens; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// Send new state to the channel for broadcasting | ||||||||||||||||||||||||||||||||||||||||
if let Err(err) = queue_events.send(*engine_state.read().await) { | ||||||||||||||||||||||||||||||||||||||||
tracing::warn!( | ||||||||||||||||||||||||||||||||||||||||
"Failed to send BatchEvent::QueueChanged({}): {err}", | ||||||||||||||||||||||||||||||||||||||||
num_queued_tokens + entry_num_tokens | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+155
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Function modify then send the new state in the SSE |
||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
QueueCommand::NextBatch { | ||||||||||||||||||||||||||||||||||||||||
min_size, | ||||||||||||||||||||||||||||||||||||||||
|
@@ -154,7 +184,33 @@ async fn queue_task( | |||||||||||||||||||||||||||||||||||||||
.instrument(span) | ||||||||||||||||||||||||||||||||||||||||
.await; | ||||||||||||||||||||||||||||||||||||||||
response_sender.send(next_batch).unwrap(); | ||||||||||||||||||||||||||||||||||||||||
metrics::gauge!("tgi_queue_size").set(state.entries.len() as f64); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
let num_batch_tokens = state | ||||||||||||||||||||||||||||||||||||||||
.entries | ||||||||||||||||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||||||||||||||||
.map(|(_, e)| e.request.input_length) | ||||||||||||||||||||||||||||||||||||||||
.sum::<u32>(); | ||||||||||||||||||||||||||||||||||||||||
metrics::gauge!("tgi_queue_size").set(state.entries.len() as f64); | ||||||||||||||||||||||||||||||||||||||||
metrics::gauge!("tgi_queue_size_tokens").set(num_batch_tokens as f64); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// Dispatch new state to the proxy | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
// Critical section, doing as little as possible here | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
let mut engine_state = engine_state.write().await; | ||||||||||||||||||||||||||||||||||||||||
engine_state.in_queue = num_batch_tokens; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// Send new state to the channel for broadcasting | ||||||||||||||||||||||||||||||||||||||||
if let Err(err) = queue_events.send(*engine_state.read().await) { | ||||||||||||||||||||||||||||||||||||||||
tracing::warn!( | ||||||||||||||||||||||||||||||||||||||||
"Failed to send BatchEvent::QueueChanged({}): {err}", | ||||||||||||||||||||||||||||||||||||||||
num_batch_tokens | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should send the current state as soon as a new connection is open (new subscribe)