[Feature] Layer-wise Prefill (#7634)
Signed-off-by: jason-fxz <jason341132@qq.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
@@ -68,6 +68,8 @@ class ForwardMode(IntEnum):
|
||||
MIXED = auto()
|
||||
# No sequence to forward. For data parallel attention, some workers will be IDLE if no sequence are allocated.
|
||||
IDLE = auto()
|
||||
# Split Prefill for PD multiplexing
|
||||
SPLIT_PREFILL = auto()
|
||||
|
||||
# Used in speculative decoding: verify a batch in the target model.
|
||||
TARGET_VERIFY = auto()
|
||||
@@ -95,6 +97,9 @@ class ForwardMode(IntEnum):
|
||||
def is_mixed(self):
|
||||
return self == ForwardMode.MIXED
|
||||
|
||||
def is_split_prefill(self):
|
||||
return self == ForwardMode.SPLIT_PREFILL
|
||||
|
||||
def is_idle(self):
|
||||
return self == ForwardMode.IDLE
|
||||
|
||||
@@ -194,6 +199,14 @@ class ForwardBatch:
|
||||
extend_logprob_start_lens_cpu: Optional[List[int]] = None
|
||||
extend_input_logprob_token_ids_gpu: Optional[torch.Tensor] = None
|
||||
|
||||
# For split prefill
|
||||
# intermediate values for split prefill
|
||||
hidden_states: torch.Tensor = None
|
||||
residual: torch.Tensor = None
|
||||
model_specific_states: Dict[str, any] = None
|
||||
# current split index of layer
|
||||
split_index: int = 0
|
||||
|
||||
# For MLA chunked prefix cache used in chunked prefill
|
||||
# Tell attention backend whether the kv cache needs to be attended in current pass
|
||||
attn_attend_prefix_cache: Optional[bool] = None
|
||||
|
||||
@@ -1513,11 +1513,34 @@ class ModelRunner:
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def forward_split_prefill(
|
||||
self,
|
||||
forward_batch: ForwardBatch,
|
||||
reinit_attn_backend: bool = False,
|
||||
forward_count: int = 1,
|
||||
) -> LogitsProcessorOutput:
|
||||
if forward_batch.split_index == 0 or reinit_attn_backend:
|
||||
self.attn_backend.init_forward_metadata(forward_batch)
|
||||
next_split_index = min(
|
||||
forward_batch.split_index + forward_count,
|
||||
self.model_config.num_hidden_layers,
|
||||
)
|
||||
ret = self.model.forward_split_prefill(
|
||||
forward_batch.input_ids,
|
||||
forward_batch.positions,
|
||||
forward_batch,
|
||||
(forward_batch.split_index, next_split_index),
|
||||
)
|
||||
forward_batch.split_index = next_split_index
|
||||
return ret
|
||||
|
||||
def forward(
|
||||
self,
|
||||
forward_batch: ForwardBatch,
|
||||
skip_attn_backend_init: bool = False,
|
||||
pp_proxy_tensors: Optional[PPProxyTensors] = None,
|
||||
reinit_attn_backend: bool = False,
|
||||
split_forward_count: int = 1,
|
||||
) -> Tuple[Union[LogitsProcessorOutput, PPProxyTensors], bool]:
|
||||
self.forward_pass_id += 1
|
||||
|
||||
@@ -1526,7 +1549,11 @@ class ModelRunner:
|
||||
forward_batch,
|
||||
):
|
||||
output = self._forward_raw(
|
||||
forward_batch, skip_attn_backend_init, pp_proxy_tensors
|
||||
forward_batch,
|
||||
skip_attn_backend_init,
|
||||
pp_proxy_tensors,
|
||||
reinit_attn_backend,
|
||||
split_forward_count,
|
||||
)
|
||||
|
||||
if self.eplb_manager is not None:
|
||||
@@ -1539,6 +1566,8 @@ class ModelRunner:
|
||||
forward_batch: ForwardBatch,
|
||||
skip_attn_backend_init: bool,
|
||||
pp_proxy_tensors: Optional[PPProxyTensors],
|
||||
reinit_attn_backend: bool = False,
|
||||
split_forward_count: int = 1,
|
||||
) -> Tuple[Union[LogitsProcessorOutput, PPProxyTensors], bool]:
|
||||
can_run_cuda_graph = bool(
|
||||
forward_batch.forward_mode.is_cuda_graph()
|
||||
@@ -1559,6 +1588,12 @@ class ModelRunner:
|
||||
skip_attn_backend_init=skip_attn_backend_init,
|
||||
pp_proxy_tensors=pp_proxy_tensors,
|
||||
)
|
||||
elif forward_batch.forward_mode.is_split_prefill():
|
||||
ret = self.forward_split_prefill(
|
||||
forward_batch,
|
||||
reinit_attn_backend=reinit_attn_backend,
|
||||
forward_count=split_forward_count,
|
||||
)
|
||||
elif forward_batch.forward_mode.is_idle():
|
||||
ret = self.forward_idle(forward_batch, pp_proxy_tensors=pp_proxy_tensors)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user