Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bdca8ee
[Model] Support EAGLE3 for Sarvam
mohit-sarvam Aug 20, 2026
1c5b972
[Model] Add EAGLE3 unit tests for Sarvam
mohit-sarvam Aug 20, 2026
c46e3bb
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Aug 20, 2026
5dcea91
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Aug 24, 2026
c3267ea
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Aug 25, 2026
6758be7
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Aug 25, 2026
db9772c
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Aug 25, 2026
d4d75bb
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Aug 27, 2026
b6f5efb
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 1, 2026
11bf00b
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 1, 2026
b142fec
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 2, 2026
51f652b
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 3, 2026
1c6a4ee
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 3, 2026
85fe491
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 4, 2026
761f0ef
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 4, 2026
6c64b61
Document Sarvam EAGLE3 pipeline scope and test ordinary PP
mohit-sarvam Sep 5, 2026
c112d91
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 5, 2026
176b762
Document Sarvam EAGLE3 pipeline requirement in README
mohit-sarvam Sep 5, 2026
6dfe671
Merge remote-tracking branch 'fork/mohit/sarvam-eagle3' into mohit/sa…
mohit-sarvam Sep 5, 2026
4e1c874
Revert Sarvam-specific README note
mohit-sarvam Sep 5, 2026
3c9d8b7
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 7, 2026
e336e1e
Remove Sarvam EAGLE3 unit test file
mohit-sarvam Sep 7, 2026
5d86fd4
Merge branch 'main' into mohit/sarvam-eagle3
mohit-sarvam Sep 7, 2026
59465fe
Merge branch 'main' into mohit/sarvam-eagle3
vadiklyutiy Sep 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions tests/model_executor/test_sarvam_eagle3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

from types import SimpleNamespace
from unittest.mock import Mock

import torch

from vllm.model_executor.models import sarvam as sarvam_mod
from vllm.model_executor.models.interfaces import supports_eagle3
from vllm.model_executor.models.sarvam import SarvamMLAForCausalLM, SarvamMLAModel
from vllm.sequence import IntermediateTensors


def _make_model(
*,
start_layer: int = 0,
end_layer: int = 1,
layers: list | None = None,
aux_hidden_state_layers: tuple[int, ...] = (),
) -> SarvamMLAModel:
"""Build a SarvamMLAModel without running __init__.

Constructing the real module needs a full VllmConfig plus an initialized
distributed environment, neither of which the aux hidden state plumbing
depends on.
"""
model = object.__new__(SarvamMLAModel)
object.__setattr__(model, "start_layer", start_layer)
object.__setattr__(model, "end_layer", end_layer)
object.__setattr__(model, "layers", layers if layers is not None else [])
object.__setattr__(model, "aux_hidden_state_layers", aux_hidden_state_layers)
object.__setattr__(model, "embedding_dropout", lambda x: x)
# RMSNorm in fused-residual mode returns (norm(h + r), h + r); the scaling
# is irrelevant here, so pass the sum through unchanged.
object.__setattr__(
model, "norm", lambda h, r=None: (h + r, h + r) if r is not None else h
)
return model


def _layer(hidden_states: torch.Tensor, residual: torch.Tensor) -> Mock:
return Mock(return_value=(hidden_states, residual))


def _patch_pp_group(monkeypatch, *, is_first_rank=True, is_last_rank=True) -> None:
monkeypatch.setattr(
sarvam_mod,
"get_pp_group",
lambda: SimpleNamespace(is_first_rank=is_first_rank, is_last_rank=is_last_rank),
)


def test_sarvam_mla_advertises_eagle3_support():
assert supports_eagle3(SarvamMLAForCausalLM)


def test_sarvam_mla_configures_aux_hidden_state_layers():
# SarvamMLAForCausalLM inherits both hooks from the SupportsEagle3
# protocol, so this pins that the protocol defaults actually reach the
# inner SarvamMLAModel rather than silently doing nothing.
target = object.__new__(SarvamMLAForCausalLM)
torch.nn.Module.__init__(target)
model = _make_model(layers=[None] * 12)
object.__setattr__(target, "model", model)

target.set_aux_hidden_state_layers((2, 6, 9))

assert model.aux_hidden_state_layers == (2, 6, 9)
assert target.get_eagle3_default_aux_hidden_state_layers() == (2, 6, 9)


def test_sarvam_mla_forward_captures_aux_hidden_states(monkeypatch):
inputs_embeds = torch.tensor([[1.0, 2.0]])
layer_hidden_states = torch.tensor([[3.0, 4.0]])
layer_residual = torch.tensor([[5.0, 6.0]])
model = _make_model(
layers=[_layer(layer_hidden_states, layer_residual)],
aux_hidden_state_layers=(0, 1),
)
_patch_pp_group(monkeypatch)

output, aux_hidden_states = model.forward(
input_ids=None,
positions=torch.tensor([0]),
intermediate_tensors=None,
inputs_embeds=inputs_embeds,
)

# Index 0 is the embedding output; index 1 is the output of layer 0, which
# is only complete once the pending residual is added back.
expected_layer_output = layer_hidden_states + layer_residual
torch.testing.assert_close(output, expected_layer_output)
assert len(aux_hidden_states) == 2
torch.testing.assert_close(aux_hidden_states[0], inputs_embeds)
torch.testing.assert_close(aux_hidden_states[1], expected_layer_output)


def test_sarvam_mla_forward_returns_bare_tensor_without_eagle3(monkeypatch):
# Without a drafter the runner unpacks a single tensor, so returning a
# tuple unconditionally would break every non-speculative request.
model = _make_model(
layers=[_layer(torch.tensor([[3.0, 4.0]]), torch.tensor([[5.0, 6.0]]))],
)
_patch_pp_group(monkeypatch)

output = model.forward(
input_ids=None,
positions=torch.tensor([0]),
intermediate_tensors=None,
inputs_embeds=torch.tensor([[1.0, 2.0]]),
)

assert isinstance(output, torch.Tensor)


def test_sarvam_mla_forward_uses_absolute_layer_indices(monkeypatch):
# On a pipeline stage that does not start at layer 0, the capture points
# are numbered by absolute layer index. With relative numbering the
# requested layers would silently resolve to different tensors.
stage_hidden_states = torch.tensor([[1.0, 2.0]])
stage_residual = torch.tensor([[0.5, 0.5]])
second_hidden_states = torch.tensor([[3.0, 4.0]])
second_residual = torch.tensor([[5.0, 6.0]])
model = _make_model(
start_layer=1,
end_layer=3,
layers=[
None,
_layer(torch.tensor([[7.0, 8.0]]), torch.tensor([[9.0, 10.0]])),
_layer(second_hidden_states, second_residual),
],
aux_hidden_state_layers=(1, 3),
)
_patch_pp_group(monkeypatch, is_first_rank=False)

_, aux_hidden_states = model.forward(
input_ids=None,
positions=torch.tensor([0]),
intermediate_tensors=IntermediateTensors(
{"hidden_states": stage_hidden_states, "residual": stage_residual}
),
inputs_embeds=None,
)

assert len(aux_hidden_states) == 2
torch.testing.assert_close(
aux_hidden_states[0], stage_hidden_states + stage_residual
)
torch.testing.assert_close(
aux_hidden_states[1], second_hidden_states + second_residual
)
33 changes: 27 additions & 6 deletions vllm/model_executor/models/sarvam.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@
from vllm.sequence import IntermediateTensors

from .bailing_moe import BailingMoeForCausalLM
from .interfaces import MixtureOfExperts, SupportsLoRA, SupportsPP
from .interfaces import (
EagleModelMixin,
MixtureOfExperts,
SupportsEagle3,
SupportsLoRA,
SupportsPP,
)
from .utils import (
AutoWeightsLoader,
PPMissingLayer,
Expand Down Expand Up @@ -442,7 +448,7 @@ def forward(
return hidden_states, residual


class SarvamMLAModel(nn.Module):
class SarvamMLAModel(nn.Module, EagleModelMixin):
hf_to_vllm_mapper = WeightsMapper(
orig_to_new_stacked={
# .experts.gate_up_proj must be handled by MoERunner.load_weights for EP
Expand Down Expand Up @@ -508,7 +514,7 @@ def forward(
positions: torch.Tensor,
intermediate_tensors: IntermediateTensors | None,
inputs_embeds: torch.Tensor | None = None,
) -> torch.Tensor | IntermediateTensors:
) -> torch.Tensor | IntermediateTensors | tuple[torch.Tensor, list[torch.Tensor]]:
if get_pp_group().is_first_rank:
if inputs_embeds is not None:
hidden_states = inputs_embeds
Expand All @@ -521,12 +527,22 @@ def forward(
hidden_states = intermediate_tensors["hidden_states"]
residual = intermediate_tensors["residual"]

for layer in islice(self.layers, self.start_layer, self.end_layer):
aux_hidden_states = self._maybe_add_hidden_state(
[], self.start_layer, hidden_states, residual
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
for layer_idx, layer in enumerate(
islice(self.layers, self.start_layer, self.end_layer),
start=self.start_layer,
):
hidden_states, residual = layer(
hidden_states,
positions,
residual,
)
self._maybe_add_hidden_state(
aux_hidden_states, layer_idx + 1, hidden_states, residual
)

if not get_pp_group().is_last_rank:
return IntermediateTensors(
{"hidden_states": hidden_states, "residual": residual}
Expand All @@ -535,6 +551,9 @@ def forward(
hidden_states = self.norm(hidden_states)
else:
hidden_states, _ = self.norm(hidden_states, residual)

if len(aux_hidden_states) > 0:
return hidden_states, aux_hidden_states
return hidden_states

def load_weights(
Expand Down Expand Up @@ -591,7 +610,9 @@ def set_eplb_state(self, eplb_state) -> None:
moe.set_eplb_state(eplb_state)


class SarvamMLAForCausalLM(nn.Module, SupportsPP, SupportsLoRA, SarvamMixtureOfExperts):
class SarvamMLAForCausalLM(
nn.Module, SupportsPP, SupportsLoRA, SupportsEagle3, SarvamMixtureOfExperts
):
packed_modules_mapping = {
"q_proj": ["q_proj"],
"q_a_proj": ["q_a_proj"],
Expand Down Expand Up @@ -658,7 +679,7 @@ def forward(
positions: torch.Tensor,
intermediate_tensors: IntermediateTensors | None = None,
inputs_embeds: torch.Tensor | None = None,
) -> torch.Tensor | IntermediateTensors:
) -> torch.Tensor | IntermediateTensors | tuple[torch.Tensor, list[torch.Tensor]]:
return self.model(
input_ids=input_ids,
positions=positions,
Expand Down
Loading