-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[V1] Add BlockTable class #11693
Merged
Merged
[V1] Add BlockTable class #11693
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4264a97
[V1] Add BlockTable abstraction
WoosukKwon b181413
Minor
WoosukKwon 8550fc8
Merge branch 'main' into v1-blocktable
WoosukKwon 66b6f81
Make BlockTable hardware agnostic
WoosukKwon 3bcc153
Merge branch 'main' into v1-blocktable
WoosukKwon 233f844
minor
WoosukKwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from typing import List | ||
|
||
import numpy as np | ||
import torch | ||
|
||
from vllm.logger import init_logger | ||
|
||
logger = init_logger(__name__) | ||
|
||
|
||
class BlockTable: | ||
|
||
def __init__( | ||
self, | ||
max_num_reqs: int, | ||
max_model_len: int, | ||
max_num_blocks_per_req: int, | ||
pin_memory: bool, | ||
device: torch.device, | ||
): | ||
self.max_num_reqs = max_num_reqs | ||
self.max_model_len = max_model_len | ||
self.max_num_blocks_per_req = max_num_blocks_per_req | ||
self.pin_memory = pin_memory | ||
self.device = device | ||
|
||
self.block_table = torch.zeros( | ||
(max_num_reqs, max_num_blocks_per_req), | ||
device=self.device, | ||
dtype=torch.int32, | ||
) | ||
self.block_table_cpu = torch.zeros( | ||
(max_num_reqs, max_num_blocks_per_req), | ||
device="cpu", | ||
dtype=torch.int32, | ||
pin_memory=pin_memory, | ||
) | ||
self.block_table_np = self.block_table_cpu.numpy() | ||
self.num_blocks_per_row = np.zeros(max_num_reqs, dtype=np.int32) | ||
|
||
def append_row( | ||
self, | ||
row_idx: int, | ||
start: int, | ||
block_ids: List[int], | ||
) -> None: | ||
num_blocks = len(block_ids) | ||
self.block_table_np[row_idx, start:start + num_blocks] = block_ids | ||
self.num_blocks_per_row[row_idx] = start + num_blocks | ||
|
||
def add_row(self, row_idx: int, block_ids: List[int]) -> None: | ||
self.append_row(row_idx, 0, block_ids) | ||
|
||
def move_row(self, src: int, tgt: int) -> None: | ||
num_blocks = self.num_blocks_per_row[src] | ||
self.block_table_np[tgt, :num_blocks] = self.block_table_np[ | ||
src, :num_blocks] | ||
self.num_blocks_per_row[tgt] = num_blocks | ||
|
||
def commit(self, num_reqs: int) -> None: | ||
self.block_table[:num_reqs].copy_(self.block_table_cpu[:num_reqs], | ||
non_blocking=True) | ||
|
||
def clear(self) -> None: | ||
self.block_table.fill_(0) | ||
self.block_table_cpu.fill_(0) | ||
|
||
def to_device(self) -> torch.Tensor: | ||
"""Ruturns the device tensor of the block table.""" | ||
return self.block_table | ||
|
||
def cpu(self) -> torch.Tensor: | ||
"""Returns the CPU tensor of the block table.""" | ||
return self.block_table_cpu | ||
|
||
def numpy(self) -> np.ndarray: | ||
"""Returns the numpy array of the block table.""" | ||
return self.block_table_np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at this API again and found it's a bit weird to call it
to_device
, because we are not actually transferring tensors in this call (liketorch_tensor.to("cuda")
). Following the naming convention of.cpu()
, this API should be name.device()
, but I'm not sure if this makes sense to others.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.
I actually named it
device
first, and then found that the class already had thedevice
attribute 😂 and PyTorch's convention isx.device
returns the devicex
lives in.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that's true...then another way is naming everything with verb, like
to_device
,to_cpu
,to_numpy
. Although we don't actually do any transfer in these calls, this may be less confusion. @robertgshaw2-neuralmagic WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@comaniac @robertgshaw2-neuralmagic Renamed them to
get_device_tensor
,get_cpu_tensor
andget_numpy_array
. Does this sound good to you?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!