"vllm/vscode:/vscode.git/clone" did not exist on "00b14e0f16927fe94e4ee68ffaf9c4857c9e400e"
sequence.rs 18.8 KB
Newer Older
1
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
// SPDX-License-Identifier: Apache-2.0

4
use crate::common::protocols::MoveBlock;
5
use derive_getters::Getters;
6
7
use dynamo_tokens::blocks::UniqueBlock;
use dynamo_tokens::{TokenBlockSequence, Tokens};
8
use rand::random;
9
use validator::Validate;
10
11
12
13

/// Create unique blocks from a TokenBlockSequence
fn create_unique_blocks_from_sequence(
    tokens: &TokenBlockSequence,
14
15
    block_size: usize,
    enable_prefix_caching: bool,
16
17
18
19
) -> Vec<UniqueBlock> {
    let mut unique_blocks: Vec<UniqueBlock> = tokens
        .blocks()
        .iter()
20
21
22
23
24
25
26
        .map(|block| {
            if enable_prefix_caching {
                UniqueBlock::FullBlock(block.sequence_hash())
            } else {
                UniqueBlock::FullBlock(random::<u64>())
            }
        })
27
28
29
        .collect();

    // Only push the partial block if tokens count isn't a multiple of block_size
30
    if !tokens.total_tokens().is_multiple_of(block_size) {
Yan Ru Pei's avatar
Yan Ru Pei committed
31
        unique_blocks.push(UniqueBlock::default());
32
33
34
35
36
37
    }
    unique_blocks
}

/// A sequence that is actively being built, with the ability to add tokens and commit to hashes
/// TODO: reuse tokens
38
#[derive(Debug, Getters, Validate)]
39
40
41
42
43
44
pub struct ActiveSequence {
    unique_blocks: Vec<UniqueBlock>,

    tokens: TokenBlockSequence,

    #[getter(copy)]
45
    #[validate(range(min = 2))]
46
    block_size: usize,
47
48
49
50
51
52
53
54
55
56

    #[getter(copy)]
    max_output_tokens: usize,

    #[getter(copy)]
    generated_tokens: usize,

    #[getter(copy)]
    num_input_tokens: usize,

57
58
    #[getter(copy)]
    num_allocated_tokens: usize,
59
60
61

    #[getter(copy)]
    enable_prefix_caching: bool,
62
63
64

    #[getter(copy)]
    emit_token_ids: bool,
65
66
67
68
69
70
71
}

impl ActiveSequence {
    /// Create a new ActiveSequence instance with the provided tokens
    pub fn new(
        tokens: Vec<u32>,
        max_output_tokens: usize,
72
73
        block_size: Option<usize>,
        enable_prefix_caching: bool,
74
        emit_token_ids: bool,
75
76
77
78
    ) -> Self {
        let block_size = block_size.unwrap_or(64);
        let num_input_tokens = tokens.len();

79
        let tokens = Tokens::from(tokens).into_sequence(block_size as u32, Some(1337));
80
        let unique_blocks =
Yan Ru Pei's avatar
Yan Ru Pei committed
81
            create_unique_blocks_from_sequence(&tokens, block_size, enable_prefix_caching);
82

83
        let seq = Self {
84
85
86
87
88
89
            unique_blocks,
            tokens,
            block_size,
            max_output_tokens,
            generated_tokens: 0,
            num_input_tokens,
90
            num_allocated_tokens: 0,
91
            enable_prefix_caching,
92
            emit_token_ids,
93
94
95
        };
        seq.validate().expect("invalid ActiveSequence");
        seq
96
97
    }

98
    pub fn extra_tokens(&self) -> u32 {
99
        (self.len() % self.block_size) as u32
100
101
102
103
104
105
106
107
108
109
    }

    pub fn len(&self) -> usize {
        self.tokens.total_tokens()
    }

    pub fn is_empty(&self) -> bool {
        self.tokens.total_tokens() == 0
    }

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
    /// Build a `MoveBlock::Use` signal for blocks up to `cumulative_tokens`
    /// without updating internal state. Returns `None` if no new blocks are needed.
    /// Call `commit_allocation` after the signal is successfully processed.
    pub fn prepare_allocation(&self, cumulative_tokens: usize) -> Option<MoveBlock> {
        let prev_blocks = self
            .num_allocated_tokens
            .div_ceil(self.block_size)
            .min(self.unique_blocks.len());
        let target_blocks = cumulative_tokens
            .div_ceil(self.block_size)
            .min(self.unique_blocks.len());
        if target_blocks <= prev_blocks {
            return None;
        }

        let range = prev_blocks..target_blocks;
        let blocks = self.unique_blocks[range.clone()].to_vec();

        let all_hashes = self.block_hashes();
        let num_full = all_hashes.len();
        let hash_start = prev_blocks.min(num_full);
        let hash_end = target_blocks.min(num_full);
        let hashes = all_hashes[hash_start..hash_end].to_vec();

        let token_ids = if self.emit_token_ids && hash_start < hash_end {
            let all_token_ids: Vec<Vec<u32>> = self
                .tokens
                .blocks()
                .iter()
                .map(|b| b.tokens().to_vec())
                .collect();
            Some(all_token_ids[hash_start..hash_end].to_vec())
        } else {
            None
        };

146
147
148
149
150
151
        let parent = if prev_blocks > 0 {
            Some(self.unique_blocks[prev_blocks - 1].clone())
        } else {
            None
        };
        Some(MoveBlock::Use(blocks, hashes, token_ids, parent))
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    }

    /// Commit a successful allocation by advancing `num_allocated_tokens`.
    pub fn commit_allocation(&mut self, cumulative_tokens: usize) {
        self.num_allocated_tokens = cumulative_tokens;
    }

    /// Prepare + commit in one call (convenience for paths where failure is impossible).
    pub fn allocate_blocks_for_chunk(&mut self, cumulative_tokens: usize) -> Option<MoveBlock> {
        let signal = self.prepare_allocation(cumulative_tokens);
        self.commit_allocation(cumulative_tokens);
        signal
    }

    /// Allocate all remaining blocks at once (backward compat).
167
    pub fn take_creation_signal(&mut self) -> Option<MoveBlock> {
168
        self.allocate_blocks_for_chunk(self.len())
169
170
    }

171
172
173
174
175
176
177
178
    pub fn block_hashes(&self) -> Vec<u64> {
        self.tokens
            .blocks()
            .iter()
            .map(|block| block.block_hash())
            .collect()
    }

179
180
181
182
    /// Create a new ActiveSequence instance and return the creation signal
    pub fn new_with_signal(
        tokens: Vec<u32>,
        max_output_tokens: usize,
183
184
        block_size: Option<usize>,
        enable_prefix_caching: bool,
185
    ) -> (Self, Option<MoveBlock>) {
186
187
188
189
190
191
192
        let mut sequence = Self::new(
            tokens,
            max_output_tokens,
            block_size,
            enable_prefix_caching,
            false,
        );
193
        let signal = sequence.take_creation_signal();
194
195
196
197
198
199
200
201
        (sequence, signal)
    }

    /// Push a token to the sequence
    pub fn push(&mut self, token: u32) -> Option<Vec<MoveBlock>> {
        self.tokens.append(token).expect("Token push failed.");
        self.generated_tokens += 1;

202
        if self.len() % self.block_size != 1 {
203
204
205
206
207
208
209
210
211
            return None;
        }

        // Add a partial block for the first token in a new partial sequence
        // Send Use signal (to allocate space for this new generation block)
        let mut signals = Vec::new();

        // Replace last partial block with full block if it exists
        if let Some(UniqueBlock::PartialBlock(uuid)) = self.unique_blocks.last().cloned() {
212
            let last_complete = self.tokens.last_complete_block().unwrap();
Yan Ru Pei's avatar
Yan Ru Pei committed
213
            let last_seq_hash = if self.enable_prefix_caching {
214
                last_complete.sequence_hash()
215
216
217
            } else {
                random::<u64>()
            };
218
219
220
221
222
223
            let last_block_hash = last_complete.block_hash();
            let promote_token_ids = if self.emit_token_ids {
                Some(last_complete.tokens().to_vec())
            } else {
                None
            };
224
            self.unique_blocks.pop();
Yan Ru Pei's avatar
Yan Ru Pei committed
225
226
227
228
229
230
231

            // After pop, the last element is the parent block
            let second_to_last_hash = self.unique_blocks.last().map(|block| match block {
                UniqueBlock::FullBlock(hash) => *hash,
                UniqueBlock::PartialBlock(_) => panic!("Cannot have a partial block as parent"),
            });

232
            self.unique_blocks
Yan Ru Pei's avatar
Yan Ru Pei committed
233
                .push(UniqueBlock::FullBlock(last_seq_hash));
234
235
            signals.push(MoveBlock::Promote(
                uuid,
Yan Ru Pei's avatar
Yan Ru Pei committed
236
237
                last_seq_hash,
                second_to_last_hash,
238
                last_block_hash,
239
                promote_token_ids,
240
            ));
241
242
243
244
        }

        let new_partial_block = UniqueBlock::default();
        self.unique_blocks.push(new_partial_block.clone());
245
        signals.push(MoveBlock::Use(vec![new_partial_block], vec![], None, None));
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
        Some(signals)
    }

    /// Generate a random token, push it to the sequence, and increment generation count.
    ///
    /// This function:
    /// - Generates a random token and adds it to the current sequence
    /// - Acquires a new partial block if needed or promotes an existing partial block to a full block
    /// - Returns appropriate signals for the KvManager to process
    ///
    /// # Panics
    ///
    /// Calling this function when max_output_tokens has already been reached will cause a panic.
    /// Always check `generated_tokens < max_output_tokens` before calling this method.
    pub fn generate(&mut self) -> Vec<MoveBlock> {
        // Assert that we haven't reached the maximum output tokens
        assert!(
            self.generated_tokens < self.max_output_tokens,
            "Cannot generate more tokens: reached max_output_tokens limit"
        );

        // Generate a random token
        let token = random::<u32>();

        // Collect signals
        let mut signals = Vec::new();

        // Push the token to the sequence and collect any signals
        if let Some(move_blocks) = self.push(token) {
            signals.extend(move_blocks);
        }

        // Check if we've reached the limit after pushing
        if self.generated_tokens != self.max_output_tokens {
            return signals;
        }

        // Free all blocks when we reach max tokens
        signals.extend(self.free_signal());
        signals
    }

    /// Free all blocks, generating appropriate signals for each block type
    pub fn free_signal(&self) -> Vec<MoveBlock> {
        self.unique_blocks
            .iter()
            .rev()
            .map(|block| match block {
                UniqueBlock::PartialBlock(uuid) => {
                    MoveBlock::Destroy(vec![UniqueBlock::PartialBlock(*uuid)])
                }
                UniqueBlock::FullBlock(hash) => {
                    MoveBlock::Deref(vec![UniqueBlock::FullBlock(*hash)])
                }
            })
            .collect()
    }

304
    /// Move the request to a preempted state and return the free signals from freeing current blocks.
305
    /// Upon preemption, the sequence retains the tokens generated during the decode phase (if any).
306
    /// Resets `num_allocated_tokens` so re-admission will re-allocate from scratch.
307
308
    pub fn reset_with_signal(&mut self) -> Vec<MoveBlock> {
        let free_signal = self.free_signal();
309
        self.num_allocated_tokens = 0;
310
311
312
313
314
315
316
317
318
        free_signal
    }

    /// Pops last token in the sequence.
    pub fn pop(&mut self) {
        self.tokens.pop();
        self.generated_tokens = self.generated_tokens.saturating_sub(1);

        // Reverts to the last full block
319
        if self.tokens.total_tokens().is_multiple_of(self.block_size) {
320
321
322
323
324
325
326
327
328
329
330
331
332
333
            self.unique_blocks.pop();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_active_sequence_push() {
        // Create a sequence with block size 16 initialized with tokens [0..15]
        let initial_tokens: Vec<u32> = (0..15).collect();
        let (mut seq1, signal1) =
334
            ActiveSequence::new_with_signal(initial_tokens, 100, Some(16), true);
335
336
337
338
339
340
        assert_eq!(seq1.num_input_tokens(), 15);
        assert_eq!(seq1.len(), 15);

        // Check that we got a Use signal
        assert!(signal1.is_some());
        match &signal1 {
341
            Some(MoveBlock::Use(blocks, _hashes, ..)) => {
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
                assert_eq!(blocks.len(), 1);
            }
            _ => panic!("Expected Use signal"),
        }

        // Push token 15 which should complete the block (no signals yet)
        let signal_15 = seq1.push(15);
        assert!(
            signal_15.is_none(),
            "Completing a block should not trigger signals"
        );

        // Push token 16 which should trigger both Promote and Use signals
        let signal_16 = seq1.push(16);
        assert!(signal_16.is_some());
        let signal_16 = signal_16.unwrap();
        assert_eq!(signal_16.len(), 2);

360
361
        // First signal should be Promote for the previous block
        match &signal_16[0] {
362
            MoveBlock::Promote(_, _, parent_hash, _hash, ..) => {
363
364
365
366
367
                assert_eq!(*parent_hash, None);
            }
            _ => panic!("Expected Promote signal as second signal"),
        }

368
369
        // Second signal should be Use for new partial block
        match &signal_16[1] {
370
            MoveBlock::Use(blocks, _hashes, ..) => {
371
372
373
374
375
376
377
378
379
                assert_eq!(blocks.len(), 1);
                assert!(matches!(blocks[0], UniqueBlock::PartialBlock(_)));
            }
            _ => panic!("Expected Use signal as first signal"),
        }

        // Verify state after pushing tokens
        assert_eq!(seq1.unique_blocks().len(), 2); // One full block and one partial block
        assert_eq!(seq1.len(), 17);
380
        assert_eq!(seq1.len() % seq1.block_size(), 1);
381
382
383

        // Create another sequence with block size 16 initialized with tokens [0..17]
        let extended_tokens: Vec<u32> = (0..16).collect();
384
        let (mut seq2, _) = ActiveSequence::new_with_signal(extended_tokens, 100, Some(16), true);
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
        seq2.push(16);
        seq2.pop();
        seq2.push(16);

        // Simplified assertions
        assert_eq!(
            seq1.unique_blocks()[0],
            seq2.unique_blocks()[0],
            "First blocks should be the same"
        );

        assert_ne!(
            seq1.unique_blocks()[1],
            seq2.unique_blocks()[1],
            "Second blocks should be different"
        );

        // Reset partial block on seq1 and push back token 16
        seq1.push(17);
        seq1.pop();
        seq1.pop();
        seq1.push(16);

        // Now push tokens 17..32 to both sequences
        for token in 17..33 {
            seq1.push(token);
            seq2.push(token);
        }

        // Both sequences should now have 2 blocks:
        // 1. FullBlock for tokens 0-15
        // 2. FullBlock for tokens 16-31
        // 3. No partial block since there are no remaining tokens
        assert_eq!(
            seq1.unique_blocks().len(),
            3,
            "seq1 should have exactly 3 blocks"
        );
        assert_eq!(
            seq2.unique_blocks().len(),
            3,
            "seq2 should have exactly 3 blocks"
        );
        assert_eq!(
429
            seq1.len() % seq1.block_size(),
430
431
432
433
            1,
            "seq1 should have 1 partial token"
        );
        assert_eq!(
434
            seq2.len() % seq2.block_size(),
435
436
437
438
439
440
441
442
443
444
445
            1,
            "seq2 should have 1 partial token"
        );

        // Verify that both sequences have identical blocks up to the second position
        assert_eq!(
            &seq1.unique_blocks()[0..2],
            &seq2.unique_blocks()[0..2],
            "First two blocks should be identical"
        );

446
447
448
449
450
451
452
453
454
455
456
        // Push tokens 34..47 to seq1
        for token in 33..48 {
            seq1.push(token);
        }

        // Push token 48 and get the signal - this completes the block and triggers signals
        let signal = seq1.push(48);
        let signal = signal.unwrap();

        // Check that signal[0] is promote
        match &signal[0] {
457
            MoveBlock::Promote(_, _, parent_hash, _hash, ..) => {
458
459
460
461
462
463
464
465
466
467
468
469
470
471
                // Check that the parent_hash matches unique_blocks[1], which should be a full block
                if let UniqueBlock::FullBlock(expected_hash) = seq1.unique_blocks()[1] {
                    assert_eq!(
                        *parent_hash,
                        Some(expected_hash),
                        "Parent hash should match unique_blocks[1]"
                    );
                } else {
                    panic!("unique_blocks[1] should be a full block");
                }
            }
            _ => panic!("Expected Promote signal as first signal"),
        }

472
473
474
        // Reset seq1 and check that it equals the original clone
        let free_signals = seq1.reset_with_signal();

475
        // 49 - 15 generated tokens
476
        assert_eq!(seq1.generated_tokens(), 34);
477

478
479
480
481
482
483
484
485
        // Verify the reset signals include proper cleanup events
        assert!(!free_signals.is_empty());
    }

    #[test]
    fn test_active_sequence_generate_signals() {
        // Create a sequence with block size 16, max_output_tokens 4, initialized with tokens [0..14)
        let initial_tokens: Vec<u32> = (0..14).collect();
486
        let (mut seq, signal) = ActiveSequence::new_with_signal(initial_tokens, 5, Some(16), true);
487
488
489
490

        // Initial signal - should have received a Use signal for the partial block
        assert!(signal.is_some());
        match signal {
491
            Some(MoveBlock::Use(blocks, _hashes, ..)) => {
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
                assert_eq!(blocks.len(), 1);
                assert!(matches!(blocks[0], UniqueBlock::PartialBlock(_)));
            }
            _ => panic!("Expected Use signal for the initial partial block"),
        }

        // Generate first two tokens - should not trigger new signals
        seq.generate();
        let signals_first = seq.generate();
        assert_eq!(signals_first.len(), 0);

        // Generate third token - this fills the block and should trigger both Promote and Use signals
        let signals_second = seq.generate();
        assert_eq!(signals_second.len(), 2);

507
508
        // First signal should be Promote
        match &signals_second[0] {
509
            MoveBlock::Promote(_, _, parent_hash, _hash, ..) => {
510
511
512
513
514
515
                assert_eq!(*parent_hash, None);
            }
            _ => panic!("Expected Promote signal as first signal after second token"),
        }

        // Second signal should be Use for new partial block
516
        match &signals_second[1] {
517
            MoveBlock::Use(blocks, _hashes, ..) => {
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
                assert_eq!(blocks.len(), 1);
                assert!(matches!(blocks[0], UniqueBlock::PartialBlock(_)));
            }
            _ => panic!("Expected Use signal as second signal after second token"),
        }

        // Generate fourth token - should not trigger new signals as it's adding to partial block
        let signals_third = seq.generate();
        assert_eq!(signals_third.len(), 0);

        // Generate last token - we reach max_output_tokens, should trigger Destroy and Deref signals
        let signals_last = seq.generate();
        assert_eq!(signals_last.len(), 2);

        // First signal should be Destroy for the partial block
        match &signals_last[0] {
            MoveBlock::Destroy(blocks) => {
                assert_eq!(blocks.len(), 1);
                assert!(matches!(blocks[0], UniqueBlock::PartialBlock(_)));
            }
            _ => panic!("Expected Destroy signal for partial block after fourth token"),
        }

        // Second signal should be Deref for the full block
        match &signals_last[1] {
            MoveBlock::Deref(blocks) => {
                assert_eq!(blocks.len(), 1);
                assert!(matches!(blocks[0], UniqueBlock::FullBlock(_)));
            }
            _ => panic!("Expected Deref signal for full block after fourth token"),
        }
    }
}