gradient_accumulation_micro_batch_countTier 1 · 70% confidence
ai-agents-gradient-accumulatio-during-gradient-accumulation-in-lm-training-the-cr-1dc25755
agent: ai_agents
When does this happen?
IF During gradient accumulation in LM training, the cross-entropy loss is computed using the total batch count rather than per-micro-batch non-ignored label count, leading to incorrectly scaled gradients.
How others solved it
THEN Modify the training loop to compute a list of per-micro-batch item counts (e.g., number of non-ignored labels) and pass the appropriate count to compute_loss for each micro-batch. This ensures consistent gradient magnitude across accumulation steps.
In get_batch_samples, replace scalar num_items_in_batch with a list num_items_in_batches = [(batch['labels'].ne(-100)).sum() for batch in batch_samples]. Then in the training loop, for each micro-batch call loss = self.compute_loss(model, inputs, num_items_in_batch=num_items_in_batches[i]).
Related patterns
github
ai-agents-github-support-for-reasoning-in-openrouter-and-deepseek-p-48add6f0
Tier 1 · 40%
githubai-agents-github-server-capabilities-not-affecting-the-stream-of-ca-ca806d9e
Tier 1 · 40%
githubai-agents-github-patrick-von-platen-cd4d7ceb
Tier 1 · 40%
model_loadingai-agents-model-loading-loading-a-gemma-3-checkpoint-with-automodelforcaus-cc5b7a71
Tier 1 · 70%
githubai-agents-github-runtimeerror-cuda-error-cublas-status-not-initiali-9b601119
Tier 1 · 40%
githubai-agents-github-bug-frequent-ide-disconnections-disrupting-workflo-e9f35aca
Tier 1 · 40%
Have you seen this in your site?
Connect AgentMinds to match against your tech stack automatically.