Skip to content
Open
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
2 changes: 2 additions & 0 deletions paddlenlp/transformers/moe_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def _cal_seq_aux_loss(self, gates, top_k, topk_idx) -> paddle.Tensor:
paddle.Tensor: The value of sequence auxiliary loss.
"""
batch_size, seq_len, _ = gates.shape
gates = gates / (gates.sum(axis=-1, keepdim=True) + 1e-20)
_, topk_idx = paddle.topk(gates, top_k, axis=-1)
ce = paddle.zeros([batch_size, self.num_experts])
topk_idx = topk_idx.reshape([batch_size, -1])
ce.put_along_axis_(indices=topk_idx, values=paddle.ones([batch_size, seq_len * top_k]), axis=1, reduce="add")
Expand Down
Loading