"...v1alpha1/dynamographdeploymentrequest_conversion_test.go" did not exist on "7bbacce196c9058c522627e4f210286d6ebb7472"
test_utils.rs 4.61 KB
Newer Older
Yan Ru Pei's avatar
Yan Ru Pei committed
1
2
3
4
5
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Shared test utilities for radix tree tests.

6
7
use std::future;

Yan Ru Pei's avatar
Yan Ru Pei committed
8
use crate::protocols::{
9
10
11
    ActiveLoad, ActiveSequenceEvent, ExternalSequenceBlockHash, KvCacheEvent, KvCacheEventData,
    KvCacheRemoveData, KvCacheStoreData, KvCacheStoredBlockData, LocalBlockHash, RouterEvent,
    WorkerConfigLike, WorkerId, WorkerWithDpRank,
Yan Ru Pei's avatar
Yan Ru Pei committed
12
};
13
use crate::sequences::SequencePublisher;
Yan Ru Pei's avatar
Yan Ru Pei committed
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
pub fn router_event(
    worker_id: WorkerId,
    event_id: u64,
    dp_rank: u32,
    data: KvCacheEventData,
) -> RouterEvent {
    RouterEvent::new(
        worker_id,
        KvCacheEvent {
            event_id,
            data,
            dp_rank,
        },
    )
}

pub fn stored_blocks_with_sequence_hashes(
    local_hashes: &[LocalBlockHash],
    seq_hashes: &[u64],
) -> Vec<KvCacheStoredBlockData> {
    local_hashes
        .iter()
        .zip(seq_hashes.iter())
        .map(|(&local, &seq)| KvCacheStoredBlockData {
            tokens_hash: local,
            block_hash: ExternalSequenceBlockHash(seq),
            mm_extra_info: None,
        })
        .collect()
}

pub fn remove_event(
    worker_id: WorkerId,
    event_id: u64,
    dp_rank: u32,
    block_hashes: Vec<ExternalSequenceBlockHash>,
) -> RouterEvent {
    router_event(
        worker_id,
        event_id,
        dp_rank,
        KvCacheEventData::Removed(KvCacheRemoveData { block_hashes }),
    )
}

Yan Ru Pei's avatar
Yan Ru Pei committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/// Creates blocks with artificial hash mapping (hash * 100) for testing.
pub fn make_blocks(hashes: Vec<u64>) -> Vec<KvCacheStoredBlockData> {
    hashes
        .iter()
        .map(|i| KvCacheStoredBlockData {
            tokens_hash: LocalBlockHash(*i),
            block_hash: ExternalSequenceBlockHash(*i * 100),
            mm_extra_info: None,
        })
        .collect()
}

pub fn add_blocks(
    hashes: Vec<u64>,
    parent_hash: Option<ExternalSequenceBlockHash>,
75
76
77
78
79
80
81
82
) -> KvCacheEventData {
    add_blocks_with_start_position(hashes, parent_hash, None)
}

pub fn add_blocks_with_start_position(
    hashes: Vec<u64>,
    parent_hash: Option<ExternalSequenceBlockHash>,
    start_position: Option<u32>,
Yan Ru Pei's avatar
Yan Ru Pei committed
83
84
85
) -> KvCacheEventData {
    KvCacheEventData::Stored(KvCacheStoreData {
        parent_hash,
86
        start_position,
Yan Ru Pei's avatar
Yan Ru Pei committed
87
88
89
90
91
92
93
94
95
96
        blocks: make_blocks(hashes),
    })
}

pub fn create_store_event(
    worker_id: WorkerId,
    event_id: u64,
    hashes: Vec<u64>,
    parent: Option<ExternalSequenceBlockHash>,
) -> RouterEvent {
97
    router_event(worker_id, event_id, 0, add_blocks(hashes, parent))
Yan Ru Pei's avatar
Yan Ru Pei committed
98
99
}

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
pub fn create_store_event_with_start_position(
    worker_id: WorkerId,
    event_id: u64,
    hashes: Vec<u64>,
    parent: Option<ExternalSequenceBlockHash>,
    start_position: Option<u32>,
) -> RouterEvent {
    router_event(
        worker_id,
        event_id,
        0,
        add_blocks_with_start_position(hashes, parent, start_position),
    )
}

Yan Ru Pei's avatar
Yan Ru Pei committed
115
pub fn create_remove_event(worker_id: WorkerId, event_id: u64, hashes: Vec<u64>) -> RouterEvent {
116
    remove_event(
Yan Ru Pei's avatar
Yan Ru Pei committed
117
        worker_id,
118
119
120
121
122
123
124
        event_id,
        0,
        hashes
            .iter()
            .map(|i| ExternalSequenceBlockHash(*i * 100))
            .collect(),
    )
Yan Ru Pei's avatar
Yan Ru Pei committed
125
}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

/// No-op [`SequencePublisher`] for tests and benchmarks that don't need event transport.
pub struct NoopSequencePublisher;

impl SequencePublisher for NoopSequencePublisher {
    fn publish_event(
        &self,
        _event: &ActiveSequenceEvent,
    ) -> impl future::Future<Output = anyhow::Result<()>> + Send {
        future::ready(Ok(()))
    }

    fn publish_load(&self, _load: ActiveLoad) {}

    fn observe_load(&self, _: &WorkerWithDpRank, _: &str, _: usize, _: usize) {}
}

/// Minimal [`WorkerConfigLike`] for scheduler/queue tests and benchmarks.
144
#[derive(Debug, Clone, PartialEq, Eq)]
145
pub struct SimpleWorkerConfig {
146
    pub data_parallel_start_rank: u32,
147
148
149
150
151
152
153
154
    pub data_parallel_size: u32,
    pub max_num_batched_tokens: Option<u64>,
    pub total_kv_blocks: Option<u64>,
}

impl Default for SimpleWorkerConfig {
    fn default() -> Self {
        Self {
155
            data_parallel_start_rank: 0,
156
157
158
159
160
161
162
163
            data_parallel_size: 1,
            max_num_batched_tokens: None,
            total_kv_blocks: None,
        }
    }
}

impl WorkerConfigLike for SimpleWorkerConfig {
164
165
166
167
    fn data_parallel_start_rank(&self) -> u32 {
        self.data_parallel_start_rank
    }

168
169
170
171
172
173
174
175
176
177
178
179
    fn data_parallel_size(&self) -> u32 {
        self.data_parallel_size
    }

    fn max_num_batched_tokens(&self) -> Option<u64> {
        self.max_num_batched_tokens
    }

    fn total_kv_blocks(&self) -> Option<u64> {
        self.total_kv_blocks
    }
}