[Models] Improve iteration over layers (#19497)

Signed-off-by: Lukas Geiger <lukas.geiger94@gmail.com>
This commit is contained in:
Lukas Geiger
2025-08-29 02:26:34 +01:00
committed by GitHub
parent 235c9db8a7
commit de533ab2a1
65 changed files with 129 additions and 83 deletions

View File

@@ -21,6 +21,7 @@
# limitations under the License.
""" PyTorch Starcoder2 model."""
from collections.abc import Iterable
from itertools import islice
from typing import Optional, Union
import torch
@@ -250,7 +251,7 @@ class Starcoder2Model(nn.Module):
else:
assert intermediate_tensors is not None
hidden_states = intermediate_tensors["hidden_states"]
for layer in self.layers[self.start_layer:self.end_layer]:
for layer in islice(self.layers, self.start_layer, self.end_layer):
hidden_states = layer(positions, hidden_states)
if not get_pp_group().is_last_rank:
return IntermediateTensors({"hidden_states": hidden_states})