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
use async_trait;
use crate;
use ItemStream;
/// # Autoregressive
///
/// A trait for models that support autoregressive generation with batched processing.
/// ```rust
/// # use std::io;
/// use hibachi::autoregressive::Autoregressive;
/// use candle_core::{DType, Device, Tensor};
/// use futures::StreamExt;
/// use async_trait::async_trait;
///
///
/// pub struct Model {}
///
/// #[async_trait]
/// impl Autoregressive<Tensor> for Model {
///
/// async fn forward(&self, tensor: Tensor) -> Tensor {
/// // Extract the dimensions we need
/// let batch_size = tensor.dims()[0];
/// Tensor::ones(&[batch_size], tensor.dtype(), tensor.device()).unwrap()
/// }
/// }
///
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// let device = Device::Cpu;
/// let model = Model {};
///
/// let input = Tensor::zeros(&[3], DType::U8, &device).expect("creates start token");
/// let output = model.forward(input).await;
/// # Ok(())
/// # }
/// ```
///
/// This trait defines the core interface for models that generate output tokens
/// sequentially based on previously generated tokens. It is specifically designed
/// for use with the batched inference engine, which handles the mechanics of
/// efficiently processing multiple generation requests concurrently.
///
/// ## Input/Output Dimensions
///
/// The expected input dimensions are `(batch, seq, **tok_dimensions)`, where:
/// - `batch`: The batch size (number of sequences being processed)
/// - `seq`: The sequence length (number of tokens in each sequence)
/// - `**tok_dimensions`: Any additional dimensions describing token representations
///
/// The output dimensions are `(batch, **tok_dimensions)`, representing the next token
/// for each sequence in the batch.
///
/// ## Implementation Notes
///
/// When implementing this trait:
/// - The input tensor includes the full context of previously generated tokens
/// - The model should return logits or representations for the next token only
/// - The batching engine will automatically append generated tokens to the input
/// for subsequent generation steps
/// - The implementation should be compatible with the [`Backend`] and [`Unsqueezable`] constraints
///
///
/// ## Usage Context
///
/// This trait is primarily used by the batching engine to coordinate efficient
/// autoregressive generation across multiple requests. Models implementing this trait
/// can be plugged into the batching system to benefit from optimized resource utilization.
/// # AutoregressiveBatcher
///
/// A trait for components that manage batched processing of autoregressive generation requests.
///
/// The `AutoregressiveBatcher` is responsible for:
/// 1. Collecting multiple generation requests
/// 2. Batching them efficiently
/// 3. Scheduling their execution on the underlying model
/// 4. Streaming results back to the requesters
///
/// ## Type Parameters
///
/// * `T` - The input item type (typically a generation request with parameters)
/// * `Q` - The output item type (typically generated tokens or sequences)
///
/// ## Implementation Notes
///
/// Implementations of this trait should handle:
/// - Dynamic batch construction and management
/// - Efficient scheduling of model invocations
/// - Proper distribution of results to the correct output streams
/// - Resource management (e.g., ensuring that memory usage is bounded)
/// - Error handling and recovery
///
/// ## Usage Context
///
/// This trait represents the primary interface for clients to interact with
/// the batched inference engine. Clients submit generation requests through
/// the `run` method and receive results asynchronously through the returned
/// `ItemStream`.