Tiny extract ModelRunnerOutput (#15400)

This commit is contained in:
fzyzcjy
2025-12-18 22:18:45 +08:00
committed by GitHub
parent c5f4e20f2f
commit ad9616f13a
7 changed files with 34 additions and 27 deletions
+2 -2
View File
@@ -373,7 +373,7 @@ def extend(reqs, model_runner):
_maybe_prepare_mlp_sync_batch(batch, model_runner) _maybe_prepare_mlp_sync_batch(batch, model_runner)
model_worker_batch = batch.get_model_worker_batch() model_worker_batch = batch.get_model_worker_batch()
forward_batch = ForwardBatch.init_new(model_worker_batch, model_runner) forward_batch = ForwardBatch.init_new(model_worker_batch, model_runner)
logits_output, _ = model_runner.forward(forward_batch) logits_output = model_runner.forward(forward_batch).logits_output
next_token_ids = model_runner.sample(logits_output, forward_batch) next_token_ids = model_runner.sample(logits_output, forward_batch)
return next_token_ids, logits_output.next_token_logits, batch return next_token_ids, logits_output.next_token_logits, batch
@@ -385,7 +385,7 @@ def decode(input_token_ids, batch, model_runner):
_maybe_prepare_mlp_sync_batch(batch, model_runner) _maybe_prepare_mlp_sync_batch(batch, model_runner)
model_worker_batch = batch.get_model_worker_batch() model_worker_batch = batch.get_model_worker_batch()
forward_batch = ForwardBatch.init_new(model_worker_batch, model_runner) forward_batch = ForwardBatch.init_new(model_worker_batch, model_runner)
logits_output, _ = model_runner.forward(forward_batch) logits_output = model_runner.forward(forward_batch).logits_output
next_token_ids = model_runner.sample(logits_output, forward_batch) next_token_ids = model_runner.sample(logits_output, forward_batch)
return next_token_ids, logits_output.next_token_logits return next_token_ids, logits_output.next_token_logits
@@ -35,9 +35,8 @@ class LowConfidence(DllmAlgorithm):
if torch.sum(mask_index).item() == 0: if torch.sum(mask_index).item() == 0:
break break
logits_output, can_run_cuda_graph = model_runner.forward( out = model_runner.forward(forward_batch, pp_proxy_tensors=None)
forward_batch, pp_proxy_tensors=None logits_output, can_run_cuda_graph = out.logits_output, out.can_run_graph
)
x = torch.argmax(logits_output.full_logits, dim=-1) x = torch.argmax(logits_output.full_logits, dim=-1)
p = torch.squeeze( p = torch.squeeze(
@@ -58,9 +57,8 @@ class LowConfidence(DllmAlgorithm):
forward_batch.input_ids[transfer_index] = x[transfer_index] forward_batch.input_ids[transfer_index] = x[transfer_index]
logits_output, can_run_cuda_graph = model_runner.forward( out = model_runner.forward(forward_batch, pp_proxy_tensors=None)
forward_batch, pp_proxy_tensors=None logits_output, can_run_cuda_graph = out.logits_output, out.can_run_graph
)
next_token_ids = forward_batch.input_ids[start:] next_token_ids = forward_batch.input_ids[start:]
return logits_output, next_token_ids, can_run_cuda_graph return logits_output, next_token_ids, can_run_cuda_graph
@@ -614,7 +614,7 @@ class SchedulerPPMixin:
forward_batch = ForwardBatch.init_new( forward_batch = ForwardBatch.init_new(
model_worker_batch, self.tp_worker.model_runner model_worker_batch, self.tp_worker.model_runner
) )
_, _ = self.tp_worker.model_runner.forward( _ = self.tp_worker.model_runner.forward(
forward_batch=forward_batch, pp_proxy_tensors=pp_proxy forward_batch=forward_batch, pp_proxy_tensors=pp_proxy
) )
+7 -4
View File
@@ -196,7 +196,7 @@ class BaseTpWorker(ABC):
def forward_batch_embedding(self, model_worker_batch: ModelWorkerBatch): def forward_batch_embedding(self, model_worker_batch: ModelWorkerBatch):
forward_batch = ForwardBatch.init_new(model_worker_batch, self.model_runner) forward_batch = ForwardBatch.init_new(model_worker_batch, self.model_runner)
logits_output, _ = self.model_runner.forward(forward_batch) logits_output = self.model_runner.forward(forward_batch).logits_output
embeddings = logits_output.embeddings embeddings = logits_output.embeddings
return embeddings return embeddings
@@ -397,11 +397,12 @@ class TpModelWorker(BaseTpWorker):
return self._forward_batch_generation_dllm(forward_batch) return self._forward_batch_generation_dllm(forward_batch)
if self.pp_group.is_last_rank: if self.pp_group.is_last_rank:
logits_output, can_run_cuda_graph = self.model_runner.forward( out = self.model_runner.forward(
forward_batch, forward_batch,
pp_proxy_tensors=pp_proxy_tensors, pp_proxy_tensors=pp_proxy_tensors,
skip_attn_backend_init=skip_attn_backend_init, skip_attn_backend_init=skip_attn_backend_init,
) )
logits_output, can_run_cuda_graph = out.logits_output, out.can_run_graph
batch_result = GenerationBatchResult( batch_result = GenerationBatchResult(
logits_output=logits_output, logits_output=logits_output,
can_run_cuda_graph=can_run_cuda_graph, can_run_cuda_graph=can_run_cuda_graph,
@@ -450,11 +451,12 @@ class TpModelWorker(BaseTpWorker):
return batch_result return batch_result
else: else:
pp_proxy_tensors, can_run_cuda_graph = self.model_runner.forward( out = self.model_runner.forward(
forward_batch, forward_batch,
pp_proxy_tensors=pp_proxy_tensors, pp_proxy_tensors=pp_proxy_tensors,
skip_attn_backend_init=skip_attn_backend_init, skip_attn_backend_init=skip_attn_backend_init,
) )
pp_proxy_tensors, can_run_cuda_graph = out.logits_output, out.can_run_graph
return GenerationBatchResult( return GenerationBatchResult(
pp_hidden_states_proxy_tensors=pp_proxy_tensors, pp_hidden_states_proxy_tensors=pp_proxy_tensors,
can_run_cuda_graph=can_run_cuda_graph, can_run_cuda_graph=can_run_cuda_graph,
@@ -469,9 +471,10 @@ class TpModelWorker(BaseTpWorker):
else: else:
model_worker_batch = batch.get_model_worker_batch(batch.seq_lens_cpu_cache) model_worker_batch = batch.get_model_worker_batch(batch.seq_lens_cpu_cache)
logits_output, can_run_cuda_graph = self.model_runner.forward( out = self.model_runner.forward(
batch.split_forward_batch, split_forward_count=batch.split_forward_count batch.split_forward_batch, split_forward_count=batch.split_forward_count
) )
logits_output, can_run_cuda_graph = out.logits_output, out.can_run_graph
if logits_output: if logits_output:
next_token_ids = self.model_runner.sample(logits_output, model_worker_batch) next_token_ids = self.model_runner.sample(logits_output, model_worker_batch)
else: else:
@@ -268,6 +268,12 @@ class RankZeroFilter(logging.Filter):
return True return True
@dataclass
class ModelRunnerOutput:
logits_output: Union[LogitsProcessorOutput, PPProxyTensors]
can_run_graph: bool
class ModelRunner: class ModelRunner:
"""ModelRunner runs the forward passes of the models.""" """ModelRunner runs the forward passes of the models."""
@@ -2726,7 +2732,7 @@ class ModelRunner:
pp_proxy_tensors: Optional[PPProxyTensors] = None, pp_proxy_tensors: Optional[PPProxyTensors] = None,
reinit_attn_backend: bool = False, reinit_attn_backend: bool = False,
split_forward_count: int = 1, split_forward_count: int = 1,
) -> Tuple[Union[LogitsProcessorOutput, PPProxyTensors], bool]: ) -> ModelRunnerOutput:
self.forward_pass_id += 1 self.forward_pass_id += 1
with get_global_expert_distribution_recorder().with_forward_pass( with get_global_expert_distribution_recorder().with_forward_pass(
@@ -2753,7 +2759,7 @@ class ModelRunner:
pp_proxy_tensors: Optional[PPProxyTensors], pp_proxy_tensors: Optional[PPProxyTensors],
reinit_attn_backend: bool = False, reinit_attn_backend: bool = False,
split_forward_count: int = 1, split_forward_count: int = 1,
) -> Tuple[Union[LogitsProcessorOutput, PPProxyTensors], bool]: ) -> ModelRunnerOutput:
mode_check = ( mode_check = (
forward_batch.forward_mode.is_cpu_graph forward_batch.forward_mode.is_cpu_graph
if self.device == "cpu" if self.device == "cpu"
@@ -2771,7 +2777,7 @@ class ModelRunner:
skip_attn_backend_init=skip_attn_backend_init, skip_attn_backend_init=skip_attn_backend_init,
pp_proxy_tensors=pp_proxy_tensors, pp_proxy_tensors=pp_proxy_tensors,
) )
return ret, can_run_graph return ModelRunnerOutput(logits_output=ret, can_run_graph=can_run_graph)
# For MLP sync # For MLP sync
if forward_batch.global_num_tokens_cpu is not None: if forward_batch.global_num_tokens_cpu is not None:
@@ -2819,7 +2825,7 @@ class ModelRunner:
): ):
forward_batch.post_forward_mlp_sync_batch(ret) forward_batch.post_forward_mlp_sync_batch(ret)
return ret, can_run_graph return ModelRunnerOutput(logits_output=ret, can_run_graph=can_run_graph)
def _preprocess_logits( def _preprocess_logits(
self, logits_output: LogitsProcessorOutput, sampling_info: SamplingBatchInfo self, logits_output: LogitsProcessorOutput, sampling_info: SamplingBatchInfo
@@ -646,9 +646,9 @@ class EAGLEWorker(TpModelWorker):
spec_info.hidden_states = hidden_states spec_info.hidden_states = hidden_states
# Run forward # Run forward
logits_output, _ = self.draft_model_runner.forward( logits_output = self.draft_model_runner.forward(
forward_batch, skip_attn_backend_init=True forward_batch, skip_attn_backend_init=True
) ).logits_output
if self.server_args.enable_nan_detection: if self.server_args.enable_nan_detection:
detect_nan(logits_output) detect_nan(logits_output)
probs = torch.softmax(logits_output.next_token_logits, dim=-1) probs = torch.softmax(logits_output.next_token_logits, dim=-1)
@@ -946,7 +946,7 @@ class EAGLEWorker(TpModelWorker):
model_worker_batch, self.draft_model_runner model_worker_batch, self.draft_model_runner
) )
forward_batch.return_logprob = False forward_batch.return_logprob = False
logits_output, _ = self.draft_model_runner.forward(forward_batch) logits_output = self.draft_model_runner.forward(forward_batch).logits_output
if self.enable_nan_detection: if self.enable_nan_detection:
detect_nan(logits_output) detect_nan(logits_output)
assert isinstance(forward_batch.spec_info, EagleDraftInput) assert isinstance(forward_batch.spec_info, EagleDraftInput)
@@ -1023,9 +1023,9 @@ class EAGLEWorker(TpModelWorker):
self.draft_model_runner.attn_backend.init_forward_metadata( self.draft_model_runner.attn_backend.init_forward_metadata(
forward_batch forward_batch
) )
logits_output, _ = self.draft_model_runner.forward( logits_output = self.draft_model_runner.forward(
forward_batch, skip_attn_backend_init=True forward_batch, skip_attn_backend_init=True
) ).logits_output
self.capture_for_decode(logits_output, forward_batch.spec_info) self.capture_for_decode(logits_output, forward_batch.spec_info)
if self.enable_nan_detection: if self.enable_nan_detection:
@@ -391,9 +391,9 @@ class EagleDraftWorker(BaseDraftWorker):
spec_info.hidden_states = hidden_states spec_info.hidden_states = hidden_states
# Run forward # Run forward
logits_output, _ = self.draft_runner.forward( logits_output = self.draft_runner.forward(
forward_batch, skip_attn_backend_init=True forward_batch, skip_attn_backend_init=True
) ).logits_output
if self.server_args.enable_nan_detection: if self.server_args.enable_nan_detection:
detect_nan(logits_output) detect_nan(logits_output)
probs = torch.softmax(logits_output.next_token_logits, dim=-1) probs = torch.softmax(logits_output.next_token_logits, dim=-1)
@@ -465,7 +465,7 @@ class EagleDraftWorker(BaseDraftWorker):
# Run forward # Run forward
forward_batch = ForwardBatch.init_new(batch, self.draft_runner) forward_batch = ForwardBatch.init_new(batch, self.draft_runner)
logits_output, _ = self.draft_runner.forward(forward_batch) logits_output = self.draft_runner.forward(forward_batch).logits_output
# Update spec_info for the next draft step # Update spec_info for the next draft step
probs = torch.softmax(logits_output.next_token_logits, dim=-1) probs = torch.softmax(logits_output.next_token_logits, dim=-1)
@@ -516,9 +516,9 @@ class EagleDraftWorker(BaseDraftWorker):
forward_batch forward_batch
) )
else: else:
draft_logits_output, _ = self.draft_runner.forward( draft_logits_output = self.draft_runner.forward(
forward_batch, skip_attn_backend_init=True forward_batch, skip_attn_backend_init=True
) ).logits_output
# Reorganize the spec info for the next batch # Reorganize the spec info for the next batch
draft_logits_output.next_token_logits = draft_logits_output.next_token_logits[ draft_logits_output.next_token_logits = draft_logits_output.next_token_logits[