This commit is contained in:
OlivierDehaene 2023-02-02 14:17:20 +01:00
parent d2d5394991
commit e92eb15d45
3 changed files with 63 additions and 64 deletions

View File

@ -1,7 +1,7 @@
/// Batching and inference logic /// Batching and inference logic
use crate::validation::{Validation, ValidationError}; use crate::validation::{Validation, ValidationError};
use crate::GenerateRequest; use crate::GenerateRequest;
use crate::{Db, Entry, Token}; use crate::{Queue, Entry, Token};
use nohash_hasher::IntMap; use nohash_hasher::IntMap;
use std::future::Future; use std::future::Future;
use std::sync::Arc; use std::sync::Arc;
@ -20,8 +20,8 @@ use tracing::instrument;
pub struct Infer { pub struct Infer {
/// Validation /// Validation
validation: Validation, validation: Validation,
/// Request database /// Request queue
db: Db, queue: Queue,
/// Shared state /// Shared state
shared: Arc<Shared>, shared: Arc<Shared>,
/// Inference limit /// Inference limit
@ -43,7 +43,7 @@ impl Infer {
max_concurrent_requests: usize, max_concurrent_requests: usize,
) -> Self { ) -> Self {
// Infer shared state // Infer shared state
let db = Db::new(); let queue = Queue::new();
let shared = Arc::new(Shared { let shared = Arc::new(Shared {
batching_task: Notify::new(), batching_task: Notify::new(),
}); });
@ -53,7 +53,7 @@ impl Infer {
client, client,
max_batch_size, max_batch_size,
max_waiting_tokens, max_waiting_tokens,
db.clone(), queue.clone(),
shared.clone(), shared.clone(),
)); ));
@ -62,13 +62,13 @@ impl Infer {
Self { Self {
validation, validation,
db, queue,
shared, shared,
limit_concurrent_requests: semaphore, limit_concurrent_requests: semaphore,
} }
} }
/// Add a new request to the database and return a stream of InferStreamResponse /// Add a new request to the queue and return a stream of InferStreamResponse
pub(crate) async fn generate_stream( pub(crate) async fn generate_stream(
&self, &self,
request: GenerateRequest, request: GenerateRequest,
@ -83,8 +83,8 @@ impl Infer {
// MPSC channel to communicate with the background batching task // MPSC channel to communicate with the background batching task
let (response_tx, response_rx) = mpsc::unbounded_channel(); let (response_tx, response_rx) = mpsc::unbounded_channel();
// Append the request to the database // Append the request to the queue
self.db.append(Entry { self.queue.append(Entry {
request: valid_request, request: valid_request,
response_tx, response_tx,
time: Instant::now(), time: Instant::now(),
@ -92,7 +92,7 @@ impl Infer {
_permit: permit, _permit: permit,
}); });
// Notify the background task that we have a new entry in the database that needs // Notify the background task that we have a new entry in the queue that needs
// to be batched // to be batched
self.shared.batching_task.notify_one(); self.shared.batching_task.notify_one();
@ -100,7 +100,7 @@ impl Infer {
Ok(UnboundedReceiverStream::new(response_rx)) Ok(UnboundedReceiverStream::new(response_rx))
} }
/// Add a new request to the database and return a InferResponse /// Add a new request to the queue and return a InferResponse
pub(crate) async fn generate( pub(crate) async fn generate(
&self, &self,
request: GenerateRequest, request: GenerateRequest,
@ -169,12 +169,12 @@ impl Infer {
/// Will be launched in a background Tokio task /// Will be launched in a background Tokio task
/// ///
/// Batches requests and sends them to the inference server /// Batches requests and sends them to the inference server
#[instrument(skip(client, db, shared))] #[instrument(skip(client, queue, shared))]
async fn batching_task( async fn batching_task(
mut client: ShardedClient, mut client: ShardedClient,
max_batch_size: usize, max_batch_size: usize,
max_waiting_tokens: usize, max_waiting_tokens: usize,
db: Db, queue: Queue,
shared: Arc<Shared>, shared: Arc<Shared>,
) { ) {
// Minimum batch size after which we try to add more requests // Minimum batch size after which we try to add more requests
@ -185,10 +185,10 @@ async fn batching_task(
// Wait for a notification from the Infer struct // Wait for a notification from the Infer struct
shared.batching_task.notified().await; shared.batching_task.notified().await;
// Get the next batch from the DB // Get the next batch from the queue
// This batch might be smaller than the maximum batch size if there are not enough requests // This batch might be smaller than the maximum batch size if there are not enough requests
// waiting in the DB // waiting in the queue
while let Some((mut entries, batch)) = db.next_batch(None, max_batch_size).await { while let Some((mut entries, batch)) = queue.next_batch(None, max_batch_size).await {
let mut cached_batch = wrap_future(client.prefill(batch), &mut entries).await; let mut cached_batch = wrap_future(client.prefill(batch), &mut entries).await;
let mut waiting_tokens = 1; let mut waiting_tokens = 1;
@ -210,7 +210,7 @@ async fn batching_task(
}; };
// Try to get a new batch // Try to get a new batch
if let Some((mut new_entries, new_batch)) = db if let Some((mut new_entries, new_batch)) = queue
.next_batch(min_size, max_batch_size - batch_size as usize) .next_batch(min_size, max_batch_size - batch_size as usize)
.await .await
{ {

View File

@ -1,10 +1,10 @@
/// Text Generation Inference Webserver /// Text Generation Inference Webserver
mod db; mod queue;
mod infer; mod infer;
pub mod server; pub mod server;
mod validation; mod validation;
use db::{Db, Entry}; use queue::{Queue, Entry};
use infer::Infer; use infer::Infer;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use validation::Validation; use validation::Validation;

View File

@ -1,4 +1,3 @@
/// This code is massively inspired by Tokio mini-redis
use crate::infer::InferError; use crate::infer::InferError;
use crate::infer::InferStreamResponse; use crate::infer::InferStreamResponse;
use crate::validation::ValidGenerateRequest; use crate::validation::ValidGenerateRequest;
@ -9,7 +8,7 @@ use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::{mpsc, oneshot, OwnedSemaphorePermit}; use tokio::sync::{mpsc, oneshot, OwnedSemaphorePermit};
use tokio::time::Instant; use tokio::time::Instant;
/// Database entry /// Queue entry
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Entry { pub(crate) struct Entry {
/// Request /// Request
@ -24,29 +23,29 @@ pub(crate) struct Entry {
pub _permit: OwnedSemaphorePermit, pub _permit: OwnedSemaphorePermit,
} }
/// Request Database /// Request Queue
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct Db { pub(crate) struct Queue {
/// Channel to communicate with the background database task /// Channel to communicate with the background queue task
db_sender: UnboundedSender<DatabaseCommand>, queue_sender: UnboundedSender<QueueCommand>,
} }
impl Db { impl Queue {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
// Create channel // Create channel
let (db_sender, db_receiver) = mpsc::unbounded_channel(); let (queue_sender, queue_receiver) = mpsc::unbounded_channel();
// Launch background database task // Launch background queue task
tokio::spawn(database_task(db_receiver)); tokio::spawn(queue_task(queue_receiver));
Self { db_sender } Self { queue_sender }
} }
/// Append an entry to the database /// Append an entry to the queue
pub(crate) fn append(&self, entry: Entry) { pub(crate) fn append(&self, entry: Entry) {
// Send append command to the background task managing the state // Send append command to the background task managing the state
// Unwrap is safe here // Unwrap is safe here
self.db_sender.send(DatabaseCommand::Append(entry)).unwrap(); self.queue_sender.send(QueueCommand::Append(entry)).unwrap();
} }
// Get the next batch // Get the next batch
@ -59,8 +58,8 @@ impl Db {
let (response_sender, response_receiver) = oneshot::channel(); let (response_sender, response_receiver) = oneshot::channel();
// Send next batch command to the background task managing the state // Send next batch command to the background task managing the state
// Unwrap is safe here // Unwrap is safe here
self.db_sender self.queue_sender
.send(DatabaseCommand::NextBatch { .send(QueueCommand::NextBatch {
min_size, min_size,
max_size, max_size,
response_sender, response_sender,
@ -72,14 +71,14 @@ impl Db {
} }
} }
// Background task responsible of the database state // Background task responsible of the queue state
async fn database_task(mut receiver: UnboundedReceiver<DatabaseCommand>) { async fn queue_task(mut receiver: UnboundedReceiver<QueueCommand>) {
let mut state = State::new(); let mut state = State::new();
while let Some(cmd) = receiver.recv().await { while let Some(cmd) = receiver.recv().await {
match cmd { match cmd {
DatabaseCommand::Append(entry) => state.append(entry), QueueCommand::Append(entry) => state.append(entry),
DatabaseCommand::NextBatch { QueueCommand::NextBatch {
min_size, min_size,
max_size, max_size,
response_sender, response_sender,
@ -91,10 +90,10 @@ async fn database_task(mut receiver: UnboundedReceiver<DatabaseCommand>) {
} }
} }
/// Database State /// Queue State
#[derive(Debug)] #[derive(Debug)]
struct State { struct State {
/// Database entries organized in a Vec /// Queue entries organized in a Vec
entries: Vec<(u64, Entry)>, entries: Vec<(u64, Entry)>,
/// Id of the next entry /// Id of the next entry
@ -113,7 +112,7 @@ impl State {
} }
} }
/// Append an entry to the database /// Append an entry to the queue
fn append(&mut self, entry: Entry) { fn append(&mut self, entry: Entry) {
self.entries.push((self.next_id, entry)); self.entries.push((self.next_id, entry));
self.next_id += 1; self.next_id += 1;
@ -125,7 +124,7 @@ impl State {
return None; return None;
} }
// Check if we have enough entries in DB // Check if we have enough entries
if let Some(min_size) = min_size { if let Some(min_size) = min_size {
if self.entries.len() < min_size { if self.entries.len() < min_size {
return None; return None;
@ -170,7 +169,7 @@ impl State {
type NextBatch = (IntMap<u64, Entry>, Batch); type NextBatch = (IntMap<u64, Entry>, Batch);
#[derive(Debug)] #[derive(Debug)]
enum DatabaseCommand { enum QueueCommand {
Append(Entry), Append(Entry),
NextBatch { NextBatch {
min_size: Option<usize>, min_size: Option<usize>,
@ -299,26 +298,26 @@ mod tests {
} }
#[tokio::test] #[tokio::test]
async fn test_db_append() { async fn test_queue_append() {
let db = Db::new(); let queue = Queue::new();
db.append(default_entry()); queue.append(default_entry());
} }
#[tokio::test] #[tokio::test]
async fn test_db_next_batch_empty() { async fn test_queue_next_batch_empty() {
let db = Db::new(); let queue = Queue::new();
assert!(db.next_batch(None, 1).await.is_none()); assert!(queue.next_batch(None, 1).await.is_none());
assert!(db.next_batch(Some(1), 1).await.is_none()); assert!(queue.next_batch(Some(1), 1).await.is_none());
} }
#[tokio::test] #[tokio::test]
async fn test_db_next_batch_min_size() { async fn test_queue_next_batch_min_size() {
let db = Db::new(); let queue = Queue::new();
db.append(default_entry()); queue.append(default_entry());
db.append(default_entry()); queue.append(default_entry());
let (entries, batch) = db.next_batch(None, 2).await.unwrap(); let (entries, batch) = queue.next_batch(None, 2).await.unwrap();
assert_eq!(entries.len(), 2); assert_eq!(entries.len(), 2);
assert!(entries.contains_key(&0)); assert!(entries.contains_key(&0));
assert!(entries.contains_key(&1)); assert!(entries.contains_key(&1));
@ -327,26 +326,26 @@ mod tests {
assert_eq!(batch.id, 0); assert_eq!(batch.id, 0);
assert_eq!(batch.size, 2); assert_eq!(batch.size, 2);
db.append(default_entry()); queue.append(default_entry());
assert!(db.next_batch(Some(2), 2).await.is_none()); assert!(queue.next_batch(Some(2), 2).await.is_none());
} }
#[tokio::test] #[tokio::test]
async fn test_db_next_batch_max_size() { async fn test_queue_next_batch_max_size() {
let db = Db::new(); let queue = Queue::new();
db.append(default_entry()); queue.append(default_entry());
db.append(default_entry()); queue.append(default_entry());
let (entries, batch) = db.next_batch(None, 1).await.unwrap(); let (entries, batch) = queue.next_batch(None, 1).await.unwrap();
assert_eq!(entries.len(), 1); assert_eq!(entries.len(), 1);
assert!(entries.contains_key(&0)); assert!(entries.contains_key(&0));
assert_eq!(batch.id, 0); assert_eq!(batch.id, 0);
assert_eq!(batch.size, 1); assert_eq!(batch.size, 1);
db.append(default_entry()); queue.append(default_entry());
let (entries, batch) = db.next_batch(None, 3).await.unwrap(); let (entries, batch) = queue.next_batch(None, 3).await.unwrap();
assert_eq!(entries.len(), 2); assert_eq!(entries.len(), 2);
assert!(entries.contains_key(&1)); assert!(entries.contains_key(&1));
assert!(entries.contains_key(&2)); assert!(entries.contains_key(&2));