Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 2 additions & 7 deletions src/transformers/models/dbrx/modeling_dbrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ def forward(
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
use_cache: bool | None = None,
output_router_logits: bool | None = None,
cache_position: torch.LongTensor | None = None,
logits_to_keep: int | torch.Tensor = 0,
**kwargs: Unpack[TransformersKwargs],
Expand Down Expand Up @@ -725,10 +724,6 @@ def forward(
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
```
"""
output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.output_router_logits
)

# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs: MoeModelOutputWithPast = self.transformer(
input_ids=input_ids,
Expand All @@ -737,8 +732,8 @@ def forward(
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_router_logits=output_router_logits,
cache_position=cache_position,
return_dict=True,
**kwargs,
)

Expand All @@ -752,7 +747,7 @@ def forward(
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)

aux_loss = None
if output_router_logits:
if outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
9 changes: 2 additions & 7 deletions src/transformers/models/dbrx/modular_dbrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,6 @@ def forward(
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
use_cache: bool | None = None,
output_router_logits: bool | None = None,
cache_position: torch.LongTensor | None = None,
logits_to_keep: int | torch.Tensor = 0,
**kwargs: Unpack[TransformersKwargs],
Expand Down Expand Up @@ -513,10 +512,6 @@ def forward(
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
```
"""
output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.output_router_logits
)

# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs: MoeModelOutputWithPast = self.transformer(
input_ids=input_ids,
Expand All @@ -525,8 +520,8 @@ def forward(
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_router_logits=output_router_logits,
cache_position=cache_position,
return_dict=True,
**kwargs,
)

Expand All @@ -540,7 +535,7 @@ def forward(
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)

aux_loss = None
if output_router_logits:
if outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
8 changes: 2 additions & 6 deletions src/transformers/models/doge/modeling_doge.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,6 @@ def forward(
use_cache: bool | None = None,
cache_position: torch.LongTensor | None = None,
logits_to_keep: int | torch.Tensor = 0,
output_router_logits: bool | None = None,
**kwargs: Unpack[TransformersKwargs],
) -> MoeCausalLMOutputWithPast:
r"""
Expand All @@ -780,10 +779,6 @@ def forward(
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
```"""
output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.output_router_logits
)

# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs: MoeModelOutputWithPast = self.model(
input_ids=input_ids,
Expand All @@ -793,6 +788,7 @@ def forward(
inputs_embeds=inputs_embeds,
use_cache=use_cache,
cache_position=cache_position,
return_dict=True,
**kwargs,
)

Expand All @@ -806,7 +802,7 @@ def forward(
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)

aux_loss = None
if output_router_logits:
if self.config.is_moe and outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
8 changes: 2 additions & 6 deletions src/transformers/models/doge/modular_doge.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@ def forward(
use_cache: bool | None = None,
cache_position: torch.LongTensor | None = None,
logits_to_keep: int | torch.Tensor = 0,
output_router_logits: bool | None = None,
**kwargs: Unpack[TransformersKwargs],
) -> MoeCausalLMOutputWithPast:
r"""
Expand All @@ -703,10 +702,6 @@ def forward(
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
```"""
output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.output_router_logits
)

# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs: MoeModelOutputWithPast = self.model(
input_ids=input_ids,
Expand All @@ -716,6 +711,7 @@ def forward(
inputs_embeds=inputs_embeds,
use_cache=use_cache,
cache_position=cache_position,
return_dict=True,
**kwargs,
)

Expand All @@ -729,7 +725,7 @@ def forward(
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)

aux_loss = None
if output_router_logits:
if self.config.is_moe and outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
10 changes: 2 additions & 8 deletions src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,6 @@ def forward(
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
use_cache: bool | None = None,
output_router_logits: bool | None = None,
cache_position: torch.LongTensor | None = None,
logits_to_keep: int | torch.Tensor = 0,
**kwargs: Unpack[TransformersKwargs],
Expand All @@ -703,11 +702,6 @@ def forward(
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
"""

output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.output_router_logits
)

# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs: MoeModelOutputWithPast = self.model(
input_ids=input_ids,
Expand All @@ -716,8 +710,8 @@ def forward(
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_router_logits=output_router_logits,
cache_position=cache_position,
return_dict=True,
**kwargs,
)

Expand All @@ -731,7 +725,7 @@ def forward(
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)

aux_loss = None
if output_router_logits:
if outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,6 @@ def forward(
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
use_cache: bool | None = None,
output_router_logits: bool | None = None,
pixel_values: torch.Tensor | None = None,
pixel_values_videos: torch.FloatTensor | None = None,
image_grid_thw: torch.LongTensor | None = None,
Expand All @@ -1620,10 +1619,6 @@ def forward(
rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*):
The rope index difference between sequence length and multimodal rope.
"""
output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.text_config.output_router_logits
)

outputs = self.model(
input_ids=input_ids,
attention_mask=attention_mask,
Expand All @@ -1633,7 +1628,6 @@ def forward(
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_router_logits=output_router_logits,
return_dict=True,
pixel_values=pixel_values,
pixel_values_videos=pixel_values_videos,
Expand All @@ -1654,7 +1648,7 @@ def forward(
loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.text_config.vocab_size)

aux_loss = None
if output_router_logits:
if outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,6 @@ def forward(
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
use_cache: bool | None = None,
output_router_logits: bool | None = None,
pixel_values: torch.Tensor | None = None,
pixel_values_videos: torch.FloatTensor | None = None,
image_grid_thw: torch.LongTensor | None = None,
Expand All @@ -1426,10 +1425,6 @@ def forward(
rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*):
The rope index difference between sequence length and multimodal rope.
"""
output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.text_config.output_router_logits
)

outputs = self.model(
input_ids=input_ids,
attention_mask=attention_mask,
Expand All @@ -1439,7 +1434,6 @@ def forward(
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_router_logits=output_router_logits,
return_dict=True,
pixel_values=pixel_values,
pixel_values_videos=pixel_values_videos,
Expand All @@ -1460,7 +1454,7 @@ def forward(
loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.text_config.vocab_size)

aux_loss = None
if output_router_logits:
if outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
10 changes: 2 additions & 8 deletions src/transformers/models/flex_olmo/modeling_flex_olmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ def forward(
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
use_cache: bool | None = None,
output_router_logits: bool | None = None,
cache_position: torch.LongTensor | None = None,
logits_to_keep: int | torch.Tensor = 0,
**kwargs: Unpack[TransformersKwargs],
Expand Down Expand Up @@ -662,11 +661,6 @@ def forward(
'Hey, are you conscious? Can you talk to me?\nI’m not sure if you’re conscious of this, but I’m'
```
"""

output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.output_router_logits
)

# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs: MoeModelOutputWithPast = self.model(
input_ids=input_ids,
Expand All @@ -675,8 +669,8 @@ def forward(
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_router_logits=output_router_logits,
cache_position=cache_position,
return_dict=True,
**kwargs,
)

Expand All @@ -690,7 +684,7 @@ def forward(
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)

aux_loss = None
if output_router_logits:
if outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
5 changes: 5 additions & 0 deletions src/transformers/models/glm4v_moe/configuration_glm4v_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class Glm4vMoeTextConfig(PreTrainedConfig):
Beginning of stream token id.
router_aux_loss_coef (`float`, *optional*, defaults to 0.0001):
The aux loss factor for the loss.
output_router_logits (`bool`, *optional*, defaults to `False`):
Whether or not the router logits should be returned by the model. Enabling this will also
allow the model to output the auxiliary loss.

```python
>>> from transformers import Glm4vMoeTextModel, Glm4vMoeConfig
Expand Down Expand Up @@ -161,6 +164,7 @@ def __init__(
eos_token_id: int | None = None,
bos_token_id: int | None = None,
router_aux_loss_coef: float | None = 0.0001,
output_router_logits: bool | None = False,
**kwargs,
):
self.pad_token_id = pad_token_id
Expand Down Expand Up @@ -194,6 +198,7 @@ def __init__(
self.first_k_dense_replace = first_k_dense_replace
self.norm_topk_prob = norm_topk_prob
self.router_aux_loss_coef = router_aux_loss_coef
self.output_router_logits = output_router_logits
super().__init__(ignore_keys_at_rope_validation={"mrope_section"}, **kwargs)


Expand Down
5 changes: 5 additions & 0 deletions src/transformers/models/glm4v_moe/modular_glm4v_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class Glm4vMoeTextConfig(Glm4MoeConfig):
Beginning of stream token id.
router_aux_loss_coef (`float`, *optional*, defaults to 0.0001):
The aux loss factor for the loss.
output_router_logits (`bool`, *optional*, defaults to `False`):
Whether or not the router logits should be returned by the model. Enabling this will also
allow the model to output the auxiliary loss.

```python
>>> from transformers import Glm4vMoeTextModel, Glm4vMoeConfig
Expand Down Expand Up @@ -193,6 +196,7 @@ def __init__(
eos_token_id: int | None = None,
bos_token_id: int | None = None,
router_aux_loss_coef: float | None = 0.0001,
output_router_logits: bool | None = False,
**kwargs,
):
self.pad_token_id = pad_token_id
Expand Down Expand Up @@ -226,6 +230,7 @@ def __init__(
self.first_k_dense_replace = first_k_dense_replace
self.norm_topk_prob = norm_topk_prob
self.router_aux_loss_coef = router_aux_loss_coef
self.output_router_logits = output_router_logits
PreTrainedConfig.__init__(self, ignore_keys_at_rope_validation={"mrope_section"}, **kwargs)


Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/gpt_oss/configuration_gpt_oss.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
},
attention_dropout: float | None = 0.0,
num_experts_per_tok: int | None = 4,
router_aux_loss_coef: float | None = 0.9,
router_aux_loss_coef: float | None = 0.001,
output_router_logits: bool | None = False,
use_cache: bool | None = True,
layer_types: list[str] | None = None,
Expand Down
10 changes: 2 additions & 8 deletions src/transformers/models/gpt_oss/modeling_gpt_oss.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ def forward(
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
use_cache: bool | None = None,
output_router_logits: bool | None = None,
cache_position: torch.LongTensor | None = None,
logits_to_keep: int | torch.Tensor = 0,
**kwargs: Unpack[TransformersKwargs],
Expand All @@ -652,11 +651,6 @@ def forward(
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
```"""

output_router_logits = (
output_router_logits if output_router_logits is not None else self.config.output_router_logits
)

# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs: MoeModelOutputWithPast = self.model(
input_ids=input_ids,
Expand All @@ -665,8 +659,8 @@ def forward(
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_router_logits=output_router_logits,
cache_position=cache_position,
return_dict=True,
**kwargs,
)

Expand All @@ -680,7 +674,7 @@ def forward(
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)

aux_loss = None
if output_router_logits:
if outputs.router_logits is not None:
aux_loss = load_balancing_loss_func(
outputs.router_logits,
self.num_experts,
Expand Down
Loading