Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
73032f48
Commit
73032f48
authored
Mar 11, 2026
by
xuxz
Browse files
[PD]回退p2pncclconnector
parent
a997359c
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
540 additions
and
48 deletions
+540
-48
vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_connector.py
...ted/kv_transfer/kv_connector/v1/p2p/p2p_nccl_connector.py
+269
-31
vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_engine.py
...ibuted/kv_transfer/kv_connector/v1/p2p/p2p_nccl_engine.py
+270
-16
vllm/distributed/kv_transfer/kv_connector/v1/p2p/tensor_memory_pool.py
...ted/kv_transfer/kv_connector/v1/p2p/tensor_memory_pool.py
+1
-1
No files found.
vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_connector.py
View file @
73032f48
This diff is collapsed.
Click to expand it.
vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_engine.py
View file @
73032f48
...
...
@@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Any, Optional
import
msgpack
import
torch
import
zmq
import
regex
from
vllm.config
import
KVTransferConfig
from
vllm.distributed.device_communicators.pynccl_wrapper
import
(
...
...
@@ -20,6 +21,13 @@ from vllm.distributed.device_communicators.pynccl_wrapper import (
from
vllm.distributed.kv_transfer.kv_connector.v1.p2p.tensor_memory_pool
import
(
# noqa: E501
TensorMemoryPool
)
from
vllm.utils
import
current_stream
,
get_ip
from
vllm
import
envs
from
vllm.distributed.parallel_state
import
get_pp_group
,
get_tp_group
from
dataclasses
import
dataclass
from
vllm.model_executor.models.utils
import
extract_layer_index
from
vllm.distributed.utils
import
get_pp_indices
from
vllm.config
import
ModelConfig
if
TYPE_CHECKING
:
from
vllm.forward_context
import
ForwardContext
...
...
@@ -28,6 +36,11 @@ logger = logging.getLogger(__name__)
DEFAULT_MEM_POOL_SIZE_GB
=
32
# @dataclass
# class SendQueueItem:
# tensor_id: str
# remote_address: str
# tensor: torch.Tensor
@
contextmanager
def
set_p2p_nccl_context
(
num_channels
:
str
):
...
...
@@ -59,17 +72,37 @@ def set_p2p_nccl_context(num_channels: str):
os
.
environ
.
pop
(
var
,
None
)
@
dataclass
class
RemoteAddr
:
pd_pair_id
:
str
=
""
zmq_address
:
str
=
""
comm_rank
:
int
=
0
class
P2pNcclEngine
:
def
__init__
(
self
,
local_rank
:
int
,
port_offset
:
int
,
config
:
KVTransferConfig
,
hostname
:
str
=
""
,
port_offset
:
int
=
0
,
model_config
:
ModelConfig
,
dp_rank
:
int
=
0
,
pp_rank
:
int
=
0
,
tp_rank
:
int
=
0
,
dp_size
:
int
=
0
,
pp_size
:
int
=
0
,
tp_size
:
int
=
0
,
library_path
:
Optional
[
str
]
=
None
)
->
None
:
self
.
config
=
config
self
.
model_config
=
model_config
self
.
rank
=
port_offset
self
.
local_rank
=
local_rank
self
.
dp_rank
=
dp_rank
self
.
pp_rank
=
pp_rank
self
.
tp_rank
=
tp_rank
self
.
dp_size
=
dp_size
self
.
pp_size
=
pp_size
self
.
tp_size
=
tp_size
self
.
device
=
torch
.
device
(
f
"cuda:
{
self
.
local_rank
}
"
)
self
.
nccl
=
NCCLLibrary
(
library_path
)
...
...
@@ -95,7 +128,7 @@ class P2pNcclEngine:
port
=
int
(
self
.
config
.
kv_port
)
+
port_offset
if
port
==
0
:
raise
ValueError
(
"Port cannot be 0"
)
self
.
_hostname
=
hostname
self
.
_hostname
=
get_ip
()
self
.
_port
=
port
# Each card corresponds to a ZMQ address.
...
...
@@ -129,6 +162,10 @@ class P2pNcclEngine:
self
.
send_stream
=
torch
.
cuda
.
Stream
()
self
.
recv_stream
=
torch
.
cuda
.
Stream
()
self
.
p2p_async_kv_tokens
=
envs
.
VLLM_P2P_BUF_TOKENS
self
.
p2p_async_buf
=
None
self
.
tensor_split_num
:
int
=
0
mem_pool_size_gb
=
self
.
config
.
get_from_extra_config
(
"mem_pool_size_gb"
,
DEFAULT_MEM_POOL_SIZE_GB
)
self
.
pool
=
TensorMemoryPool
(
max_block_size
=
int
(
mem_pool_size_gb
)
*
...
...
@@ -167,11 +204,16 @@ class P2pNcclEngine:
self
.
_listener_thread
.
start
()
self
.
_ping_thread
=
None
if
self
.
multiple_machines
:
if
port_offset
==
0
and
self
.
proxy_address
!=
""
:
self
.
_ping_thread
=
threading
.
Thread
(
target
=
self
.
_ping
,
daemon
=
True
)
self
.
_ping_thread
.
start
()
else
:
if
self
.
proxy_address
!=
""
:
self
.
_ping_thread
=
threading
.
Thread
(
target
=
self
.
_ping_new
,
daemon
=
True
)
self
.
_ping_thread
.
start
()
logger
.
info
(
"💯P2pNcclEngine init, rank:%d, local_rank:%d, http_address:%s, "
"zmq_address:%s, proxy_address:%s, send_type:%s, buffer_size_"
...
...
@@ -179,6 +221,21 @@ class P2pNcclEngine:
self
.
http_address
,
self
.
zmq_address
,
self
.
proxy_address
,
self
.
send_type
,
self
.
buffer_size_threshold
,
self
.
nccl_num_channels
)
def
_create_connect_new
(
self
,
remote_address
:
typing
.
Optional
[
str
]
=
None
):
assert
remote_address
is
not
None
if
remote_address
not
in
self
.
socks
:
sock
=
self
.
context
.
socket
(
zmq
.
DEALER
)
sock
.
setsockopt
(
zmq
.
SNDHWM
,
10000
)
sock
.
setsockopt
(
zmq
.
RCVHWM
,
5000
)
sock
.
setsockopt
(
zmq
.
LINGER
,
0
)
sock
.
setsockopt
(
zmq
.
TCP_KEEPALIVE
,
1
)
sock
.
setsockopt_string
(
zmq
.
IDENTITY
,
f
"P-
{
self
.
zmq_address
}
"
)
sock
.
connect
(
f
"tcp://
{
remote_address
}
"
)
self
.
socks
[
remote_address
]
=
sock
return
self
.
socks
[
remote_address
]
def
_create_connect
(
self
,
remote_address
:
typing
.
Optional
[
str
]
=
None
):
assert
remote_address
is
not
None
if
remote_address
not
in
self
.
socks
:
...
...
@@ -206,11 +263,73 @@ class P2pNcclEngine:
return
self
.
socks
[
remote_address
],
self
.
comms
[
remote_address
]
def
get_send_queue_items
(
self
,
request_id
:
str
,
layer_name
:
str
,
tensor
:
torch
.
Tensor
,
is_mla
:
bool
)
->
list
[
any
]:
tensor_id
=
self
.
get_tensor_id
(
request_id
,
layer_name
)
remote_ip
,
remote_port
=
self
.
parse_request_id
(
request_id
,
True
)
p_ip
,
p_port
=
self
.
parse_request_id
(
request_id
,
False
)
pd_pair_id
=
p_ip
+
":"
+
str
(
p_port
)
+
"_"
+
remote_ip
+
":"
+
str
(
remote_port
)
if
not
self
.
enable_asymmetric_p2p
:
remote_address
=
remote_ip
+
":"
+
str
(
remote_port
+
self
.
rank
)
remote_addr
=
RemoteAddr
(
pd_pair_id
,
remote_address
,
self
.
rank
+
self
.
pp_size
*
self
.
tp_size
)
# logger.info(f"""+++++xiabo tensor_id:{tensor_id} request_id:{request_id} remote_address:{remote_address}""")
return
[(
tensor_id
,
remote_addr
,
tensor
)]
if
not
is_mla
:
logger
.
error
(
" P2PNCCL only support mla model symmetric PP/TP!!!!"
)
remote_pp_rank
=
self
.
compute_remote_pp_rank
(
layer_name
)
items
:
list
[
Any
]
=
[]
for
d_tp_rank
in
range
(
self
.
remote_tp_size
):
for
mul_tp
in
range
(
self
.
multp
):
if
self
.
tp_rank
+
mul_tp
*
self
.
tp_size
==
d_tp_rank
:
remote_port_offset
=
remote_pp_rank
*
self
.
remote_tp_size
+
d_tp_rank
remote_address
=
remote_ip
+
":"
+
str
(
remote_port
+
remote_port_offset
)
remote_addr
=
RemoteAddr
(
pd_pair_id
,
remote_address
,
remote_port_offset
+
self
.
pp_size
*
self
.
tp_size
)
logger
.
debug
(
"Wait to send::%s, tensor_shape:%s, "
"(pp=%d, tp=%d) -> remote_address=%s(pp=%d, tp=%d) comm_rank (%d -> %d)"
,
tensor_id
,
tensor
.
shape
,
self
.
pp_rank
,
self
.
tp_rank
,
remote_address
,
remote_pp_rank
,
self
.
rank
*
mul_tp
+
self
.
rank
,
self
.
rank
,
remote_port_offset
+
self
.
pp_size
*
self
.
tp_size
)
items
.
append
([
tensor_id
,
remote_addr
,
tensor
])
return
items
def
send_tensor_new
(
self
,
request_id
:
str
,
layer_name
:
str
,
tensor
:
torch
.
Tensor
,
is_mla
:
bool
=
False
,
)
->
bool
:
tensor_id
=
self
.
get_tensor_id
(
request_id
,
layer_name
)
if
self
.
send_type
==
"PUT"
:
return
all
(
self
.
_send_sync_new
(
item
)
for
item
in
self
.
get_send_queue_items
(
request_id
,
layer_name
,
tensor
,
is_mla
))
if
self
.
send_type
==
"PUT_ASYNC"
:
with
self
.
send_queue_cv
:
for
item
in
self
.
get_send_queue_items
(
request_id
,
layer_name
,
tensor
,
is_mla
):
self
.
send_queue
.
append
(
item
)
self
.
send_queue_cv
.
notify
()
return
True
if
self
.
send_type
==
"GET"
:
logger
.
error
(
" P2PNCCL new not support GET model, please set VLLM_P2PNCCL_NEW=0 use defalut model!!!!"
)
def
send_tensor
(
self
,
tensor_id
:
str
,
tensor
:
torch
.
Tensor
,
remote_address
:
typing
.
Optional
[
str
]
=
None
,
tbo_evt
=
None
,
)
->
bool
:
if
remote_address
is
None
:
with
self
.
recv_store_cv
:
...
...
@@ -251,6 +370,53 @@ class P2pNcclEngine:
return
True
def
p2p_async_send_tensor
(
self
,
tensor_id
:
str
,
tensor
:
torch
.
Tensor
,
remote_address
:
typing
.
Optional
[
str
]
=
None
,
tbo_evt
=
None
,
)
->
bool
:
if
remote_address
is
None
:
with
self
.
recv_store_cv
:
self
.
recv_store
[
tensor_id
]
=
tensor
self
.
recv_store_cv
.
notify
()
return
True
else
:
if
self
.
send_type
==
"PUT"
:
return
self
.
_send_sync
(
tensor_id
,
tensor
,
remote_address
)
elif
self
.
send_type
==
"PUT_ASYNC"
:
with
self
.
send_queue_cv
:
kv_layer
,
slot_mapping
=
tensor
# tesor (kv_layer, slot_mapping)
self
.
send_queue
.
append
([
tensor_id
,
remote_address
,
kv_layer
,
slot_mapping
,
tbo_evt
])
self
.
send_queue_cv
.
notify
()
else
:
# GET
with
self
.
send_store_cv
:
tensor_size
=
tensor
.
element_size
()
*
tensor
.
numel
()
while
(
self
.
buffer_size
+
tensor_size
>
self
.
buffer_size_threshold
):
oldest_tenser_id
=
next
(
iter
(
self
.
send_store
))
oldest_tenser
=
self
.
send_store
.
pop
(
oldest_tenser_id
)
oldest_tenser_size
=
oldest_tenser
.
element_size
(
)
*
oldest_tenser
.
numel
()
self
.
buffer_size
-=
oldest_tenser_size
logger
.
info
(
"⛔[GET]Send to %s, tensor_id:%s, tensor_size:%d,"
" buffer_size:%d, oldest_tenser_size:%d, rank:%d"
,
remote_address
,
tensor_id
,
tensor_size
,
self
.
buffer_size
,
oldest_tenser_size
,
self
.
rank
)
self
.
send_store
[
tensor_id
]
=
tensor
self
.
buffer_size
+=
tensor_size
logger
.
debug
(
"🔵[GET]Send to %s, tensor_id:%s, tensor_size:%d, "
"shape:%s, rank:%d, buffer_size:%d(%.2f%%)"
,
remote_address
,
tensor_id
,
tensor_size
,
tensor
.
shape
,
self
.
rank
,
self
.
buffer_size
,
self
.
buffer_size
/
self
.
buffer_size_threshold
*
100
)
return
True
def
recv_tensor
(
self
,
tensor_id
:
str
,
...
...
@@ -327,6 +493,8 @@ class P2pNcclEngine:
self
.
zmq_address
,
remote_address
.
decode
(),
rank
)
elif
data
[
"cmd"
]
==
"PUT"
:
tensor_id
=
data
[
"tensor_id"
]
if
"tensor_split_num"
in
data
:
self
.
tensor_split_num
=
data
[
"tensor_split_num"
]
try
:
with
torch
.
cuda
.
stream
(
self
.
recv_stream
):
tensor
=
torch
.
empty
(
data
[
"shape"
],
...
...
@@ -343,10 +511,6 @@ class P2pNcclEngine:
# Store Tensor in memory pool
addr
=
self
.
pool
.
store_tensor
(
tensor
)
tensor
=
(
addr
,
tensor
.
dtype
,
tensor
.
shape
)
logger
.
warning
(
"🔴[PUT]Recv Tensor, Out Of Threshold, "
"%s👈%s, data:%s, addr:%d"
,
self
.
zmq_address
,
remote_address
.
decode
(),
data
,
addr
)
else
:
self
.
buffer_size
+=
tensor_size
...
...
@@ -363,7 +527,56 @@ class P2pNcclEngine:
self
.
recv_store
[
tensor_id
]
=
tensor
self
.
_have_received_tensor_id
(
tensor_id
)
self
.
recv_store_cv
.
notify
()
elif
data
[
"cmd"
]
==
"PUT_NEW"
:
tensor_id
=
data
[
"tensor_id"
]
if
"tensor_split_num"
in
data
:
self
.
tensor_split_num
=
data
[
"tensor_split_num"
]
try
:
with
torch
.
cuda
.
stream
(
self
.
recv_stream
):
tensor
=
torch
.
empty
(
data
[
"shape"
],
dtype
=
getattr
(
torch
,
data
[
"dtype"
]),
device
=
self
.
device
)
self
.
router_socket
.
send_multipart
(
[
remote_address
,
b
"0"
])
# comm, rank = self.comms[remote_address.decode()]
# self._recv(comm, tensor, rank ^ 1, self.recv_stream)
comm
,
rank
=
self
.
comms
[
data
[
"pd_pair_id"
]]
self
.
_recv
(
comm
,
tensor
,
int
(
data
[
"comm_rank"
]),
self
.
recv_stream
)
tensor_size
=
tensor
.
element_size
()
*
tensor
.
numel
()
if
(
self
.
buffer_size
+
tensor_size
>
self
.
buffer_size_threshold
):
# Store Tensor in memory pool
addr
=
self
.
pool
.
store_tensor
(
tensor
)
tensor
=
(
addr
,
tensor
.
dtype
,
tensor
.
shape
)
else
:
self
.
buffer_size
+=
tensor_size
except
torch
.
cuda
.
OutOfMemoryError
:
self
.
router_socket
.
send_multipart
(
[
remote_address
,
b
"1"
])
tensor
=
None
logger
.
warning
(
"🔴[PUT]Recv Tensor, Out Of Memory, %s👈%s, "
"data:%s"
,
self
.
zmq_address
,
remote_address
.
decode
(),
data
)
with
self
.
recv_store_cv
:
self
.
recv_store
[
tensor_id
]
=
tensor
self
.
_have_received_tensor_id
(
tensor_id
)
self
.
recv_store_cv
.
notify
()
elif
data
[
"cmd"
]
==
"comm_init"
:
unique_id
=
self
.
nccl
.
unique_id_from_bytes
(
bytes
(
data
[
"unique_id"
]))
with
torch
.
cuda
.
device
(
self
.
device
):
rank
=
int
(
data
[
"rank"
])
world_size
=
int
(
data
[
"world_size"
])
with
set_p2p_nccl_context
(
self
.
nccl_num_channels
):
comm
:
ncclComm_t
=
self
.
nccl
.
ncclCommInitRank
(
world_size
,
unique_id
,
rank
)
self
.
comms
[
data
[
"pd_pair_id"
]]
=
(
comm
,
rank
)
logger
.
info
(
"🤝ncclCommInitRank Success, %s👈%s, MyRank:%s"
,
self
.
zmq_address
,
data
[
"pd_pair_id"
],
rank
)
elif
data
[
"cmd"
]
==
"GET"
:
tensor_id
=
data
[
"tensor_id"
]
with
self
.
send_store_cv
:
...
...
@@ -410,10 +623,21 @@ class P2pNcclEngine:
with
self
.
send_queue_cv
:
while
not
self
.
send_queue
:
self
.
send_queue_cv
.
wait
()
if
envs
.
VLLM_ENABLE_TBO
or
envs
.
VLLM_P2P_ASYNC
:
tensor_id
,
remote_address
,
kv_layer
,
slot_mapping
,
tbo_evt
=
self
.
send_queue
.
popleft
()
else
:
tensor_id
,
remote_address
,
tensor
=
self
.
send_queue
.
popleft
()
if
not
self
.
send_queue
:
self
.
send_queue_cv
.
notify
()
if
(
envs
.
VLLM_ENABLE_TBO
or
envs
.
VLLM_P2P_ASYNC
)
and
tbo_evt
is
not
None
:
self
.
send_stream
.
wait_event
(
tbo_evt
)
self
.
_send_kv_p2p_sync
(
tensor_id
,
kv_layer
,
slot_mapping
,
remote_address
)
else
:
if
self
.
multiple_machines
:
self
.
_send_sync
(
tensor_id
,
tensor
,
remote_address
)
else
:
# logger.info(f"""=============xiabo tensor_id:{tensor_id} remote_address:{remote_address}""")
self
.
_send_sync_new
(
tensor_id
,
tensor
,
remote_address
)
def
wait_for_sent
(
self
):
if
self
.
send_type
==
"PUT_ASYNC"
:
...
...
@@ -518,7 +742,7 @@ class P2pNcclEngine:
"pd_pair_id"
:
remote_address
.
pd_pair_id
,
"comm_rank"
:
rank
}
#
logger.info(f"""_send_sync_new:{data}""")
logger
.
info
(
f
"""_send_sync_new:
{
data
}
"""
)
sock
.
send
(
msgpack
.
dumps
(
data
))
response
=
sock
.
recv
()
...
...
@@ -627,6 +851,36 @@ class P2pNcclEngine:
sock
.
send
(
msgpack
.
dumps
(
data
))
time
.
sleep
(
3
)
def
_ping_new
(
self
):
sock
=
self
.
context
.
socket
(
zmq
.
DEALER
)
sock
.
setsockopt_string
(
zmq
.
IDENTITY
,
self
.
zmq_address
)
logger
.
debug
(
"ping start, zmq_address:%s"
,
self
.
zmq_address
)
sock
.
connect
(
f
"tcp://
{
self
.
proxy_address
}
"
)
if
self
.
rank
==
0
:
data
=
{
"type"
:
"P_init"
if
self
.
config
.
is_kv_producer
else
"D_init"
,
"http_address"
:
self
.
http_address
,
"zmq_address"
:
self
.
zmq_address
,
"dp_size"
:
self
.
dp_size
,
"pp_size"
:
self
.
pp_size
,
"tp_size"
:
self
.
tp_size
}
# logger.info(f"""_ping data:{data}""")
sock
.
send
(
msgpack
.
dumps
(
data
))
data
=
{
"type"
:
"P"
if
self
.
config
.
is_kv_producer
else
"D"
,
"http_address"
:
self
.
http_address
,
"dp_rank"
:
self
.
dp_rank
,
"pp_rank"
:
self
.
pp_rank
,
"tp_rank"
:
self
.
tp_rank
,
"zmq_address"
:
self
.
zmq_address
}
# while True:
# logger.info(f"""_ping data:{data}""")
sock
.
send
(
msgpack
.
dumps
(
data
))
# time.sleep(3)
def
_send
(
self
,
comm
,
tensor
:
torch
.
Tensor
,
dst
:
int
,
stream
=
None
):
assert
tensor
.
device
==
self
.
device
,
(
f
"this nccl communicator is created to work on
{
self
.
device
}
, "
...
...
vllm/distributed/kv_transfer/kv_connector/v1/p2p/tensor_memory_pool.py
View file @
73032f48
...
...
@@ -63,7 +63,7 @@ class TensorMemoryPool:
than min_block_size
"""
def
__init__
(
self
,
max_block_size
:
int
,
min_block_size
:
int
=
5
12
):
def
__init__
(
self
,
max_block_size
:
int
,
min_block_size
:
int
=
12
8
):
if
max_block_size
<=
0
or
min_block_size
<=
0
:
raise
ValueError
(
"Block sizes must be positive"
)
if
max_block_size
<
min_block_size
:
...
...
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