@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 3b312fb792
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 10, 20:15 UTC

[Minor] Various small code cleanups/simplifications (#31508)

Signed-off-by: njhill <nickhill123@gmail.com>

Nick Hill committed Dec 29, 2025 at 22:42 UTC 3b312fb792d200ba4b1c3b4bf5ad06a46cd2fa1e
14 files changed +29 -55
vllm/config/model.py
+4 -4
@@ -1579,14 +1579,14 @@ class ModelConfig:
1579
1580 @property
1581 def is_hybrid(self) -> bool:
1582 + if not self._model_info.is_hybrid:
1583 + return False
1584 # Handle granite-4.0-micro case which uses hybrid config but does not
1585 # actually contain any non-attention layers.
1586 layer_types = getattr(self.hf_config, "layer_types", None)
1585 - if layer_types is not None and all(
1587 + return layer_types is None or not all(
1588 layer == "attention" for layer in layer_types
1587 - ):
1588 - return False
1589 - return self._model_info.is_hybrid
1589 + )
1590
1591 @property
1592 def has_noops(self) -> bool:
vllm/entrypoints/openai/serving_responses.py
-1
@@ -2005,7 +2005,6 @@ class OpenAIServingResponses(OpenAIServing):
2005 return event
2006
2007 async with AsyncExitStack() as exit_stack:
2008 - processer = None
2008 if self.use_harmony:
2009 # TODO: in streaming, we noticed this bug:
2010 # https://github.com/vllm-project/vllm/issues/25697
vllm/entrypoints/renderer.py
+2 -5
@@ -44,11 +44,8 @@ class RenderConfig:
44 def verify_truncate_prompt_tokens(self, model_config: ModelConfig) -> int | None:
45 """Validate and normalize `truncate_prompt_tokens` parameter."""
46 truncate_prompt_tokens = self.truncate_prompt_tokens
47 - if truncate_prompt_tokens is None:
48 - return None
49 -
50 - if truncate_prompt_tokens == 0:
51 - return 0
47 + if truncate_prompt_tokens is None or truncate_prompt_tokens == 0:
48 + return truncate_prompt_tokens
49
50 if truncate_prompt_tokens < 0:
51 truncate_prompt_tokens = model_config.max_model_len
vllm/inputs/preprocess.py
+1 -5
@@ -686,11 +686,7 @@ class InputPreprocessor:
686 mm_uuids: MultiModalUUIDDict | None = None,
687 ) -> ProcessorInputs:
688 """Preprocess the input prompt."""
689 - res = self._preprocess(
690 - prompt,
691 - tokenization_kwargs,
692 - mm_uuids=mm_uuids,
693 - )
689 + res = self._preprocess(prompt, tokenization_kwargs, mm_uuids=mm_uuids)
690
691 if self.mm_processor_cache and self.mm_cache_stats is not None:
692 delta = self.mm_processor_cache.make_stats(delta=True)
vllm/multimodal/inputs.py
+2 -11
@@ -171,10 +171,7 @@ class PlaceholderRange:
171
172 @cached_property
173 def embeds_cumsum(self) -> torch.Tensor | None:
174 - if self.is_embed is None:
175 - return None
176 -
177 - return self.is_embed.cumsum(dim=0)
174 + return None if self.is_embed is None else self.is_embed.cumsum(dim=0)
175
176 @cached_property
177 def get_num_embeds(self) -> int:
@@ -308,13 +305,7 @@ def batched_tensors_equal(a: BatchedTensorInputs, b: BatchedTensorInputs) -> boo
305 Equality check between
306 [`BatchedTensorInputs`][vllm.multimodal.inputs.BatchedTensorInputs] objects.
307 """
311 - for k in a:
312 - if k not in b:
313 - return False
314 - if not nested_tensors_equal(a[k], b[k]):
315 - return False
316 -
317 - return True
308 + return all(k in b and nested_tensors_equal(a[k], b[k]) for k in a)
309
310
311 @dataclass
vllm/v1/engine/core.py
+2 -4
@@ -487,10 +487,8 @@ class EngineCore:
487 request_ids = []
488 while not self.aborts_queue.empty():
489 ids = self.aborts_queue.get_nowait()
490 - if isinstance(ids, str):
491 - # Should be a list here, but also handle string just in case.
492 - ids = (ids,)
493 - request_ids.extend(ids)
490 + # Should be a list here, but also handle string just in case.
491 + request_ids.extend((ids,) if isinstance(ids, str) else ids)
492 # More efficient to abort all as a single batch.
493 self.abort_requests(request_ids)
494
vllm/v1/engine/input_processor.py
+1 -1
@@ -618,7 +618,7 @@ class InputProcessor:
618
619 tokenizer = self.tokenizer
620 if tokenizer is not None:
621 - max_input_id = max(prompt_ids or [], default=0)
621 + max_input_id = max(prompt_ids or (), default=0)
622
623 # NOTE: tokenizer.max_token_id is the tokenizer’s vocab size while
624 # self.model_config.get_vocab_size() is the model’s vocab size.
vllm/v1/engine/output_processor.py
+2 -7
@@ -339,10 +339,7 @@ class RequestState:
339 stop_reason=stop_reason if finished else None,
340 )
341
342 - def _new_pooling_output(
343 - self,
344 - pooling_output: torch.Tensor,
345 - ) -> PoolingOutput:
342 + def _new_pooling_output(self, pooling_output: torch.Tensor) -> PoolingOutput:
343 return PoolingOutput(data=pooling_output)
344
345
@@ -695,9 +692,7 @@ class OutputProcessor:
692 assert req_state.stats is not None
693 iteration_stats.update_from_finished_request(
694 finish_reason=finish_reason,
698 - num_prompt_tokens=length_from_prompt_token_ids_or_embeds(
699 - req_state.prompt_token_ids, req_state.prompt_embeds
700 - ),
695 + num_prompt_tokens=req_state.prompt_len,
696 max_tokens_param=req_state.max_tokens_param,
697 req_stats=req_state.stats,
698 num_cached_tokens=req_state.num_cached_tokens,
vllm/v1/executor/multiproc_executor.py
+1 -1
@@ -695,7 +695,7 @@ class WorkerProc:
695 worker = None
696 # tuple[Connection, Connection]
697 reader, ready_writer = kwargs.pop("ready_pipe")
698 - death_pipe = kwargs.pop("death_pipe", None)
698 + death_pipe: Connection | None = kwargs.pop("death_pipe", None)
699 shutdown_event = threading.Event()
700 # Start death monitoring thread if death_pipe is provided
701 if death_pipe is not None:
vllm/v1/request.py
+1 -2
@@ -211,8 +211,7 @@ class Request:
211
212 def get_num_encoder_embeds(self, input_id: int) -> int:
213 assert input_id < len(self.mm_features)
214 - num_embeds = self.mm_features[input_id].mm_position.get_num_embeds
215 - return num_embeds
214 + return self.mm_features[input_id].mm_position.get_num_embeds
215
216 def record_event(
217 self,
vllm/v1/structured_output/__init__.py
+8 -6
@@ -1,6 +1,8 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 +import itertools
4 import multiprocessing
5 +from collections.abc import Iterable
6 from concurrent.futures import Future, ThreadPoolExecutor
7 from typing import TYPE_CHECKING
8
@@ -172,7 +174,7 @@ class StructuredOutputManager:
174
175 def _fill_bitmasks(
176 self,
175 - batch: list[tuple[StructuredOutputGrammar, int, bool]],
177 + batch: Iterable[tuple[StructuredOutputGrammar, int, bool]],
178 ) -> None:
179 assert self._grammar_bitmask is not None
180 for grammar, index, apply_bitmask in batch:
@@ -265,16 +267,16 @@ class StructuredOutputManager:
267 apply_bitmask = self.should_fill_bitmask(request)
268
269 state_advancements = 0
268 - req_tokens = scheduled_spec_decode_tokens.get(req_id, [])
269 - for i, token in enumerate(req_tokens + [None]):
270 + req_tokens = scheduled_spec_decode_tokens.get(req_id, ())
271 + for token in itertools.chain(req_tokens, (None,)):
272 self._fill_bitmasks(
271 - [
273 + (
274 (
275 structured_output_request.grammar,
276 cumulative_index,
277 apply_bitmask,
276 - )
277 - ]
278 + ),
279 + )
280 )
281
282 if (
vllm/v1/structured_output/request.py
+3 -6
@@ -28,12 +28,9 @@ class StructuredOutputRequest:
28 if sampling_params is None:
29 return None
30 params = sampling_params.structured_outputs
31 - if params:
32 - if params.all_constraints_none():
33 - return None
34 - else:
35 - return StructuredOutputRequest(params=params)
36 - return None
31 + if not params or params.all_constraints_none():
32 + return None
33 + return StructuredOutputRequest(params=params)
34
35 def _check_grammar_completion(self) -> bool:
36 # NOTE: We have to lazy import to gate circular imports
vllm/v1/worker/gpu_input_batch.py
+1 -1
@@ -829,7 +829,7 @@ class InputBatch:
829 presence_penalties=self.presence_penalties[:num_reqs],
830 repetition_penalties=self.repetition_penalties[:num_reqs],
831 output_token_ids=output_token_ids,
832 - spec_token_ids=cast(list[list[int]], self.spec_token_ids),
832 + spec_token_ids=self.spec_token_ids,
833 no_penalties=self.no_penalties,
834 allowed_token_ids_mask=allowed_token_ids_mask,
835 bad_words_token_ids=self.bad_words_token_ids,
vllm/v1/worker/gpu_model_runner.py
+1 -1
@@ -1026,7 +1026,7 @@ class GPUModelRunner(
1026 each sequence, and a shifting is done during the next iteration
1027 based on the number of accepted tokens.
1028 """
1029 - if not self.model_config.is_hybrid or not self.speculative_config:
1029 + if not self.speculative_config or not self.model_config.is_hybrid:
1030 return
1031
1032 # Find the number of accepted tokens for each sequence.