1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
//! `batched-fn` provides a macro that can be used to easily a wrap a function that runs on //! batches of inputs in such a way that it can be called with //! a single input, yet where that single input is run as part of a batch of other inputs behind //! the scenes. //! //! This is useful when you have a high throughput application where processing inputs in a batch //! is more efficient that processing inputs one-by-one. The trade-off is a small delay that is incurred //! while waiting for a batch to be filled, though this can be tuned with the //! [`delay`](macro.batched_fn.html#config) and [`max_batch_size`](macro.batched_fn.html#config) //! parameters. //! //! A typical use-case is when you have a GPU-backed deep learning model deployed on a webserver that provides //! a prediction for each input that comes in through an HTTP request. //! //! Even though inputs come individually - and outputs need to be served back individually - it //! is usually more efficient to process a group of inputs together in order to fully utilize the GPU. //! //! In this case the model API might look like this: //! //! ```rust //! // `Batch` could be anything that implements the `batched_fn::Batch` trait. //! type Batch<T> = Vec<T>; //! //! #[derive(Debug)] //! struct Input { //! // ... //! } //! //! #[derive(Debug)] //! struct Output { //! // ... //! } //! //! struct Model { //! // ... //! } //! //! impl Model { //! fn predict(&self, batch: Batch<Input>) -> Batch<Output> { //! // ... //! # batch.iter().map(|_| Output {}).collect() //! } //! //! fn load() -> Self { //! // ... //! # Self {} //! } //! } //! ``` //! //! Without `batched-fn`, the webserver route would need to call `Model::predict` on each //! individual input which would result in a bottleneck from under-utilizing the GPU: //! //! ```rust //! use once_cell::sync::Lazy; //! # use batched_fn::{batched_fn, Batch as BatchTrait}; //! # type Batch<T> = Vec<T>; //! # #[derive(Debug)] //! # struct Input {} //! # #[derive(Debug)] //! # struct Output {} //! # struct Model {} //! # impl Model { //! # fn predict(&self, batch: Batch<Input>) -> Batch<Output> { //! # batch.iter().map(|_| Output {}).collect() //! # } //! # fn load() -> Self { Self {} } //! # } //! static MODEL: Lazy<Model> = Lazy::new(Model::load); //! //! fn predict_for_http_request(input: Input) -> Output { //! let mut batched_input = Batch::with_capacity(1); //! batched_input.push(input); //! MODEL.predict(batched_input).pop().unwrap() //! } //! ``` //! //! But by dropping the [`batched_fn`](macro.batched_fn.html) macro into this function, you automatically get batched //! inference behind the scenes without changing the one-to-one relationship between inputs and //! outputs: //! //! ```rust //! # use batched_fn::{batched_fn, Batch as BatchTrait}; //! # type Batch<T> = Vec<T>; //! # #[derive(Debug)] //! # struct Input {} //! # #[derive(Debug)] //! # struct Output {} //! # struct Model {} //! # impl Model { //! # fn predict(&self, batch: Batch<Input>) -> Batch<Output> { //! # batch.iter().map(|_| Output {}).collect() //! # } //! # fn load() -> Self { Self {} } //! # } //! async fn predict_for_http_request(input: Input) -> Output { //! let batch_predict = batched_fn! { //! handler = |batch: Batch<Input>, model: &Model| -> Batch<Output> { //! model.predict(batch) //! }; //! config = { //! max_batch_size: 16, //! delay: 50, //! }; //! context = { //! model: Model::load(), //! }; //! }; //! batch_predict(input).await //! } //! ``` //! //! ❗️ *Note that the `predict_for_http_request` function now has to be `async`.* //! //! Here we set the [`max_batch_size`](macro.batch.html#config) to 16 and [`delay`](macro.batched_fn.html#config) //! to 50 milliseconds. This means the batched function will wait at most 50 milliseconds after receiving a single //! input to fill a batch of 16. If 15 more inputs are not received within 50 milliseconds //! then the partial batch will be ran as-is. //! //! # Tuning max batch size and delay //! //! The optimal batch size and delay will depend on the specifics of your use case, such as how big of a batch you can fit in memory //! (typically on the order of 8, 16, 32, or 64 for a deep learning model) and how long of a delay you can afford. //! In general you want to set both of these as high as you can. //! //! It's worth noting that the response time of your application might actually go *down* under high load. //! This is because the batch handler will be called as soon as either a batch of `max_batch_size` is filled or `delay` milliseconds //! has passed, whichever happens first. //! So under high load batches will be filled quickly, but under low load the response time will be at least `delay` milliseconds (adding the time //! it takes to actually process a batch and respond). //! //! # Implementation details //! //! When the `batched_fn` macro is invoked it spawns a new thread where the //! [`handler`](macro.batched_fn.html#hanlder) will //! be ran. Within that thread, every object specified in the [`context`](macro.batched_fn.html#context) //! is initialized and then passed by reference to the handler each time it is run. //! //! The object returned by the macro is just a closure that sends a single input and a callback //! through an asyncronous channel to the handler thread. When the handler finishes //! running a batch it invokes the callback corresponding to each input with the corresponding output, //! which triggers the closure to wake up and return the output. extern crate once_cell; extern crate tokio; #[doc(hidden)] pub use once_cell::sync::Lazy; #[doc(hidden)] pub use tokio::sync::{mpsc::UnboundedSender, Mutex}; use std::sync::mpsc::Sender; use tokio::sync::mpsc as async_mpsc; /// The `Batch` trait is essentially an abstraction of `Vec<T>`. The input and output of a batch /// [`handler`](macro.batched_fn.html#handler) must implement `Batch`. /// /// It represents an owned collection of ordered items of a single type. pub trait Batch: IntoIterator<Item = <Self as Batch>::Item> { type Item; fn with_capacity(n: usize) -> Self; fn len(&self) -> usize; fn push(&mut self, item: <Self as Batch>::Item); fn is_empty(&self) -> bool { self.len() == 0 } } impl<T> Batch for Vec<T> { type Item = T; fn with_capacity(n: usize) -> Vec<T> { Vec::<T>::with_capacity(n) } fn len(&self) -> usize { self.len() } fn push(&mut self, item: T) { self.push(item); } } /// A `BatchedFn` is a wrapper around a `handler` that provides the interface for /// evaluating a single input as part of a batch of other inputs. #[doc(hidden)] pub struct BatchedFn<T, R> where T: 'static + Send + Sync + std::fmt::Debug, R: 'static + Send + Sync + std::fmt::Debug, { tx: Mutex<Sender<(T, UnboundedSender<R>)>>, } impl<T, R> BatchedFn<T, R> where T: 'static + Send + Sync + std::fmt::Debug, R: 'static + Send + Sync + std::fmt::Debug, { pub fn new(tx: Sender<(T, UnboundedSender<R>)>) -> Self { Self { tx: Mutex::new(tx) } } /// Evaluate a single input as part of a batch of other inputs. pub async fn evaluate_in_batch(&self, input: T) -> R { let (result_tx, mut result_rx) = async_mpsc::unbounded_channel::<R>(); self.tx.lock().await.send((input, result_tx)).unwrap(); result_rx.recv().await.unwrap() } } /// Macro for creating a batched function. /// /// This macro has 3 parameters: [`handler`](#handler), [`config`](#config), and /// [`context`](#context). /// /// # Parameters /// /// ### `handler` /// /// The handler must be in the form of a closure declaration that takes a batch /// and any number of references to objects in the context as input and /// returns a different type of batch. /// /// ### `config` /// /// Within the config you must specify the `max_batch_size` and `delay`. /// /// The batched function will wait at most `delay` milliseconds after receiving a single /// input to fill a batch of size `max_batch_size`. If enough inputs to fill a full batch /// are not received within `delay` milliseconds then the partial batch will be ran as-is. /// /// ## `context` /// /// Any additional reference that the handler takes as input must be defined within /// the context. /// /// # Examples /// /// ```rust /// # #[macro_use] extern crate batched_fn; /// # use batched_fn::batched_fn; /// async fn double(x: i32) -> i32 { /// let batched_double = batched_fn! { /// handler = |batch: Vec<i32>| -> Vec<i32> { /// batch.into_iter().map(|x| x*2).collect() /// }; /// config = { /// max_batch_size: 4, /// delay: 50, /// }; /// context = {}; /// }; /// /// batched_double(x).await /// } /// ``` /// /// You can also provide an arbitrary number of additional arguments to the handler by reference. /// All of the objects have to be initialized in the [`context`](#context): /// /// ```rust /// # #[macro_use] extern crate batched_fn; /// # use batched_fn::batched_fn; /// /// async fn multiply(x: i32) -> i32 { /// let batched_multiply = batched_fn! { /// handler = |batch: Vec<i32>, factor: &i32| -> Vec<i32> { /// batch.into_iter().map(|x| *factor * x ).collect() /// }; /// config = { /// max_batch_size: 4, /// delay: 50, /// }; /// context = { /// factor: 3, /// }; /// }; /// /// batched_multiply(x).await /// } /// ``` #[macro_export] macro_rules! batched_fn { ( handler = |$batch:ident: $batch_input_type:ty $(, $ctx_arg:ident: &$ctx_arg_ty:ty )*| -> $batch_output_type:ty $fn_body:block ; config = { max_batch_size: $max_batch_size:expr, delay: $delay:expr $(,)? }; context = { $( $ctx:ident: $ctx_init:expr ),* $(,)? } $(;)? ) => {{ static BATCHED_FN: $crate::Lazy< $crate::BatchedFn< <$batch_input_type as $crate::Batch>::Item, <$batch_output_type as $crate::Batch>::Item, >, > = $crate::Lazy::new(|| { let (tx, mut rx) = std::sync::mpsc::channel::<( <$batch_input_type as $crate::Batch>::Item, $crate::UnboundedSender<<$batch_output_type as $crate::Batch>::Item>, )>(); std::thread::spawn(move || { // Create handler closure. let handler = |$batch: $batch_input_type $(, $ctx_arg: &$ctx_arg_ty )*| -> $batch_output_type { $fn_body }; // Set config vars. let max_batch_size: usize = $max_batch_size; let delay: u128 = $delay; // Initialize handler context. struct _Context { $( $ctx_arg: $ctx_arg_ty, )* } let context = _Context { $( $ctx: $ctx_init, )* }; // Wait for an input. while let Ok((input, result_tx)) = rx.recv() { let mut batch_input = <$batch_input_type as $crate::Batch>::with_capacity(max_batch_size); let mut batch_txs = Vec::with_capacity(max_batch_size); batch_input.push(input); batch_txs.push(result_tx); let mut vacancy = max_batch_size - 1; let mut time_left = delay as u64; let start = std::time::Instant::now(); // While there is still room in the batch we'll wait at most `delay` // milliseconds to try to fill it. while vacancy > 0 && time_left > 0 { if let Ok((next_input, next_result_tx)) = rx.recv_timeout(std::time::Duration::from_millis(time_left)) { batch_input.push(next_input); batch_txs.push(next_result_tx); vacancy -= 1; let elapsed = start.elapsed().as_millis(); time_left = if elapsed > delay { 0 } else { (delay - elapsed) as u64 }; } else { break; } } let batch_output = handler(batch_input $(, &context.$ctx_arg )*); for (output, mut result_tx) in batch_output.into_iter().zip(batch_txs) { result_tx.send(output).unwrap(); } } }); $crate::BatchedFn::new(tx) }); |input| BATCHED_FN.evaluate_in_batch(input) }}; }