2024-09-24 06:38:17 +00:00
|
|
|
use crate::client::{
|
|
|
|
Batch, GrammarType, NextTokenChooserParameters, Request, StoppingCriteriaParameters,
|
2024-06-04 13:56:56 +00:00
|
|
|
};
|
2023-02-02 13:59:27 +00:00
|
|
|
use nohash_hasher::{BuildNoHashHasher, IntMap};
|
2023-09-28 07:55:47 +00:00
|
|
|
use std::cmp::min;
|
2024-09-24 03:06:55 +00:00
|
|
|
use std::collections::VecDeque;
|
2024-09-24 06:38:17 +00:00
|
|
|
use text_generation_router::infer::InferError;
|
|
|
|
use text_generation_router::infer::InferStreamResponse;
|
|
|
|
use text_generation_router::validation::{
|
|
|
|
ChunksToString, ValidGenerateRequest, ValidGrammar, ValidParameters, ValidStoppingParameters,
|
2024-06-04 13:56:56 +00:00
|
|
|
};
|
2023-10-23 13:51:12 +00:00
|
|
|
use tokio::sync::{mpsc, oneshot};
|
2023-02-02 13:59:27 +00:00
|
|
|
use tokio::time::Instant;
|
2023-02-13 12:02:45 +00:00
|
|
|
use tracing::{info_span, instrument, Span};
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
/// Queue entry
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct Entry {
|
|
|
|
/// Request
|
|
|
|
pub request: ValidGenerateRequest,
|
|
|
|
/// Response sender to communicate between the Infer struct and the batching_task
|
2023-10-23 13:51:12 +00:00
|
|
|
pub response_tx: mpsc::UnboundedSender<Result<InferStreamResponse, InferError>>,
|
2023-02-13 12:02:45 +00:00
|
|
|
/// Span that will live as long as entry
|
|
|
|
pub span: Span,
|
|
|
|
/// Temporary span used as a guard when logging inference, wait times...
|
|
|
|
pub temp_span: Option<Span>,
|
|
|
|
/// Instant when this entry was queued
|
|
|
|
pub queue_time: Instant,
|
2023-02-02 13:59:27 +00:00
|
|
|
/// Instant when this entry was added to a batch
|
|
|
|
pub batch_time: Option<Instant>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Request Queue
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct Queue {
|
|
|
|
/// Channel to communicate with the background queue task
|
2023-10-23 13:51:12 +00:00
|
|
|
queue_sender: mpsc::UnboundedSender<QueueCommand>,
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Queue {
|
2023-12-11 08:24:27 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
requires_padding: bool,
|
|
|
|
block_size: u32,
|
2023-12-11 11:46:30 +00:00
|
|
|
window_size: Option<u32>,
|
|
|
|
speculate: u32,
|
2023-12-11 08:24:27 +00:00
|
|
|
) -> Self {
|
2023-02-02 13:59:27 +00:00
|
|
|
// Create channel
|
2023-10-23 13:51:12 +00:00
|
|
|
let (queue_sender, queue_receiver) = mpsc::unbounded_channel();
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
// Launch background queue task
|
2023-09-28 07:55:47 +00:00
|
|
|
tokio::spawn(queue_task(
|
|
|
|
requires_padding,
|
|
|
|
block_size,
|
|
|
|
window_size,
|
2023-12-11 11:46:30 +00:00
|
|
|
speculate,
|
2023-09-28 07:55:47 +00:00
|
|
|
queue_receiver,
|
|
|
|
));
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
Self { queue_sender }
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:02:45 +00:00
|
|
|
#[instrument(skip_all)]
|
2023-02-02 13:59:27 +00:00
|
|
|
pub(crate) fn append(&self, entry: Entry) {
|
|
|
|
// Send append command to the background task managing the state
|
|
|
|
// Unwrap is safe here
|
2023-02-13 12:02:45 +00:00
|
|
|
self.queue_sender
|
2023-06-16 14:25:11 +00:00
|
|
|
.send(QueueCommand::Append(Box::new(entry), Span::current()))
|
2023-02-13 12:02:45 +00:00
|
|
|
.unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next batch
|
2023-02-13 12:02:45 +00:00
|
|
|
#[instrument(skip(self))]
|
2023-02-02 13:59:27 +00:00
|
|
|
pub(crate) async fn next_batch(
|
|
|
|
&self,
|
|
|
|
min_size: Option<usize>,
|
2024-02-09 11:38:41 +00:00
|
|
|
max_size: Option<usize>,
|
2023-06-30 17:09:59 +00:00
|
|
|
prefill_token_budget: u32,
|
2023-04-24 15:59:00 +00:00
|
|
|
token_budget: u32,
|
2023-02-02 13:59:27 +00:00
|
|
|
) -> Option<NextBatch> {
|
|
|
|
// Create response channel
|
|
|
|
let (response_sender, response_receiver) = oneshot::channel();
|
|
|
|
// Send next batch command to the background task managing the state
|
|
|
|
// Unwrap is safe here
|
|
|
|
self.queue_sender
|
|
|
|
.send(QueueCommand::NextBatch {
|
|
|
|
min_size,
|
2024-02-09 11:38:41 +00:00
|
|
|
max_size,
|
2023-06-30 17:09:59 +00:00
|
|
|
prefill_token_budget,
|
2023-04-24 15:59:00 +00:00
|
|
|
token_budget,
|
2023-02-02 13:59:27 +00:00
|
|
|
response_sender,
|
2023-02-13 12:02:45 +00:00
|
|
|
span: Span::current(),
|
2023-02-02 13:59:27 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
// Await on response channel
|
|
|
|
// Unwrap is safe here
|
|
|
|
response_receiver.await.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Background task responsible of the queue state
|
2023-07-19 07:31:25 +00:00
|
|
|
async fn queue_task(
|
|
|
|
requires_padding: bool,
|
|
|
|
block_size: u32,
|
2023-09-28 07:55:47 +00:00
|
|
|
window_size: Option<u32>,
|
2023-12-11 11:46:30 +00:00
|
|
|
speculate: u32,
|
2023-10-23 13:51:12 +00:00
|
|
|
mut receiver: mpsc::UnboundedReceiver<QueueCommand>,
|
2023-07-19 07:31:25 +00:00
|
|
|
) {
|
2024-09-24 03:06:55 +00:00
|
|
|
let mut state = State::new(requires_padding, block_size, window_size, speculate);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2023-10-23 13:51:12 +00:00
|
|
|
while let Some(cmd) = receiver.recv().await {
|
2023-02-02 13:59:27 +00:00
|
|
|
match cmd {
|
2023-04-24 15:59:00 +00:00
|
|
|
QueueCommand::Append(entry, span) => {
|
2023-06-16 14:25:11 +00:00
|
|
|
span.in_scope(|| state.append(*entry));
|
2024-07-08 14:03:59 +00:00
|
|
|
metrics::gauge!("tgi_queue_size").increment(1.0);
|
2023-04-24 15:59:00 +00:00
|
|
|
}
|
2023-02-02 13:59:27 +00:00
|
|
|
QueueCommand::NextBatch {
|
|
|
|
min_size,
|
2024-02-09 11:38:41 +00:00
|
|
|
max_size,
|
2023-06-30 17:09:59 +00:00
|
|
|
prefill_token_budget,
|
2023-04-24 15:59:00 +00:00
|
|
|
token_budget,
|
2023-02-02 13:59:27 +00:00
|
|
|
response_sender,
|
2023-02-13 12:02:45 +00:00
|
|
|
span,
|
|
|
|
} => span.in_scope(|| {
|
2024-02-09 11:38:41 +00:00
|
|
|
let next_batch =
|
|
|
|
state.next_batch(min_size, max_size, prefill_token_budget, token_budget);
|
2023-06-23 12:58:28 +00:00
|
|
|
response_sender.send(next_batch).unwrap();
|
2024-07-08 14:03:59 +00:00
|
|
|
metrics::gauge!("tgi_queue_size").set(state.entries.len() as f64);
|
2023-02-13 12:02:45 +00:00
|
|
|
}),
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Queue State
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct State {
|
2024-09-24 03:06:55 +00:00
|
|
|
/// Queue entries organized in a Vec
|
|
|
|
entries: VecDeque<(u64, Entry)>,
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
/// Id of the next entry
|
|
|
|
next_id: u64,
|
|
|
|
|
|
|
|
/// Id of the next batch
|
|
|
|
next_batch_id: u64,
|
2023-04-24 15:59:00 +00:00
|
|
|
|
|
|
|
/// Whether the model is using padding
|
|
|
|
requires_padding: bool,
|
2023-07-19 07:31:25 +00:00
|
|
|
|
|
|
|
/// Paged Attention block size
|
|
|
|
block_size: u32,
|
2023-09-28 07:55:47 +00:00
|
|
|
|
|
|
|
/// Sliding window
|
|
|
|
window_size: Option<u32>,
|
2023-12-11 11:46:30 +00:00
|
|
|
|
|
|
|
/// Speculation amount
|
|
|
|
speculate: u32,
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
2023-12-11 08:24:27 +00:00
|
|
|
fn new(
|
|
|
|
requires_padding: bool,
|
|
|
|
block_size: u32,
|
2024-02-29 09:56:26 +00:00
|
|
|
window_size: Option<u32>,
|
2023-12-11 11:46:30 +00:00
|
|
|
speculate: u32,
|
2023-12-11 08:24:27 +00:00
|
|
|
) -> Self {
|
2023-02-02 13:59:27 +00:00
|
|
|
Self {
|
2024-09-24 03:06:55 +00:00
|
|
|
entries: VecDeque::with_capacity(128),
|
2023-02-02 13:59:27 +00:00
|
|
|
next_id: 0,
|
|
|
|
next_batch_id: 0,
|
2023-04-24 15:59:00 +00:00
|
|
|
requires_padding,
|
2023-07-19 07:31:25 +00:00
|
|
|
block_size,
|
2023-09-28 07:55:47 +00:00
|
|
|
window_size,
|
2023-12-11 11:46:30 +00:00
|
|
|
speculate,
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Append an entry to the queue
|
2023-02-13 12:02:45 +00:00
|
|
|
fn append(&mut self, mut entry: Entry) {
|
|
|
|
// Create a span that will live as long as the entry is in the queue waiting to be batched
|
|
|
|
let queue_span = info_span!(parent: &entry.span, "queued");
|
|
|
|
entry.temp_span = Some(queue_span);
|
|
|
|
|
|
|
|
// Push entry in the queue
|
2024-09-24 03:06:55 +00:00
|
|
|
self.entries.push_back((self.next_id, entry));
|
2023-02-02 13:59:27 +00:00
|
|
|
self.next_id += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next batch
|
2023-06-30 17:09:59 +00:00
|
|
|
fn next_batch(
|
|
|
|
&mut self,
|
|
|
|
min_size: Option<usize>,
|
2024-02-09 11:38:41 +00:00
|
|
|
max_size: Option<usize>,
|
2023-06-30 17:09:59 +00:00
|
|
|
prefill_token_budget: u32,
|
|
|
|
token_budget: u32,
|
|
|
|
) -> Option<NextBatch> {
|
2023-02-02 13:59:27 +00:00
|
|
|
if self.entries.is_empty() {
|
2024-04-12 12:20:31 +00:00
|
|
|
tracing::debug!("No queue");
|
2023-02-02 13:59:27 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have enough entries
|
|
|
|
if let Some(min_size) = min_size {
|
|
|
|
if self.entries.len() < min_size {
|
2024-04-12 12:20:31 +00:00
|
|
|
tracing::debug!("Not enough entries");
|
2023-02-02 13:59:27 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 08:54:32 +00:00
|
|
|
if let Some(max_size) = max_size {
|
|
|
|
if max_size == 0 {
|
|
|
|
tracing::debug!("No capacity");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
// Pad prefill_token_budget to be a multiple of block size
|
|
|
|
let prefill_token_budget =
|
|
|
|
((prefill_token_budget + self.block_size - 1) / self.block_size) * self.block_size;
|
2024-02-29 09:56:26 +00:00
|
|
|
|
2023-02-13 12:02:45 +00:00
|
|
|
// Create span for this batch to add context to inference calls
|
2023-04-20 09:07:40 +00:00
|
|
|
let next_batch_span = info_span!(parent: None, "batch", batch_size = tracing::field::Empty);
|
2024-09-24 06:38:17 +00:00
|
|
|
next_batch_span.follows_from(Span::current());
|
2023-02-13 12:02:45 +00:00
|
|
|
|
2023-04-24 15:59:00 +00:00
|
|
|
let mut batch_requests = Vec::with_capacity(self.entries.len());
|
2023-02-02 13:59:27 +00:00
|
|
|
let mut batch_entries =
|
2023-04-24 15:59:00 +00:00
|
|
|
IntMap::with_capacity_and_hasher(self.entries.len(), BuildNoHashHasher::default());
|
2023-04-20 09:07:40 +00:00
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
let mut max_input_length = 0;
|
2023-04-24 15:59:00 +00:00
|
|
|
let mut prefill_tokens: u32 = 0;
|
|
|
|
let mut decode_tokens: u32 = 0;
|
|
|
|
|
|
|
|
// Pop entries starting from the front of the queue
|
2024-09-24 03:06:55 +00:00
|
|
|
while let Some((id, mut entry)) = self.entries.pop_front() {
|
2023-04-20 09:07:40 +00:00
|
|
|
// Filter entries where the response receiver was dropped (== entries where the request
|
|
|
|
// was dropped by the client)
|
2023-10-23 13:51:12 +00:00
|
|
|
if entry.response_tx.is_closed() {
|
2024-07-08 14:03:59 +00:00
|
|
|
metrics::counter!("tgi_request_failure", "err" => "dropped").increment(1);
|
2024-04-12 12:20:31 +00:00
|
|
|
tracing::debug!("Dropping entry");
|
2023-04-20 09:07:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:59:00 +00:00
|
|
|
if self.requires_padding {
|
|
|
|
// We pad to max input length in the Python shards
|
|
|
|
// We need to take these padding tokens into the equation
|
2024-09-24 03:06:55 +00:00
|
|
|
max_input_length = max_input_length.max(entry.request.input_length);
|
|
|
|
prefill_tokens = (batch_requests.len() + 1) as u32 * max_input_length
|
2023-04-24 15:59:00 +00:00
|
|
|
} else {
|
2023-07-19 07:31:25 +00:00
|
|
|
// pad to block size
|
|
|
|
prefill_tokens += ((entry.request.input_length + self.block_size - 1)
|
|
|
|
/ self.block_size)
|
|
|
|
* self.block_size;
|
2023-04-24 15:59:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 07:31:25 +00:00
|
|
|
if self.requires_padding {
|
2024-09-24 03:06:55 +00:00
|
|
|
decode_tokens += entry.request.stopping_parameters.max_new_tokens;
|
2023-07-19 07:31:25 +00:00
|
|
|
} else {
|
2023-09-28 07:55:47 +00:00
|
|
|
let max_new_tokens = match self.window_size {
|
|
|
|
None => entry.request.stopping_parameters.max_new_tokens,
|
|
|
|
Some(window_size) => min(
|
|
|
|
window_size.saturating_sub(entry.request.input_length),
|
|
|
|
entry.request.stopping_parameters.max_new_tokens,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2023-07-19 07:31:25 +00:00
|
|
|
// pad to block size
|
|
|
|
decode_tokens +=
|
2023-09-28 07:55:47 +00:00
|
|
|
((max_new_tokens + self.block_size - 1) / self.block_size) * self.block_size;
|
2023-07-19 07:31:25 +00:00
|
|
|
}
|
2023-04-24 15:59:00 +00:00
|
|
|
|
2023-06-30 17:09:59 +00:00
|
|
|
if prefill_tokens > prefill_token_budget
|
2023-12-11 11:46:30 +00:00
|
|
|
|| (prefill_tokens + decode_tokens + self.speculate) > token_budget
|
2023-06-30 17:09:59 +00:00
|
|
|
{
|
2023-04-24 15:59:00 +00:00
|
|
|
// Entry is over budget
|
|
|
|
// Add it back to the front
|
2024-04-12 12:20:31 +00:00
|
|
|
tracing::debug!("Over budget: prefill_tokens={prefill_tokens} > {prefill_token_budget} || {prefill_tokens} + {decode_tokens} + {} > {token_budget}", self.speculate);
|
2024-09-24 03:06:55 +00:00
|
|
|
self.entries.push_front((id, entry));
|
2023-04-24 15:59:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:20:31 +00:00
|
|
|
tracing::debug!("Accepting entry");
|
2023-04-20 09:07:40 +00:00
|
|
|
// Create a new span to link the batch back to this entry
|
|
|
|
let entry_batch_span = info_span!(parent: &entry.span, "infer");
|
|
|
|
// Add relationships
|
|
|
|
next_batch_span.follows_from(&entry_batch_span);
|
|
|
|
entry_batch_span.follows_from(&next_batch_span);
|
|
|
|
// Update entry
|
|
|
|
entry.temp_span = Some(entry_batch_span);
|
|
|
|
|
|
|
|
batch_requests.push(Request {
|
|
|
|
id,
|
2023-06-02 15:12:30 +00:00
|
|
|
prefill_logprobs: entry.request.decoder_input_details,
|
2024-06-03 07:27:22 +00:00
|
|
|
inputs: entry.request.inputs.chunks_to_string(),
|
2023-04-20 09:07:40 +00:00
|
|
|
truncate: entry.request.truncate,
|
2024-06-04 13:56:56 +00:00
|
|
|
parameters: Some(NextTokenChooserParameters::from(
|
|
|
|
entry.request.parameters.clone(),
|
|
|
|
)),
|
|
|
|
stopping_parameters: Some(StoppingCriteriaParameters::from(
|
|
|
|
entry.request.stopping_parameters.clone(),
|
|
|
|
)),
|
2023-08-28 09:43:47 +00:00
|
|
|
top_n_tokens: entry.request.top_n_tokens,
|
2023-02-02 13:59:27 +00:00
|
|
|
});
|
2023-04-20 09:07:40 +00:00
|
|
|
// Set batch_time
|
|
|
|
entry.batch_time = Some(Instant::now());
|
|
|
|
// Insert in batch_entries IntMap
|
|
|
|
batch_entries.insert(id, entry);
|
2024-02-09 11:38:41 +00:00
|
|
|
|
|
|
|
// Check if max_size
|
|
|
|
if Some(batch_requests.len()) == max_size {
|
|
|
|
break;
|
|
|
|
}
|
2023-04-20 09:07:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 15:59:00 +00:00
|
|
|
// Empty batch
|
2023-04-20 09:07:40 +00:00
|
|
|
if batch_requests.is_empty() {
|
2024-06-04 13:56:56 +00:00
|
|
|
tracing::debug!("Filtered out all entries");
|
2023-04-20 09:07:40 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:59:00 +00:00
|
|
|
// Check if our batch is big enough
|
|
|
|
if let Some(min_size) = min_size {
|
|
|
|
// Batch is too small
|
|
|
|
if batch_requests.len() < min_size {
|
|
|
|
// Add back entries to the queue in the correct order
|
|
|
|
for r in batch_requests.into_iter().rev() {
|
|
|
|
let id = r.id;
|
|
|
|
let entry = batch_entries.remove(&id).unwrap();
|
2024-09-24 03:06:55 +00:00
|
|
|
self.entries.push_front((id, entry));
|
2023-04-24 15:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Final batch size
|
2023-04-20 09:07:40 +00:00
|
|
|
let size = batch_requests.len() as u32;
|
|
|
|
next_batch_span.record("batch_size", size);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
let batch = Batch {
|
|
|
|
id: self.next_batch_id,
|
|
|
|
requests: batch_requests,
|
2023-04-20 09:07:40 +00:00
|
|
|
size,
|
2023-04-24 15:59:00 +00:00
|
|
|
max_tokens: (prefill_tokens + decode_tokens),
|
2023-02-02 13:59:27 +00:00
|
|
|
};
|
|
|
|
// Increment batch id
|
|
|
|
self.next_batch_id += 1;
|
|
|
|
|
2024-07-08 14:03:59 +00:00
|
|
|
metrics::histogram!("tgi_batch_next_size").record(batch.size as f64);
|
2023-04-24 15:59:00 +00:00
|
|
|
|
2023-02-13 12:02:45 +00:00
|
|
|
Some((batch_entries, batch, next_batch_span))
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:02:45 +00:00
|
|
|
type NextBatch = (IntMap<u64, Entry>, Batch, Span);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum QueueCommand {
|
2023-06-16 14:25:11 +00:00
|
|
|
Append(Box<Entry>, Span),
|
2023-02-02 13:59:27 +00:00
|
|
|
NextBatch {
|
|
|
|
min_size: Option<usize>,
|
2024-02-09 11:38:41 +00:00
|
|
|
max_size: Option<usize>,
|
2023-06-30 17:09:59 +00:00
|
|
|
prefill_token_budget: u32,
|
2023-04-24 15:59:00 +00:00
|
|
|
token_budget: u32,
|
2023-02-02 13:59:27 +00:00
|
|
|
response_sender: oneshot::Sender<Option<NextBatch>>,
|
2023-02-13 12:02:45 +00:00
|
|
|
span: Span,
|
2023-02-02 13:59:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:56:56 +00:00
|
|
|
impl From<ValidParameters> for NextTokenChooserParameters {
|
|
|
|
fn from(value: ValidParameters) -> Self {
|
|
|
|
let (grammar, grammar_type) = match value.grammar {
|
|
|
|
None => (String::new(), GrammarType::None),
|
|
|
|
|
|
|
|
Some(grammar) => match grammar {
|
|
|
|
ValidGrammar::Json(grammar_string) => (grammar_string, GrammarType::Json),
|
|
|
|
ValidGrammar::Regex(grammar_string) => (grammar_string, GrammarType::Regex),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
temperature: value.temperature,
|
|
|
|
top_k: value.top_k,
|
|
|
|
top_p: value.top_p,
|
|
|
|
typical_p: value.typical_p,
|
|
|
|
do_sample: value.do_sample,
|
|
|
|
seed: value.seed,
|
|
|
|
repetition_penalty: value.repetition_penalty,
|
|
|
|
frequency_penalty: value.frequency_penalty,
|
|
|
|
watermark: value.watermark,
|
|
|
|
grammar,
|
|
|
|
grammar_type: grammar_type.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ValidStoppingParameters> for StoppingCriteriaParameters {
|
|
|
|
fn from(value: ValidStoppingParameters) -> Self {
|
|
|
|
Self {
|
|
|
|
max_new_tokens: value.max_new_tokens,
|
|
|
|
stop_sequences: value.stop_sequences,
|
|
|
|
ignore_eos_token: value.ignore_eos_token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 13:59:27 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2024-09-24 06:38:17 +00:00
|
|
|
use std::sync::Arc;
|
2023-02-13 12:02:45 +00:00
|
|
|
use tracing::info_span;
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2023-04-20 09:07:40 +00:00
|
|
|
fn default_entry() -> (
|
|
|
|
Entry,
|
2023-10-23 13:51:12 +00:00
|
|
|
mpsc::UnboundedReceiver<Result<InferStreamResponse, InferError>>,
|
2023-04-20 09:07:40 +00:00
|
|
|
) {
|
2023-10-23 13:51:12 +00:00
|
|
|
let (response_tx, receiver_tx) = mpsc::unbounded_channel();
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2023-04-20 09:07:40 +00:00
|
|
|
let entry = Entry {
|
2023-02-02 13:59:27 +00:00
|
|
|
request: ValidGenerateRequest {
|
2024-06-03 07:27:22 +00:00
|
|
|
inputs: vec![],
|
2024-09-24 06:38:17 +00:00
|
|
|
input_ids: Some(Arc::new(vec![])),
|
2023-04-24 15:59:00 +00:00
|
|
|
input_length: 0,
|
2024-09-24 06:38:17 +00:00
|
|
|
add_special_tokens: true,
|
2023-04-09 18:22:27 +00:00
|
|
|
truncate: 0,
|
2023-06-02 15:12:30 +00:00
|
|
|
decoder_input_details: false,
|
2024-06-04 13:56:56 +00:00
|
|
|
parameters: ValidParameters {
|
2023-02-02 13:59:27 +00:00
|
|
|
temperature: 0.0,
|
|
|
|
top_k: 0,
|
|
|
|
top_p: 0.0,
|
2023-03-09 10:33:57 +00:00
|
|
|
typical_p: 0.0,
|
2023-02-02 13:59:27 +00:00
|
|
|
do_sample: false,
|
|
|
|
seed: 0,
|
|
|
|
repetition_penalty: 0.0,
|
2024-02-08 17:41:25 +00:00
|
|
|
frequency_penalty: 0.0,
|
2023-03-06 13:39:36 +00:00
|
|
|
watermark: false,
|
2024-06-04 13:56:56 +00:00
|
|
|
grammar: None,
|
2023-02-02 13:59:27 +00:00
|
|
|
},
|
2024-06-04 13:56:56 +00:00
|
|
|
stopping_parameters: ValidStoppingParameters {
|
2023-03-30 13:26:27 +00:00
|
|
|
ignore_eos_token: false,
|
2023-04-24 15:59:00 +00:00
|
|
|
max_new_tokens: 1,
|
2023-02-02 13:59:27 +00:00
|
|
|
stop_sequences: vec![],
|
|
|
|
},
|
2023-08-28 09:43:47 +00:00
|
|
|
top_n_tokens: 0,
|
2024-06-25 18:46:27 +00:00
|
|
|
adapter_id: None,
|
2023-02-02 13:59:27 +00:00
|
|
|
},
|
|
|
|
response_tx,
|
2023-02-13 12:02:45 +00:00
|
|
|
span: info_span!("entry"),
|
|
|
|
temp_span: None,
|
|
|
|
queue_time: Instant::now(),
|
2023-02-02 13:59:27 +00:00
|
|
|
batch_time: None,
|
2023-04-20 09:07:40 +00:00
|
|
|
};
|
|
|
|
(entry, receiver_tx)
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_append() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let mut state = State::new(false, 1, None, 0);
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry, _guard) = default_entry();
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
assert_eq!(state.next_id, 0);
|
|
|
|
assert_eq!(state.entries.len(), 0);
|
|
|
|
|
|
|
|
state.append(entry);
|
|
|
|
|
|
|
|
assert_eq!(state.next_id, 1);
|
|
|
|
assert_eq!(state.entries.len(), 1);
|
2024-09-24 03:06:55 +00:00
|
|
|
let (id, _) = state.entries.remove(0).unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(id, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_next_batch_empty() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let mut state = State::new(false, 1, None, 0);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-02-09 11:38:41 +00:00
|
|
|
assert!(state.next_batch(None, None, 1, 1).is_none());
|
|
|
|
assert!(state.next_batch(Some(1), None, 1, 1).is_none());
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_next_batch_min_size() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let mut state = State::new(false, 1, None, 0);
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry1, _guard1) = default_entry();
|
|
|
|
let (entry2, _guard2) = default_entry();
|
|
|
|
state.append(entry1);
|
|
|
|
state.append(entry2);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
let (entries, batch, _) = state.next_batch(None, None, 2, 2).unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(entries.len(), 2);
|
|
|
|
assert!(entries.contains_key(&0));
|
|
|
|
assert!(entries.contains_key(&1));
|
|
|
|
assert!(entries.get(&0).unwrap().batch_time.is_some());
|
|
|
|
assert!(entries.get(&1).unwrap().batch_time.is_some());
|
|
|
|
assert_eq!(batch.id, 0);
|
|
|
|
assert_eq!(batch.size, 2);
|
|
|
|
|
|
|
|
assert_eq!(state.next_id, 2);
|
|
|
|
assert_eq!(state.entries.len(), 0);
|
|
|
|
assert_eq!(state.next_batch_id, 1);
|
|
|
|
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry3, _guard3) = default_entry();
|
|
|
|
state.append(entry3);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-02-09 11:38:41 +00:00
|
|
|
assert!(state.next_batch(Some(2), None, 2, 2).is_none());
|
2023-02-02 13:59:27 +00:00
|
|
|
|
|
|
|
assert_eq!(state.next_id, 3);
|
|
|
|
assert_eq!(state.entries.len(), 1);
|
2024-09-24 03:06:55 +00:00
|
|
|
let (id, _) = state.entries.remove(0).unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(id, 2);
|
|
|
|
}
|
|
|
|
|
2024-02-09 11:38:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_next_batch_max_size() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let mut state = State::new(false, 1, None, 0);
|
2024-02-09 11:38:41 +00:00
|
|
|
let (entry1, _guard1) = default_entry();
|
|
|
|
let (entry2, _guard2) = default_entry();
|
|
|
|
state.append(entry1);
|
|
|
|
state.append(entry2);
|
|
|
|
|
|
|
|
let (entries, batch, _) = state.next_batch(None, Some(1), 2, 2).unwrap();
|
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
assert!(entries.contains_key(&0));
|
|
|
|
assert!(entries.get(&0).unwrap().batch_time.is_some());
|
|
|
|
assert_eq!(batch.id, 0);
|
|
|
|
assert_eq!(batch.size, 1);
|
|
|
|
|
|
|
|
assert_eq!(state.next_id, 2);
|
|
|
|
assert_eq!(state.entries.len(), 1);
|
|
|
|
assert_eq!(state.next_batch_id, 1);
|
|
|
|
}
|
|
|
|
|
2023-02-02 13:59:27 +00:00
|
|
|
#[test]
|
2023-04-24 15:59:00 +00:00
|
|
|
fn test_next_batch_token_budget() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let mut state = State::new(false, 1, None, 0);
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry1, _guard1) = default_entry();
|
|
|
|
let (entry2, _guard2) = default_entry();
|
|
|
|
state.append(entry1);
|
|
|
|
state.append(entry2);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
let (entries, batch, _) = state.next_batch(None, None, 1, 1).unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
assert!(entries.contains_key(&0));
|
|
|
|
assert_eq!(batch.id, 0);
|
|
|
|
assert_eq!(batch.size, 1);
|
|
|
|
|
|
|
|
assert_eq!(state.next_id, 2);
|
|
|
|
assert_eq!(state.entries.len(), 1);
|
|
|
|
assert_eq!(state.next_batch_id, 1);
|
|
|
|
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry3, _guard3) = default_entry();
|
|
|
|
state.append(entry3);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
let (entries, batch, _) = state.next_batch(None, None, 3, 3).unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(entries.len(), 2);
|
|
|
|
assert!(entries.contains_key(&1));
|
|
|
|
assert!(entries.contains_key(&2));
|
|
|
|
assert_eq!(batch.id, 1);
|
|
|
|
assert_eq!(batch.size, 2);
|
|
|
|
|
|
|
|
assert_eq!(state.next_id, 3);
|
|
|
|
assert_eq!(state.entries.len(), 0);
|
|
|
|
assert_eq!(state.next_batch_id, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_queue_append() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let queue = Queue::new(false, 1, None, 0);
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry, _guard) = default_entry();
|
|
|
|
queue.append(entry);
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_queue_next_batch_empty() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let queue = Queue::new(false, 1, None, 0);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-02-09 11:38:41 +00:00
|
|
|
assert!(queue.next_batch(None, None, 1, 1).await.is_none());
|
|
|
|
assert!(queue.next_batch(Some(1), None, 1, 1).await.is_none());
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_queue_next_batch_min_size() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let queue = Queue::new(false, 1, None, 0);
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry1, _guard1) = default_entry();
|
|
|
|
let (entry2, _guard2) = default_entry();
|
|
|
|
queue.append(entry1);
|
|
|
|
queue.append(entry2);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
let (entries, batch, _) = queue.next_batch(None, None, 2, 2).await.unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(entries.len(), 2);
|
|
|
|
assert!(entries.contains_key(&0));
|
|
|
|
assert!(entries.contains_key(&1));
|
|
|
|
assert!(entries.get(&0).unwrap().batch_time.is_some());
|
|
|
|
assert!(entries.get(&1).unwrap().batch_time.is_some());
|
|
|
|
assert_eq!(batch.id, 0);
|
|
|
|
assert_eq!(batch.size, 2);
|
|
|
|
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry3, _guard3) = default_entry();
|
|
|
|
queue.append(entry3);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2023-04-26 14:14:40 +00:00
|
|
|
// Not enough requests pending
|
2024-02-09 11:38:41 +00:00
|
|
|
assert!(queue.next_batch(Some(2), None, 2, 2).await.is_none());
|
2023-04-26 14:14:40 +00:00
|
|
|
// Not enough token budget
|
2024-02-09 11:38:41 +00:00
|
|
|
assert!(queue.next_batch(Some(1), None, 0, 0).await.is_none());
|
2023-04-26 14:14:40 +00:00
|
|
|
// Ok
|
2024-09-24 03:06:55 +00:00
|
|
|
let (entries2, batch2, _) = queue.next_batch(Some(1), None, 2, 2).await.unwrap();
|
2023-04-26 14:14:40 +00:00
|
|
|
assert_eq!(entries2.len(), 1);
|
|
|
|
assert!(entries2.contains_key(&2));
|
|
|
|
assert!(entries2.get(&2).unwrap().batch_time.is_some());
|
|
|
|
assert_eq!(batch2.id, 1);
|
|
|
|
assert_eq!(batch2.size, 1);
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 11:38:41 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_queue_next_batch_max_size() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let queue = Queue::new(false, 1, None, 0);
|
2024-02-09 11:38:41 +00:00
|
|
|
let (entry1, _guard1) = default_entry();
|
|
|
|
let (entry2, _guard2) = default_entry();
|
|
|
|
queue.append(entry1);
|
|
|
|
queue.append(entry2);
|
|
|
|
|
|
|
|
let (entries, batch, _) = queue.next_batch(None, Some(1), 2, 2).await.unwrap();
|
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
assert!(entries.contains_key(&0));
|
|
|
|
assert!(entries.get(&0).unwrap().batch_time.is_some());
|
|
|
|
assert_eq!(batch.id, 0);
|
|
|
|
assert_eq!(batch.size, 1);
|
|
|
|
}
|
|
|
|
|
2023-02-02 13:59:27 +00:00
|
|
|
#[tokio::test]
|
2023-04-24 15:59:00 +00:00
|
|
|
async fn test_queue_next_batch_token_budget() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let queue = Queue::new(false, 1, None, 0);
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry1, _guard1) = default_entry();
|
|
|
|
let (entry2, _guard2) = default_entry();
|
|
|
|
queue.append(entry1);
|
|
|
|
queue.append(entry2);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
let (entries, batch, _) = queue.next_batch(None, None, 1, 1).await.unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
assert!(entries.contains_key(&0));
|
|
|
|
assert_eq!(batch.id, 0);
|
|
|
|
assert_eq!(batch.size, 1);
|
|
|
|
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry3, _guard3) = default_entry();
|
|
|
|
queue.append(entry3);
|
2023-02-02 13:59:27 +00:00
|
|
|
|
2024-09-24 03:06:55 +00:00
|
|
|
let (entries, batch, _) = queue.next_batch(None, None, 3, 3).await.unwrap();
|
2023-02-02 13:59:27 +00:00
|
|
|
assert_eq!(entries.len(), 2);
|
|
|
|
assert!(entries.contains_key(&1));
|
|
|
|
assert!(entries.contains_key(&2));
|
|
|
|
assert_eq!(batch.id, 1);
|
|
|
|
assert_eq!(batch.size, 2);
|
|
|
|
}
|
2023-04-20 09:07:40 +00:00
|
|
|
|
2023-12-11 11:46:30 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_queue_next_batch_token_speculate() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let queue = Queue::new(false, 1, None, 2);
|
2023-12-11 11:46:30 +00:00
|
|
|
let (entry1, _guard1) = default_entry();
|
|
|
|
let (entry2, _guard2) = default_entry();
|
|
|
|
queue.append(entry1);
|
|
|
|
queue.append(entry2);
|
|
|
|
|
|
|
|
// Budget of 1 is not enough
|
2024-02-09 11:38:41 +00:00
|
|
|
assert!(queue.next_batch(None, None, 1, 1).await.is_none());
|
2023-12-11 11:46:30 +00:00
|
|
|
|
2024-02-09 11:38:41 +00:00
|
|
|
let (entries, batch, _) = queue.next_batch(None, None, 6, 6).await.unwrap();
|
2023-12-11 11:46:30 +00:00
|
|
|
assert_eq!(entries.len(), 2);
|
|
|
|
assert!(entries.contains_key(&0));
|
|
|
|
assert!(entries.contains_key(&1));
|
|
|
|
assert_eq!(batch.id, 0);
|
|
|
|
assert_eq!(batch.size, 2);
|
|
|
|
}
|
|
|
|
|
2023-04-20 09:07:40 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_queue_next_batch_dropped_receiver() {
|
2024-09-24 03:06:55 +00:00
|
|
|
let queue = Queue::new(false, 1, None, 0);
|
2023-04-20 09:07:40 +00:00
|
|
|
let (entry, _) = default_entry();
|
|
|
|
queue.append(entry);
|
|
|
|
|
2024-02-09 11:38:41 +00:00
|
|
|
assert!(queue.next_batch(None, None, 1, 1).await.is_none());
|
2023-04-20 09:07:40 +00:00
|
|
|
}
|
2023-02-02 13:59:27 +00:00
|
|
|
}
|