[ROCm][Perf] Enable DPA+ETP prefill compute/comm dual-batch-overlap for DeepSeek-V4 (#57773)
Signed-off-by: Shanshan Shen <87969357+shen-shanshan@users.noreply.github.com> Signed-off-by: shen-shanshan <467638484@qq.com>
This commit is contained in:
@@ -213,6 +213,7 @@ class UBatchWrapper:
|
||||
def _run_ubatches(self, ubatch_metadata, model) -> torch.Tensor:
|
||||
@torch.inference_mode()
|
||||
def _ubatch_thread(results, model, ubatch_metadata):
|
||||
torch.accelerator.set_device_index(self.device)
|
||||
with ubatch_metadata.context:
|
||||
model_output = model(
|
||||
input_ids=ubatch_metadata.input_ids,
|
||||
@@ -291,6 +292,7 @@ class UBatchWrapper:
|
||||
compute_stream=compute_stream,
|
||||
forward_contexts=forward_contexts,
|
||||
ready_barrier=self.ready_barrier,
|
||||
token_offsets=[s.token_slice.start for s in ubatch_slices],
|
||||
)
|
||||
|
||||
ubatch_metadata: list[UbatchMetadata] = []
|
||||
|
||||
@@ -176,7 +176,12 @@ def maybe_create_ubatch_slices(
|
||||
if split_point is None:
|
||||
split_point = int(num_tokens_padded) // num_ubatches
|
||||
|
||||
token_split_points = [split_point * i for i in range(1, num_ubatches)]
|
||||
if isinstance(split_point, int):
|
||||
token_split_points = [split_point * i for i in range(1, num_ubatches)]
|
||||
else:
|
||||
# Explicit split points, one per ubatch boundary.
|
||||
assert len(split_point) == num_ubatches - 1
|
||||
token_split_points = [int(p) for p in split_point]
|
||||
|
||||
# TODO(lucas): Refactor the gpu_model_runner.py so we can pass
|
||||
# in cu_num_tokens directly (i.e. query_start_loc)
|
||||
@@ -187,7 +192,7 @@ def maybe_create_ubatch_slices(
|
||||
start_token = 0
|
||||
|
||||
# Add the end point to the split points to make iteration easier
|
||||
all_points = token_split_points + [cu_num_tokens[-1]]
|
||||
all_points = token_split_points + [int(cu_num_tokens[-1])]
|
||||
|
||||
for end_token in all_points:
|
||||
token_slice = slice(start_token, end_token)
|
||||
@@ -316,6 +321,10 @@ def _make_metadata_with_slice(
|
||||
block_table_tensor = attn_metadata.block_table_tensor[request_slice]
|
||||
slot_mapping = attn_metadata.slot_mapping[token_slice]
|
||||
|
||||
positions = attn_metadata.positions
|
||||
if positions is not None:
|
||||
positions = positions[token_slice]
|
||||
|
||||
return CommonAttentionMetadata(
|
||||
query_start_loc=query_start_loc,
|
||||
query_start_loc_cpu=query_start_loc_cpu,
|
||||
@@ -326,6 +335,7 @@ def _make_metadata_with_slice(
|
||||
max_seq_len=max_seq_len,
|
||||
block_table_tensor=block_table_tensor,
|
||||
slot_mapping=slot_mapping,
|
||||
positions=positions,
|
||||
seq_lens_cpu_upper_bound=seq_lens_cpu_upper_bound,
|
||||
)
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ class UBatchContext:
|
||||
gpu_comm_done_event: torch.Event,
|
||||
gpu_compute_done_event: torch.Event,
|
||||
schedule: str = "default",
|
||||
token_offset: int = 0,
|
||||
):
|
||||
self.id = id
|
||||
self.comm_stream = comm_stream
|
||||
@@ -44,6 +45,7 @@ class UBatchContext:
|
||||
self.gpu_comm_done_event = gpu_comm_done_event
|
||||
self.gpu_compute_done_event = gpu_compute_done_event
|
||||
self.schedule = schedule
|
||||
self.token_offset = token_offset
|
||||
self.recv_hook = None
|
||||
|
||||
def __enter__(self):
|
||||
@@ -155,6 +157,17 @@ def dbo_current_ubatch_id() -> int:
|
||||
return _THREAD_ID_TO_CONTEXT[threading.get_ident()]
|
||||
|
||||
|
||||
def dbo_current_ubatch_token_offset() -> int:
|
||||
"""Row offset of the calling ubatch inside the parent batch.
|
||||
|
||||
Returns 0 when no DBO context is active on this thread.
|
||||
"""
|
||||
if len(_THREAD_ID_TO_CONTEXT) == 0:
|
||||
return 0
|
||||
ctx = _CURRENT_CONTEXTS[_THREAD_ID_TO_CONTEXT[threading.get_ident()]]
|
||||
return ctx.token_offset
|
||||
|
||||
|
||||
def _register_ubatch_function(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
if len(_THREAD_ID_TO_CONTEXT) > 0:
|
||||
@@ -204,6 +217,7 @@ def make_ubatch_contexts(
|
||||
forward_contexts: list[ForwardContext],
|
||||
ready_barrier: threading.Barrier,
|
||||
schedule: str = "default",
|
||||
token_offsets: list[int] | None = None,
|
||||
) -> list[UBatchContext]:
|
||||
global _NUM_UBATCHES, _CURRENT_CONTEXTS
|
||||
assert num_micro_batches > 1, "num_micro_batches must be greater than 1"
|
||||
@@ -233,6 +247,7 @@ def make_ubatch_contexts(
|
||||
gpu_comm_done_event=gpu_comm_done_events[i],
|
||||
gpu_compute_done_event=gpu_compute_done_events[i],
|
||||
schedule=schedule,
|
||||
token_offset=token_offsets[i] if token_offsets else 0,
|
||||
)
|
||||
ctxs.append(ctx)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user