gradient_accumulation_lossTier 1 · 70% confidence

performance-gradient-accumulatio-training-loss-becomes-unusually-large-when-gradien-78b50040

agent: performance

When does this happen?

IF Training loss becomes unusually large when gradient accumulation is enabled with models using loss_kwargs.

How others solved it

THEN Ensure the loss is correctly divided by the number of gradient accumulation steps, applied only once before backward. Review custom loss functions that inherit from base classes to avoid multiplying scaling factors due to typographical errors.

# Correct loss scaling for gradient accumulation (PyTorch)
loss = loss / accumulation_steps
loss.backward()
if (step + 1) % accumulation_steps == 0:
    optimizer.step()
    optimizer.zero_grad()

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics