Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Z-loss calculation #634

Merged
merged 1 commit into from
Jun 26, 2024
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
10 changes: 4 additions & 6 deletions olmo/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ def model_forward(
return ce_loss, z_loss, logits

def train_micro_batch(
self, micro_batch: Dict[str, Any], batch_size_in_tokens: int, num_micro_batches: int
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
self, micro_batch: Dict[str, Any], batch_size_in_tokens: int
) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]:
ce_loss, z_loss, logits = self.model_forward(
micro_batch, compute_z_loss=self.cfg.softmax_auxiliary_loss, loss_reduction="sum"
)
Expand All @@ -666,7 +666,7 @@ def train_micro_batch(
# Get loss to optimize for.
if self.cfg.softmax_auxiliary_loss:
assert z_loss is not None
z_loss = z_loss / num_micro_batches
z_loss = z_loss / batch_size_in_tokens
loss = ce_loss + z_loss
else:
loss = ce_loss
Expand Down Expand Up @@ -701,9 +701,7 @@ def train_batch(self, batch: Dict[str, Any]) -> Tuple[torch.Tensor, Optional[tor
with grad_sync_context():
with torch.autocast("cuda", enabled=True, dtype=self.cfg.autocast_precision):
# Run forward pass.
loss, ce_loss, z_loss = self.train_micro_batch(
micro_batch, batch_size_in_tokens, num_micro_batches
)
loss, ce_loss, z_loss = self.train_micro_batch(micro_batch, batch_size_in_tokens)

# Update overall CE batch loss.
ce_batch_loss += ce_loss.detach()
Expand Down
Loading