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
//! # Feedforward Module
//!
//! This module provides infrastructure for efficient batch processing of feed-forward models.
//!
//! ```rust
//! # use std::io;
//! use hibachi::feedforward::*;
//! use candle_core::{Tensor, DType, Device};
//! use async_trait::async_trait;
//!
//! struct MyModel {
//! weights: Tensor,
//! }
//!
//! #[async_trait]
//! impl Feedforward<Tensor, Tensor> for MyModel {
//! async fn forward(&self, input: Tensor) -> Tensor {
//! input.matmul(&self.weights).unwrap()
//! }
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> io::Result<()> {
//! let device = Device::Cpu;
//! let model = MyModel { weights: Tensor::ones(&[64, 10], DType::F16, &device).unwrap() };
//! // Batcher with max batch size of 16
//! let batcher = FeedforwardBatchInference::<Tensor, Tensor, 16>::new(model);
//!
//! // Notice the singular dimension
//! let input = Tensor::ones(&[64], DType::F16, &device).expect("creates start token");
//! let result_item = batcher.run(input).await;
//!
//! let output = result_item.await.unwrap();
//! # Ok(())
//! # }
//! ```
//!
//! ## Overview
//!
//! Feed-forward models process inputs in a single pass without autoregressive behavior.
//! This module enables dynamic batching for such models, allowing multiple inference
//! requests to be processed together for improved throughput and resource utilization.
//!
//! ## Components
//!
//! The module consists of the following key components:
//!
//! - **Traits**:
//! - [`Feedforward`]: Defines the interface for feed-forward model implementations
//! - [`FeedforwardBatcher`]: Defines the interface for submitting inference requests
//!
pub use ;
pub use FeedforwardBatchInference;