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 broken backprop #62

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
8 changes: 5 additions & 3 deletions dolomite_engine/model_wrapper/pretraining.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(
assert not self.reset_attention_mask, "reset_attention_mask is not supported with pipeline parallelism"
assert not self.reset_position_ids, "reset_position_ids is not supported with pipeline parallelism"

def forward(self, batch: dict) -> dict:
def forward(self, batch: dict, lm_loss_multiplier: float = 1) -> dict:
"""forward function for a batch

Args:
Expand All @@ -117,11 +117,11 @@ def forward(self, batch: dict) -> dict:

# without pipeline parallel, we compute the loss outside
if not self.is_pipeline_parallel_enabled:
output = MetricsTrackingDict(self.get_loss(output, labels))
output = self.get_loss(output, labels, lm_loss_multiplier=lm_loss_multiplier)

return output

def get_loss(self, model_outputs, labels: torch.Tensor) -> torch.Tensor:
def get_loss(self, model_outputs, labels: torch.Tensor, lm_loss_multiplier: float = 1) -> torch.Tensor:
if isinstance(model_outputs, torch.Tensor):
logits = model_outputs
else:
Expand All @@ -146,6 +146,8 @@ def get_loss(self, model_outputs, labels: torch.Tensor) -> torch.Tensor:
with loss_context():
lm_loss = F.cross_entropy(logits.view(-1, logits.size(-1)), labels.reshape(-1), reduction="sum")

lm_loss = lm_loss * lm_loss_multiplier

if hasattr(model_outputs, "aux_loss"):
aux_loss = model_outputs.aux_loss
loss = lm_loss + self.router_aux_loss_coef * aux_loss
Expand Down
10 changes: 4 additions & 6 deletions dolomite_engine/train_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ def _train_step_without_pipeline_parallel(
grad_norm = None
optimizer.zero_grad()

lm_loss_multiplier = 1 / (micro_batch_size * sequence_length)

with no_sync():
for _ in range(gradient_accumulation_steps - 1):
batch = get_next_batch(train_dataloader)
with forward_context():
loss_micro_step_dict = model(batch)
# divide by the total unmasked tokens and update the dict
loss_micro_step_dict = loss_micro_step_dict / (micro_batch_size * sequence_length)
loss_micro_step_dict = model(batch, lm_loss_multiplier=lm_loss_multiplier)

# compute gradients
with backward_context():
Expand All @@ -241,9 +241,7 @@ def _train_step_without_pipeline_parallel(

batch = get_next_batch(train_dataloader)
with forward_context():
loss_micro_step_dict = model(batch)
# divide by the total unmasked tokens and update the dict
loss_micro_step_dict = loss_micro_step_dict / (micro_batch_size * sequence_length)
loss_micro_step_dict = model(batch, lm_loss_multiplier=lm_loss_multiplier)

# compute gradients
with backward_context():
Expand Down
Loading