Skip to content
Merged
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 swift/megatron/trainers/gkd_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,10 @@ def loss_func(self,
if self.sft_alpha > 0 and data_source != DataSource.STUDENT:
args = self.args
logits_sbv = student_logits.transpose(0, 1).contiguous()
per_token_loss = self.unwrapped_models[0].compute_language_model_loss(labels, logits_sbv)

model = self.unwrapped_models[0]
if hasattr(model, 'language_model'):
model = model.language_model
per_token_loss = model.compute_language_model_loss(labels, logits_sbv)
Comment on lines +667 to +670
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved readability and conciseness, you can use getattr with a default value. This avoids reassigning the model variable and makes the logic for obtaining the correct model more direct and idiomatic.

            language_model = getattr(self.unwrapped_models[0], 'language_model', self.unwrapped_models[0])
            per_token_loss = language_model.compute_language_model_loss(labels, logits_sbv)

loss_mask = labels != -100
sft_loss_sum = (per_token_loss * loss_mask).sum()
sft_loss_count = loss_mask.sum().float()
Expand Down
Loading