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 11, 00:00 UTC
Use helper function instead of looping through attribute names (#29788)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Harry Mellor committed
Dec 23, 2025 at 17:31 UTC
c016c95b45765fcd432c533b16f6d17d77cc5f6d
2 files changed
+15
-14
vllm/config/model.py
+8
-11
@@ -1094,11 +1094,10 @@ class ModelConfig:
1094
# The size of inputs_embeds is usually identical to the size
1095
# of the hidden states, however there are exceptions, such as
1096
# embedding models like CLIP and SigLIP
1097
- for target_attr in ("projection_dim", "projection_size"):
1098
- if hasattr(self.hf_text_config, target_attr):
1099
- return getattr(self.hf_text_config, target_attr)
1100
-
1101
- return self.get_hidden_size()
1097
+ names = ("projection_dim", "projection_size")
1098
+ return getattr_iter(
1099
+ self.hf_text_config, names, default_factory=self.get_hidden_size
1100
+ )
1101
1102
@property
1103
def is_deepseek_mla(self) -> bool:
@@ -1231,14 +1230,12 @@ class ModelConfig:
1230
# For ChatGLM:
1231
"multi_query_group_num",
1232
]
1234
- for attr in attributes:
1235
- num_kv_heads = getattr(self.hf_text_config, attr, None)
1236
- if num_kv_heads is not None:
1237
- return num_kv_heads
1238
-
1233
# For non-grouped-query attention models, the number of KV heads is
1234
# equal to the number of attention heads.
1241
- return self.hf_text_config.num_attention_heads
1235
+ default_factory = lambda: self.hf_text_config.num_attention_heads
1236
+ return getattr_iter(
1237
+ self.hf_text_config, attributes, default_factory=default_factory
1238
+ )
1239
1240
def get_num_kv_heads(self, parallel_config: ParallelConfig) -> int:
1241
"""Returns the number of KV heads per GPU."""
vllm/config/utils.py
+7
-3
@@ -9,7 +9,7 @@ import inspect
9
import json
10
import pathlib
11
import textwrap
12
-from collections.abc import Iterable, Mapping, Sequence, Set
12
+from collections.abc import Callable, Iterable, Mapping, Sequence, Set
13
from dataclasses import MISSING, Field, dataclass, field, fields, is_dataclass, replace
14
from itertools import pairwise
15
from typing import TYPE_CHECKING, Any, Protocol, TypeVar
@@ -74,7 +74,11 @@ def get_field(cls: ConfigType, name: str) -> Field:
74
75
76
def getattr_iter(
77
- object: object, names: Iterable[str], default: Any, warn: bool = False
77
+ object: object,
78
+ names: Iterable[str],
79
+ default: Any | None = None,
80
+ default_factory: Callable[[], Any] | None = None,
81
+ warn: bool = False,
82
) -> Any:
83
"""
84
A helper function that retrieves an attribute from an object which may
@@ -96,7 +100,7 @@ def getattr_iter(
100
names[0],
101
)
102
return getattr(object, name)
99
- return default
103
+ return default_factory() if default_factory is not None else default
104
105
106
def contains_object_print(text: str) -> bool: