# Copyright 2026 Bytedance Ltd. and/or its affiliates # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Runtime monkey-patches that teach vLLM-Omni's AR path to return the thinker's last-layer, post-final-norm hidden states (the exact tensor ``model.compute_logits`` consumes) for full-vocabulary OPD. The per-request payloads ride the existing ``multimodal_output`` channel under the key ``"teacher_hidden_states"``, so no new wire fields are needed: runner : hidden per request -> OmniModelRunnerOutput.multimodal_outputs scheduler: -> OmniEngineCoreOutput.multimodal_output processor: -> OmniRequestState.mm_accumulated client : -> CompletionOutput.multimodal_output["teacher_hidden_states"] Chunked prefill is not supported (a request with unscheduled prompt tokens raises). With the omni prefix cache enabled, requests resuming from a cached prefix get their full-prompt hidden states rebuilt from the cache. Graph-mode and sequence-parallel tail padding is stripped before the hidden states are validated and sliced. These patches are the *legacy* transport, for the vLLM-Omni pin that still has its V1 AR runners. vLLM-Omni builds with the typed per-request ``return_hidden_states`` field serve the channel natively on the worker-v2 AR runner, with the same ``SamplingParams.extra_args`` opt-in and the same ``multimodal_output`` key, so nothing is patched and :func:`extract_hidden_states` is unchanged. :func:`hidden_states_transport` tells the two apart; a build with neither cannot serve the teacher channel. """ import logging from importlib.util import find_spec import torch logger = logging.getLogger(__file__) # Key under which the hidden states ride the multimodal_output channel. TEACHER_HIDDEN_STATES_KEY = "teacher_hidden_states" # Flag key carried on the request's SamplingParams.extra_args. RETURN_FLAG_KEY = "return_hidden_states" _applied = False def hidden_states_transport() -> str | None: """How the installed vLLM-Omni can return teacher hidden states. Returns ``"native"`` when it has the typed per-request opt-in, ``"legacy"`` when the V1 AR runners these patches target exist, and ``None`` when it has neither. Checks module presence only, so it is safe to call in the server process; the patches themselves are installed in the worker process. """ try: from vllm_omni.engine import OmniEngineCoreRequest if "return_hidden_states" in getattr(OmniEngineCoreRequest, "__struct_fields__", ()): return "native" except ImportError: pass if any( find_spec(module) is not None for module in ( "vllm_omni.worker.gpu_ar_model_runner", "vllm_omni.platforms.npu.worker.npu_ar_model_runner", ) ): return "legacy" return None def request_wants_hidden_states(runner, req_id: str) -> bool: """True when the request opted in via ``SamplingParams.extra_args``.""" req_state = runner.requests.get(req_id) params = getattr(req_state, "sampling_params", None) extra = getattr(params, "extra_args", None) return bool(isinstance(extra, dict) and extra.get(RETURN_FLAG_KEY)) def apply_hidden_states_patches() -> None: """Install the hidden-state output channel patches (idempotent).""" global _applied if _applied: return try: from vllm_omni.worker.gpu_ar_model_runner import GPUARModelRunner except ImportError: pass # non-CUDA deployment; the NPU patch below still applies else: _patch_runner(GPUARModelRunner) _patch_npu_runner() _applied = True logger.info("vllm_omni hidden-states patches applied") def _is_step_hidden_states(hidden_states, scheduler_output) -> bool: if not isinstance(hidden_states, torch.Tensor) or hidden_states.ndim != 2: return False total = sum(int(n) for n in scheduler_output.num_scheduled_tokens.values()) return hidden_states.shape[0] == total def _strip_padding(hidden_states, scheduler_output) -> torch.Tensor: """Trim graph-mode / sequence-parallel tail padding back to the scheduled rows. Padding rows are appended after the last real request, so the scheduled tokens occupy rows [0, total). """ total = sum(int(n) for n in scheduler_output.num_scheduled_tokens.values()) return hidden_states[:total] def _patch_npu_runner() -> None: """Patch the Ascend AR runner, which builds OmniModelRunnerOutput inline. NPUARModelRunner.sample_tokens clears ``execute_model_state`` and returns either an OmniModelRunnerOutput or an AsyncGPUModelRunnerOutput wrapper. We peek the state first (passing through when the engine did not opt in), call the original, then attach the per-request hidden payloads to the inner model_runner_output. """ try: from vllm_omni.platforms.npu.worker.npu_ar_model_runner import NPUARModelRunner except ImportError: return # non-Ascend deployment original_sample = NPUARModelRunner.sample_tokens def _sample_with_hidden_states(self, grammar_output): state = self.execute_model_state if state is None: return original_sample(self, grammar_output) # Pass through unless this engine opted into hidden-state output (the # flag is uniform per engine), so ordinary requests are never affected. scheduler_output = getattr(state, "scheduler_output", None) if ( scheduler_output is None or not scheduler_output.num_scheduled_tokens or not request_wants_hidden_states(self, next(iter(scheduler_output.num_scheduled_tokens))) ): return original_sample(self, grammar_output) hidden_states = getattr(state, "hidden_states", None) if hidden_states is None: raise RuntimeError( "vllm_omni hidden-states patch: execute_model_state lacks the expected " f"hidden_states field (got {type(state).__name__}); the NPU AR runner's " "state layout changed — update the patch for the new vllm-omni pin." ) if isinstance(hidden_states, torch.Tensor): hidden_states = _strip_padding(hidden_states, scheduler_output) if not _is_step_hidden_states(hidden_states, scheduler_output): raise RuntimeError( "vllm_omni hidden-states patch: execute_model_state.hidden_states is not the expected " f"[num_scheduled_tokens, D] tensor (got {type(hidden_states).__name__} " f"of shape {tuple(hidden_states.shape)}). The NPU AR runner's state layout " "changed; update the patch for the new vllm-omni pin." ) output = original_sample(self, grammar_output) payloads = _collect_hidden_states_payloads(self, scheduler_output, hidden_states) if not payloads: return output # Sync path returns OmniModelRunnerOutput directly; async scheduling # wraps it in AsyncGPUModelRunnerOutput (private _model_runner_output). target = getattr(output, "_model_runner_output", None) or output _merge_into_multimodal_outputs(target, payloads) return output NPUARModelRunner.sample_tokens = _sample_with_hidden_states def _patch_runner(runner_cls: type) -> None: """Attach hidden-state payloads to OmniModelRunnerOutput.multimodal_outputs.""" original_build = runner_cls._build_omni_model_runner_output_from_snapshot def _build_with_hidden_states(self, **kwargs): output = original_build(self, **kwargs) if output is None: return output scheduler_output = kwargs["scheduler_output"] hidden_states = kwargs.get("hidden_states") if hidden_states is None: return output hidden_payloads = _collect_hidden_states_payloads(self, scheduler_output, hidden_states) if not hidden_payloads: return output _merge_into_multimodal_outputs(output, hidden_payloads) return output runner_cls._build_omni_model_runner_output_from_snapshot = _build_with_hidden_states def _merge_prefix_hidden_states( runner, scheduler_output, hidden_states, query_start_loc_cpu ) -> dict[str, torch.Tensor] | None: """Rebuild full-prompt hidden states from the omni prefix cache.""" cache = getattr(runner, "omni_prefix_cache", None) if cache is None or not getattr(cache, "has_prefix_cached_new_req_ids", lambda: False)(): return None if not getattr(runner, "_model_needs_full_prefix_hidden_states", lambda: True)(): return None return cache.get_merged_hidden_states( query_start_loc=query_start_loc_cpu, input_batch=runner.input_batch, hidden_states=hidden_states, num_scheduled_tokens=scheduler_output.num_scheduled_tokens, ) def _collect_hidden_states_payloads(runner, scheduler_output, hidden_states) -> dict[str, dict]: """Slice per-request hidden states [S, D] (bf16, CPU) from the step's hidden tensor.""" num_scheduled_tokens = scheduler_output.num_scheduled_tokens snapshot_qsl = getattr(runner, "_snapshot_query_start_loc_cpu", None) if callable(snapshot_qsl): query_start_loc_cpu = snapshot_qsl() else: # NPU AR runner keeps query_start_loc as a CpuGpuBuffer (vllm.v1.utils) qsl = runner.query_start_loc qsl_cpu = getattr(qsl, "cpu", None) if qsl_cpu is None: qsl_cpu = qsl query_start_loc_cpu = qsl_cpu() if callable(qsl_cpu) else qsl_cpu merged = _merge_prefix_hidden_states(runner, scheduler_output, hidden_states, query_start_loc_cpu) payloads: dict[str, dict] = {} for req_id, num_tokens in num_scheduled_tokens.items(): if not request_wants_hidden_states(runner, req_id): continue request = runner.requests.get(req_id) if request is None or request.prompt_token_ids is None: continue num_prompt_tokens = len(request.prompt_token_ids) start_idx = request.num_computed_tokens num_tokens = int(num_tokens) num_remaining = num_prompt_tokens - start_idx - num_tokens if num_remaining > 0: raise RuntimeError( "return_hidden_states does not support chunked prefill " f"(req={req_id}: {num_remaining} prompt tokens left unscheduled). " "Launch the teacher engine with enable_chunked_prefill=False." ) req_idx = runner.input_batch.req_id_to_index.get(req_id) if req_idx is None: continue if merged is not None: h = merged.get(req_id) if h is None or h.shape[0] != num_prompt_tokens: raise RuntimeError( "vllm_omni hidden-states patch: merged hidden states for req " f"{req_id} has shape {h.shape if h is not None else None}, " f"expected ({num_prompt_tokens}, D)." ) elif start_idx > 0: raise RuntimeError( f"return_hidden_states cannot reconstruct the full-prompt hidden states for req {req_id}: " f"{start_idx} tokens were already computed (prefix cache hit) but the merged hidden " "states are unavailable. The omni prefix cache must be enabled, and the model must " "not opt out via requires_full_prefix_cached_hidden_states=False." ) else: offset = int(query_start_loc_cpu[req_idx]) h = hidden_states[offset : offset + num_tokens] payloads[req_id] = {TEACHER_HIDDEN_STATES_KEY: h.detach().cpu()} return payloads def _merge_into_multimodal_outputs(output, hidden_payloads: dict[str, dict]) -> None: """Merge hidden payloads into the per-request multimodal_outputs list.""" req_id_to_index = output.req_id_to_index num_reqs = len(output.req_ids) mm: list = list(output.multimodal_outputs) if output.multimodal_outputs else [None] * num_reqs while len(mm) < num_reqs: mm.append(None) for req_id, payload in hidden_payloads.items(): idx = req_id_to_index.get(req_id) if idx is None or idx >= num_reqs: continue entry = mm[idx] if entry is None: mm[idx] = payload elif isinstance(entry, dict): entry = dict(entry) entry.update(payload) mm[idx] = entry else: raise RuntimeError( f"cannot attach teacher_hidden_states to req {req_id}: multimodal_outputs[{idx}] " f"already holds a {type(entry).__name__}, expected None or dict." ) output.multimodal_outputs = mm def extract_hidden_states(req_output) -> object: """Read the hidden states off a client-facing OmniRequestOutput.""" outputs = getattr(req_output, "outputs", None) if not outputs: return None mm = getattr(outputs[0], "multimodal_output", None) if mm is None: return None try: return mm.get(TEACHER_HIDDEN_STATES_KEY) except AttributeError: if isinstance(mm, dict): return mm.get(TEACHER_HIDDEN_STATES_KEY) return None