[feature] extend DBO to XBO (#30120)
Signed-off-by: jiangkuaixue123 <jiangxiaozhou111@163.com> Co-authored-by: root <root@hk01dgx028.cm.cluster>
This commit is contained in:
@@ -27,14 +27,16 @@ class UBatchSlice:
|
||||
UBatchSlices: TypeAlias = list[UBatchSlice]
|
||||
|
||||
|
||||
def is_second_ubatch_empty(orig_num_tokens: int, padded_num_tokens: int) -> bool:
|
||||
return (padded_num_tokens // 2) >= orig_num_tokens
|
||||
def is_last_ubatch_empty(
|
||||
orig_num_tokens: int, padded_num_tokens: int, num_ubatches: int
|
||||
) -> bool:
|
||||
return (padded_num_tokens // num_ubatches) * (num_ubatches - 1) >= orig_num_tokens
|
||||
|
||||
|
||||
def check_ubatch_thresholds(
|
||||
config: ParallelConfig, num_tokens: int, uniform_decode: bool
|
||||
) -> bool:
|
||||
if not config.enable_dbo:
|
||||
if not config.use_ubatching:
|
||||
return False
|
||||
if uniform_decode:
|
||||
return num_tokens >= config.dbo_decode_token_threshold
|
||||
@@ -42,21 +44,17 @@ def check_ubatch_thresholds(
|
||||
return num_tokens >= config.dbo_prefill_token_threshold
|
||||
|
||||
|
||||
# This just pads the second ubatch slice out to the total number of tokens
|
||||
# This pads the last ubatch slice out to the total number of tokens
|
||||
# (num_tokens + padding) since we do `create_ubatch_slices` before applying DP padding.
|
||||
def _pad_out_ubatch_slices(
|
||||
ubatch_slices: UBatchSlices, num_total_tokens: int, num_reqs_padded: int
|
||||
) -> UBatchSlices:
|
||||
# TODO(lucas): handle empty second ubatch
|
||||
padded_second_request_slice = slice(
|
||||
ubatch_slices[1].request_slice.start, num_reqs_padded
|
||||
)
|
||||
padded_second_token_slice = slice(
|
||||
ubatch_slices[1].token_slice.start, num_total_tokens
|
||||
)
|
||||
return [
|
||||
ubatch_slices[0],
|
||||
UBatchSlice(padded_second_request_slice, padded_second_token_slice),
|
||||
last_slice = ubatch_slices[-1]
|
||||
padded_last_request_slice = slice(last_slice.request_slice.start, num_reqs_padded)
|
||||
padded_last_token_slice = slice(last_slice.token_slice.start, num_total_tokens)
|
||||
|
||||
return ubatch_slices[:-1] + [
|
||||
UBatchSlice(padded_last_request_slice, padded_last_token_slice)
|
||||
]
|
||||
|
||||
|
||||
@@ -65,40 +63,45 @@ def maybe_create_ubatch_slices(
|
||||
num_scheduled_tokens: np.ndarray,
|
||||
num_tokens_padded: int,
|
||||
num_reqs_padded: int,
|
||||
split_point: int | None = None,
|
||||
num_ubatches: int,
|
||||
split_point: list[int] | int | None = None,
|
||||
) -> tuple[UBatchSlices | None, UBatchSlices | None]:
|
||||
if not should_ubatch:
|
||||
return None, None
|
||||
|
||||
if split_point is None:
|
||||
split_point = int(num_tokens_padded) // 2
|
||||
split_point = int(num_tokens_padded) // num_ubatches
|
||||
|
||||
token_split_points = [split_point * i for i in range(1, num_ubatches)]
|
||||
|
||||
# TODO(lucas): Refactor the gpu_model_runner.py so we can pass
|
||||
# in cu_num_tokens directly (i.e. query_start_loc)
|
||||
cu_num_tokens = np.zeros(len(num_scheduled_tokens) + 1, dtype=np.int32)
|
||||
np.cumsum(num_scheduled_tokens, dtype=np.int32, out=cu_num_tokens[1:])
|
||||
|
||||
first_ubatch_token_slice = slice(0, split_point)
|
||||
second_ubatch_token_slice = slice(split_point, cu_num_tokens[-1])
|
||||
ubatch_slices = []
|
||||
start_token = 0
|
||||
|
||||
# Determine request slices using exclusive stop semantics
|
||||
# First ubatch includes requests whose tokens overlap [0, split_point)
|
||||
first_ubatch_req_stop = int(
|
||||
np.searchsorted(cu_num_tokens, split_point, side="left")
|
||||
)
|
||||
first_ubatch_req_slice = slice(0, first_ubatch_req_stop)
|
||||
# Add the end point to the split points to make iteration easier
|
||||
all_points = token_split_points + [cu_num_tokens[-1]]
|
||||
|
||||
# Second ubatch starts at the request that contains the split_point
|
||||
# or the request starting exactly at split_point (if on boundary)
|
||||
second_ubatch_req_start = int(
|
||||
np.searchsorted(cu_num_tokens, split_point, side="right") - 1
|
||||
)
|
||||
second_ubatch_req_slice = slice(second_ubatch_req_start, len(cu_num_tokens) - 1)
|
||||
for end_token in all_points:
|
||||
token_slice = slice(start_token, end_token)
|
||||
|
||||
ubatch_slices = [
|
||||
UBatchSlice(first_ubatch_req_slice, first_ubatch_token_slice),
|
||||
UBatchSlice(second_ubatch_req_slice, second_ubatch_token_slice),
|
||||
]
|
||||
# Determine request slices using exclusive stop semantics
|
||||
# Ubatch includes requests whose tokens overlap [start_token, end_token)
|
||||
|
||||
# Start at the request that contains the start_token
|
||||
# or the request starting exactly at start_token (if on boundary)
|
||||
req_start = int(np.searchsorted(cu_num_tokens, start_token, side="right") - 1)
|
||||
|
||||
# Stop at the request that starts at or after end_token
|
||||
req_stop = int(np.searchsorted(cu_num_tokens, end_token, side="left"))
|
||||
|
||||
req_slice = slice(req_start, req_stop)
|
||||
ubatch_slices.append(UBatchSlice(req_slice, token_slice))
|
||||
|
||||
start_token = end_token
|
||||
|
||||
ubatch_slices_padded = _pad_out_ubatch_slices(
|
||||
ubatch_slices, num_tokens_padded, num_reqs_padded
|
||||
|
||||
Reference in New Issue
Block a user