@@ -171,9 +171,12 @@ def __init__(
171171 self .share_inputs .init_share_inputs ()
172172 self .max_num_seqs = self .fd_config .scheduler_config .max_num_seqs
173173
174+ self .increment_value = (
175+ 4 if not self .speculative_decoding else (self .speculative_config .num_speculative_tokens + 1 ) * 4
176+ )
174177 self .infer_seed_increment = paddle .full (
175178 shape = [self .scheduler_config .max_num_seqs , 1 ],
176- fill_value = 4 ,
179+ fill_value = self . increment_value ,
177180 dtype = "int64" ,
178181 ).cpu ()
179182
@@ -837,22 +840,8 @@ def _prepare_inputs(self, is_dummy_run=False) -> None:
837840 if self .use_cudagraph :
838841 # Update Batch type for cuda graph for only_decode_batch
839842 if_only_decode = self .only_decode ()
840-
841- only_decode_use_cudagraph = self .use_cudagraph and if_only_decode
842- # Update config about moe for better performance
843- # TODO(wanglongzhi):Modifying the config at runtime is not appropriate; it needs to be moved to forward_meta. It will be used in MoEMethodBase.apply()
844- if self .fd_config .parallel_config .use_ep and self .fd_config .scheduler_config .splitwise_role == "mixed" :
845- self .fd_config .model_config .moe_phase .phase = "decode" if if_only_decode else "prefill"
846- if self .speculative_decoding :
847- self .proposer .fd_config .parallel_config .moe_phase .phase = "decode" if if_only_decode else "prefill"
848-
849- # Update Batch type for cuda graph for only_prefill_batch
850- only_prefill_use_cudagraph = self .use_cudagraph and self .cudagraph_only_prefill and self .only_prefill ()
851-
852843 self .forward_meta .step_use_cudagraph = (
853- only_prefill_use_cudagraph
854- if self .cudagraph_only_prefill
855- else only_decode_use_cudagraph and self .forward_meta .ids_remove_padding .shape [0 ] > 0
844+ self .use_cudagraph and if_only_decode and self .forward_meta .ids_remove_padding .shape [0 ] > 0
856845 )
857846
858847 # Update bad tokens len
@@ -864,11 +853,10 @@ def _prepare_inputs(self, is_dummy_run=False) -> None:
864853 if self .pd_disaggregation_mode == "per_chunk" or self .pd_disaggregation_mode == "per_query" :
865854 self .forward_meta .kv_signal_sender = self .share_inputs ["kv_signal_sender" ]
866855
867- if (
868- self .fd_config .scheduler_config .splitwise_role == "mixed" and envs .FD_XPU_ENABLE_MIXED_EP_MODE
869- ): # Centralized scenario: the phase is initialized as "prefill" by default. During inference runtime, different types of batches can achieve phase switching at this point.
856+ if self .fd_config .parallel_config .use_ep and self .fd_config .scheduler_config .splitwise_role == "mixed" :
870857 if_only_decode = self .only_decode ()
871858 self .fd_config .model_config .moe_phase .phase = "decode" if if_only_decode else "prefill"
859+ # TODO: sync proposer.fd_config.model_config.moe_phase.phase for MTP draft model in mixed EP mode
872860
873861 # Get sampling metadata
874862 # TODU(lilujia): sync with GPU
@@ -1122,6 +1110,7 @@ def _dummy_run(
11221110 batch_size : paddle .Tensor ,
11231111 expected_decode_len : int = 1 ,
11241112 in_capturing : bool = False ,
1113+ accept_all_drafts = False ,
11251114 ) -> paddle .Tensor :
11261115 """
11271116 Use dummy inputs to run before formal execution.
@@ -1146,11 +1135,11 @@ def _dummy_run(
11461135 self .proposer .dummy_prefill_inputs (
11471136 num_tokens = num_tokens ,
11481137 batch_size = batch_size ,
1149- expected_decode_len = 1 ,
1138+ expected_decode_len = expected_decode_len ,
11501139 )
11511140
11521141 while True :
1153- self .execute_model (is_dummy_run = True , in_capturing = in_capturing )
1142+ self .execute_model (is_dummy_run = True , in_capturing = in_capturing , accept_all_drafts = accept_all_drafts )
11541143
11551144 if int ((self .share_inputs ["seq_lens_this_time" ] > 0 ).sum ()) == 0 :
11561145 break
@@ -1199,14 +1188,30 @@ def capture_model(self) -> None:
11991188 capture_sizes = self .cudagraph_capture_sizes .copy ()
12001189
12011190 try :
1202- for batch_size in sorted (capture_sizes , reverse = True ):
1203- self ._dummy_run (
1204- num_tokens = self .scheduler_config .max_num_batched_tokens ,
1205- batch_size = batch_size ,
1206- expected_decode_len = expected_decode_len ,
1207- in_capturing = True ,
1208- )
1209- logger .info (f"Warm up the model with the batch size:{ batch_size } , num tokens:{ expected_decode_len } " )
1191+ if self .speculative_decoding and self .spec_method in [SpecMethod .MTP , SpecMethod .SUFFIX ]:
1192+ for capture_size in sorted (capture_sizes , reverse = True ):
1193+ expected_decode_len = (self .speculative_config .num_speculative_tokens + 1 ) * 2
1194+ self ._dummy_run (
1195+ num_tokens = self .fd_config .get_max_chunk_tokens (),
1196+ batch_size = int (capture_size / (self .speculative_config .num_speculative_tokens + 1 )),
1197+ in_capturing = True ,
1198+ expected_decode_len = expected_decode_len ,
1199+ accept_all_drafts = True ,
1200+ )
1201+ logger .info (
1202+ f"Warm up the model with the num_tokens:{ capture_size } , expected_decode_len:{ expected_decode_len } "
1203+ )
1204+ else :
1205+ for batch_size in sorted (capture_sizes , reverse = True ):
1206+ self ._dummy_run (
1207+ num_tokens = self .scheduler_config .max_num_batched_tokens ,
1208+ batch_size = batch_size ,
1209+ expected_decode_len = expected_decode_len ,
1210+ in_capturing = True ,
1211+ )
1212+ logger .info (
1213+ f"Warm up the model with the batch size:{ batch_size } , num tokens:{ expected_decode_len } "
1214+ )
12101215 except RuntimeError as e :
12111216 if "out of memory" in str (e ):
12121217 raise RuntimeError (
@@ -1263,6 +1268,7 @@ def execute_model(
12631268 num_running_requests : int = None ,
12641269 is_dummy_run : bool = False ,
12651270 in_capturing : bool = False ,
1271+ accept_all_drafts : bool = False ,
12661272 ) -> Optional [ModelRunnerOutput ]:
12671273 """
12681274 The Entrance of model execute.
@@ -1276,14 +1282,18 @@ class at the server level, which is too granular for ModelRunner.
12761282 # 0. set debug level
12771283 # self._set_debug_level(0x1, model_forward_batch, is_dummy_run)
12781284 with kv_signal_sender_context_manager (self .pd_disaggregation_mode ) as sender :
1279-
12801285 self .share_inputs ["kv_signal_sender" ] = sender
12811286 # 1. Prepare inputs of model and decoder.
12821287 self ._prepare_inputs (is_dummy_run = is_dummy_run )
1288+ # 2. Padding inputs for cuda graph
1289+ self .padding_cudagraph_inputs ()
12831290 if is_dummy_run :
12841291 self .forward_meta .step_use_cudagraph = in_capturing and self .forward_meta .step_use_cudagraph
1285- # 2. Padding inputs for cuda grph
1286- self .padding_cudagraph_inputs ()
1292+ else :
1293+ self .forward_meta .step_use_cudagraph = (
1294+ self .forward_meta .step_use_cudagraph
1295+ and self .real_token_num <= self .fd_config .graph_opt_config .max_capture_size
1296+ )
12871297
12881298 num_tokens = self .share_inputs ["ids_remove_padding" ].shape [0 ]
12891299 if not self .parallel_config .enable_expert_parallel and num_tokens <= 0 :
@@ -1300,7 +1310,7 @@ class at the server level, which is too granular for ModelRunner.
13001310 model_inputs ["ids_remove_padding" ] = self .share_inputs ["ids_remove_padding" ]
13011311 if self .enable_mm :
13021312 model_inputs ["image_features" ] = self .share_inputs ["image_features" ]
1303- # 3. Execute model
1313+ # 3. Execute
13041314 model_output = self .model (
13051315 model_inputs ,
13061316 forward_meta = self .forward_meta ,
@@ -1331,6 +1341,8 @@ class at the server level, which is too granular for ModelRunner.
13311341 self .sampling_metadata ,
13321342 self .model_config .max_model_len ,
13331343 self .share_inputs ,
1344+ self .increment_value ,
1345+ accept_all_drafts = accept_all_drafts ,
13341346 )
13351347 if self .parallel_config .tensor_parallel_size > 1 :
13361348 paddle .distributed .broadcast (
@@ -1428,13 +1440,18 @@ class at the server level, which is too granular for ModelRunner.
14281440 # 6. Draft model propose
14291441 if self .speculative_decoding and self .proposer is not None :
14301442 if self .spec_method == SpecMethod .MTP :
1431- self .proposer .run (full_hidden_states = model_output )
1443+ self .proposer .run (
1444+ full_hidden_states = model_output ,
1445+ step_use_cudagraph = self .forward_meta .step_use_cudagraph ,
1446+ is_dummy_run = is_dummy_run ,
1447+ )
14321448 else :
14331449 self .proposer .run (share_inputs = self .share_inputs )
14341450
14351451 # 7. Updata 'infer_seed' and step_paddle()
1436- self .share_inputs ["infer_seed" ].add_ (self .infer_seed_increment )
1437- self .share_inputs ["infer_seed" ][:] %= self .MAX_INFER_SEED
1452+ if not self .speculative_decoding :
1453+ self .share_inputs ["infer_seed" ].add_ (self .infer_seed_increment )
1454+ self .share_inputs ["infer_seed" ][:] %= self .MAX_INFER_SEED
14381455
14391456 if self .speculative_decoding :
14401457 speculate_schedule_cache (
0 commit comments