Skip to content
Closed
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
6 changes: 4 additions & 2 deletions src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,10 @@ def load_balancing_loss_func(
# Compute the average probability of routing to these experts
router_prob_per_expert = torch.mean(routing_weights, dim=0)
else:
batch_size, sequence_length = attention_mask.shape
num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
num_hidden_layers = len(gate_logits)
batch_size = attention_mask.shape[0]
sequence_length = gate_logits[0].shape[0] // batch_size
attention_mask = attention_mask[:, -sequence_length:]

# Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
expert_attention_mask = (
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/models/gpt_oss/modeling_gpt_oss.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,10 @@ def load_balancing_loss_func(
# Compute the average probability of routing to these experts
router_prob_per_expert = torch.mean(routing_weights, dim=0)
else:
batch_size, sequence_length = attention_mask.shape
num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
num_hidden_layers = len(gate_logits)
batch_size = attention_mask.shape[0]
sequence_length = gate_logits[0].shape[0] // batch_size
attention_mask = attention_mask[:, -sequence_length:]

# Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
expert_attention_mask = (
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/models/minimax/modeling_minimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,10 @@ def load_balancing_loss_func(
# Compute the average probability of routing to these experts
router_prob_per_expert = torch.mean(routing_weights, dim=0)
else:
batch_size, sequence_length = attention_mask.shape
num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
num_hidden_layers = len(gate_logits)
batch_size = attention_mask.shape[0]
sequence_length = gate_logits[0].shape[0] // batch_size
attention_mask = attention_mask[:, -sequence_length:]

# Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
expert_attention_mask = (
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/models/mixtral/modeling_mixtral.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,10 @@ def load_balancing_loss_func(
# Compute the average probability of routing to these experts
router_prob_per_expert = torch.mean(routing_weights, dim=0)
else:
batch_size, sequence_length = attention_mask.shape
num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
num_hidden_layers = len(gate_logits)
batch_size = attention_mask.shape[0]
sequence_length = gate_logits[0].shape[0] // batch_size
attention_mask = attention_mask[:, -sequence_length:]

# Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
expert_attention_mask = (
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/models/mixtral/modular_mixtral.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ def load_balancing_loss_func(
# Compute the average probability of routing to these experts
router_prob_per_expert = torch.mean(routing_weights, dim=0)
else:
batch_size, sequence_length = attention_mask.shape
num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
num_hidden_layers = len(gate_logits)
batch_size = attention_mask.shape[0]
sequence_length = gate_logits[0].shape[0] // batch_size
attention_mask = attention_mask[:, -sequence_length:]

# Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
expert_attention_mask = (
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/models/qwen3_moe/modeling_qwen3_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,10 @@ def load_balancing_loss_func(
# Compute the average probability of routing to these experts
router_prob_per_expert = torch.mean(routing_weights, dim=0)
else:
batch_size, sequence_length = attention_mask.shape
num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
num_hidden_layers = len(gate_logits)
batch_size = attention_mask.shape[0]
sequence_length = gate_logits[0].shape[0] // batch_size
attention_mask = attention_mask[:, -sequence_length:]

# Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
expert_attention_mask = (
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/models/qwen3_next/modeling_qwen3_next.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,10 @@ def load_balancing_loss_func(
# Compute the average probability of routing to these experts
router_prob_per_expert = torch.mean(routing_weights, dim=0)
else:
batch_size, sequence_length = attention_mask.shape
num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
num_hidden_layers = len(gate_logits)
batch_size = attention_mask.shape[0]
sequence_length = gate_logits[0].shape[0] // batch_size
attention_mask = attention_mask[:, -sequence_length:]

# Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
expert_attention_mask = (
Expand Down