@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / de533ab2a1
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 14, 05:30 UTC

[Models] Improve iteration over layers (#19497)

Signed-off-by: Lukas Geiger <lukas.geiger94@gmail.com>

Lukas Geiger committed Aug 29, 2025 at 02:26 UTC de533ab2a14192e461900a4950e2b426d99a6862
65 files changed +129 -83
vllm/model_executor/models/arcee.py
+2 -1
@@ -9,6 +9,7 @@
9 # activation.
10
11 from collections.abc import Iterable
12 +from itertools import islice
13 from typing import Any, Optional, Union
14
15 import torch
@@ -243,7 +244,7 @@ class ArceeModel(nn.Module):
244
245 aux_hidden_states: list[torch.Tensor] = []
246 for idx, layer in enumerate(
246 - self.layers[self.start_layer:self.end_layer]):
247 + islice(self.layers, self.start_layer, self.end_layer)):
248 if idx in self.aux_hidden_state_layers:
249 aux_hidden_states.append(
250 hidden_states +
vllm/model_executor/models/arctic.py
+2 -1
@@ -2,6 +2,7 @@
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Inference-only Snowflake Arctic model."""
4 from collections.abc import Iterable
5 +from itertools import islice
6 from typing import Optional, Union
7
8 import torch
@@ -403,7 +404,7 @@ class ArcticModel(nn.Module):
404 else:
405 assert intermediate_tensors is not None
406 hidden_states = intermediate_tensors["hidden_states"]
406 - for layer in self.layers[self.start_layer:self.end_layer]:
407 + for layer in islice(self.layers, self.start_layer, self.end_layer):
408 hidden_states = layer(positions, hidden_states)
409 if not get_pp_group().is_last_rank:
410 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/baichuan.py
+2 -1
@@ -22,6 +22,7 @@
22 """Inference-only BaiChuan model compatible with HuggingFace weights."""
23 import math
24 from collections.abc import Iterable
25 +from itertools import islice
26 from typing import Optional, Union
27
28 import torch
@@ -309,7 +310,7 @@ class BaiChuanModel(nn.Module):
310 assert intermediate_tensors is not None
311 hidden_states = intermediate_tensors["hidden_states"]
312 residual = intermediate_tensors["residual"]
312 - for layer in self.layers[self.start_layer:self.end_layer]:
313 + for layer in islice(self.layers, self.start_layer, self.end_layer):
314 hidden_states, residual = layer(
315 positions,
316 hidden_states,
vllm/model_executor/models/bailing_moe.py
+2 -2
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only BailingMoE model compatible with HuggingFace weights."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Optional, Union
29
30 import torch
@@ -359,8 +360,7 @@ class BailingMoeModel(nn.Module):
360 hidden_states = intermediate_tensors["hidden_states"]
361 residual = intermediate_tensors["residual"]
362
362 - for i in range(self.start_layer, self.end_layer):
363 - layer = self.layers[i]
363 + for layer in islice(self.layers, self.start_layer, self.end_layer):
364 hidden_states, residual = layer(
365 hidden_states,
366 position_ids,
vllm/model_executor/models/bamba.py
+1 -2
@@ -345,8 +345,7 @@ class BambaModel(nn.Module):
345
346 residual = None
347 num_attn = 0
348 - for i in range(len(self.layers)):
349 - layer = self.layers[i]
348 + for i, layer in enumerate(self.layers):
349 if isinstance(layer, BambaAttentionDecoderLayer):
350 num_attn += 1
351
vllm/model_executor/models/bloom.py
+2 -1
@@ -20,6 +20,7 @@
20 """Inference-only BLOOM model compatible with HuggingFace weights."""
21 import math
22 from collections.abc import Iterable
23 +from itertools import islice
24 from typing import Optional, Union
25
26 import torch
@@ -273,7 +274,7 @@ class BloomModel(nn.Module):
274 else:
275 assert intermediate_tensors is not None
276 hidden_states = intermediate_tensors["hidden_states"]
276 - for layer in self.h[self.start_layer:self.end_layer]:
277 + for layer in islice(self.h, self.start_layer, self.end_layer):
278 hidden_states = layer(position_ids, hidden_states)
279 if not get_pp_group().is_last_rank:
280 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/chameleon.py
+2 -1
@@ -3,6 +3,7 @@
3
4 from collections.abc import Iterable, Mapping, Sequence
5 from functools import cached_property
6 +from itertools import islice
7 from typing import Annotated, Any, Literal, Optional, Union
8
9 import torch
@@ -914,7 +915,7 @@ class ChameleonModel(nn.Module):
915 assert intermediate_tensors is not None
916 hidden_states = intermediate_tensors["hidden_states"]
917 residual = intermediate_tensors["residual"]
917 - for layer in self.layers[self.start_layer:self.end_layer]:
918 + for layer in islice(self.layers, self.start_layer, self.end_layer):
919 hidden_states, residual = layer(
920 positions,
921 hidden_states,
vllm/model_executor/models/chatglm.py
+2 -1
@@ -5,6 +5,7 @@
5 """Inference-only ChatGLM model compatible with THUDM weights."""
6 import json
7 from collections.abc import Iterable
8 +from itertools import islice
9 from typing import Optional, Union
10
11 import torch
@@ -281,7 +282,7 @@ class GLMTransformer(nn.Module):
282 hidden_states: torch.Tensor,
283 position_ids: torch.Tensor,
284 ) -> Union[torch.Tensor, IntermediateTensors]:
284 - for layer in self.layers[self.start_layer:self.end_layer]:
285 + for layer in islice(self.layers, self.start_layer, self.end_layer):
286 hidden_states = layer(hidden_states=hidden_states,
287 position_ids=position_ids)
288
vllm/model_executor/models/commandr.py
+2 -1
@@ -23,6 +23,7 @@
23 # This file is based on the LLama model definition file in transformers
24 """PyTorch Cohere model."""
25 from collections.abc import Iterable
26 +from itertools import islice
27 from typing import Optional, Union
28
29 import torch
@@ -322,7 +323,7 @@ class CohereModel(nn.Module):
323 assert intermediate_tensors is not None
324 hidden_states = intermediate_tensors["hidden_states"]
325 residual = intermediate_tensors["residual"]
325 - for layer in self.layers[self.start_layer:self.end_layer]:
326 + for layer in islice(self.layers, self.start_layer, self.end_layer):
327 hidden_states, residual = layer(
328 positions,
329 hidden_states,
vllm/model_executor/models/dbrx.py
+2 -1
@@ -2,6 +2,7 @@
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from collections.abc import Iterable
5 +from itertools import islice
6 from typing import Optional, Union
7
8 import torch
@@ -359,7 +360,7 @@ class DbrxModel(nn.Module):
360 else:
361 assert intermediate_tensors
362 hidden_states = intermediate_tensors["hidden_states"]
362 - for block in self.blocks[self.start_layer:self.end_layer]:
363 + for block in islice(self.blocks, self.start_layer, self.end_layer):
364 hidden_states = block(position_ids, hidden_states)
365 if not get_pp_group().is_last_rank:
366 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/deepseek.py
+3 -2
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only Deepseek model."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Any, Optional, Union
29
30 import torch
@@ -377,7 +378,7 @@ class DeepseekModel(nn.Module):
378 else:
379 hidden_states = intermediate_tensors["hidden_states"]
380 residual = intermediate_tensors["residual"]
380 - for layer in self.layers[self.start_layer:self.end_layer]:
381 + for layer in islice(self.layers, self.start_layer, self.end_layer):
382 hidden_states, residual = layer(positions, hidden_states, residual)
383 if not get_pp_group().is_last_rank:
384 return IntermediateTensors({
@@ -483,4 +484,4 @@ class DeepseekForCausalLM(nn.Module, SupportsPP):
484 def load_weights(self, weights: Iterable[tuple[str,
485 torch.Tensor]]) -> set[str]:
486 loader = AutoWeightsLoader(self)
486 - return loader.load_weights(weights)
\ No newline at end of file
487 + return loader.load_weights(weights)
vllm/model_executor/models/deepseek_v2.py
+2 -1
@@ -25,6 +25,7 @@
25 """Inference-only DeepseekV2/DeepseekV3 model."""
26 import typing
27 from collections.abc import Callable, Iterable
28 +from itertools import islice
29 from typing import Any, Optional, Union
30
31 import torch
@@ -712,7 +713,7 @@ class DeepseekV2Model(nn.Module):
713 hidden_states = intermediate_tensors["hidden_states"]
714 residual = intermediate_tensors["residual"]
715
715 - for layer in self.layers[self.start_layer:self.end_layer]:
716 + for layer in islice(self.layers, self.start_layer, self.end_layer):
717 hidden_states, residual = layer(positions, hidden_states, residual)
718
719 if not get_pp_group().is_last_rank:
vllm/model_executor/models/dots1.py
+2 -1
@@ -25,6 +25,7 @@
25 # limitations under the License.
26 """Inference-only dots1 model."""
27 from collections.abc import Iterable
28 +from itertools import islice
29 from typing import Any, Optional, Union
30
31 import torch
@@ -391,7 +392,7 @@ class Dots1Model(nn.Module):
392 assert intermediate_tensors is not None
393 hidden_states = intermediate_tensors["hidden_states"]
394 residual = intermediate_tensors["residual"]
394 - for layer in self.layers[self.start_layer:self.end_layer]:
395 + for layer in islice(self.layers, self.start_layer, self.end_layer):
396 hidden_states, residual = layer(
397 positions,
398 hidden_states,
vllm/model_executor/models/ernie45_moe.py
+2 -2
@@ -23,6 +23,7 @@
23 # limitations under the License.
24 """Inference-only ErineMoE model compatible with HuggingFace weights."""
25 from collections.abc import Iterable
26 +from itertools import islice
27 from typing import Any, Optional, Union
28
29 import torch
@@ -419,8 +420,7 @@ class Ernie4_5_MoeModel(nn.Module):
420 hidden_states = intermediate_tensors["hidden_states"]
421 residual = intermediate_tensors["residual"]
422
422 - for i in range(self.start_layer, self.end_layer):
423 - layer = self.layers[i]
423 + for layer in islice(self.layers, self.start_layer, self.end_layer):
424 hidden_states, residual = layer(positions, hidden_states, residual)
425
426 if not get_pp_group().is_last_rank:
vllm/model_executor/models/ernie45_vl_moe.py
+2 -2
@@ -23,6 +23,7 @@
23 # limitations under the License.
24 """Inference-only Erine VL model compatible with HuggingFace weights."""
25 from collections.abc import Iterable
26 +from itertools import islice
27 from typing import Any, Optional, Union
28
29 import torch
@@ -508,8 +509,7 @@ class Ernie4_5_VLMoeModel(nn.Module):
509 hidden_states = intermediate_tensors["hidden_states"]
510 residual = intermediate_tensors["residual"]
511
511 - for i in range(self.start_layer, self.end_layer):
512 - layer = self.layers[i]
512 + for layer in islice(self.layers, self.start_layer, self.end_layer):
513 hidden_states, residual = layer(positions, hidden_states, residual,
514 visual_token_mask, **kwargs)
515
vllm/model_executor/models/exaone.py
+2 -1
@@ -26,6 +26,7 @@
26 """Inference-only Exaone model compatible with HuggingFace weights."""
27
28 from collections.abc import Iterable
29 +from itertools import islice
30 from typing import Any, Optional, Union
31
32 import torch
@@ -371,7 +372,7 @@ class ExaoneModel(nn.Module):
372 hidden_states = intermediate_tensors["hidden_states"]
373 residual = intermediate_tensors["residual"]
374
374 - for layer in self.h[self.start_layer:self.end_layer]:
375 + for layer in islice(self.h, self.start_layer, self.end_layer):
376 hidden_states, residual = layer(
377 positions,
378 hidden_states,
vllm/model_executor/models/exaone4.py
+2 -1
@@ -22,6 +22,7 @@
22 """Inference-only Exaone model compatible with HuggingFace weights."""
23
24 from collections.abc import Iterable
25 +from itertools import islice
26 from typing import Any, Optional, Union
27
28 import torch
@@ -354,7 +355,7 @@ class Exaone4Model(nn.Module):
355 hidden_states = intermediate_tensors["hidden_states"]
356 residual = intermediate_tensors["residual"]
357
357 - for layer in self.layers[self.start_layer:self.end_layer]:
358 + for layer in islice(self.layers, self.start_layer, self.end_layer):
359 hidden_states, residual = layer(
360 positions,
361 hidden_states,
vllm/model_executor/models/falcon.py
+2 -1
@@ -22,6 +22,7 @@
22
23 import math
24 from collections.abc import Iterable
25 +from itertools import islice
26 from typing import Optional, Union
27
28 import torch
@@ -389,7 +390,7 @@ class FalconModel(nn.Module):
390 hidden_states = self.get_input_embeddings(input_ids)
391 else:
392 hidden_states = intermediate_tensors["hidden_states"]
392 - for layer in self.h[self.start_layer:self.end_layer]:
393 + for layer in islice(self.h, self.start_layer, self.end_layer):
394 hidden_states = layer(positions, hidden_states)
395 if not get_pp_group().is_last_rank:
396 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/gemma.py
+2 -1
@@ -18,6 +18,7 @@
18 """Inference-only Gemma model compatible with HuggingFace weights."""
19 from collections.abc import Iterable
20 from functools import cache
21 +from itertools import islice
22 from typing import Optional, Union
23
24 import torch
@@ -308,7 +309,7 @@ class GemmaModel(nn.Module):
309 else:
310 hidden_states = intermediate_tensors["hidden_states"]
311 residual = intermediate_tensors["residual"]
311 - for layer in self.layers[self.start_layer:self.end_layer]:
312 + for layer in islice(self.layers, self.start_layer, self.end_layer):
313 hidden_states, residual = layer(
314 positions,
315 hidden_states,
vllm/model_executor/models/gemma2.py
+2 -1
@@ -17,6 +17,7 @@
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 from collections.abc import Iterable
20 +from itertools import islice
21 from typing import Optional, Union
22
23 import torch
@@ -292,7 +293,7 @@ class Gemma2Model(nn.Module):
293 assert intermediate_tensors is not None
294 hidden_states = intermediate_tensors["hidden_states"]
295 residual = intermediate_tensors["residual"]
295 - for layer in self.layers[self.start_layer:self.end_layer]:
296 + for layer in islice(self.layers, self.start_layer, self.end_layer):
297 hidden_states, residual = layer(
298 positions,
299 hidden_states,
vllm/model_executor/models/gemma3.py
+2 -1
@@ -16,6 +16,7 @@
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 from collections.abc import Iterable
19 +from itertools import islice
20 from typing import Optional, Union
21
22 import torch
@@ -398,7 +399,7 @@ class Gemma3Model(nn.Module):
399 assert intermediate_tensors is not None
400 hidden_states = intermediate_tensors["hidden_states"]
401 residual = intermediate_tensors["residual"]
401 - for layer in self.layers[self.start_layer:self.end_layer]:
402 + for layer in islice(self.layers, self.start_layer, self.end_layer):
403 hidden_states, residual = layer(
404 positions,
405 hidden_states,
vllm/model_executor/models/glm4_moe.py
+2 -2
@@ -24,6 +24,7 @@
24 """Inference-only GLM-4.5 model compatible with HuggingFace weights."""
25 import typing
26 from collections.abc import Callable, Iterable
27 +from itertools import islice
28 from typing import Any, Optional, Union
29
30 import torch
@@ -440,8 +441,7 @@ class Glm4MoeModel(nn.Module):
441 hidden_states = intermediate_tensors["hidden_states"]
442 residual = intermediate_tensors["residual"]
443
443 - for i in range(self.start_layer, self.end_layer):
444 - layer = self.layers[i]
444 + for layer in islice(self.layers, self.start_layer, self.end_layer):
445 hidden_states, residual = layer(positions, hidden_states, residual)
446
447 if not get_pp_group().is_last_rank:
vllm/model_executor/models/gpt2.py
+2 -1
@@ -20,6 +20,7 @@
20 # limitations under the License.
21 """Inference-only GPT-2 model compatible with HuggingFace weights."""
22 from collections.abc import Iterable
23 +from itertools import islice
24 from typing import Optional, Union
25
26 import torch
@@ -228,7 +229,7 @@ class GPT2Model(nn.Module):
229 assert intermediate_tensors is not None
230 hidden_states = intermediate_tensors["hidden_states"]
231
231 - for layer in self.h[self.start_layer:self.end_layer]:
232 + for layer in islice(self.h, self.start_layer, self.end_layer):
233 hidden_states = layer(hidden_states)
234
235 if not get_pp_group().is_last_rank:
vllm/model_executor/models/gpt_bigcode.py
+2 -1
@@ -21,6 +21,7 @@
21 # limitations under the License.
22 """Inference-only GPTBigCode model compatible with HuggingFace weights."""
23 from collections.abc import Iterable
24 +from itertools import islice
25 from typing import Optional, Union
26
27 import torch
@@ -246,7 +247,7 @@ class GPTBigCodeModel(nn.Module):
247 else:
248 hidden_states = intermediate_tensors["hidden_states"]
249
249 - for layer in self.h[self.start_layer:self.end_layer]:
250 + for layer in islice(self.h, self.start_layer, self.end_layer):
251 hidden_states = layer(hidden_states)
252
253 if not get_pp_group().is_last_rank:
vllm/model_executor/models/gpt_j.py
+3 -2
@@ -19,6 +19,7 @@
19 # limitations under the License.
20 """Inference-only GPT-J model compatible with HuggingFace weights."""
21 from collections.abc import Iterable
22 +from itertools import islice
23 from typing import Optional, Union
24
25 import torch
@@ -223,7 +224,7 @@ class GPTJModel(nn.Module):
224 hidden_states = self.get_input_embeddings(input_ids)
225 else:
226 hidden_states = intermediate_tensors["hidden_states"]
226 - for layer in self.h[self.start_layer:self.end_layer]:
227 + for layer in islice(self.h, self.start_layer, self.end_layer):
228 hidden_states = layer(position_ids, hidden_states)
229 if not get_pp_group().is_last_rank:
230 return IntermediateTensors({"hidden_states": hidden_states})
@@ -336,4 +337,4 @@ class GPTJForCausalLM(nn.Module, SupportsPP):
337 def load_weights(self, weights: Iterable[tuple[str,
338 torch.Tensor]]) -> set[str]:
339 loader = AutoWeightsLoader(self)
339 - return loader.load_weights(weights)
\ No newline at end of file
340 + return loader.load_weights(weights)
vllm/model_executor/models/gpt_neox.py
+2 -1
@@ -19,6 +19,7 @@
19 # limitations under the License.
20 """Inference-only GPT-NeoX model compatible with HuggingFace weights."""
21 from collections.abc import Iterable
22 +from itertools import islice
23 from typing import Optional, Union
24
25 import torch
@@ -235,7 +236,7 @@ class GPTNeoXModel(nn.Module):
236 hidden_states = self.get_input_embeddings(input_ids)
237 else:
238 hidden_states = intermediate_tensors["hidden_states"]
238 - for layer in self.layers[self.start_layer:self.end_layer]:
239 + for layer in islice(self.layers, self.start_layer, self.end_layer):
240 hidden_states = layer(position_ids, hidden_states)
241 if not get_pp_group().is_last_rank:
242 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/granite.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only IBM Granite model compatible with HuggingFace weights."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Any, Optional, Union
29
30 import torch
@@ -316,7 +317,7 @@ class GraniteModel(nn.Module):
317 hidden_states = intermediate_tensors["hidden_states"]
318 residual = intermediate_tensors["residual"]
319
319 - for layer in self.layers[self.start_layer:self.end_layer]:
320 + for layer in islice(self.layers, self.start_layer, self.end_layer):
321 hidden_states = layer(positions, hidden_states)
322
323 if not get_pp_group().is_last_rank:
vllm/model_executor/models/granitemoe.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only GraniteMoe model."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Any, Optional
29
30 import torch
@@ -303,7 +304,7 @@ class GraniteMoeModel(nn.Module):
304 assert intermediate_tensors is not None
305 hidden_states = intermediate_tensors["hidden_states"]
306 residual = intermediate_tensors["residual"]
306 - for layer in self.layers[self.start_layer:self.end_layer]:
307 + for layer in islice(self.layers, self.start_layer, self.end_layer):
308 hidden_states = layer(positions, hidden_states)
309 if not get_pp_group().is_last_rank:
310 return IntermediateTensors({
vllm/model_executor/models/granitemoehybrid.py
+1 -2
@@ -397,8 +397,7 @@ class GraniteMoeHybridModel(nn.Module):
397 residual = intermediate_tensors["residual"]
398
399 num_attn = 0
400 - for i in range(len(self.layers)):
401 - layer = self.layers[i]
400 + for i, layer in enumerate(self.layers):
401 if isinstance(layer, GraniteMoeHybridAttentionDecoderLayer):
402 num_attn += 1
403
vllm/model_executor/models/granitemoeshared.py
+2 -2
@@ -6,6 +6,7 @@ The architecture is the same as granitemoe but with the addition of shared
6 experts.
7 """
8 from collections.abc import Iterable
9 +from itertools import islice
10 from typing import Optional
11
12 import torch
@@ -200,8 +201,7 @@ class GraniteMoeSharedModel(nn.Module):
201 assert intermediate_tensors is not None
202 hidden_states = intermediate_tensors["hidden_states"]
203 residual = intermediate_tensors["residual"]
203 - for i in range(self.start_layer, self.end_layer):
204 - layer = self.layers[i]
204 + for layer in islice(self.layers, self.start_layer, self.end_layer):
205 hidden_states = layer(positions, hidden_states)
206 if not get_pp_group().is_last_rank:
207 return IntermediateTensors({
vllm/model_executor/models/grok1.py
+2 -2
@@ -23,6 +23,7 @@
23 # limitations under the License.
24 """Inference-only Grok1 model."""
25 from collections.abc import Iterable
26 +from itertools import islice
27 from typing import Optional, Union
28
29 import torch
@@ -347,8 +348,7 @@ class Grok1Model(nn.Module):
348 hidden_states = intermediate_tensors["hidden_states"]
349 residual = intermediate_tensors["residual"]
350
350 - for i in range(self.start_layer, self.end_layer):
351 - layer = self.layers[i]
351 + for layer in islice(self.layers, self.start_layer, self.end_layer):
352 hidden_states, residual = layer(positions, hidden_states, residual)
353
354 if not get_pp_group().is_last_rank:
vllm/model_executor/models/internlm2.py
+2 -1
@@ -3,6 +3,7 @@
3
4 from collections.abc import Iterable
5 from functools import partial
6 +from itertools import islice
7 from typing import Any, Optional, Union
8
9 import torch
@@ -297,7 +298,7 @@ class InternLM2Model(nn.Module):
298 assert intermediate_tensors is not None
299 hidden_states = intermediate_tensors["hidden_states"]
300 residual = intermediate_tensors["residual"]
300 - for layer in self.layers[self.start_layer:self.end_layer]:
301 + for layer in islice(self.layers, self.start_layer, self.end_layer):
302 hidden_states, residual = layer(positions, hidden_states, residual)
303 if not get_pp_group().is_last_rank:
304 return IntermediateTensors({
vllm/model_executor/models/internlm2_ve.py
+2 -1
@@ -1,6 +1,7 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 +from itertools import islice
5 from typing import Optional, Union
6
7 import torch
@@ -123,7 +124,7 @@ class InternLM2VEModel(InternLM2Model):
124 assert intermediate_tensors is not None
125 hidden_states = intermediate_tensors["hidden_states"]
126 residual = intermediate_tensors["residual"]
126 - for layer in self.layers[self.start_layer:self.end_layer]:
127 + for layer in islice(self.layers, self.start_layer, self.end_layer):
128 hidden_states, residual = layer(
129 positions,
130 hidden_states,
vllm/model_executor/models/jais.py
+2 -1
@@ -23,6 +23,7 @@
23
24 import math
25 from collections.abc import Iterable
26 +from itertools import islice
27 from typing import Optional, Union
28
29 import torch
@@ -276,7 +277,7 @@ class JAISModel(nn.Module):
277 assert intermediate_tensors is not None
278 hidden_states = intermediate_tensors["hidden_states"]
279
279 - for layer in self.h[self.start_layer:self.end_layer]:
280 + for layer in islice(self.h, self.start_layer, self.end_layer):
281 hidden_states = layer(hidden_states)
282
283 if not get_pp_group().is_last_rank:
vllm/model_executor/models/jamba.py
+2 -1
@@ -2,6 +2,7 @@
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Inference-only Jamba model."""
4 from collections.abc import Iterable
5 +from itertools import islice
6 from typing import Optional
7
8 import torch
@@ -350,7 +351,7 @@ class JambaModel(nn.Module):
351
352 kv_cache_index = 0
353 mamba_cache_index = 0
353 - for layer in self.layers[self.start_layer:self.end_layer]:
354 + for layer in islice(self.layers, self.start_layer, self.end_layer):
355 layer_mamba_cache_params = None
356 if isinstance(layer, JambaAttentionDecoderLayer):
357 kv_cache_index += 1
vllm/model_executor/models/lfm2.py
+3 -2
@@ -1,6 +1,7 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 from collections.abc import Iterable
4 +from itertools import islice
5 from typing import Any, Optional
6
7 import torch
@@ -374,7 +375,7 @@ class Lfm2Model(nn.Module):
375 hidden_states = intermediate_tensors["hidden_states"]
376 residual = intermediate_tensors["residual"]
377
377 - for layer in self.layers[self.start_layer:self.end_layer]:
378 + for layer in islice(self.layers, self.start_layer, self.end_layer):
379 hidden_states, residual = layer(
380 positions=positions,
381 hidden_states=hidden_states,
@@ -554,4 +555,4 @@ class Lfm2ForCausalLM(nn.Module, HasInnerState, SupportsLoRA, SupportsPP,
555 skip_prefixes=(["lm_head."]
556 if self.config.tie_word_embeddings else None),
557 )
557 - return loader.load_weights(weights)
\ No newline at end of file
558 + return loader.load_weights(weights)
vllm/model_executor/models/llama.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only LLaMA model compatible with HuggingFace weights."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Any, Optional, Union
29
30 import torch
@@ -383,7 +384,7 @@ class LlamaModel(nn.Module):
384
385 aux_hidden_states = []
386 for idx, layer in enumerate(
386 - self.layers[self.start_layer:self.end_layer]):
387 + islice(self.layers, self.start_layer, self.end_layer)):
388 if idx in self.aux_hidden_state_layers:
389 aux_hidden_states.append(hidden_states + residual)
390 hidden_states, residual = layer(positions, hidden_states, residual)
vllm/model_executor/models/mamba2.py
+1 -3
@@ -164,9 +164,7 @@ class Mamba2Model(nn.Module):
164 # v1 get mamba2_metadata from forward_context
165 mamba2_metadata = None
166
167 - for i in range(len(self.layers)):
168 - layer = self.layers[i]
169 -
167 + for i, layer in enumerate(self.layers):
168 hidden_states, residual = layer(
169 positions=positions,
170 hidden_states=hidden_states,
vllm/model_executor/models/mimo.py
+2 -1
@@ -26,6 +26,7 @@
26 # limitations under the License.
27 """Inference-only MiMo model compatible with HuggingFace weights."""
28 from collections.abc import Iterable
29 +from itertools import islice
30 from typing import Optional, Union
31
32 import torch
@@ -74,7 +75,7 @@ class MiMoModel(Qwen2Model):
75 assert intermediate_tensors is not None
76 hidden_states = intermediate_tensors["hidden_states"]
77 residual = intermediate_tensors["residual"]
77 - for layer in self.layers[self.start_layer:self.end_layer]:
78 + for layer in islice(self.layers, self.start_layer, self.end_layer):
79 hidden_states, residual = layer(
80 positions,
81 hidden_states,
vllm/model_executor/models/minicpm.py
+2 -1
@@ -25,6 +25,7 @@
25 """Inference-only MiniCPM model compatible with HuggingFace weights."""
26 import math
27 from collections.abc import Iterable
28 +from itertools import islice
29 from typing import Any, Optional, Union
30
31 import torch
@@ -414,7 +415,7 @@ class MiniCPMModel(nn.Module):
415 hidden_states = intermediate_tensors["hidden_states"]
416 residual = intermediate_tensors["residual"]
417
417 - for layer in self.layers[self.start_layer:self.end_layer]:
418 + for layer in islice(self.layers, self.start_layer, self.end_layer):
419 hidden_states, residual = layer(
420 positions,
421 hidden_states,
vllm/model_executor/models/minimax_text_01.py
+2 -2
@@ -3,6 +3,7 @@
3 """Inference-only MiniMaxText01 model."""
4 import math
5 from collections.abc import Iterable
6 +from itertools import islice
7 from typing import TYPE_CHECKING, Optional, Union
8
9 if TYPE_CHECKING:
@@ -1019,8 +1020,7 @@ class MiniMaxText01Model(nn.Module):
1020
1021 minimax_cache_index = 0
1022
1022 - for i in range(self.start_layer, self.end_layer):
1023 - layer = self.layers[i]
1023 + for layer in islice(self.layers, self.start_layer, self.end_layer):
1024 _caches = None
1025 if not envs.VLLM_USE_V1 and isinstance(
1026 layer.self_attn, MiniMaxText01LinearAttention):
vllm/model_executor/models/mixtral.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only Mixtral model."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Optional, Union
29
30 import torch
@@ -307,7 +308,7 @@ class MixtralModel(nn.Module):
308 assert intermediate_tensors is not None
309 hidden_states = intermediate_tensors["hidden_states"]
310 residual = intermediate_tensors["residual"]
310 - for layer in self.layers[self.start_layer:self.end_layer]:
311 + for layer in islice(self.layers, self.start_layer, self.end_layer):
312 hidden_states, residual = layer(positions, hidden_states, residual)
313 if not get_pp_group().is_last_rank:
314 return IntermediateTensors({
vllm/model_executor/models/mixtral_quant.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only Mixtral model."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Optional, Union
29
30 import numpy as np
@@ -346,7 +347,7 @@ class MixtralModel(nn.Module):
347 assert intermediate_tensors is not None
348 hidden_states = intermediate_tensors["hidden_states"]
349 residual = intermediate_tensors["residual"]
349 - for layer in self.layers[self.start_layer:self.end_layer]:
350 + for layer in islice(self.layers, self.start_layer, self.end_layer):
351 hidden_states, residual = layer(positions, hidden_states, residual)
352 if not get_pp_group().is_last_rank:
353 return IntermediateTensors({
vllm/model_executor/models/molmo.py
+2 -1
@@ -5,6 +5,7 @@ import math
5 from collections.abc import Iterable, Mapping, Sequence
6 from dataclasses import dataclass
7 from functools import cached_property, partial
8 +from itertools import islice
9 from typing import Annotated, Optional, Union
10
11 import numpy as np
@@ -842,7 +843,7 @@ class MolmoModel(nn.Module, SupportsQuant):
843 residual = intermediate_tensors["residual"]
844
845 # Apply blocks one-by-one.
845 - for layer in self.layers[self.start_layer:self.end_layer]:
846 + for layer in islice(self.layers, self.start_layer, self.end_layer):
847 hidden_states, residual = layer(
848 positions,
849 hidden_states,
vllm/model_executor/models/mpt.py
+2 -1
@@ -4,6 +4,7 @@
4 # Adapted from https://huggingface.co/mosaicml/mpt-7b/tree/main
5 import math
6 from collections.abc import Iterable
7 +from itertools import islice
8 from typing import Optional, Union
9
10 import torch
@@ -260,7 +261,7 @@ class MPTModel(nn.Module):
261 assert intermediate_tensors is not None
262 hidden_states = intermediate_tensors["hidden_states"]
263
263 - for block in self.blocks[self.start_layer:self.end_layer]:
264 + for block in islice(self.blocks, self.start_layer, self.end_layer):
265 hidden_states = block(position_ids, hidden_states)
266 if not get_pp_group().is_last_rank:
267 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/nemotron.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only Nemotron model compatible with HuggingFace weights."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Any, Optional, Union
29
30 import torch
@@ -353,7 +354,7 @@ class NemotronModel(nn.Module):
354 hidden_states = intermediate_tensors["hidden_states"]
355 residual = intermediate_tensors["residual"]
356
356 - for layer in self.layers[self.start_layer:self.end_layer]:
357 + for layer in islice(self.layers, self.start_layer, self.end_layer):
358 hidden_states, residual = layer(positions, hidden_states, residual)
359
360 if not get_pp_group().is_last_rank:
vllm/model_executor/models/nemotron_h.py
+1 -2
@@ -399,8 +399,7 @@ class NemotronHModel(nn.Module):
399
400 residual = None
401 num_non_mamba_layers = 0
402 - for i in range(len(self.layers)):
403 - layer = self.layers[i]
402 + for i, layer in enumerate(self.layers):
403 layer_mamba_cache_params = None
404 if isinstance(layer,
405 NemotronHMambaDecoderLayer) and mamba_cache_params:
vllm/model_executor/models/nemotron_nas.py
+2 -2
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only deci model compatible with HuggingFace weights."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Any, Optional, Union
29
30 import torch
@@ -287,8 +288,7 @@ class DeciModel(nn.Module):
288 residual = intermediate_tensors["residual"]
289
290 kv_cache_index = 0
290 - for i in range(self.start_layer, self.end_layer):
291 - layer = self.layers[i]
291 + for layer in islice(self.layers, self.start_layer, self.end_layer):
292 if not layer._is_no_op_attention:
293 hidden_states, residual = layer(positions, hidden_states,
294 residual)
vllm/model_executor/models/olmo.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only OLMo model compatible with HuggingFace weights."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Optional, Union
29
30 import torch
@@ -280,7 +281,7 @@ class OlmoModel(nn.Module):
281 hidden_states = intermediate_tensors["hidden_states"]
282
283 # Apply blocks one-by-one.
283 - for layer in self.layers[self.start_layer:self.end_layer]:
284 + for layer in islice(self.layers, self.start_layer, self.end_layer):
285 # shape: (batch_size, seq_len, d_model)
286 hidden_states = layer(positions, hidden_states)
287
vllm/model_executor/models/olmo2.py
+2 -1
@@ -26,6 +26,7 @@
26
27 from collections.abc import Iterable
28 from functools import partial
29 +from itertools import islice
30 from typing import Optional, Union
31
32 import torch
@@ -305,7 +306,7 @@ class Olmo2Model(nn.Module):
306 assert isinstance(hidden_states, torch.Tensor)
307
308 # Apply blocks one-by-one.
308 - for layer in self.layers[self.start_layer:self.end_layer]:
309 + for layer in islice(self.layers, self.start_layer, self.end_layer):
310 # shape: (batch_size, seq_len, d_model)
311 hidden_states = layer(positions, hidden_states)
312
vllm/model_executor/models/olmoe.py
+2 -1
@@ -15,6 +15,7 @@
15 """Inference-only OLMoE model compatible with HuggingFace weights."""
16 from collections.abc import Iterable
17 from functools import partial
18 +from itertools import islice
19 from typing import Any, Optional, Union
20
21 import torch
@@ -314,7 +315,7 @@ class OlmoeModel(nn.Module):
315 hidden_states = intermediate_tensors["hidden_states"]
316 residual = intermediate_tensors["residual"]
317
317 - for layer in self.layers[self.start_layer:self.end_layer]:
318 + for layer in islice(self.layers, self.start_layer, self.end_layer):
319 hidden_states, residual = layer(
320 positions,
321 hidden_states,
vllm/model_executor/models/opt.py
+2 -1
@@ -20,6 +20,7 @@
20 # limitations under the License.
21 """Inference-only OPT model compatible with HuggingFace weights."""
22 from collections.abc import Iterable
23 +from itertools import islice
24 from typing import Optional, Union
25
26 import torch
@@ -269,7 +270,7 @@ class OPTDecoder(nn.Module):
270 assert intermediate_tensors is not None
271 hidden_states = intermediate_tensors["hidden_states"]
272
272 - for layer in self.layers[self.start_layer:self.end_layer]:
273 + for layer in islice(self.layers, self.start_layer, self.end_layer):
274 hidden_states = layer(hidden_states)
275
276 if not get_pp_group().is_last_rank:
vllm/model_executor/models/orion.py
+2 -1
@@ -7,6 +7,7 @@
7 # LICENSE: https://huggingface.co/OrionStarAI/Orion-14B-Base/blob/main/LICENSE
8 """Inference-only Orion-14B model compatible with HuggingFace weights."""
9 from collections.abc import Iterable
10 +from itertools import islice
11 from typing import Any, Optional, Union
12
13 import torch
@@ -252,7 +253,7 @@ class OrionModel(nn.Module):
253 else:
254 assert intermediate_tensors is not None
255 hidden_states = intermediate_tensors["hidden_states"]
255 - for layer in self.layers[self.start_layer:self.end_layer]:
256 + for layer in islice(self.layers, self.start_layer, self.end_layer):
257 hidden_states = layer(positions, hidden_states)
258 if not get_pp_group().is_last_rank:
259 return IntermediateTensors({
vllm/model_executor/models/persimmon.py
+2 -1
@@ -23,6 +23,7 @@
23 # limitations under the License.
24 """Inference-only persimmon model compatible with HuggingFace weights."""
25 from collections.abc import Iterable
26 +from itertools import islice
27 from typing import Optional, Union
28
29 import torch
@@ -255,7 +256,7 @@ class PersimmonModel(nn.Module):
256 else:
257 assert intermediate_tensors is not None
258 hidden_states = intermediate_tensors["hidden_states"]
258 - for layer in self.layers[self.start_layer:self.end_layer]:
259 + for layer in islice(self.layers, self.start_layer, self.end_layer):
260 hidden_states = layer(positions, hidden_states)
261 if not get_pp_group().is_last_rank:
262 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/phi.py
+2 -1
@@ -38,6 +38,7 @@
38 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 """Inference-only Phi-1.5 model compatible with HuggingFace weights."""
40 from collections.abc import Iterable
41 +from itertools import islice
42 from typing import Optional, Union
43
44 import torch
@@ -240,7 +241,7 @@ class PhiModel(nn.Module):
241 else:
242 assert intermediate_tensors is not None
243 hidden_states = intermediate_tensors["hidden_states"]
243 - for layer in self.layers[self.start_layer:self.end_layer]:
244 + for layer in islice(self.layers, self.start_layer, self.end_layer):
245 hidden_states = layer(positions, hidden_states)
246
247 if not get_pp_group().is_last_rank:
vllm/model_executor/models/phimoe.py
+2 -1
@@ -24,6 +24,7 @@
24 # limitations under the License.
25 """Inference-only PhiMoE model."""
26 from collections.abc import Iterable
27 +from itertools import islice
28 from typing import Optional, Union
29
30 import torch
@@ -500,7 +501,7 @@ class PhiMoEModel(nn.Module):
501 hidden_states = intermediate_tensors["hidden_states"]
502 residual = intermediate_tensors["residual"]
503
503 - for layer in self.layers[self.start_layer:self.end_layer]:
504 + for layer in islice(self.layers, self.start_layer, self.end_layer):
505 hidden_states, residual = layer(
506 positions,
507 hidden_states,
vllm/model_executor/models/plamo2.py
+2 -1
@@ -2,6 +2,7 @@
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Inference-only PLaMo2 model."""
4 from collections.abc import Iterable
5 +from itertools import islice
6 from typing import Optional
7
8 import torch
@@ -614,7 +615,7 @@ class Plamo2Decoder(torch.nn.Module):
615 mamba2_metadata: Mamba2Metadata,
616 ) -> torch.Tensor:
617 mamba_cache_index = 0
617 - for layer in self.layers[self.start_layer:self.end_layer]:
618 + for layer in islice(self.layers, self.start_layer, self.end_layer):
619 layer_mamba_cache_params = None
620 if layer.is_mamba:
621 layer_mamba_cache_params = mamba_cache_params.at_layer_idx(
vllm/model_executor/models/qwen.py
+2 -1
@@ -8,6 +8,7 @@
8 """Inference-only QWen model compatible with HuggingFace weights."""
9 import json
10 from collections.abc import Iterable
11 +from itertools import islice
12 from typing import Any, Optional, Union
13
14 import torch
@@ -234,7 +235,7 @@ class QWenModel(nn.Module):
235 hidden_states = intermediate_tensors["hidden_states"]
236 residual = intermediate_tensors["residual"]
237
237 - for layer in self.h[self.start_layer:self.end_layer]:
238 + for layer in islice(self.h, self.start_layer, self.end_layer):
239 hidden_states, residual = layer(
240 positions,
241 hidden_states,
vllm/model_executor/models/qwen2.py
+2 -1
@@ -25,6 +25,7 @@
25 # limitations under the License.
26 """Inference-only Qwen2 model compatible with HuggingFace weights."""
27 from collections.abc import Iterable
28 +from itertools import islice
29 from typing import Any, Optional, Union
30
31 import torch
@@ -358,7 +359,7 @@ class Qwen2Model(nn.Module):
359
360 aux_hidden_states = []
361 for idx, layer in enumerate(
361 - self.layers[self.start_layer:self.end_layer]):
362 + islice(self.layers, self.start_layer, self.end_layer)):
363 if idx in self.aux_hidden_state_layers:
364 aux_hidden_states.append(hidden_states + residual)
365 hidden_states, residual = layer(positions, hidden_states, residual)
vllm/model_executor/models/qwen2_moe.py
+2 -1
@@ -25,6 +25,7 @@
25 # limitations under the License.
26 """Inference-only Qwen2MoE model compatible with HuggingFace weights."""
27 from collections.abc import Iterable
28 +from itertools import islice
29 from typing import Any, Optional, Union
30
31 import torch
@@ -381,7 +382,7 @@ class Qwen2MoeModel(nn.Module):
382 assert intermediate_tensors is not None
383 hidden_states = intermediate_tensors["hidden_states"]
384 residual = intermediate_tensors["residual"]
384 - for layer in self.layers[self.start_layer:self.end_layer]:
385 + for layer in islice(self.layers, self.start_layer, self.end_layer):
386 hidden_states, residual = layer(positions, hidden_states, residual)
387 if not get_pp_group().is_last_rank:
388 return IntermediateTensors({
vllm/model_executor/models/qwen3_moe.py
+2 -2
@@ -24,6 +24,7 @@
24 """Inference-only Qwen3MoE model compatible with HuggingFace weights."""
25 import typing
26 from collections.abc import Callable, Iterable
27 +from itertools import islice
28 from typing import Any, Optional, Union
29
30 import torch
@@ -420,8 +421,7 @@ class Qwen3MoeModel(nn.Module):
421 assert intermediate_tensors is not None
422 hidden_states = intermediate_tensors["hidden_states"]
423 residual = intermediate_tensors["residual"]
423 - for i in range(self.start_layer, self.end_layer):
424 - layer = self.layers[i]
424 + for layer in islice(self.layers, self.start_layer, self.end_layer):
425 hidden_states, residual = layer(positions, hidden_states, residual)
426 if not get_pp_group().is_last_rank:
427 return IntermediateTensors({
vllm/model_executor/models/seed_oss.py
+2 -1
@@ -23,6 +23,7 @@
23 # limitations under the License.
24 """Inference-only SeedOss model compatible with HuggingFace weights."""
25 from collections.abc import Iterable
26 +from itertools import islice
27 from typing import Optional, Union
28
29 import torch
@@ -340,7 +341,7 @@ class SeedOssModel(nn.Module):
341 assert intermediate_tensors is not None
342 hidden_states = intermediate_tensors["hidden_states"]
343 residual = intermediate_tensors["residual"]
343 - for layer in self.layers[self.start_layer:self.end_layer]:
344 + for layer in islice(self.layers, self.start_layer, self.end_layer):
345 hidden_states, residual = layer(
346 positions,
347 hidden_states,
vllm/model_executor/models/stablelm.py
+2 -1
@@ -22,6 +22,7 @@
22 """Inference-only StabeLM (https://github.com/Stability-AI/StableLM)
23 model compatible with HuggingFace weights."""
24 from collections.abc import Iterable
25 +from itertools import islice
26 from typing import Optional, Union
27
28 import torch
@@ -247,7 +248,7 @@ class StableLMEpochModel(nn.Module):
248 else:
249 assert intermediate_tensors is not None
250 hidden_states = intermediate_tensors["hidden_states"]
250 - for layer in self.layers[self.start_layer:self.end_layer]:
251 + for layer in islice(self.layers, self.start_layer, self.end_layer):
252 hidden_states, residual = layer(positions, hidden_states)
253 if not get_pp_group().is_last_rank:
254 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/starcoder2.py
+2 -1
@@ -21,6 +21,7 @@
21 # limitations under the License.
22 """ PyTorch Starcoder2 model."""
23 from collections.abc import Iterable
24 +from itertools import islice
25 from typing import Optional, Union
26
27 import torch
@@ -250,7 +251,7 @@ class Starcoder2Model(nn.Module):
251 else:
252 assert intermediate_tensors is not None
253 hidden_states = intermediate_tensors["hidden_states"]
253 - for layer in self.layers[self.start_layer:self.end_layer]:
254 + for layer in islice(self.layers, self.start_layer, self.end_layer):
255 hidden_states = layer(positions, hidden_states)
256 if not get_pp_group().is_last_rank:
257 return IntermediateTensors({"hidden_states": hidden_states})
vllm/model_executor/models/step3_text.py
+2 -2
@@ -2,6 +2,7 @@
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Inference-only Jurassic model."""
4 from collections.abc import Iterable
5 +from itertools import islice
6 from typing import Any, Optional
7
8 import torch
@@ -346,8 +347,7 @@ class Step3TextModel(nn.Module):
347 hidden_states = intermediate_tensors["hidden_states"]
348 residual = intermediate_tensors["residual"]
349
349 - for i in range(self.start_layer, self.end_layer):
350 - layer = self.layers[i]
350 + for layer in islice(self.layers, self.start_layer, self.end_layer):
351 hidden_states, residual = layer(positions, hidden_states, residual)
352
353 if not get_pp_group().is_last_rank: