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
use crate;
use ;
/// Identifies batch indices where generated tokens match the stop token.
///
/// This function compares each output token in the batch with the stop token
/// and returns the indices of batch elements that have generated the stop token.
///
/// # Parameters
///
/// * `outputs` - Tensor of shape `(batch, ...tok)` containing generated tokens
/// * `stop_token` - Tensor of shape `(1, ...tok)` representing the stop token
///
/// # Returns
///
/// A vector of batch indices (positions in dimension 0) where the generated token
/// exactly matches the stop token.
///
/// # Implementation Notes
///
/// The function:
/// 1. Repeats the stop token across the batch dimension to match the output shape
/// 2. Performs element-wise equality comparison between outputs and the repeated stop token
/// 3. Identifies indices where all elements in the token representation are equal
pub
/// Appends a new token to sequences in the active tensor.
///
/// This function concatenates newly generated tokens to their respective sequences
/// in the active tensor, extending each sequence by one token.
///
/// # Parameters
///
/// * `input` - Tensor of shape `(batch, seq, ...)` containing current sequences
/// * `output` - Tensor of shape `(batch, ...)` containing newly generated tokens
///
/// # Returns
///
/// A new tensor of shape `(batch, seq+1, ...)` with the new tokens appended
/// to each sequence in the batch.
///
/// # Implementation Notes
///
/// The function:
/// 1. Unsqueezes the output tensor to add a sequence dimension of size 1
/// 2. Concatenates the original input and the reshaped output along the sequence dimension
pub
/// Trims sequences to a specified maximum length.
///
/// When sequences grow beyond a certain length, this function trims them by
/// removing tokens from the beginning while preserving the most recent tokens
/// up to the specified maximum length.
///
/// # Parameters
///
/// * `tensor` - Optional tensor that may contain sequences to trim
/// * `max_sequence_length` - Maximum number of tokens to retain in each sequence
///
/// # Returns
///
/// An optional tensor with sequences trimmed to the specified maximum length.
/// If the input tensor is `None`, returns `None`. If `max_sequence_length` is 0,
/// returns a tensor with empty sequences.
///
/// # Implementation Notes
///
/// The function:
/// 1. Handles the case when `tensor` is `None` by returning `None`
/// 2. Handles zero length by returning an empty slice
/// 3. Otherwise, calculates the starting index to keep tokens from the end
/// 4. Slices the tensor along the sequence dimension to retain only recent tokens
/// Pads all sequences in the active tensor.
///
/// When new sequences longer than existing ones are added to the batch,
/// this function pads all existing sequences to match the new length.
///
/// # Parameters
///
/// * `active_tensor` - Tensor of shape `(batch, seq, ...)` containing existing sequences
/// * `amount` - Number of padding tokens to add to each sequence
/// * `padding_token` - Token to use for padding
///
/// # Returns
///
/// A new tensor with all sequences padded by the specified amount.
///
/// # Implementation Notes
///
/// The function:
/// 1. Creates a padding tensor by repeating the padding token
/// 2. Concatenates the padding to all sequences along the sequence dimension
pub
/// Adds a new sequence to the batch.
///
/// This function appends a new sequence to the existing batch of sequences
/// in the active tensor, increasing the batch size by one.
///
/// # Parameters
///
/// * `active_tensor` - Tensor of shape `(batch, seq, ...)` containing existing sequences
/// * `sequence` - Tensor of shape `(seq, ...)` representing the new sequence to add
///
/// # Returns
///
/// A new tensor with the additional sequence added to the batch.
///
/// # Implementation Notes
///
/// The function:
/// 1. Unsqueezes the sequence to add a batch dimension of size 1
/// 2. Concatenates the original tensor and the reshaped sequence along the batch dimension
pub
/// Removes a sequence from the batch.
///
/// When a sequence completes (by generating a stop token), this function
/// removes it from the active tensor to free up space in the batch.
///
/// # Parameters
///
/// * `active_tensor` - Optional tensor that may contain sequences
/// * `index` - Batch index of the sequence to remove
///
/// # Returns
///
/// An optional tensor with the specified sequence removed. If removing the last
/// sequence, returns `None` to indicate an empty batch.
///
/// # Implementation Notes
///
/// The function:
/// 1. Handles the case when `active_tensor` is `None` by returning `None`
/// 2. Checks if this is the last sequence in the batch (returns `None` if so)
/// 3. Otherwise, uses the `pop` operation to remove the sequence at the specified index
pub
/// Pads a single sequence to match the batch sequence length.
///
/// When adding a shorter sequence to a batch with longer sequences,
/// this function pads the sequence to match the required length.
///
/// # Parameters
///
/// * `sequence` - Tensor of shape `(seq, ...)` representing the sequence to pad
/// * `amount` - Number of padding tokens to add
/// * `padding_token` - Token to use for padding
///
/// # Returns
///
/// A new tensor with the sequence padded to the required length.
///
/// # Implementation Notes
///
/// The function:
/// 1. Creates a padding tensor by repeating the padding token
/// 2. Concatenates the padding with the original sequence along the sequence dimension
///
/// Note: The padding is added at the beginning of the sequence, preserving the
/// most recent tokens at the end.
pub
/// Splits a tensor into individual samples along the batch dimension.
///
/// This function divides a batched tensor into a vector of individual tensors,
/// one for each element in the batch.
///
/// # Parameters
///
/// * `tensor` - Tensor of shape `(batch, ...)` to split along the batch dimension
///
/// # Returns
///
/// A vector of tensors, each representing one element from the original batch.
///
/// # Implementation Notes
///
/// The function uses the `vectorize_dim` operation provided by the [`Backend`] trait
/// to split the tensor along the batch dimension (dimension 0).
///
/// This is typically used to distribute model outputs to the corresponding result streams.
pub