+7








4d51588e23
Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai> Signed-off-by: Woosuk Kwon <woosuk@inferact.ai> Signed-off-by: qizixi <zixi@inferact.ai> Signed-off-by: Jee Jee Li <pandaleefree@gmail.com> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Yongye Zhu <yongye@inferact.ai> Co-authored-by: Simon Mo <simon@inferact.ai> Co-authored-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: Giancarlo Delfin <gdelfin@inferact.ai> Co-authored-by: Jee Jee Li <pandaleefree@gmail.com> Co-authored-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Roger Wang <hey@rogerw.io> Co-authored-by: Roy Wang <yasong.wang@inferact.ai> Co-authored-by: Woosuk Kwon <woosuk@inferact.ai> Co-authored-by: youkaichao <youkaichao@gmail.com> Co-authored-by: Zhewen Li <jerven.vllm@gmail.com> Co-authored-by: Zijing Liu <liuzijing2014@gmail.com> Co-authored-by: khluu <khluu000@gmail.com> Co-authored-by: qizixi <zixi@inferact.ai> Co-authored-by: Zhewen Li <zhewenli@inferact.ai>
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
|
|
from collections.abc import Callable
|
|
from enum import Enum
|
|
from typing import Any
|
|
|
|
import torch
|
|
|
|
|
|
class AuxStreamType(Enum):
|
|
Attention = 1
|
|
|
|
|
|
class EventType(Enum):
|
|
Main = 0
|
|
Attention = 1
|
|
|
|
|
|
def maybe_execute_in_parallel(
|
|
fn0: Callable[[], Any],
|
|
fn1: Callable[[], Any],
|
|
event0: torch.cuda.Event,
|
|
event1: torch.cuda.Event,
|
|
aux_stream: torch.cuda.Stream | None = None,
|
|
) -> tuple[Any, Any]:
|
|
"""Run two functions potentially in parallel on separate CUDA streams.
|
|
|
|
When aux_stream is provided, fn0 runs on the current (default) stream and
|
|
fn1 runs on aux_stream, synchronized via CUDA events. When aux_stream is
|
|
None, both functions execute sequentially on the current stream.
|
|
|
|
This design follows TensorRT-LLM's maybe_execute_in_parallel pattern
|
|
(tensorrt_llm/_torch/modules/multi_stream_utils.py).
|
|
|
|
Args:
|
|
fn0: Callable for the default stream.
|
|
fn1: Callable for the auxiliary stream.
|
|
event0: CUDA event recorded before fn0 so aux_stream can wait.
|
|
event1: CUDA event recorded after fn1 so default stream can wait.
|
|
aux_stream: The second CUDA stream for fn1.
|
|
Multi-stream is disabled when aux_stream is None.
|
|
|
|
Returns:
|
|
Tuple of (fn0_result, fn1_result).
|
|
"""
|
|
if aux_stream is not None:
|
|
event0.record()
|
|
result0 = fn0()
|
|
with torch.cuda.stream(aux_stream):
|
|
event0.wait()
|
|
result1 = fn1()
|
|
event1.record()
|
|
event1.wait()
|
|
else:
|
|
result0 = fn0()
|
|
result1 = fn1()
|
|
return (result0, result1)
|