-
Notifications
You must be signed in to change notification settings - Fork 891
Expand file tree
/
Copy pathblock_manager.rs
More file actions
333 lines (286 loc) · 11 KB
/
block_manager.rs
File metadata and controls
333 lines (286 loc) · 11 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
use anyhow::Result;
use dynamo_llm::block_manager::block::{
data::logical::distributed_leader_worker::DistributedLeaderWorkerResources, locality::Logical,
};
use dynamo_llm::block_manager::{BasicMetadata, BlockParallelismStrategy};
use pyo3::PyResult;
use tokio_util::sync::CancellationToken;
mod controller;
mod distributed;
pub mod vllm;
/// Add bingings from this crate to the provided module
pub fn add_to_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<BlockManager>()?;
m.add_class::<distributed::KvbmWorker>()?;
m.add_class::<distributed::KvbmLeader>()?;
m.add_class::<controller::BlockManagerClient>()?;
m.add_class::<controller::BlockPoolStatus>()?;
m.add_class::<controller::ResetBlocksResponse>()?;
vllm::add_to_module(m)?;
Ok(())
}
type VllmBlockManager = dynamo_llm::block_manager::KvBlockManager<
Logical<DistributedLeaderWorkerResources>,
BasicMetadata,
>;
type VllmController = Arc<
dynamo_llm::block_manager::controller::Controller<
Logical<DistributedLeaderWorkerResources>,
BasicMetadata,
>,
>;
#[pyclass]
#[derive(Clone)]
pub struct BlockManager {
inner: VllmBlockManager,
drt: DistributedRuntime,
_controller: Option<VllmController>,
}
// TODO: This is in desperate need of a massive refactor. We bind and instantiate this in Python, but we never actually use it.
#[pymethods]
#[allow(unused_variables)]
impl BlockManager {
#[new]
#[pyo3(signature = (worker_id, leader = None, page_size = 32, num_device_blocks = None, disable_device_pool = false))]
fn new(
worker_id: u64,
leader: Option<distributed::KvbmLeader>,
page_size: usize,
num_device_blocks: Option<usize>,
disable_device_pool: bool,
) -> PyResult<Self> {
let cancel_token = CancellationToken::new();
let mut config = dynamo_llm::block_manager::KvBlockManagerConfig::builder().runtime(
dynamo_llm::block_manager::KvManagerRuntimeConfig::builder()
.worker_id(worker_id)
.cancellation_token(cancel_token.clone())
.build()
.map_err(to_pyerr)?,
);
let model_config = dynamo_llm::block_manager::KvManagerModelConfig::builder()
.num_layers(1)
.outer_dim(1)
.page_size(page_size)
.inner_dim(1);
config = config.model(model_config.build().map_err(to_pyerr)?);
let (leader, drt) = if let Some(leader) = leader {
let (leader, rt) = leader.dissolve();
if !disable_device_pool {
config = config.device_layout(
dynamo_llm::block_manager::KvManagerLayoutConfig::builder()
.num_blocks(leader.num_device_blocks())
.logical(Some(BlockParallelismStrategy::LeaderWorkerSharded))
.build()
.map_err(to_pyerr)?,
);
}
if leader.num_host_blocks() > 0 {
tracing::info!("Using {} host blocks", leader.num_host_blocks());
config = config.host_layout(
dynamo_llm::block_manager::KvManagerLayoutConfig::builder()
.num_blocks(leader.num_host_blocks())
.logical(Some(BlockParallelismStrategy::LeaderWorkerSharded))
.build()
.map_err(to_pyerr)?,
);
}
if leader.num_disk_blocks() > 0 {
tracing::info!("Using {} disk blocks", leader.num_disk_blocks());
config = config.disk_layout(
dynamo_llm::block_manager::KvManagerLayoutConfig::builder()
.num_blocks(leader.num_disk_blocks())
.logical(Some(BlockParallelismStrategy::LeaderWorkerSharded))
.build()
.map_err(to_pyerr)?,
);
}
(Some(leader), rt)
} else {
tracing::info!("Leader not provided. Block transfer functionality will be disabled.");
// let num_device_blocks = num_device_blocks
// .expect("num_device_blocks must be provided if leader is not provided");
// config = config.device_layout(
// dynamo_llm::block_manager::KvManagerLayoutConfig::builder()
// .num_blocks(num_device_blocks)
// .logical(Some(BlockParallelismStrategy::LeaderWorkerSharded))
// .build()
// .map_err(to_pyerr)?,
// );
unimplemented!("Leader not provided");
// (
// None,
// Arc::new(
// tokio::runtime::Builder::new_multi_thread()
// .enable_all()
// .build()
// .map_err(to_pyerr)?,
// ),
// )
};
let rt = drt.inner().runtime().primary();
let config = config.build().map_err(to_pyerr)?;
Ok(BlockManager {
inner: rt
.block_on(async {
let resources =
DistributedLeaderWorkerResources::new(leader, cancel_token.child_token())?;
dynamo_llm::block_manager::KvBlockManager::<
Logical<DistributedLeaderWorkerResources>,
BasicMetadata,
>::new(config, resources)
.await
})
.map_err(to_pyerr)?,
drt,
_controller: None,
})
}
fn block_size(&self) -> usize {
self.inner.block_size()
}
fn init_controller(&mut self, component: Component) -> PyResult<()> {
if self._controller.is_some() {
tracing::warn!("Controller already initialized. Ignoring init_controller call.");
return Ok(());
}
let block_manager = self.inner.clone();
let controller = self
.drt
.inner()
.runtime()
.primary()
.block_on(controller::Controller::new(
block_manager,
component.inner.clone(),
))
.map_err(to_pyerr)?;
self._controller = Some(Arc::new(controller));
let instance_id = component
.inner
.drt()
.primary_lease()
.map(|lease| lease.id())
.ok_or_else(|| to_pyerr(anyhow::anyhow!("no instance id")))?;
tracing::info!(
"Dynamo KVBM Controller: {}.{}:{}",
component.inner.namespace().name(),
component.inner.name(),
instance_id
);
Ok(())
}
}
impl BlockManager {
#[inline(always)]
pub fn get_block_manager(&self) -> &VllmBlockManager {
&self.inner
}
}
#[derive(Default)]
pub struct BlockManagerBuilder {
worker_id: u64,
leader: Option<distributed::KvbmLeader>,
page_size: usize,
disable_device_pool: bool,
}
impl BlockManagerBuilder {
pub fn new() -> Self {
Self {
page_size: 32, // default consistent with BlockManager::new
..Default::default()
}
}
pub fn worker_id(mut self, id: u64) -> Self {
self.worker_id = id;
self
}
pub fn page_size(mut self, ps: usize) -> Self {
self.page_size = ps;
self
}
pub fn leader(mut self, l: distributed::KvbmLeader) -> Self {
self.leader = Some(l);
self
}
pub fn disable_device_pool(mut self, yes: bool) -> Self {
self.disable_device_pool = yes;
self
}
/// Async build (call from an async context).
pub async fn build(self) -> Result<BlockManager> {
let worker_id = self.worker_id;
let leader = self.leader.ok_or_else(|| {
anyhow::anyhow!("leader is required (runtime is always taken from leader)")
})?;
// Get (inner leader handle, runtime) from the provided leader.
let (leader_inner, drt) = leader.dissolve();
let cancel_token = CancellationToken::new();
// Runtime & model config
let runtime_config = dynamo_llm::block_manager::KvManagerRuntimeConfig::builder()
.worker_id(worker_id)
.cancellation_token(cancel_token.clone())
.build()?;
let mut config =
dynamo_llm::block_manager::KvBlockManagerConfig::builder().runtime(runtime_config);
let model_config = dynamo_llm::block_manager::KvManagerModelConfig::builder()
.num_layers(1)
.outer_dim(1)
.page_size(self.page_size)
.inner_dim(1)
.build()?;
config = config.model(model_config);
// Layouts derived from leader’s counts
if !self.disable_device_pool {
config = config.device_layout(
dynamo_llm::block_manager::KvManagerLayoutConfig::builder()
.num_blocks(leader_inner.num_device_blocks())
.logical(Some(BlockParallelismStrategy::LeaderWorkerSharded))
.build()?,
);
}
if leader_inner.num_host_blocks() > 0 {
config = config.host_layout(
dynamo_llm::block_manager::KvManagerLayoutConfig::builder()
.num_blocks(leader_inner.num_host_blocks())
.logical(Some(BlockParallelismStrategy::LeaderWorkerSharded))
.build()?,
);
}
if leader_inner.num_disk_blocks() > 0 {
config = config.disk_layout(
dynamo_llm::block_manager::KvManagerLayoutConfig::builder()
.num_blocks(leader_inner.num_disk_blocks())
.logical(Some(BlockParallelismStrategy::LeaderWorkerSharded))
.build()?,
);
}
let config = config.build()?;
let resources =
DistributedLeaderWorkerResources::new(Some(leader_inner), cancel_token.child_token())?;
let inner = dynamo_llm::block_manager::KvBlockManager::<
Logical<DistributedLeaderWorkerResources>,
BasicMetadata,
>::new(config, resources)
.await?;
Ok(BlockManager {
inner,
drt,
_controller: None,
})
}
}