Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
norm
vllm
Commits
1f739f9b
Commit
1f739f9b
authored
Feb 14, 2023
by
Woosuk Kwon
Browse files
Fix a bug in swap_in and swap_out
parent
b1644f76
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
9 deletions
+27
-9
cacheflow/master/block_manager.py
cacheflow/master/block_manager.py
+27
-9
No files found.
cacheflow/master/block_manager.py
View file @
1f739f9b
...
...
@@ -155,42 +155,60 @@ class BlockSpaceManager:
return
len
(
blocks
)
+
num_swapped_seqs
<=
num_free_blocks
def
swap_in
(
self
,
seq_group
:
SequenceGroup
)
->
Dict
[
int
,
int
]:
#
src_
block
_number -> dst_block_number
mapping
:
Dict
[
int
,
int
]
=
{}
#
CPU
block
-> GPU block.
mapping
:
Dict
[
PhysicalTokenBlock
,
PhysicalTokenBlock
]
=
{}
for
seq
in
seq_group
.
seqs
:
if
seq
.
status
==
SequenceStatus
.
FINISHED
:
continue
new_block_table
:
BlockTable
=
[]
block_table
=
self
.
block_tables
[
seq
.
seq_id
]
for
cpu_block
in
block_table
:
if
cpu_block
in
mapping
:
new_block_table
.
append
(
mapping
[
cpu_block
])
continue
gpu_block
=
self
.
gpu_allocator
.
allocate
()
mapping
[
cpu_block
.
block_number
]
=
gpu_block
.
block_number
mapping
[
cpu_block
]
=
gpu_block
# Free the CPU block swapped in to GPU.
self
.
cpu_allocator
.
free
(
cpu_block
)
return
mapping
self
.
block_tables
[
seq
.
seq_id
]
=
new_block_table
block_number_mapping
=
{
cpu_block
.
block_number
:
gpu_block
.
block_number
for
cpu_block
,
gpu_block
in
mapping
.
items
()
}
return
block_number_mapping
def
can_swap_out
(
self
,
seq_group
:
SequenceGroup
)
->
bool
:
blocks
=
self
.
_get_physical_blocks
(
seq_group
)
return
len
(
blocks
)
<=
self
.
cpu_allocator
.
get_num_free_blocks
()
def
swap_out
(
self
,
seq_group
:
SequenceGroup
)
->
Dict
[
int
,
int
]:
#
src_
block
_number -> dst_block_number
mapping
:
Dict
[
int
,
int
]
=
{}
#
GPU
block
-> CPU block.
mapping
:
Dict
[
PhysicalTokenBlock
,
PhysicalTokenBlock
]
=
{}
for
seq
in
seq_group
.
seqs
:
if
seq
.
status
==
SequenceStatus
.
FINISHED
:
continue
new_block_table
:
BlockTable
=
[]
block_table
=
self
.
block_tables
[
seq
.
seq_id
]
for
gpu_block
in
block_table
:
if
gpu_block
.
block_number
in
mapping
:
if
gpu_block
in
mapping
:
new_block_table
.
append
(
mapping
[
gpu_block
])
continue
cpu_block
=
self
.
cpu_allocator
.
allocate
()
mapping
[
gpu_block
.
block_number
]
=
cpu_block
.
block_number
new_block_table
.
append
(
cpu_block
)
mapping
[
gpu_block
]
=
cpu_block
# Free the GPU block swapped out to CPU.
self
.
gpu_allocator
.
free
(
gpu_block
)
return
mapping
self
.
block_tables
[
seq
.
seq_id
]
=
new_block_table
block_number_mapping
=
{
gpu_block
.
block_number
:
cpu_block
.
block_number
for
gpu_block
,
cpu_block
in
mapping
.
items
()
}
return
block_number_mapping
def
_free_blocks
(
self
,
blocks
:
Iterable
[
PhysicalTokenBlock
])
->
None
:
for
block
in
blocks
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment