[DCP] Support dcp kv_cache interleave size > 1 (#26696)
Signed-off-by: zhangsicheng5 <zhangsicheng5@huawei.com> Signed-off-by: QiuChunshuo <qiuchunshuo@huawei.com> Signed-off-by: Qiu <qiuchunshuo@huawei.com> Co-authored-by: QiuChunshuo <qiuchunshuo@huawei.com>
This commit is contained in:
co-authored by
QiuChunshuo
parent
47604137a2
commit
2108a571d7
@@ -225,6 +225,7 @@ from vllm.utils.math_utils import cdiv, round_down
|
||||
from vllm.v1.attention.backends.utils import (
|
||||
AttentionMetadataBuilder,
|
||||
CommonAttentionMetadata,
|
||||
get_dcp_local_seq_lens,
|
||||
get_per_layer_parameters,
|
||||
infer_global_hyperparameters,
|
||||
split_decodes_and_prefills,
|
||||
@@ -361,10 +362,9 @@ class MLACommonPrefillMetadata:
|
||||
workspace: torch.Tensor
|
||||
|
||||
# for mla DCP
|
||||
cp_chunk_seq_lens: list[list[int]] | None = None
|
||||
origin_context_lens: list[int] | None = None
|
||||
cp_cu_seq_lens: torch.Tensor | None = None
|
||||
chunk_size: int | None = None
|
||||
padded_local_chunk_seq_lens: list[list[int]] | None = None
|
||||
local_context_lens_allranks: list[list[int]] | None = None
|
||||
padded_local_cu_seq_lens: torch.Tensor | None = None
|
||||
cu_seq_lens_lst: list[list[int]] | None = None
|
||||
|
||||
block_table: torch.Tensor
|
||||
@@ -568,6 +568,8 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
|
||||
# DCP might not be initialized in testing
|
||||
self.dcp_world_size = 1
|
||||
self.dcp_rank = 0
|
||||
self.dcp_local_block_size = parallel_config.dcp_kv_cache_interleave_size
|
||||
self.dcp_virtual_block_size = self.dcp_local_block_size * self.dcp_world_size
|
||||
|
||||
# Don't try to access the runner on AMD
|
||||
if self.aot_schedule:
|
||||
@@ -794,15 +796,6 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
|
||||
)
|
||||
)
|
||||
|
||||
# Note(hc): update seq_lens of decode reqs under DCP.
|
||||
if self.dcp_world_size > 1:
|
||||
assert dcp_local_seq_lens is not None
|
||||
dcp_local_seq_lens[:num_decodes] = seq_lens[
|
||||
:num_decodes
|
||||
] // self.dcp_world_size + (
|
||||
self.dcp_rank < seq_lens[:num_decodes] % self.dcp_world_size
|
||||
)
|
||||
|
||||
assert num_decodes + num_prefills == num_reqs
|
||||
assert num_decode_tokens + num_prefill_tokens == num_tokens
|
||||
|
||||
@@ -811,11 +804,6 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
|
||||
reqs_start = num_decodes # prefill_start
|
||||
|
||||
context_lens_cpu = num_computed_tokens_cpu[reqs_start:num_reqs]
|
||||
# Note(hc): The context lengths in the perspective of dcp rank0.
|
||||
cp_context_lens_cpu = torch.ceil(
|
||||
context_lens_cpu.float() / self.dcp_world_size
|
||||
).int()
|
||||
origin_context_lens = context_lens_cpu.tolist()
|
||||
max_context_len_cpu = context_lens_cpu.max().item()
|
||||
num_prefills_with_context_cpu = (context_lens_cpu > 0).sum().item()
|
||||
prefill_query_start_loc = (
|
||||
@@ -871,32 +859,56 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
|
||||
)
|
||||
|
||||
if self.dcp_world_size > 1:
|
||||
local_context_lens_allranks = get_dcp_local_seq_lens(
|
||||
context_lens_cpu,
|
||||
self.dcp_world_size,
|
||||
None,
|
||||
self.dcp_local_block_size,
|
||||
)
|
||||
# Note(qcs): The max local context lengths
|
||||
# padded to `dcp_local_block_size`.
|
||||
padded_local_context_lens_cpu = (
|
||||
cdiv(
|
||||
context_lens_cpu,
|
||||
self.dcp_virtual_block_size,
|
||||
)
|
||||
* self.dcp_local_block_size
|
||||
)
|
||||
# Note(hc): The above max_context_chunk already enforces
|
||||
# block_size alignment, DCP just need the block_size can
|
||||
# be divisible by dcp_world_size, because DCP use
|
||||
# cp_gather_cache which not require `cp_chunk_starts`
|
||||
# aligned to page_size.
|
||||
assert max_context_chunk % self.dcp_world_size == 0
|
||||
cp_max_context_chunk = max_context_chunk // self.dcp_world_size
|
||||
cp_chunk_starts = (
|
||||
padded_local_max_context_chunk_across_ranks = (
|
||||
cdiv(
|
||||
max_context_chunk,
|
||||
self.dcp_virtual_block_size,
|
||||
)
|
||||
* self.dcp_local_block_size
|
||||
)
|
||||
local_chunk_starts = (
|
||||
torch.arange(num_chunks, dtype=torch.int32)
|
||||
.unsqueeze(1)
|
||||
.expand(-1, num_prefills)
|
||||
* cp_max_context_chunk
|
||||
* padded_local_max_context_chunk_across_ranks
|
||||
)
|
||||
cp_chunk_ends = torch.min(
|
||||
cp_context_lens_cpu.unsqueeze(0),
|
||||
cp_chunk_starts + cp_max_context_chunk,
|
||||
local_chunk_ends = torch.min(
|
||||
padded_local_context_lens_cpu.unsqueeze(0),
|
||||
local_chunk_starts
|
||||
+ padded_local_max_context_chunk_across_ranks,
|
||||
)
|
||||
cp_chunk_seq_lens = (cp_chunk_ends - cp_chunk_starts).clamp(min=0)
|
||||
padded_local_chunk_seq_lens = (
|
||||
local_chunk_ends - local_chunk_starts
|
||||
).clamp(min=0)
|
||||
|
||||
cp_cu_seq_lens_cpu = torch.zeros(
|
||||
padded_local_cu_chunk_seq_lens_cpu = torch.zeros(
|
||||
num_chunks, num_prefills + 1, dtype=torch.int32, pin_memory=True
|
||||
)
|
||||
torch.cumsum(
|
||||
cp_chunk_seq_lens,
|
||||
padded_local_chunk_seq_lens,
|
||||
dim=1,
|
||||
out=cp_cu_seq_lens_cpu[:, 1:],
|
||||
out=padded_local_cu_chunk_seq_lens_cpu[:, 1:],
|
||||
dtype=torch.int32,
|
||||
)
|
||||
|
||||
@@ -908,15 +920,16 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
|
||||
if self.dcp_world_size > 1:
|
||||
chunked_context_metadata = chunked_context_metadata_cls(
|
||||
cu_seq_lens=cu_seq_lens_cpu.to(device, non_blocking=True),
|
||||
starts=cp_chunk_starts.to(device, non_blocking=True),
|
||||
seq_tot=cp_chunk_seq_lens.sum(dim=1).tolist(),
|
||||
starts=local_chunk_starts.to(device, non_blocking=True),
|
||||
seq_tot=padded_local_chunk_seq_lens.sum(dim=1).tolist(),
|
||||
max_seq_lens=chunk_seq_lens.max(dim=1).values.tolist(),
|
||||
seq_lens=chunk_seq_lens,
|
||||
workspace=self.chunked_prefill_workspace,
|
||||
cp_chunk_seq_lens=cp_chunk_seq_lens.tolist(),
|
||||
origin_context_lens=origin_context_lens,
|
||||
cp_cu_seq_lens=cp_cu_seq_lens_cpu.to(device, non_blocking=True),
|
||||
chunk_size=max_context_chunk,
|
||||
padded_local_chunk_seq_lens=padded_local_chunk_seq_lens.tolist(),
|
||||
local_context_lens_allranks=local_context_lens_allranks.tolist(),
|
||||
padded_local_cu_seq_lens=padded_local_cu_chunk_seq_lens_cpu.to(
|
||||
device, non_blocking=True
|
||||
),
|
||||
cu_seq_lens_lst=cu_seq_lens_cpu.tolist(),
|
||||
)
|
||||
else:
|
||||
@@ -998,64 +1011,52 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
|
||||
def reorg_kvcache(
|
||||
allgatered_kv_c_normed: torch.Tensor,
|
||||
allgatered_k_pe: torch.Tensor,
|
||||
cp_chunk_seq_lens_lst: list[int],
|
||||
origin_context_lens: list[int],
|
||||
cp_world_size: int,
|
||||
padded_local_chunk_seq_lens_lst: list[int],
|
||||
local_context_lens_allranks: list[list[int]],
|
||||
sum_seq_len: int,
|
||||
max_seq_len: int,
|
||||
chunk_size: int,
|
||||
chunk_idx: int,
|
||||
toks: int,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
reorg kvcache after cp local gather to tp layout for attn kernel.
|
||||
|
||||
reorg and unpad kvcache after cp local gather to tp layout for attn kernel.
|
||||
e.g.
|
||||
allgatered_kv_c_normed = [T0_0, T0_1, T0_2, T0_3, T1_0, T1_1, ...,
|
||||
T0_4, T0_5, pad, pad, T1_2, pad, ...]
|
||||
-> reorganized_kv_c_normed = [T0_0, T0_1, T0_2, T0_3, T0_4, T0_5,
|
||||
T1_0, T1_1, T1_2, ...]
|
||||
Args:
|
||||
cp_chunk_seq_lens_lst: chunk context lengths under CP.
|
||||
origin_context_lens: origin full context lengths under CP.
|
||||
cp_world_size: CP size.
|
||||
padded_local_chunk_seq_lens_lst: local chunk context lengths
|
||||
under current CP rank.
|
||||
local_context_lens_allranks: local context lengths on each CP rank.
|
||||
sum_seq_len: the sum of cp_chunk_seq_lens_lst.
|
||||
max_seq_len: the max value of cp_chunk_seq_lens_lst.
|
||||
chunk_size: equals to max_context_chunk from
|
||||
chunked_context_metadata building.
|
||||
chunk_idx: chunk idx of chunked_prefill.
|
||||
toks: the number of tokens for local gather cache.
|
||||
"""
|
||||
kv_c_segments = []
|
||||
k_pe_segments = []
|
||||
src_token_idx = 0
|
||||
max_seq_len_check = 0
|
||||
for cp_chunk_seq_len, origin_context_len in zip(
|
||||
cp_chunk_seq_lens_lst, origin_context_lens
|
||||
for padded_local_chunk_seq_len, local_context_lens in zip(
|
||||
padded_local_chunk_seq_lens_lst, local_context_lens_allranks
|
||||
):
|
||||
chunk_context_len = chunk_size
|
||||
if cp_chunk_seq_len != 0:
|
||||
chunk_context_len = min(
|
||||
chunk_context_len, origin_context_len - chunk_size * chunk_idx
|
||||
)
|
||||
cp_target_rank = (chunk_context_len - 1) % cp_world_size
|
||||
cur_seq_len = 0
|
||||
for rank in range(cp_world_size):
|
||||
if rank > cp_target_rank and cp_chunk_seq_len:
|
||||
real_cp_chunk_seq_len = cp_chunk_seq_len - 1
|
||||
else:
|
||||
real_cp_chunk_seq_len = cp_chunk_seq_len
|
||||
if real_cp_chunk_seq_len:
|
||||
for rank, local_context_len in enumerate(local_context_lens):
|
||||
if local_context_len != 0:
|
||||
kv_c_segment = allgatered_kv_c_normed[
|
||||
rank * toks + src_token_idx : rank * toks
|
||||
+ src_token_idx
|
||||
+ real_cp_chunk_seq_len
|
||||
+ local_context_len
|
||||
]
|
||||
k_pe_segment = allgatered_k_pe[
|
||||
rank * toks + src_token_idx : rank * toks
|
||||
+ src_token_idx
|
||||
+ real_cp_chunk_seq_len
|
||||
+ local_context_len
|
||||
]
|
||||
kv_c_segments.append(kv_c_segment)
|
||||
k_pe_segments.append(k_pe_segment)
|
||||
cur_seq_len += real_cp_chunk_seq_len
|
||||
cur_seq_len += local_context_len
|
||||
max_seq_len_check = max(max_seq_len_check, cur_seq_len)
|
||||
src_token_idx += cp_chunk_seq_len
|
||||
src_token_idx += padded_local_chunk_seq_len
|
||||
reorganized_kv_c_normed = torch.cat(kv_c_segments, dim=0)
|
||||
reorganized_k_pe = torch.cat(k_pe_segments, dim=0)
|
||||
assert reorganized_kv_c_normed.shape[0] == sum_seq_len
|
||||
@@ -1296,6 +1297,9 @@ class MLACommonImpl(MLACommonBaseImpl[M], Generic[M]):
|
||||
get_current_vllm_config()
|
||||
)
|
||||
)
|
||||
self.dcp_kv_cache_interleave_size: int = (
|
||||
get_current_vllm_config().parallel_config.dcp_kv_cache_interleave_size
|
||||
)
|
||||
|
||||
def _flash_attn_varlen_diff_headdims(
|
||||
self, q, k, v, return_softmax_lse=False, softmax_scale=None, **kwargs
|
||||
@@ -1697,10 +1701,9 @@ class MLACommonImpl(MLACommonBaseImpl[M], Generic[M]):
|
||||
assert attn_metadata.prefill is not None
|
||||
prefill_metadata = attn_metadata.prefill
|
||||
assert prefill_metadata.chunked_context is not None
|
||||
assert prefill_metadata.chunked_context.cp_chunk_seq_lens is not None
|
||||
assert prefill_metadata.chunked_context.origin_context_lens is not None
|
||||
assert prefill_metadata.chunked_context.cp_cu_seq_lens is not None
|
||||
assert prefill_metadata.chunked_context.chunk_size is not None
|
||||
assert prefill_metadata.chunked_context.padded_local_chunk_seq_lens is not None
|
||||
assert prefill_metadata.chunked_context.local_context_lens_allranks is not None
|
||||
assert prefill_metadata.chunked_context.padded_local_cu_seq_lens is not None
|
||||
assert prefill_metadata.chunked_context.cu_seq_lens_lst is not None
|
||||
|
||||
output = None
|
||||
@@ -1713,7 +1716,9 @@ class MLACommonImpl(MLACommonBaseImpl[M], Generic[M]):
|
||||
src_cache=kv_c_and_k_pe_cache,
|
||||
dst=workspace,
|
||||
block_table=prefill_metadata.block_table,
|
||||
cu_seq_lens=prefill_metadata.chunked_context.cp_cu_seq_lens[i],
|
||||
cu_seq_lens=prefill_metadata.chunked_context.padded_local_cu_seq_lens[
|
||||
i
|
||||
],
|
||||
batch_size=attn_metadata.num_prefills,
|
||||
seq_starts=prefill_metadata.chunked_context.starts[i],
|
||||
)
|
||||
@@ -1743,15 +1748,12 @@ class MLACommonImpl(MLACommonBaseImpl[M], Generic[M]):
|
||||
kv_c_normed, k_pe = reorg_kvcache(
|
||||
allgatered_kv_c_normed,
|
||||
allgatered_k_pe,
|
||||
cp_chunk_seq_lens_lst=prefill_metadata.chunked_context.cp_chunk_seq_lens[
|
||||
padded_local_chunk_seq_lens_lst=prefill_metadata.chunked_context.padded_local_chunk_seq_lens[
|
||||
i
|
||||
],
|
||||
origin_context_lens=prefill_metadata.chunked_context.origin_context_lens,
|
||||
cp_world_size=dcp_world_size,
|
||||
local_context_lens_allranks=prefill_metadata.chunked_context.local_context_lens_allranks,
|
||||
sum_seq_len=prefill_metadata.chunked_context.cu_seq_lens_lst[i][-1],
|
||||
max_seq_len=prefill_metadata.chunked_context.max_seq_lens[i],
|
||||
chunk_size=prefill_metadata.chunked_context.chunk_size,
|
||||
chunk_idx=i,
|
||||
toks=toks,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user