Skip to content

Commit d77ef79

Browse files
committed
docs
1 parent 0cb8227 commit d77ef79

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/llm/src/block_manager/block/registry.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
//! # KV Cache Block Registration
17+
//!
18+
//! - This module is responsible for maintaining a registry of all blocks currently within a pool.
19+
//! This consists of two components: A global registry of all blocks, and a per-pool registry of blocks.
20+
//! - The global registry is a mapping of sequences hashes to registration handles. If two blocks in different pools
21+
//! have the same sequence hash, then they will share the same registration handle. The global registry is shared across all pools.
22+
//! - The per-pool registry is a mapping of sequence hashes to block handles. This is used to track which blocks are
23+
//! currently within a specific pool. The block handle is unique across pools, and is used to track the block's lifetime.
24+
//! - When a block is in the registered state, it has a unique block handle and a possibly shared registration handle.
25+
//!
26+
//! ## Workflow
27+
//!
28+
//! 1. When a block is registered into a pool, we create a unique block handle.
29+
//! 2. We then check the global registry to see if the block already exists in any other pool.
30+
//! 3. If it does, we use the existing registration handle. Otherwise, we create a new one.
31+
//! 4. When the block handle is dropped, it means that the block is no longer in the pool.
32+
//! 5. When the registration handle is dropped, it means that the block is no longer in any pool.
33+
1634
use std::{
1735
collections::HashMap,
1836
sync::{Arc, Mutex, Weak},
@@ -80,6 +98,7 @@ impl BlockRegistry {
8098

8199
BlockState::Complete(state) => {
82100
let sequence_hash = state.token_block().sequence_hash();
101+
// If an identical block already exists in this pool, return an error.
83102
if let Some(handle) = self.blocks.get(&sequence_hash) {
84103
if let Some(_handle) = handle.upgrade() {
85104
return Err(BlockRegistationError::BlockAlreadyRegistered(sequence_hash));
@@ -91,25 +110,30 @@ impl BlockRegistry {
91110
let block_handle = Arc::new(());
92111

93112
let reg_handle = 'reg_block: {
113+
// Now, check the global registry.
94114
let mut global_pool = self.global_pool.lock().unwrap();
95115

116+
// If an identical block exists in other pool, use the same registration handle.
96117
if let Some(handle) = global_pool.get(&sequence_hash) {
97118
if let Some(handle) = handle.upgrade() {
98119
break 'reg_block handle;
99120
}
100121
}
101122

123+
// Otherwise, create a new registration handle.
102124
publish_handle = Some(Self::create_publish_handle(
103125
state.token_block(),
104126
self.event_manager.clone(),
105127
));
106128
let reg_handle = publish_handle.as_ref().unwrap().remove_handle();
107129

130+
// Insert the registration handle into the global registry.
108131
global_pool.insert(sequence_hash, Arc::downgrade(&reg_handle));
109132

110133
reg_handle
111134
};
112135

136+
// Insert our block handle into the per-pool registry.
113137
self.blocks
114138
.insert(sequence_hash, Arc::downgrade(&block_handle));
115139

lib/llm/src/block_manager/pool.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ use priority_key::PriorityKey;
6969
pub use super::block::{ImmutableBlock, MutableBlock};
7070

7171
use super::block::{
72-
nixl::short_type_name, registry::BlockRegistry, Block, BlockError, BlockMetadata, GlobalRegistry
72+
nixl::short_type_name, registry::BlockRegistry, Block, BlockError, BlockMetadata,
73+
GlobalRegistry,
7374
};
7475
use super::events::{EventManager, NullEventManager};
7576
use super::storage::Storage;

lib/llm/src/block_manager/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::*;
1717

1818
use super::offload::OffloadManager;
1919
use super::{
20-
block::{Block, ImmutableBlock, GlobalRegistry},
20+
block::{Block, GlobalRegistry, ImmutableBlock},
2121
config::NixlOptions,
2222
pool::BlockPoolError,
2323
};

0 commit comments

Comments
 (0)