Repository navigation
[Kimi K3] Support sequence parallelism with pipeline parallelism #54347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
f982028
81f6ea5
3d5746f
b14f64e
1fece20
c43e021
c7f52ac
ddab02d
554153f
176dde7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,6 +134,29 @@ | |
| _ROUTED_DOWN_PROJ_STREAM_TOKEN_THRESHOLD = 256 | ||
|
|
||
|
|
||
| def use_kimi_k3_sequence_parallel(vllm_config: VllmConfig) -> bool: | ||
| """Select Kimi K3's model-level sequence parallel path. | ||
|
|
||
| DeepGEMM MegaMoE requires distinct token shards on the TP/EP ranks, so it | ||
| keeps sequence parallelism enabled across pipeline stages. Other MoE | ||
| backends retain the existing DP-only behavior and do not change when PP is | ||
| enabled. | ||
| """ | ||
| parallel_config = vllm_config.parallel_config | ||
| use_mega_moe = vllm_config.kernel_config.moe_backend == "deep_gemm_mega_moe" | ||
| return ( | ||
| parallel_config.enable_expert_parallel | ||
| and parallel_config.tensor_parallel_size > 1 | ||
| and ( | ||
| use_mega_moe | ||
| or ( | ||
| parallel_config.pipeline_parallel_size == 1 | ||
| and parallel_config.data_parallel_size > 1 | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def shard_sequence_parallel_mlp( | ||
| hidden_size: int, | ||
| intermediate_size: int, | ||
|
|
@@ -870,21 +893,14 @@ def __init__( | |
| layer_idx = self.layer_idx | ||
| cache_config = vllm_config.cache_config | ||
| quant_config = vllm_config.quant_config | ||
| parallel_config = vllm_config.parallel_config | ||
| self.is_moe_layer = ( | ||
| self.is_moe | ||
| and config.num_experts is not None | ||
| and layer_idx >= config.first_k_dense_replace | ||
| and layer_idx % config.moe_layer_freq == 0 | ||
| ) | ||
|
|
||
| use_mega_moe = vllm_config.kernel_config.moe_backend == "deep_gemm_mega_moe" | ||
| self.use_sequence_parallel = ( | ||
| parallel_config.pipeline_parallel_size == 1 | ||
| and parallel_config.enable_expert_parallel | ||
| and parallel_config.tensor_parallel_size > 1 | ||
| and (use_mega_moe or parallel_config.data_parallel_size > 1) | ||
| ) | ||
| self.use_sequence_parallel = use_kimi_k3_sequence_parallel(vllm_config) | ||
| if config.is_kda_layer(layer_idx): | ||
| kda_config = config.linear_attn_config | ||
| assert kda_config is not None | ||
|
|
@@ -1130,14 +1146,17 @@ def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): | |
| self.config = config | ||
| self.attn_res_block_size: int | None = config.attn_res_block_size | ||
| self.use_attn_res = self.attn_res_block_size is not None | ||
| parallel_config = vllm_config.parallel_config | ||
| use_mega_moe = vllm_config.kernel_config.moe_backend == "deep_gemm_mega_moe" | ||
| self.use_sequence_parallel = ( | ||
| parallel_config.pipeline_parallel_size == 1 | ||
| and parallel_config.enable_expert_parallel | ||
| and parallel_config.tensor_parallel_size > 1 | ||
| and (use_mega_moe or parallel_config.data_parallel_size > 1) | ||
| ) | ||
| self.use_sequence_parallel = use_kimi_k3_sequence_parallel(vllm_config) | ||
| if self.use_sequence_parallel: | ||
| parallel_config = vllm_config.parallel_config | ||
| logger.info_once( | ||
| "Kimi K3 model-level sequence parallelism is enabled " | ||
| "(TP=%d, PP=%d, MoE backend=%s).", | ||
| parallel_config.tensor_parallel_size, | ||
| parallel_config.pipeline_parallel_size, | ||
| vllm_config.kernel_config.moe_backend, | ||
| scope="global", | ||
| ) | ||
|
|
||
| self.vocab_size = config.vocab_size | ||
|
|
||
|
|
@@ -1335,10 +1354,24 @@ def forward( | |
| if envs.VLLM_MOE_SKIP_PADDING and is_forward_context_available(): | ||
| forward_context = get_forward_context() | ||
| forward_context.is_padding = sp_padding_mask( | ||
| forward_context.is_padding, hidden_states | ||
| forward_context.is_padding, positions | ||
| ) | ||
| if get_pp_group().is_first_rank: | ||
| hidden_states = sp_shard(hidden_states) | ||
| assert residual is None | ||
| else: | ||
| # PP communication transports SP-local rows directly between | ||
| # matching TP ranks. During profiling/capture the persistent | ||
| # input buffers are full-sized, so narrow both cases to the | ||
| # local token count here. | ||
| local_num_tokens = cdiv( | ||
| full_num_tokens, get_tensor_model_parallel_world_size() | ||
| ) | ||
| hidden_states = sp_shard(hidden_states) | ||
| assert residual is None, "Currently, SP is not supported with PP" | ||
| assert hidden_states.shape[0] >= local_num_tokens | ||
| hidden_states = hidden_states[:local_num_tokens] | ||
| assert residual is not None | ||
| assert residual.shape[0] >= local_num_tokens | ||
| residual = residual[:local_num_tokens] | ||
|
|
||
| # sharded aux hidden states when sp is enabled | ||
| aux_hidden_states: list[torch.Tensor] = [] | ||
|
|
@@ -1387,9 +1420,6 @@ def forward( | |
| assert hidden_states is not None | ||
| assert residual is not None | ||
| if not get_pp_group().is_last_rank: | ||
| assert not self.use_sequence_parallel, ( | ||
| "Currently, SP is not supported with PP" | ||
| ) | ||
| if prefix_sum is not None: | ||
| hidden_states = hidden_states + prefix_sum | ||
| return IntermediateTensors( | ||
|
|
@@ -1640,6 +1670,12 @@ def make_empty_intermediate_tensors( | |
| ) -> IntermediateTensors: | ||
| return self.model.make_empty_intermediate_tensors(batch_size, dtype, device) | ||
|
|
||
| def get_pp_intermediate_tensor_all_gather_overrides(self) -> dict[str, bool]: | ||
| """Keep model-level SP shards local across pipeline stages.""" | ||
| if not self.model.use_sequence_parallel: | ||
| return {} | ||
| return {"hidden_states": False, "residual": False} | ||
|
|
||
| def forward( # type: ignore[override] | ||
| self, | ||
| input_ids: torch.Tensor | None, | ||
|
|
@@ -1854,6 +1890,14 @@ def __init__( | |
| ) | ||
| self.media_placeholder: int = self.config.media_placeholder_token_id | ||
|
|
||
| def get_pp_intermediate_tensor_all_gather_overrides(self) -> dict[str, bool]: | ||
| getter = getattr( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| self.language_model, | ||
| "get_pp_intermediate_tensor_all_gather_overrides", | ||
| None, | ||
| ) | ||
| return {} if getter is None else getter() | ||
|
|
||
| # -- SupportsEncoderCudaGraph protocol methods -- | ||
|
|
||
| def get_encoder_cudagraph_config(self): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -489,6 +489,7 @@ def __init__( | |
| self.aux_hidden_states: list[torch.Tensor] = [] | ||
| self.use_aux_hidden_state_outputs = False | ||
| self.intermediate_tensors: IntermediateTensors | None = None | ||
| self.intermediate_tensor_num_tokens: dict[BatchExecutionDescriptor, int] = {} | ||
|
|
||
| def capture( | ||
| self, | ||
|
|
@@ -605,12 +606,19 @@ def forward_fn(cg_mode: CUDAGraphMode) -> None: | |
| # Non-last PP rank. | ||
| assert isinstance(model_output, IntermediateTensors) | ||
| intermediate_tensors = model_output | ||
| output_lengths = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer this output_token_counts = {
tensor.shape[0]
for tensor in intermediate_tensors.tensors.values()
}
assert len(output_token_counts) == 1
output_num_tokens = next(iter(output_token_counts))And we should have a util function for this beause it's used more than once in this PR |
||
| tensor.shape[0] | ||
| for tensor in intermediate_tensors.tensors.values() | ||
| } | ||
| assert len(output_lengths) == 1 | ||
| output_num_tokens = output_lengths.pop() | ||
| self.intermediate_tensor_num_tokens[desc] = output_num_tokens | ||
| if self.intermediate_tensors is None: | ||
| self.intermediate_tensors = IntermediateTensors.empty_like( | ||
| intermediate_tensors | ||
| ) | ||
| for k, v in intermediate_tensors.tensors.items(): | ||
| self.intermediate_tensors[k][:num_tokens] = v | ||
| self.intermediate_tensors[k][:output_num_tokens] = v | ||
|
|
||
| return forward_fn | ||
|
|
||
|
|
@@ -623,7 +631,8 @@ def run_fullgraph( | |
| super().run_fullgraph(desc) | ||
| if not self.is_last_pp_rank: | ||
| assert self.intermediate_tensors is not None | ||
| return self.intermediate_tensors[: desc.num_tokens] | ||
| num_tokens = self.intermediate_tensor_num_tokens[desc] | ||
| return self.intermediate_tensors[:num_tokens] | ||
|
|
||
| assert self.hidden_states is not None | ||
| hidden_states = self.hidden_states[: desc.num_tokens] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.