Skip to content

Commit ccabf09

Browse files
jberkhahnLeiWang1999
authored andcommitted
[mypy] Enable mypy type checking for vllm/core (vllm-project#7229)
Signed-off-by: LeiWang1999 <[email protected]>
1 parent 29f0c38 commit ccabf09

File tree

9 files changed

+31
-18
lines changed

9 files changed

+31
-18
lines changed

.github/workflows/mypy.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ jobs:
3535
mypy
3636
mypy tests --follow-imports skip
3737
mypy vllm/attention --follow-imports skip
38-
mypy vllm/core --follow-imports skip
3938
mypy vllm/distributed --follow-imports skip
4039
mypy vllm/engine --follow-imports skip
4140
mypy vllm/executor --follow-imports skip

format.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ echo 'vLLM mypy:'
9999
mypy --follow-imports skip # Note that this is less strict than CI
100100
mypy tests --follow-imports skip
101101
mypy vllm/attention --follow-imports skip
102-
mypy vllm/core --follow-imports skip
103102
mypy vllm/distributed --follow-imports skip
104103
mypy vllm/engine --follow-imports skip
105104
mypy vllm/executor --follow-imports skip

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ files = [
5858
"vllm/adapter_commons",
5959
"vllm/assets",
6060
"vllm/entrypoints",
61+
"vllm/core",
6162
"vllm/inputs",
6263
"vllm/logging",
6364
"vllm/multimodal",

vllm/block.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Token blocks."""
2-
from typing import List, Optional
2+
from typing import TYPE_CHECKING, Iterator, List, Optional
33

44
from vllm.utils import Device
55

6-
DEFAULT_LAST_ACCESSED_TIME = -1
6+
DEFAULT_LAST_ACCESSED_TIME: float = -1
77

88

99
class PhysicalTokenBlock:
@@ -59,6 +59,11 @@ def __len__(self) -> int:
5959
def __getitem__(self, key):
6060
return self._blocks[key]
6161

62+
if TYPE_CHECKING:
63+
64+
def __iter__(self) -> Iterator[PhysicalTokenBlock]:
65+
raise RuntimeError("Method should be automatically generated")
66+
6267
def __setitem__(self, key, value):
6368
if isinstance(key, slice):
6469
blocks = value

vllm/core/block/cpu_gpu_block_allocator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def allocate_mutable_block(self, prev_block: Optional[Block],
132132

133133
def allocate_immutable_blocks(self, prev_block: Optional[Block],
134134
block_token_ids: List[List[int]],
135-
device: Optional[Device]) -> List[Block]:
135+
device: Device) -> List[Block]:
136136
"""Allocates a new group of immutable blocks with the provided block
137137
token IDs on the specified device.
138138

vllm/core/block_manager_v1.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def __init__(
278278
# request ID
279279
self.cross_block_tables: Dict[str, BlockTable] = {}
280280

281-
def _get_seq_num_required_blocks(self, seq: Sequence) -> int:
281+
def _get_seq_num_required_blocks(self, seq: Optional[Sequence]) -> int:
282282
return 0 if seq is None else seq.n_blocks
283283

284284
def can_allocate(self, seq_group: SequenceGroup) -> AllocStatus:
@@ -310,13 +310,14 @@ def can_allocate(self, seq_group: SequenceGroup) -> AllocStatus:
310310
return AllocStatus.LATER
311311

312312
def _allocate_sequence(self, \
313-
seq: Sequence, \
313+
seq: Optional[Sequence], \
314314
ref_count: int, \
315315
is_encoder_decoder: bool = True) -> BlockTable:
316316
# Allocate new physical token blocks that will store the prompt tokens.
317-
num_prompt_blocks = seq.n_blocks
317+
num_prompt_blocks = self._get_seq_num_required_blocks(seq)
318318

319319
block_table: BlockTable = BlockTable()
320+
assert seq is not None
320321
for logical_idx in range(num_prompt_blocks):
321322
if (self.block_sliding_window is not None
322323
and logical_idx >= self.block_sliding_window):

vllm/core/block_manager_v2.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ def can_allocate(self, seq_group: SequenceGroup) -> AllocStatus:
120120
)
121121

122122
if seq_group.is_encoder_decoder():
123+
encoder_seq = seq_group.get_encoder_seq()
124+
assert encoder_seq is not None
123125
num_required_blocks += BlockTable.get_num_required_blocks(
124-
seq_group.get_encoder_seq().get_token_ids(),
126+
encoder_seq.get_token_ids(),
125127
block_size=self.block_size,
126128
)
127129

@@ -189,7 +191,9 @@ def allocate(self, seq_group: SequenceGroup) -> None:
189191
check_no_caching_or_swa_for_blockmgr_encdec(self, seq_group)
190192

191193
if seq_group.is_encoder_decoder():
192-
block_table = self._allocate_sequence(seq_group.get_encoder_seq())
194+
encoder_seq = seq_group.get_encoder_seq()
195+
assert encoder_seq is not None
196+
block_table = self._allocate_sequence(encoder_seq)
193197
self.cross_block_tables[request_id] = block_table
194198

195199
def can_append_slots(self, seq_group: SequenceGroup,

vllm/core/embedding_model_block_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def access_all_blocks_in_seq(
7777
pass
7878

7979
def get_common_computed_block_ids(self,
80-
seq_group: SequenceGroup) -> List[int]:
81-
return None # type: ignore
80+
seq_group: List[Sequence]) -> List[int]:
81+
return []
8282

8383
def mark_blocks_as_computed(self, seq_group: SequenceGroup):
8484
pass

vllm/core/scheduler.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ class SchedulerSwappedInOutputs:
221221
"""
222222
# Selected sequences that are going to be swapped in and is in a
223223
# decoding phase.
224-
decode_seq_groups: List[SequenceGroup]
224+
decode_seq_groups: List[ScheduledSequenceGroup]
225225
# Selected sequences that are going to be swapped in and in a prefill
226226
# phase. I.e., it means the prefill has been chunked.
227-
prefill_seq_groups: List[SequenceGroup]
227+
prefill_seq_groups: List[ScheduledSequenceGroup]
228228
# The blocks to swap in.
229229
blocks_to_swap_in: List[Tuple[int, int]]
230230
# The blocks to copy.
@@ -254,7 +254,7 @@ class SchedulerPrefillOutputs:
254254
to be recomputed from scratch.
255255
"""
256256
# Selected sequences for prefill.
257-
seq_groups: List[SequenceGroup]
257+
seq_groups: List[ScheduledSequenceGroup]
258258
# Ignored sequence groups.
259259
ignored_seq_groups: List[SequenceGroup]
260260
num_lookahead_slots: int
@@ -289,7 +289,9 @@ def scheduler_running_outputs_builder():
289289

290290

291291
def scheduled_seq_group_builder():
292-
return ScheduledSequenceGroup(seq_group=None, token_chunk_size=0)
292+
return ScheduledSequenceGroup(SequenceGroup("", [], -1),
293+
token_chunk_size=0)
294+
# return ScheduledSequenceGroup(seq_group=None, token_chunk_size=0)
293295

294296

295297
class Scheduler:
@@ -791,7 +793,7 @@ def _schedule_prefills(
791793
SchedulerPrefillOutputs.
792794
"""
793795
ignored_seq_groups: List[SequenceGroup] = []
794-
seq_groups: List[SequenceGroup] = []
796+
seq_groups: List[ScheduledSequenceGroup] = []
795797

796798
waiting_queue = self.waiting
797799

@@ -1130,7 +1132,9 @@ def schedule(
11301132

11311133
if seq_group.is_encoder_decoder():
11321134
# Encoder associated with SequenceGroup
1133-
encoder_seq_data = seq_group.get_encoder_seq().data
1135+
encoder_seq = seq_group.get_encoder_seq()
1136+
assert encoder_seq is not None
1137+
encoder_seq_data = encoder_seq.data
11341138
# Block table for cross-attention
11351139
# Also managed at SequenceGroup level
11361140
cross_block_table = self.block_manager.get_cross_block_table(

0 commit comments

Comments
 (0)